diff --git a/Cargo.lock b/Cargo.lock index 86a6afc..27e0050 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -699,6 +699,7 @@ dependencies = [ "bevy-inspector-egui", "bevy_asset_loader", "bevy_ecs_ldtk", + "bevy_ecs_tilemap", "bevy_kira_audio", "embed-resource", "image", diff --git a/Cargo.toml b/Cargo.toml index 87a3e45..5c58660 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -60,12 +60,13 @@ bevy = { version = "0.12", default-features = false, features = [ ] } bevy_kira_audio = { version = "0.18" } bevy_asset_loader = { version = "0.18", features = ["2d"] } -rand = { version = "0.8.3" } +rand = { version = "0.8.3", features = [] } webbrowser = { version = "0.8", features = ["hardened"] } # keep the following in sync with Bevy's dependencies winit = { version = "0.28.7", default-features = false } image = { version = "0.24", default-features = false } +bevy_ecs_tilemap = { git = "https://github.com/divark/bevy_ecs_tilemap", branch = "0.12-fixes" } bevy_ecs_ldtk = { git = "https://github.com/Trouv/bevy_ecs_ldtk.git", branch = "feat/bevy-0.12" } bevy-inspector-egui = "0.21.0" quadtree = { git = "https://github.com/MickHarrigan/quadtree.git" } diff --git a/assets/textures/ground/ground.png b/assets/textures/ground/ground.png index 9152306..7f21173 100644 Binary files a/assets/textures/ground/ground.png and b/assets/textures/ground/ground.png differ diff --git a/src/camera.rs b/src/camera.rs index 604524f..3bcc330 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -1,7 +1,7 @@ use bevy::input::mouse::MouseWheel; use bevy::prelude::*; use crate::GameState; -use crate::world::LevelData; +use crate::tilemap::LevelData; pub struct CameraPlugin; diff --git a/src/lib.rs b/src/lib.rs index 66e0e00..f444862 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,6 +21,7 @@ mod boids; mod debug; mod world; mod interactions; +mod tilemap; use crate::actions::ActionsPlugin; use crate::audio::InternalAudioPlugin; @@ -32,6 +33,7 @@ use crate::player::PlayerPlugin; use crate::bees::BeesPlugin; use crate::world::WorldPlugin; use crate::camera::CameraPlugin; +use crate::tilemap::MapPlugin; use bevy::app::App; #[cfg(debug_assertions)] @@ -68,16 +70,17 @@ impl Plugin for GamePlugin { // InternalAudioPlugin, // PlayerPlugin, CameraPlugin, - WorldPlugin, - InteractionsPlugin, - BeesPlugin, + MapPlugin, + // WorldPlugin, + // InteractionsPlugin, + // BeesPlugin, )); #[cfg(debug_assertions)] { app.add_plugins(( DebugPlugin, - FrameTimeDiagnosticsPlugin, + // FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin::default(), WorldInspectorPlugin::new(), )) diff --git a/src/loading.rs b/src/loading.rs index 7e07651..dc5690f 100644 --- a/src/loading.rs +++ b/src/loading.rs @@ -57,4 +57,6 @@ pub struct TextureAssets { ))] #[asset(path = "textures/airplanes.png")] pub planes: Handle, + #[asset(path = "textures/ground/ground.png")] + pub ground: Handle } diff --git a/src/tilemap.rs b/src/tilemap.rs new file mode 100644 index 0000000..990f7fc --- /dev/null +++ b/src/tilemap.rs @@ -0,0 +1,82 @@ +use crate::GameState; +use bevy::prelude::*; +use bevy_ecs_tilemap::prelude::*; +use crate::loading::TextureAssets; + +use rand::Rng; + +pub struct MapPlugin; +impl Plugin for MapPlugin { + fn build(&self, app: &mut App) { + app + .add_plugins(TilemapPlugin) + .add_systems(OnEnter(GameState::Playing), setup_level) + ; + } +} + +#[derive(Resource, Debug)] +pub struct LevelData { + pub level_height: f32, + pub level_width: f32, +} + +fn setup_level( + mut commands: Commands, + // asset_server: Res, + textures: Res, + #[cfg(all(not(feature = "atlas"), feature = "render"))] + array_texture_loader: Res, +) { + let texture_handle: Handle = textures.ground.clone(); + + let map_size = TilemapSize { x: 16, y: 16 }; + + let tilemap_entity = commands.spawn_empty().id(); + + let mut tile_storage = TileStorage::empty(map_size); + let mut rng = rand::thread_rng(); + + for x in 0..map_size.x { + for y in 0..map_size.y { + let tile_pos = TilePos{ x , y }; + let tile_entity = commands + .spawn(TileBundle { + position: tile_pos, + tilemap_id: TilemapId(tilemap_entity), + texture_index: TileTextureIndex(rng.gen_range(0..3)), + ..Default::default() + }) + .id(); + tile_storage.set(&tile_pos, tile_entity); + } + } + + let tile_size = TilemapTileSize{ x: 720.0, y: 720.0 }; + let grid_size: TilemapGridSize = tile_size.into(); + commands.insert_resource(LevelData {level_height: grid_size.y, level_width: grid_size.x}); + let map_type = TilemapType::default(); + + commands.entity(tilemap_entity).insert(TilemapBundle { + grid_size, + map_type, + size: map_size, + storage: tile_storage, + texture: TilemapTexture::Single(texture_handle), + tile_size, + transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0), + ..Default::default() + }); + + // Add atlas to array texture loader so it's preprocessed before we need to use it. + // Only used when the atlas feature is off and we are using array textures. + #[cfg(all(not(feature = "atlas"), feature = "render"))] + { + array_texture_loader.add(TilemapArrayTexture { + texture: TilemapTexture::Single(asset_server.load("tiles.png")), + tile_size, + ..Default::default() + }); + } + +} \ No newline at end of file