diff options
| -rw-r--r-- | content/encyclopediae/default_encyclopedia.json | 4 | ||||
| -rw-r--r-- | src/componentwrapper.rs | 1 | ||||
| -rw-r--r-- | src/main.rs | 1 | ||||
| -rw-r--r-- | src/room.rs | 4 | ||||
| -rw-r--r-- | src/world.rs | 23 | ||||
| -rw-r--r-- | todo.md | 4 |
6 files changed, 31 insertions, 6 deletions
diff --git a/content/encyclopediae/default_encyclopedia.json b/content/encyclopediae/default_encyclopedia.json index 19d57d0..28d834d 100644 --- a/content/encyclopediae/default_encyclopedia.json +++ b/content/encyclopediae/default_encyclopedia.json @@ -202,7 +202,7 @@ "name": "seed", "components": [ ["Grow", { - "delay": ["int", 100], + "delay": ["int", 600], "target_time": ["arg", "target_time"], "into": ["template", "radishseedling"] }] @@ -218,7 +218,7 @@ "name": "seedling", "components": [ ["Grow", { - "delay": ["int", 200], + "delay": ["int", 600], "target_time": ["arg", "target_time"], "into": ["template", "radishplant"] }] diff --git a/src/componentwrapper.rs b/src/componentwrapper.rs index 589da15..42eccff 100644 --- a/src/componentwrapper.rs +++ b/src/componentwrapper.rs @@ -196,6 +196,7 @@ components!( into, delay, target_time: if target_time == 0 { None } else { Some(Timestamp(target_time)) } + // please forgive me for using 0 as null }; Equipment () {panic!("equipment from parameters not implemented")}; CreationTime (time: Int) {CreationTime{time: Timestamp(time)}}; diff --git a/src/main.rs b/src/main.rs index 4732d77..68e0f50 100644 --- a/src/main.rs +++ b/src/main.rs @@ -125,6 +125,7 @@ fn main() -> Result<()>{ world.update(); if count % 50 == 0 { world.save(); + world.unload_rooms(); } let messages = world.view(); for (player, mut message) in messages { diff --git a/src/room.rs b/src/room.rs index 9df131a..805f94e 100644 --- a/src/room.rs +++ b/src/room.rs @@ -228,6 +228,10 @@ impl <'a, 'b>Room<'a, 'b> { } } + pub fn has_players(&self) -> bool { + !self.world.read_component::<Player>().is_empty() + } + pub fn save_players(&self) -> HashMap<PlayerId, PlayerState> { let mut states = HashMap::new(); let players = self.world.read_component::<Player>(); diff --git a/src/world.rs b/src/world.rs index ecdc540..50a3a4b 100644 --- a/src/world.rs +++ b/src/world.rs @@ -24,6 +24,7 @@ pub struct World<'a, 'b> { default_room: RoomId, players: HashMap<PlayerId, RoomId>, rooms: HashMap<RoomId, Room<'a, 'b>>, + room_age: HashMap<RoomId, u32>, encyclopedia: Encyclopedia, time: Timestamp } @@ -38,11 +39,13 @@ impl <'a, 'b>World<'a, 'b> { default_room, encyclopedia: encyclopedia.clone(), players: HashMap::new(), - rooms: hashmap!(purgatory::purgatory_id() => purgatory::create_purgatory(encyclopedia)) + rooms: hashmap!(purgatory::purgatory_id() => purgatory::create_purgatory(encyclopedia)), + room_age: HashMap::new() } } fn get_room_mut(&mut self, id: &RoomId) -> Result<&mut Room<'a, 'b>> { + self.room_age.insert(id.clone(), 0); let result = self.get_room_mut_(id); if let Err(err) = &result { println!("Failed to load room {:?}: {:?}", id, err); @@ -169,6 +172,24 @@ impl <'a, 'b>World<'a, 'b> { } } + pub fn unload_rooms(&mut self){ + let mut to_remove = Vec::new(); + for roomid in self.rooms.keys() { + if self.rooms[roomid].has_players() { + self.room_age.insert(roomid.clone(), 0); + } else { + let age = *self.room_age.get(&roomid).unwrap_or(&0) + 1; + self.room_age.insert(roomid.clone(), age); + if age > 2 { + to_remove.push(roomid.clone()); + } + } + } + for roomid in to_remove { + self.rooms.remove(&roomid); + } + } + pub fn view(&self) -> HashMap<PlayerId, WorldMessage> { let mut views = HashMap::new(); for room in self.rooms.values() { @@ -2,11 +2,9 @@ # TODO - make readme -- save time -- timer resource +- timer resource? - log world events to player - draw new entities -- room unloading - relative room locations? - improve error handling - doors |
