Skip to content

Commit e5319b1

Browse files
Merge pull request #586 from rparrett/drain
Add `TileStorage::drain` and return removed entities in `remove`
2 parents ba8d0ff + 8994c09 commit e5319b1

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

examples/spawn_despawn_tilemap.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,8 @@ fn despawn_map(mut commands: Commands, mut maps: Query<(Entity, &mut TileStorage
8484
};
8585

8686
commands.entity(tilemap_entity).despawn_recursive();
87-
88-
for maybe_entity in tile_storage.iter_mut() {
89-
if let Some(entity) = maybe_entity.take() {
90-
commands.entity(entity).despawn();
91-
}
87+
for entity in tile_storage.drain() {
88+
commands.entity(entity).despawn();
9289
}
9390
}
9491

src/tiles/storage.rs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,37 @@ impl TileStorage {
8686
self.tiles.iter_mut()
8787
}
8888

89-
/// Remove entity at the given tile position, if there was one, leaving `None` in its place.
89+
/// Removes any stored `Entity` at the given tile position, leaving `None` in its place and
90+
/// returning the `Entity`.
9091
///
9192
/// Panics if the given `tile_pos` doesn't lie within the extents of the underlying tile map.
92-
pub fn remove(&mut self, tile_pos: &TilePos) {
93-
self.tiles[tile_pos.to_index(&self.size)].take();
93+
pub fn remove(&mut self, tile_pos: &TilePos) -> Option<Entity> {
94+
self.tiles[tile_pos.to_index(&self.size)].take()
9495
}
9596

96-
/// Remove any stored entity at the given tile position, if the given `tile_pos` does lie within
97-
/// the extents of the underlying map.
97+
/// Remove any stored `Entity` at the given tile position, leaving `None` in its place and
98+
/// returning the `Entity`.
9899
///
99-
/// Otherwise, nothing is done.
100-
pub fn checked_remove(&mut self, tile_pos: &TilePos) {
101-
if tile_pos.within_map_bounds(&self.size) {
102-
self.tiles[tile_pos.to_index(&self.size)].take();
103-
}
100+
/// Checks that the given `tile_pos` lies within the extents of the underlying map.
101+
pub fn checked_remove(&mut self, tile_pos: &TilePos) -> Option<Entity> {
102+
self.tiles.get_mut(tile_pos.to_index(&self.size))?.take()
103+
}
104+
105+
/// Removes all stored `Entity`s, leaving `None` in their place and
106+
/// returning them in an iterator.
107+
///
108+
/// Example:
109+
/// ```
110+
/// # use bevy::prelude::Commands;
111+
/// # use bevy_ecs_tilemap::prelude::{TilemapSize, TileStorage};
112+
/// # fn example(mut commands: Commands) {
113+
/// # let mut storage = TileStorage::empty(TilemapSize { x: 16, y: 16 });
114+
/// for entity in storage.drain() {
115+
/// commands.entity(entity).despawn();
116+
/// }
117+
/// # }
118+
/// ```
119+
pub fn drain(&mut self) -> impl Iterator<Item = Entity> + use<'_> {
120+
self.tiles.iter_mut().filter_map(|opt| opt.take())
104121
}
105122
}

0 commit comments

Comments
 (0)