diff options
Diffstat (limited to 'src/world')
| -rw-r--r-- | src/world/mod.rs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/world/mod.rs b/src/world/mod.rs index a72031d..9ddd9e9 100644 --- a/src/world/mod.rs +++ b/src/world/mod.rs @@ -44,6 +44,32 @@ impl World { } } + /// Remove all items from the world, leaving it empty. + pub fn clear(&mut self) { + self.rooms.clear(); + self.walls.clear(); + self.icons.clear(); + self.used_ids.clear(); + } + + /// Remove the item with the given id. Returns `false` in case no item with + /// such an id existed in the first place. + pub fn remove(&mut self, id: usize) -> bool { + if self.used_ids.remove(id).is_none() { + return false; + } + + if self.rooms.remove(id).is_some() { + true + } else if self.walls.remove(id).is_some() { + true + } else if self.icons.remove(id).is_some() { + true + } else { + panic!("used_ids and the actual ids are out of sync") + } + } + /// Create a world from the raw parts of the world. The components must fit, /// meaning no id must exist twice and the used ids must correspond to the /// actually used ids. Use with care. @@ -127,16 +153,40 @@ impl World { pub fn icons(&self) -> &StableVec<Icon> { &self.icons } + /// Get an icon mutably. + pub fn get_icon_mut(&mut self, id: usize) -> Option<&mut Icon> { + self.icons.get_mut(id) + } /// Get all rooms of the world. pub fn rooms(&self) -> &StableVec<Room> { &self.rooms } + /// Get a room mutably. + pub fn get_room_mut(&mut self, id: usize) -> Option<&mut Room> { + self.rooms.get_mut(id) + } /// Get all walls of the world. pub fn walls(&self) -> &StableVec<Wall> { &self.walls } + /// Get a wall mutably. + pub fn get_wall_mut(&mut self, id: usize) -> Option<&mut Wall> { + self.walls.get_mut(id) + } + + pub fn get_mut(&mut self, id: usize) -> Option<&mut dyn Component> { + if let Some(c) = self.rooms.get_mut(id) { + Some(c as &mut dyn Component) + } else if let Some(c) = self.walls.get_mut(id) { + Some(c as &mut dyn Component) + } else if let Some(c) = self.icons.get_mut(id) { + Some(c as &mut dyn Component) + } else { + None + } + } } impl Default for World { |
