From 8d166e628ceb2072e045b9ff6b1dcc1002a34d8e Mon Sep 17 00:00:00 2001 From: Arne Dußin Date: Wed, 3 Feb 2021 23:00:09 +0100 Subject: Add most of the offline functionality back --- src/world/mod.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'src/world/mod.rs') 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 { &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 { &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 { &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 { -- cgit v1.2.3-70-g09d2