summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/purgatory.rs23
-rw-r--r--src/room.rs78
-rw-r--r--src/world.rs27
3 files changed, 56 insertions, 72 deletions
diff --git a/src/purgatory.rs b/src/purgatory.rs
index 41015f4..028df34 100644
--- a/src/purgatory.rs
+++ b/src/purgatory.rs
@@ -2,35 +2,20 @@
use serde_json::json;
use serde::Deserialize;
-use specs::{
- DispatcherBuilder
-};
use crate::{
RoomId,
Encyclopedia,
- room::Room,
- roomtemplate::RoomTemplate,
- systems::{
- Move,
- ControlInput,
- UpdateCooldowns,
- ControlAI,
- }
+ room::{Room, RoomType},
+ roomtemplate::RoomTemplate
};
pub fn purgatory_id() -> RoomId {
RoomId(String::from("+"))
}
-pub fn create_purgatory<'a, 'b>(encyclopedia: &Encyclopedia) -> Room<'a, 'b> {
- let dispatcher = DispatcherBuilder::new()
- .with(UpdateCooldowns, "cool_down", &[])
- .with(ControlInput, "controlinput", &["cool_down"])
- .with(ControlAI, "controlai", &["cool_down"])
- .with(Move, "move", &["controlinput", "controlai"])
- .build();
- let mut room = Room::new(purgatory_id(), encyclopedia.clone(), Some(dispatcher));
+pub fn create_purgatory<'a, 'b>(encyclopedia: &Encyclopedia) -> Room {
+ let mut room = Room::new(purgatory_id(), encyclopedia.clone(), RoomType::Purgatory);
room.load_from_template(&RoomTemplate::deserialize(&json!({
"width": 15,
"height": 20,
diff --git a/src/room.rs b/src/room.rs
index 4f0aa47..e629791 100644
--- a/src/room.rs
+++ b/src/room.rs
@@ -4,8 +4,6 @@ use std::collections::HashMap;
use specs::{
World,
WorldExt,
- DispatcherBuilder,
- Dispatcher,
Join,
Entity,
RunNow
@@ -75,36 +73,18 @@ use crate::{
}
};
-pub fn default_dispatcher<'a, 'b>() -> Dispatcher<'a, 'b> {
- DispatcherBuilder::new()
- .with(Replace, "replace", &[])
- .with(Timeout, "timeout", &[])
- .with(UpdateCooldowns, "cool_down", &[])
- .with(Spawn, "spawn", &[])
- .with(SpawnCheck, "spawncheck", &["spawn"])
- .with(ControlInput, "controlinput", &["cool_down"])
- .with(ControlAI, "controlai", &["cool_down"])
- .with(Take, "take", &["controlinput", "controlai"])
- .with(Use, "use", &["controlinput", "controlai"])
- .with(Interact, "interact", &["controlinput", "controlai"])
- .with(SpawnTrigger, "spawntrigger", &["spawncheck", "replace"])
- .with(Move, "move", &["controlinput", "controlai"])
- .with(Trapping, "trapping", &["move"])
- .with(Fight, "fight", &["move"])
- .with(Heal, "heal", &[])
- .with(Attacking, "attacking", &["use", "trapping", "fight", "heal", "interact", "spawntrigger"])
- .with(Die, "die", &["attacking"])
- .with(DropLoot, "droploot", &["attacking"])
- .with(Building, "building", &["attacking"])
- .with(Migrate, "migrate", &["move", "attacking", "die"])
- .build()
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum RoomType {
+ Normal,
+ Purgatory
}
-pub struct Room<'a, 'b> {
+
+pub struct Room {
world: World,
- dispatcher: Option<Dispatcher<'a, 'b>>,
pub id: RoomId,
- places: HashMap<String, Pos>
+ places: HashMap<String, Pos>,
+ room_type: RoomType
}
macro_rules! register_insert {
@@ -119,9 +99,9 @@ macro_rules! register_insert {
}
-impl <'a, 'b>Room<'a, 'b> {
+impl Room {
- pub fn new(id: RoomId, encyclopedia: Encyclopedia, dispatcher: Option<Dispatcher<'a, 'b>>) -> Room<'a, 'b> {
+ pub fn new(id: RoomId, encyclopedia: Encyclopedia, room_type: RoomType) -> Room {
let mut world = World::new();
world.insert(NewEntities::new(encyclopedia));
register_insert!(
@@ -132,9 +112,9 @@ impl <'a, 'b>Room<'a, 'b> {
Room {
world,
- dispatcher,
id,
- places: HashMap::new()
+ places: HashMap::new(),
+ room_type
}
}
@@ -165,12 +145,36 @@ impl <'a, 'b>Room<'a, 'b> {
self.world.fetch::<Output>().output.clone()
}
- pub fn update(&mut self, timestamp: Timestamp, default_dispatcher: &mut Dispatcher) {
+ pub fn update(&mut self, timestamp: Timestamp) {
self.world.fetch_mut::<Time>().time = timestamp;
- if let Some(dispatcher) = &mut self.dispatcher {
- dispatcher.dispatch(&self.world);
- } else {
- default_dispatcher.dispatch(&self.world);
+ match self.room_type {
+ RoomType::Normal => {
+ Replace.run_now(&self.world);
+ Timeout.run_now(&self.world);
+ UpdateCooldowns.run_now(&self.world);
+ Spawn.run_now(&self.world);
+ SpawnCheck.run_now(&self.world);
+ ControlInput.run_now(&self.world);
+ ControlAI.run_now(&self.world);
+ Take.run_now(&self.world);
+ Use.run_now(&self.world);
+ Interact.run_now(&self.world);
+ SpawnTrigger.run_now(&self.world);
+ Move.run_now(&self.world);
+ Trapping.run_now(&self.world);
+ Fight.run_now(&self.world);
+ Heal.run_now(&self.world);
+ Attacking.run_now(&self.world);
+ Die.run_now(&self.world);
+ DropLoot.run_now(&self.world);
+ Building.run_now(&self.world);
+ Migrate.run_now(&self.world);
+ }
+ RoomType::Purgatory => {
+ UpdateCooldowns.run_now(&self.world);
+ ControlInput.run_now(&self.world);
+ Move.run_now(&self.world);
+ }
}
Create.run_now(&self.world);
Remove.run_now(&self.world);
diff --git a/src/world.rs b/src/world.rs
index 24fe392..400f3e5 100644
--- a/src/world.rs
+++ b/src/world.rs
@@ -1,13 +1,10 @@
use std::collections::HashMap;
-use specs::Dispatcher;
-
use crate::{
PlayerId,
RoomId,
- room::Room,
- room,
+ room::{Room, RoomType},
worldloader::WorldLoader,
persistence::{PersistentStorage, LoaderError},
playerstate::{PlayerState, RoomPos},
@@ -21,16 +18,15 @@ use crate::{
purgatory
};
-pub struct World<'a, 'b> {
+pub struct World {
template_loader: WorldLoader,
persistence: Box<dyn PersistentStorage>,
default_room: RoomId,
players: HashMap<PlayerId, RoomId>,
- rooms: HashMap<RoomId, Room<'a, 'b>>,
+ rooms: HashMap<RoomId, Room>,
room_age: HashMap<RoomId, Timestamp>,
encyclopedia: Encyclopedia,
- pub time: Timestamp,
- default_dispatcher: Dispatcher<'a, 'b>
+ pub time: Timestamp
}
#[derive(Debug)]
@@ -39,7 +35,7 @@ pub enum MigrationError {
RoomError(AnyError)
}
-impl <'a, 'b>World<'a, 'b> {
+impl World {
pub fn new(encyclopedia: Encyclopedia, template_loader: WorldLoader, persistence: Box<dyn PersistentStorage>, default_room: RoomId) -> Self {
let time = match persistence.load_world_meta() {
@@ -59,12 +55,11 @@ impl <'a, 'b>World<'a, 'b> {
encyclopedia: encyclopedia,
players: HashMap::new(),
rooms: HashMap::new(),
- room_age: HashMap::new(),
- default_dispatcher: room::default_dispatcher()
+ room_age: HashMap::new()
}
}
- fn get_room_mut(&mut self, id: &RoomId) -> Result<&mut Room<'a, 'b>> {
+ fn get_room_mut(&mut self, id: &RoomId) -> Result<&mut Room> {
self.room_age.insert(id.clone(), self.time);
let result = self.get_room_mut_(id);
if let Err(err) = &result {
@@ -73,13 +68,13 @@ impl <'a, 'b>World<'a, 'b> {
result
}
- fn get_room_mut_(&mut self, id: &RoomId) -> Result<&mut Room<'a, 'b>> {
+ fn get_room_mut_(&mut self, id: &RoomId) -> Result<&mut Room> {
if !self.rooms.contains_key(id){
println!("loading room '{}'", id);
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 mut room = Room::new(id.clone(), self.encyclopedia.clone(), RoomType::Normal);
let template = self.template_loader.load_room(id.clone())?;
room.load_from_template(&template)?;
room
@@ -93,7 +88,7 @@ impl <'a, 'b>World<'a, 'b> {
}
let last_time = self.time - 1;
if room.get_time() < last_time {
- room.update(last_time, &mut self.default_dispatcher);
+ room.update(last_time);
}
self.rooms.insert(id.clone(), room);
}
@@ -177,7 +172,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, &mut self.default_dispatcher);
+ room.update(self.time);
}
self.time.0 += 1;
}