From 6766785374180fb50ccbd44875daff45d6c19c70 Mon Sep 17 00:00:00 2001 From: Rob Parrett Date: Mon, 23 Dec 2024 05:58:18 -0700 Subject: [PATCH 1/4] Add `TileStorage::drain` and return removed entities in `remove` --- examples/spawn_despawn_tilemap.rs | 7 ++---- src/tiles/storage.rs | 37 ++++++++++++++++++++++++------- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/examples/spawn_despawn_tilemap.rs b/examples/spawn_despawn_tilemap.rs index f8b924fa..289c277b 100644 --- a/examples/spawn_despawn_tilemap.rs +++ b/examples/spawn_despawn_tilemap.rs @@ -84,11 +84,8 @@ fn despawn_map(mut commands: Commands, mut maps: Query<(Entity, &mut TileStorage }; commands.entity(tilemap_entity).despawn_recursive(); - - for maybe_entity in tile_storage.iter_mut() { - if let Some(entity) = maybe_entity.take() { - commands.entity(entity).despawn(); - } + for entity in tile_storage.drain() { + commands.entity(entity).despawn() } } diff --git a/src/tiles/storage.rs b/src/tiles/storage.rs index 15a71928..f40edb7c 100644 --- a/src/tiles/storage.rs +++ b/src/tiles/storage.rs @@ -86,20 +86,41 @@ impl TileStorage { self.tiles.iter_mut() } - /// Remove entity at the given tile position, if there was one, leaving `None` in its place. + /// Removes any stored `Entity` at the given tile position, leaving `None` in its place and + /// returning the `Entity`. /// /// Panics if the given `tile_pos` doesn't lie within the extents of the underlying tile map. - pub fn remove(&mut self, tile_pos: &TilePos) { - self.tiles[tile_pos.to_index(&self.size)].take(); + pub fn remove(&mut self, tile_pos: &TilePos) -> Option { + self.tiles[tile_pos.to_index(&self.size)].take() } - /// Remove any stored entity at the given tile position, if the given `tile_pos` does lie within - /// the extents of the underlying map. + /// Remove any stored `Entity` at the given tile position, leaving `None` in its place and + /// returning the `Entity`. /// - /// Otherwise, nothing is done. - pub fn checked_remove(&mut self, tile_pos: &TilePos) { + /// Checks that the given `tile_pos` lies within the extents of the underlying map. + pub fn checked_remove(&mut self, tile_pos: &TilePos) -> Option { if tile_pos.within_map_bounds(&self.size) { - self.tiles[tile_pos.to_index(&self.size)].take(); + return self.tiles[tile_pos.to_index(&self.size)].take(); } + + None + } + + /// Removes all stored `Entity`s, leaving `None` in their place and + /// returning them in an iterator. + /// + /// Example: + /// ``` + /// # use bevy::prelude::Commands; + /// # use bevy_ecs_tilemap::prelude::{TilemapSize, TileStorage}; + /// # fn example(mut commands: Commands) { + /// # let mut storage = TileStorage::empty(TilemapSize { x: 16, y: 16 }); + /// for entity in storage.drain() { + /// commands.entity(entity).despawn(); + /// } + /// # } + /// ``` + pub fn drain(&mut self) -> impl Iterator + use<'_> { + self.tiles.iter_mut().filter_map(|opt| opt.take()) } } From 788352ba416509434ac1d114590b1378fabc8986 Mon Sep 17 00:00:00 2001 From: Rob Parrett Date: Mon, 23 Dec 2024 06:48:10 -0700 Subject: [PATCH 2/4] Semicolon --- examples/spawn_despawn_tilemap.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/spawn_despawn_tilemap.rs b/examples/spawn_despawn_tilemap.rs index 289c277b..2a9fb320 100644 --- a/examples/spawn_despawn_tilemap.rs +++ b/examples/spawn_despawn_tilemap.rs @@ -85,7 +85,7 @@ fn despawn_map(mut commands: Commands, mut maps: Query<(Entity, &mut TileStorage commands.entity(tilemap_entity).despawn_recursive(); for entity in tile_storage.drain() { - commands.entity(entity).despawn() + commands.entity(entity).despawn(); } } From 31af881c9105636008e6f53370fb4f70ca71b563 Mon Sep 17 00:00:00 2001 From: Chris Biscardi Date: Mon, 23 Dec 2024 11:52:20 -0800 Subject: [PATCH 3/4] Update src/tiles/storage.rs Co-authored-by: Rob Parrett --- src/tiles/storage.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/tiles/storage.rs b/src/tiles/storage.rs index f40edb7c..91de06ae 100644 --- a/src/tiles/storage.rs +++ b/src/tiles/storage.rs @@ -100,10 +100,7 @@ impl TileStorage { /// Checks that the given `tile_pos` lies within the extents of the underlying map. pub fn checked_remove(&mut self, tile_pos: &TilePos) -> Option { if tile_pos.within_map_bounds(&self.size) { - return self.tiles[tile_pos.to_index(&self.size)].take(); - } - - None + self.tiles.get_mut(tile_pos.to_index(&self.size))?.take() } /// Removes all stored `Entity`s, leaving `None` in their place and From 8994c090e2878aa4ce1552ca7ca93d561217e696 Mon Sep 17 00:00:00 2001 From: Rob Parrett Date: Mon, 23 Dec 2024 13:12:15 -0700 Subject: [PATCH 4/4] Fixup botched GH suggestion --- src/tiles/storage.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/tiles/storage.rs b/src/tiles/storage.rs index 91de06ae..4988c854 100644 --- a/src/tiles/storage.rs +++ b/src/tiles/storage.rs @@ -99,7 +99,6 @@ impl TileStorage { /// /// Checks that the given `tile_pos` lies within the extents of the underlying map. pub fn checked_remove(&mut self, tile_pos: &TilePos) -> Option { - if tile_pos.within_map_bounds(&self.size) { self.tiles.get_mut(tile_pos.to_index(&self.size))?.take() }