diff options
| author | troido <troido@protonmail.com> | 2020-04-08 09:54:20 +0200 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-04-08 09:54:20 +0200 |
| commit | 7a4a62eb2804b6b19b4e71eee42d6b3d7ad08a3a (patch) | |
| tree | a3f5fe0b6fdb5ab913bbaafb48f090d217428479 /src | |
| parent | 5bef1829e443985e960a3cb64106d3f2e3dbf420 (diff) | |
rooms now share the same default dispatcher
Diffstat (limited to 'src')
| -rw-r--r-- | src/purgatory.rs | 3 | ||||
| -rw-r--r-- | src/room.rs | 19 | ||||
| -rw-r--r-- | src/world.rs | 18 |
3 files changed, 23 insertions, 17 deletions
diff --git a/src/purgatory.rs b/src/purgatory.rs index 12be7cc..f5f8c06 100644 --- a/src/purgatory.rs +++ b/src/purgatory.rs @@ -1,4 +1,5 @@ + use serde_json::json; use specs::{ DispatcherBuilder @@ -30,7 +31,7 @@ pub fn create_purgatory<'a, 'b>(encyclopedia: &Encyclopedia) -> Room<'a, 'b> { .with(ControlAI, "controlai", &["cool_down"]) .with(Move, "move", &["controlinput", "controlai"]) .build(); - let mut room = Room::new(purgatory_id(), encyclopedia.clone(), dispatcher); + let mut room = Room::new(purgatory_id(), encyclopedia.clone(), Some(dispatcher)); room.load_from_template(&RoomTemplate::from_json(&json!({ "width": 15, "height": 20, diff --git a/src/room.rs b/src/room.rs index 1bdb5b5..0bb2f61 100644 --- a/src/room.rs +++ b/src/room.rs @@ -94,7 +94,7 @@ pub fn default_dispatcher<'a, 'b>() -> Dispatcher<'a, 'b> { pub struct Room<'a, 'b> { world: World, - dispatcher: Dispatcher<'a, 'b>, + dispatcher: Option<Dispatcher<'a, 'b>>, pub id: RoomId, places: HashMap<String, Pos> } @@ -113,7 +113,7 @@ macro_rules! register_insert { impl <'a, 'b>Room<'a, 'b> { - pub fn new(id: RoomId, encyclopedia: Encyclopedia, dispatcher: Dispatcher<'a, 'b>) -> Room<'a, 'b> { + pub fn new(id: RoomId, encyclopedia: Encyclopedia, dispatcher: Option<Dispatcher<'a, 'b>>) -> Room<'a, 'b> { let mut world = World::new(); world.insert(NewEntities::new(encyclopedia)); register_insert!( @@ -152,20 +152,17 @@ impl <'a, 'b>Room<'a, 'b> { Ok(()) } - - pub fn create(id: RoomId, encyclopedia: &Encyclopedia, template: &RoomTemplate) -> Result<Room<'a, 'b>> { - let mut room = Self::new(id, encyclopedia.clone(), default_dispatcher()); - room.load_from_template(template)?; - Ok(room) - } - pub fn view(&self) -> HashMap<PlayerId, WorldMessage> { self.world.fetch::<Output>().output.clone() } - pub fn update(&mut self, timestamp: Timestamp) { + pub fn update(&mut self, timestamp: Timestamp, default_dispatcher: &mut Dispatcher) { self.world.fetch_mut::<Time>().time = timestamp; - self.dispatcher.dispatch(&self.world); + if let Some(dispatcher) = &mut self.dispatcher { + dispatcher.dispatch(&self.world); + } else { + default_dispatcher.dispatch(&self.world); + } Create.run_now(&self.world); Remove.run_now(&self.world); self.world.maintain(); diff --git a/src/world.rs b/src/world.rs index 1ffacdc..59b3aba 100644 --- a/src/world.rs +++ b/src/world.rs @@ -1,10 +1,13 @@ use std::collections::HashMap; +use specs::Dispatcher; + use crate::{ PlayerId, RoomId, room::Room, + room, worldloader::WorldLoader, persistence::PersistentStorage, playerstate::{PlayerState, RoomPos}, @@ -25,9 +28,11 @@ pub struct World<'a, 'b> { rooms: HashMap<RoomId, Room<'a, 'b>>, room_age: HashMap<RoomId, u32>, encyclopedia: Encyclopedia, - time: Timestamp + time: Timestamp, + default_dispatcher: Dispatcher<'a, 'b> } + impl <'a, 'b>World<'a, 'b> { pub fn new(encyclopedia: Encyclopedia, template_loader: WorldLoader, persistence: Box<dyn PersistentStorage>, default_room: RoomId) -> Self { @@ -39,7 +44,8 @@ impl <'a, 'b>World<'a, 'b> { encyclopedia: encyclopedia, players: HashMap::new(), rooms: HashMap::new(), - room_age: HashMap::new() + room_age: HashMap::new(), + default_dispatcher: room::default_dispatcher() } } @@ -58,15 +64,17 @@ impl <'a, 'b>World<'a, 'b> { let mut room: Room = if id == &purgatory::purgatory_id() { purgatory::create_purgatory(&self.encyclopedia) } else { + let mut room = Room::new(id.clone(), self.encyclopedia.clone(), None); let template = self.template_loader.load_room(id.clone())?; - Room::create(id.clone(), &self.encyclopedia, &template)? + room.load_from_template(&template); + room }; if let Ok(state) = self.persistence.load_room(id.clone()){ room.load_saved(&state); } let last_time = self.time - 1; if room.get_time() < last_time { - room.update(last_time); + room.update(last_time, &mut self.default_dispatcher); } self.rooms.insert(id.clone(), room); } @@ -145,7 +153,7 @@ impl <'a, 'b>World<'a, 'b> { pub fn update(&mut self) { self.migrate(); for room in self.rooms.values_mut() { - room.update(self.time); + room.update(self.time, &mut self.default_dispatcher); } self.time.0 += 1; } |
