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/client/editor.rs | 28 ++++++++++++++++++++++++---- src/client/map/icon_mark.rs | 5 +++++ src/client/map/mod.rs | 29 ++++++++++++++++++++++------- src/client/map/wall_mark.rs | 6 ++++++ 4 files changed, 57 insertions(+), 11 deletions(-) (limited to 'src/client') diff --git a/src/client/editor.rs b/src/client/editor.rs index e416339..fb271d4 100644 --- a/src/client/editor.rs +++ b/src/client/editor.rs @@ -168,12 +168,32 @@ impl Editor { fn poll_net(&mut self) { while let Some(cargo) = self.server().next_packet() { match cargo { - Cargo::SetRoom((id, new_room)) => { - if let Some(mark) = self.map.get_room_mut(id) { - mark.set_room(new_room); - } else { + Cargo::SetRoom((id, new_room)) => match self.map.get_room_mut(id) { + Some(mark) => mark.set_room(new_room), + None => { self.map.add_room(id, new_room); } + }, + Cargo::SetIcon((id, new_icon)) => match self.map.get_icon_mut(id) { + Some(mark) => mark.set_icon(new_icon), + None => { + self.map.add_icon(id, new_icon); + } + }, + Cargo::SetWall((id, new_wall)) => match self.map.get_wall_mut(id) { + Some(mark) => mark.set_wall(new_wall), + None => { + self.map.add_wall(id, new_wall); + } + }, + Cargo::ClearAll => self.map.clear(), + Cargo::Remove(id) => { + self.map.remove(id); + } + Cargo::AddMapData(_) => unimplemented!(), + Cargo::UpdateMapData(_) => unimplemented!(), + Cargo::AddIcon(_) | Cargo::AddRoom(_) | Cargo::AddWall(_) => { + error!("Packet is only valid in Client -> Server direction") } other => error!("Unknown packet received: {:?}", other), } diff --git a/src/client/map/icon_mark.rs b/src/client/map/icon_mark.rs index 39fd554..02ed501 100644 --- a/src/client/map/icon_mark.rs +++ b/src/client/map/icon_mark.rs @@ -45,6 +45,11 @@ impl IconMark { textures, } } + + /// Set the inner icon this icon mark is referencing. + pub fn set_icon(&mut self, icon: Icon) { + self.icon = icon; + } } impl Mappable for IconMark { diff --git a/src/client/map/mod.rs b/src/client/map/mod.rs index 9589e59..7a0613c 100644 --- a/src/client/map/mod.rs +++ b/src/client/map/mod.rs @@ -16,7 +16,7 @@ pub use wall_mark::*; use crate::client::Transform; use crate::stable_vec::StableVec; -use crate::world::{Room, Wall, World}; +use crate::world::{Icon, Room, Wall, World}; use icon_texture_manager::IconTextureManager; use raylib::drawing::RaylibDrawHandle; use raylib::{RaylibHandle, RaylibThread}; @@ -92,9 +92,11 @@ impl Map { } /// Add an icon with a specific id. May fail if there already is an entity with that id. - pub fn add_icon(&mut self, id: usize, icon: IconMark) -> bool { + pub fn add_icon(&mut self, id: usize, icon: Icon) -> bool { if self.used_ids.try_insert(id, ()).is_ok() { - self.icons.try_insert(id, icon).unwrap(); + self.icons + .try_insert(id, IconMark::from_icon(icon, self.icon_renderer.clone())) + .unwrap(); true } else { error!("Unable to add icon. Id already in use."); @@ -162,6 +164,14 @@ impl Map { ) } + /// Remove all items from the map. + pub fn clear(&mut self) { + self.rooms.clear(); + self.walls.clear(); + self.icons.clear(); + self.used_ids.clear(); + } + /// Get the rooms of this map. pub fn rooms(&self) -> &StableVec { &self.rooms @@ -175,11 +185,19 @@ impl Map { pub fn walls(&self) -> &StableVec { &self.walls } + /// Get a wall with the given id mutably, in case it exists. + pub fn get_wall_mut(&mut self, id: usize) -> Option<&mut WallMark> { + self.walls.get_mut(id) + } /// Get the icons of this map. pub fn icons(&self) -> &StableVec { &self.icons } + /// Get an icon with the given id mutably, in case it exists. + pub fn get_icon_mut(&mut self, id: usize) -> Option<&mut IconMark> { + self.icons.get_mut(id) + } /// Replace the internal map data with the data of the world provided. /// (Load and replace) @@ -199,10 +217,7 @@ impl Map { /// items with the same id will therefore be ignored and not added. pub fn add_data(&mut self, world: World) { for (id, i) in world.icons().iter() { - self.add_icon( - *id, - IconMark::from_icon(i.clone(), self.icon_renderer.clone()), - ); + self.add_icon(*id, i.clone()); } for (id, r) in world.rooms().iter() { self.add_room(*id, r.clone()); diff --git a/src/client/map/wall_mark.rs b/src/client/map/wall_mark.rs index c51af9d..76ac03b 100644 --- a/src/client/map/wall_mark.rs +++ b/src/client/map/wall_mark.rs @@ -32,6 +32,12 @@ impl WallMark { } } + /// Set the internal wall of this wall mark. + pub fn set_wall(&mut self, wall: Wall) { + // XXX: Rounded edges?? + self.wall = wall; + } + /// Get the internal data for serialisation pub fn data(&self) -> &Wall { &self.wall -- cgit v1.2.3-70-g09d2