From a12b0fe29646d227b50fe820d4777c287356b776 Mon Sep 17 00:00:00 2001 From: Uggla Date: Thu, 18 Apr 2024 22:52:40 +0200 Subject: [PATCH 1/3] Add a system to hide tiles. Here we can see a bug as only the castle tiles are hidden. --- examples/tiled.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/examples/tiled.rs b/examples/tiled.rs index 79e59292..1f1db0cd 100644 --- a/examples/tiled.rs +++ b/examples/tiled.rs @@ -1,6 +1,8 @@ use bevy::prelude::*; use bevy_ecs_tilemap::prelude::*; +use crate::helpers::tiled::{TiledLayersStorage, TiledMap}; + mod helpers; fn startup(mut commands: Commands, asset_server: Res) { @@ -31,5 +33,27 @@ fn main() { .add_plugins(helpers::tiled::TiledMapPlugin) .add_systems(Startup, startup) .add_systems(Update, helpers::camera::movement) + .add_systems(Update, hide_tiles) .run(); } + +fn hide_tiles( + keyboard_input: Res>, + tile_storage_query: Query<&TileStorage>, + mut map_query: Query<&TiledLayersStorage>, + mut tile_query: Query<&mut TileVisible>, +) { + if keyboard_input.pressed(KeyCode::Space) { + for layer_storage in map_query.iter_mut() { + for layer_entity in layer_storage.storage.values() { + if let Ok(layer_tile_storage) = tile_storage_query.get(*layer_entity) { + for tile in layer_tile_storage.iter().flatten() { + if let Ok(mut t) = tile_query.get_mut(*tile) { + t.0 = false; + } + } + } + } + } + } +} From 17c73b2cc7a7e3912849702b57acb1aa43b45fcd Mon Sep 17 00:00:00 2001 From: Uggla Date: Sun, 21 Apr 2024 18:40:50 +0200 Subject: [PATCH 2/3] Fix the tiled helpers to also select tileset. - Rename TiledLayersStorage to TilesetLayerToStorageEntity. - Change the structure to an Hash of hash: Tileset -hash-> Layer -hash-> Storage entity --- examples/helpers/tiled.rs | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/examples/helpers/tiled.rs b/examples/helpers/tiled.rs index e36f9a1a..3e71a76e 100644 --- a/examples/helpers/tiled.rs +++ b/examples/helpers/tiled.rs @@ -18,7 +18,6 @@ use std::sync::Arc; use bevy::{ asset::{io::Reader, AssetLoader, AssetPath, AsyncReadExt}, - log, prelude::{ Added, Asset, AssetApp, AssetEvent, AssetId, Assets, Bundle, Commands, Component, DespawnRecursiveExt, Entity, EventReader, GlobalTransform, Handle, Image, Plugin, Query, @@ -54,15 +53,24 @@ pub struct TiledMap { } // Stores a list of tiled layers. -#[derive(Component, Default)] -pub struct TiledLayersStorage { - pub storage: HashMap, +#[derive(Component, Default, Debug)] +pub struct TilesetLayerToStorageEntity { + pub storage: HashMap>, +} + +impl TilesetLayerToStorageEntity { + pub fn get_entities(&self) -> Vec<&Entity> { + self.storage + .values() + .flat_map(|layer| layer.values()) + .collect() + } } #[derive(Default, Bundle)] pub struct TiledMapBundle { pub tiled_map: Handle, - pub storage: TiledLayersStorage, + pub storage: TilesetLayerToStorageEntity, pub transform: Transform, pub global_transform: GlobalTransform, pub render_settings: TilemapRenderSettings, @@ -203,7 +211,7 @@ pub fn process_loaded_maps( tile_storage_query: Query<(Entity, &TileStorage)>, mut map_query: Query<( &Handle, - &mut TiledLayersStorage, + &mut TilesetLayerToStorageEntity, &TilemapRenderSettings, )>, new_maps: Query<&Handle, Added>>, @@ -235,21 +243,21 @@ pub fn process_loaded_maps( } for changed_map in changed_maps.iter() { - for (map_handle, mut layer_storage, render_settings) in map_query.iter_mut() { + for (map_handle, mut tileset_layer_entity, render_settings) in map_query.iter_mut() { // only deal with currently changed map if map_handle.id() != *changed_map { continue; } if let Some(tiled_map) = maps.get(map_handle) { // TODO: Create a RemoveMap component.. - for layer_entity in layer_storage.storage.values() { - if let Ok((_, layer_tile_storage)) = tile_storage_query.get(*layer_entity) { - for tile in layer_tile_storage.iter().flatten() { + for entity in tileset_layer_entity.get_entities() { + if let Ok((_, map_tiles)) = tile_storage_query.get(*entity) { + for tile in map_tiles.iter().flatten() { commands.entity(*tile).despawn_recursive() } } - // commands.entity(*layer_entity).despawn_recursive(); } + // commands.entity(*layer_entity).despawn_recursive(); // The TilemapBundle requires that all tile images come exclusively from a single // tiled texture or from a Vec of independent per-tile images. Furthermore, all of @@ -274,6 +282,7 @@ pub fn process_loaded_maps( }; // Once materials have been created/added we need to then create the layers. + let mut layers_map = HashMap::new(); for (layer_index, layer) in tiled_map.map.layers().enumerate() { let offset_x = layer.offset_x; let offset_y = layer.offset_y; @@ -391,10 +400,11 @@ pub fn process_loaded_maps( ..Default::default() }); - layer_storage - .storage - .insert(layer_index as u32, layer_entity); + layers_map.insert(layer_index as u32, layer_entity); } + tileset_layer_entity + .storage + .insert(tileset_index as u32, layers_map); } } } From d1b2763487a558c0b7b3378dce5ece9126f3201a Mon Sep 17 00:00:00 2001 From: Uggla Date: Sun, 21 Apr 2024 18:44:58 +0200 Subject: [PATCH 3/3] Improve example allowing to hide/show tiles. - Space key show/hide all tiles. - C key show/hide the castle tiles only. --- examples/tiled.rs | 55 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/examples/tiled.rs b/examples/tiled.rs index 1f1db0cd..85bd19bb 100644 --- a/examples/tiled.rs +++ b/examples/tiled.rs @@ -1,10 +1,13 @@ -use bevy::prelude::*; +use bevy::{ecs::query::QueryFilter, prelude::*}; use bevy_ecs_tilemap::prelude::*; -use crate::helpers::tiled::{TiledLayersStorage, TiledMap}; +use crate::helpers::tiled::TilesetLayerToStorageEntity; mod helpers; +#[derive(Resource, Deref, DerefMut)] +pub struct DebouncedTimer(Timer); + fn startup(mut commands: Commands, asset_server: Res) { commands.spawn(Camera2dBundle::default()); @@ -31,28 +34,50 @@ fn main() { ) .add_plugins(TilemapPlugin) .add_plugins(helpers::tiled::TiledMapPlugin) + .insert_resource(DebouncedTimer(Timer::from_seconds(0.5, TimerMode::Once))) .add_systems(Startup, startup) .add_systems(Update, helpers::camera::movement) - .add_systems(Update, hide_tiles) + .add_systems(Update, show_hide_tiles) .run(); } -fn hide_tiles( +fn show_hide_tiles( + time: Res