@@ -86,20 +86,37 @@ impl TileStorage {
86
86
self . tiles . iter_mut ( )
87
87
}
88
88
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`.
90
91
///
91
92
/// 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 ( )
94
95
}
95
96
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` .
98
99
///
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 ( ) )
104
121
}
105
122
}
0 commit comments