Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bevy 0.15 #6

Merged
merged 9 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,531 changes: 1,662 additions & 869 deletions Cargo.lock

Large diffs are not rendered by default.

20 changes: 8 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "bevy_sparse_tilemap"
description = "A Tilemap crate for the Bevy game engine with a focus on large map sizes and ECS sparse maps"
version = "0.2.5"
version = "0.4.0"
edition = "2021"
license = "MIT OR Apache-2.0"
readme = "README.md"
Expand All @@ -15,7 +15,6 @@ resolver = "2"

[features]
default = ["serde", "lettuces/bevy", "hex", "square"]
# bevy_fast_tilemap = ["dep:bevy_fast_tilemap"]
serde = ["dep:serde", "serde/default", "bevy/serialize", "lettuces/serde"]
reflect = ["lettuces/bevy_reflect"]
hex = []
Expand All @@ -33,20 +32,17 @@ opt-level = 1
opt-level = 3

[dependencies]
bevy = { version = "0.13", default-features = false, features = [] }
bevy = { version = "0.15.0-rc.3", default-features = false, features = [] }
bst_map_layer_derive = { version = "0.1.0" }
# thiserror = "2.0.1"
thiserror = "1.0.44"
lettuces = { version = "0.0.6" }
lettuces = { git = "https://github.com/richchurcher/lettuces", branch = "0.15" }

# Optional feature based dependencies
# Rendering with bevy_fast_tilemap - Removed for now since we dont actually do anything with it
# bevy_fast_tilemap = { version = "0.5.1", optional = true }
serde = { version = "1.0.183", optional = true }

serde = { version = "1.0.214", optional = true }

[dev-dependencies]
bevy_fast_tilemap = { version = "0.7.0" }
bevy = { version = "0.13.0" }
bevy = { version = "0.15.0-rc.3" }
rand = { version = "0.8.5" }
serde = "1.0.183"
ron = "0.8.0"
serde = "1.0.214"
ron = "0.8.1"
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ A Tilemap crate for the Bevy game engine with a focus on large map sizes and ECS

See [Docs.rs](https://docs.rs/bevy_sparse_tilemap/latest/bevy_sparse_tilemap/) for documentation on how to use `bevy_sparse_tilemap` as well as brief examples.

See [Github Examples](https://github.com/NoahShomette/bevy_sparse_tilemap/tree/main/examples) for longer examples of each feature of the crate.
See [GitHub examples](https://github.com/NoahShomette/bevy_sparse_tilemap/tree/main/examples) for longer examples of each feature of the crate.

## What about `bevy_ecs_tilemap`?

Expand All @@ -56,13 +56,14 @@ You should use `bevy_ecs_tilemap` if:

You should use `bevy_sparse_tilemap` if:

- You want very very large maps, `bevy_sparse_tilemap` can reach substantially larger map sizes compared to `bevy_ecs_tilemap`. (The bevy_fast_tilemap_example currently spawns a 15000x15000 tile map and runs at around 900 fps)
- You are willing to implement your own tilemap rendering (This crate has an example for integration with `bevy_fast_tilemap` however that is not currently a feature that is natively supported by this crate)
- You want very very large maps, `bevy_sparse_tilemap` can reach substantially larger map sizes compared to `bevy_ecs_tilemap`.
- You are willing to implement your own tilemap rendering.

## Bevy Version

| BST Version | Bevy Version |
| :---------: | :----------: |
| 0.4 | 0.15 |
| 0.3 | 0.14 |
| 0.2 | 0.13 |
| 0.1 | 0.13 |
69 changes: 27 additions & 42 deletions examples/hexagon.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
use bevy::app::FixedUpdate;
use bevy::asset::{Assets, Handle};
use rand::Rng;

use bevy::diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin};
use bevy::math::UVec2;
use bevy::prelude::{
apply_deferred, default, App, Camera2dBundle, Commands, Entity, IntoSystemConfigs, PluginGroup,
Reflect, RegularPolygon, Res, ResMut, Resource, Startup, Window, WindowPlugin,
};
use bevy::render::color::Color;
use bevy::render::mesh::Mesh;
use bevy::sprite::{ColorMaterial, MaterialMesh2dBundle, Mesh2dHandle};
use bevy::time::{Fixed, Time};
use bevy::transform::components::Transform;
use bevy::prelude::*;
use bevy::window::PresentMode;
use bevy::DefaultPlugins;
use bevy_sparse_tilemap::hex::map_chunk_layer::HexagonChunkSettings;
use bevy_sparse_tilemap::hex::map_data::HexMapData;
use bevy_sparse_tilemap::hex::{
hex_offset_from_orientation, hex_rotation, HexTilemapBuilder, HexTilemapManager,
};

use bevy_sparse_tilemap::tilemap_builder::tilemap_layer_builder::TilemapLayer;
use bst_map_layer_derive::MapLayer;
use lettuces::cell::Cell;
use lettuces::{Hex, HexLayout, HexOrientation, Vec2, Vec3};
use rand::Rng;
use lettuces::{Hex, HexLayout, HexOrientation};

fn main() {
App::new()
Expand All @@ -36,11 +24,8 @@ fn main() {
}),
..default()
}))
.add_plugins((
LogDiagnosticsPlugin::default(),
FrameTimeDiagnosticsPlugin::default(),
))
.add_systems(Startup, (spawn_map, apply_deferred, spawn_tiles).chain())
.add_plugins((LogDiagnosticsPlugin::default(), FrameTimeDiagnosticsPlugin))
.add_systems(Startup, (spawn_map, spawn_tiles).chain())
.add_systems(FixedUpdate, change_random_tile_color)
.insert_resource(Time::<Fixed>::from_seconds(0.1))
.init_resource::<ColorHandles>()
Expand All @@ -67,7 +52,7 @@ struct TileData(u8, u8);
pub struct MapEntity(Entity);

#[derive(Resource)]
pub struct HexagonMeshHandle(Mesh2dHandle);
pub struct HexagonMeshHandle(Mesh2d);

#[derive(Resource, Default)]
pub struct ColorHandles(Vec<Handle<ColorMaterial>>);
Expand All @@ -77,10 +62,8 @@ fn spawn_map(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>) {
let max_chunk_size = UVec2::new(5, 5);

let mut tilemap_builder = HexTilemapBuilder::new(
TilemapLayer::new_dense_from_vecs(generate_random_tile_data(map_size.clone())),
HexMapData {
max_chunk_size: max_chunk_size,
},
TilemapLayer::new_dense_from_vecs(generate_random_tile_data(map_size)),
HexMapData { max_chunk_size },
HexagonChunkSettings {
orientation: HEXAGON_ORIENTATION,
max_chunk_size,
Expand All @@ -95,17 +78,18 @@ fn spawn_map(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>) {
return;
};
commands.insert_resource(MapEntity(tilemap));
commands.insert_resource(HexagonMeshHandle(Mesh2dHandle(
commands.insert_resource(HexagonMeshHandle(Mesh2d(
meshes.add(RegularPolygon::new(HEXAGON_CIRCUMFERENCE, 6)),
)));

let mut camerabundle = Camera2dBundle::default();
camerabundle.transform = Transform::from_translation(Vec3::new(
(HEXAGON_CIRCUMFERENCE * map_size.x as f32) / 2.0,
-((HEXAGON_CIRCUMFERENCE * map_size.y as f32) / 2.0),
1.0,
commands.spawn((
Camera2d,
Transform::from_translation(Vec3::new(
(HEXAGON_CIRCUMFERENCE * map_size.x as f32) / 2.0,
-((HEXAGON_CIRCUMFERENCE * map_size.y as f32) / 2.0),
1.0,
)),
));
commands.spawn(camerabundle);
}

fn generate_random_tile_data(size_to_generate: UVec2) -> Vec<Vec<TileData>> {
Expand Down Expand Up @@ -141,8 +125,8 @@ fn spawn_tiles(

let hex_layout = HexLayout {
orientation: HEXAGON_ORIENTATION,
origin: Vec2::ZERO,
hex_size: Vec2::splat(HEXAGON_CIRCUMFERENCE),
origin: lettuces::Vec2::ZERO,
hex_size: lettuces::Vec2::splat(HEXAGON_CIRCUMFERENCE),
invert_x: false,
invert_y: false,
};
Expand All @@ -158,17 +142,16 @@ fn spawn_tiles(
let handle = materials.add(color);
color_materials.0.push(handle.clone());
let entity = commands
.spawn(MaterialMesh2dBundle {
mesh: hex_mesh.0.clone(),
material: handle,
transform: Transform::from_translation(
.spawn((
hex_mesh.0.clone(),
MeshMaterial2d(handle),
Transform::from_translation(
hex_layout
.hex_to_world_pos(Hex::new(axial_coords.x, axial_coords.y))
.extend(1.0),
)
.with_rotation(hex_rotation(HEXAGON_ORIENTATION)),
..default()
})
))
.id();
let _ = map.set_tile_entity(axial_coords, entity);
}
Expand Down Expand Up @@ -203,5 +186,7 @@ fn change_random_tile_color(
return;
};

commands.entity(entity).insert(color_handle.clone());
commands
.entity(entity)
.insert(MeshMaterial2d(color_handle.clone()));
}
104 changes: 84 additions & 20 deletions examples/square.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
use rand::Rng;
use std::f32::consts::FRAC_PI_4;

use bevy::diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin};
use bevy::math::UVec2;
use bevy::prelude::{
default, App, Commands, Entity, PluginGroup, Reflect, Resource, Startup, Window, WindowPlugin,
};
use bevy::prelude::*;
use bevy::window::PresentMode;
use bevy::DefaultPlugins;
use bevy_sparse_tilemap::square::map_chunk_layer::{SquareChunkLayer, SquareChunkSettings};
use bevy_sparse_tilemap::square::map_data::SquareMapData;
use bevy_sparse_tilemap::tilemap_builder::tilemap_layer_builder::TilemapLayer;
use bevy_sparse_tilemap::tilemap_builder::TilemapBuilder;
use bevy_sparse_tilemap::{
square::{
map_chunk_layer::{SquareChunkLayer, SquareChunkSettings},
map_data::SquareMapData,
SquareTilemapManager,
},
tilemap_builder::{tilemap_layer_builder::TilemapLayer, TilemapBuilder},
};
use bst_map_layer_derive::MapLayer;
use rand::Rng;
use lettuces::cell::Cell;

fn main() {
App::new()
Expand All @@ -23,11 +26,9 @@ fn main() {
}),
..default()
}))
.add_plugins((
LogDiagnosticsPlugin::default(),
FrameTimeDiagnosticsPlugin::default(),
))
.add_systems(Startup, spawn_map)
.add_plugins((LogDiagnosticsPlugin::default(), FrameTimeDiagnosticsPlugin))
.add_systems(Startup, (spawn_map, spawn_tiles).chain())
.init_resource::<ColorHandles>()
.run();
}

Expand All @@ -41,25 +42,37 @@ pub enum MapLayers {
DenseExtra,
}

/// Change to change the square tile sizes
const TILE_SIZE: f32 = 15.0;

/// Change to change the gap between tiles
const TILE_GAP: f32 = 1.0;

#[derive(Hash, Default, Copy, Clone, Reflect)]
struct TileData(u8, u8);

// Resource to hold our map entity so we can use it in systems
#[derive(Resource)]
pub struct MapEntity(Entity);

fn spawn_map(mut commands: Commands) {
let map_size = UVec2::new(500, 500);
let max_chunk_size = UVec2::new(100, 100);
#[derive(Resource)]
pub struct SquareMeshHandle(Mesh2d);

#[derive(Resource, Default)]
pub struct ColorHandles(Vec<Handle<ColorMaterial>>);

fn spawn_map(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>) {
let map_size = UVec2::new(5, 5);
let max_chunk_size = UVec2::new(1, 1);
Copy link
Owner

@NoahShomette NoahShomette Dec 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: Make the chunk sizes larger

Suggested change
let max_chunk_size = UVec2::new(1, 1);
let max_chunk_size = UVec2::new(3, 3);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I missed this prior to merge!


let mut tilemap_builder =
TilemapBuilder::<TileData, MapLayers, SquareChunkLayer<TileData>, SquareMapData>::new(
TilemapLayer::new_dense_from_vecs(generate_random_tile_data(map_size.clone())),
TilemapLayer::new_dense_from_vecs(generate_random_tile_data(map_size)),
SquareMapData { max_chunk_size },
SquareChunkSettings { max_chunk_size },
);
tilemap_builder.add_layer(
TilemapLayer::new_dense_from_vecs(generate_random_tile_data(map_size.clone())),
TilemapLayer::new_dense_from_vecs(generate_random_tile_data(map_size)),
MapLayers::DenseExtra,
);
tilemap_builder.add_layer(
Expand All @@ -79,6 +92,17 @@ fn spawn_map(mut commands: Commands) {
return;
};
commands.insert_resource(MapEntity(tilemap));
commands.insert_resource(SquareMeshHandle(Mesh2d(
meshes.add(RegularPolygon::new(TILE_SIZE, 4)),
)));
commands.spawn((
Camera2d,
Transform::from_translation(Vec3::new(
(TILE_SIZE * map_size.x as f32) / 2.0,
(TILE_SIZE * map_size.y as f32) / 2.0,
1.0,
)),
));
}

fn generate_random_tile_data(size_to_generate: UVec2) -> Vec<Vec<TileData>> {
Expand All @@ -97,3 +121,43 @@ fn generate_random_tile_data(size_to_generate: UVec2) -> Vec<Vec<TileData>> {
}
vec
}

fn square_to_world_pos(x: i32, y: i32) -> Vec3 {
// The TILE_SIZE is actually the circumferential radius. To find the actual length of a side,
// we need to multiply by the square root of 2.
let side_length = TILE_SIZE * 2_f32.sqrt();
let coords = Vec2::new(x as f32, y as f32) * (side_length + TILE_GAP);
coords.extend(1.)
}

fn spawn_tiles(
map_entity: Res<MapEntity>,
square_mesh: ResMut<SquareMeshHandle>,
mut map: SquareTilemapManager<TileData, MapLayers>,
mut commands: Commands,
mut materials: ResMut<Assets<ColorMaterial>>,
mut color_materials: ResMut<ColorHandles>,
) {
map.set_tilemap_entity(map_entity.0);
map.set_layer(MapLayers::Base);
let Ok(dimensions) = map.dimensions() else {
return;
};

for y in 0..dimensions.y as i32 {
for x in 0..dimensions.x as i32 {
let color = Color::hsl(360. * x as f32 / y as f32, 0.95, 0.7);
let handle = materials.add(color);
color_materials.0.push(handle.clone());
let entity = commands
.spawn((
square_mesh.0.clone(),
MeshMaterial2d(handle),
Transform::from_translation(square_to_world_pos(x, y))
.with_rotation(Quat::from_rotation_z(FRAC_PI_4)),
))
.id();
let _ = map.set_tile_entity(Cell { x, y }, entity);
}
}
}
Loading