diff options
| author | troido <troido@protonmail.com> | 2020-02-28 16:41:11 +0100 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-02-28 16:41:11 +0100 |
| commit | c846e929a88837094d7a5383a306df1fec56c333 (patch) | |
| tree | 48ab058d58c090ff00199af6b3eef7d64c7c2c95 | |
| parent | b80036ef629e0803f3a70059a8fa12d1a2ae3b2d (diff) | |
more specific types!
| -rw-r--r-- | src/components/mod.rs | 7 | ||||
| -rw-r--r-- | src/encyclopedia.rs | 7 | ||||
| -rw-r--r-- | src/main.rs | 4 | ||||
| -rw-r--r-- | src/resources/mod.rs | 5 | ||||
| -rw-r--r-- | src/room.rs | 7 | ||||
| -rw-r--r-- | src/template.rs | 13 | ||||
| -rw-r--r-- | src/timestamp.rs | 19 | ||||
| -rw-r--r-- | src/world.rs | 11 |
8 files changed, 51 insertions, 22 deletions
diff --git a/src/components/mod.rs b/src/components/mod.rs index 3b0167d..6fae490 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -22,7 +22,8 @@ use crate::{ controls::Control, Template, playerstate::RoomPos, - attack::Attack + attack::Attack, + Timestamp }; #[derive(Debug, Clone)] @@ -167,7 +168,7 @@ pub struct Fighter { pub struct Healing { pub delay: i64, pub health: i64, - pub next_heal: Option<i64> + pub next_heal: Option<Timestamp> } @@ -175,7 +176,7 @@ pub struct Healing { #[storage(HashMapStorage)] pub struct Volatile { pub delay: i64, - pub end_time: Option<i64> + pub end_time: Option<Timestamp> } #[derive(Component, Debug, Clone)] diff --git a/src/encyclopedia.rs b/src/encyclopedia.rs index a7bb5b6..7b40373 100644 --- a/src/encyclopedia.rs +++ b/src/encyclopedia.rs @@ -4,12 +4,13 @@ use serde_json::Value; use crate::{ assemblage::Assemblage, componentwrapper::PreEntity, - Template + Template, + template::EntityType }; #[derive(Default, Clone)] pub struct Encyclopedia { - items: HashMap<String, Assemblage> + items: HashMap<EntityType, Assemblage> } impl Encyclopedia { @@ -17,7 +18,7 @@ impl Encyclopedia { pub fn from_json(val: Value) -> Result<Encyclopedia, &'static str> { let mut items = HashMap::new(); for (k, v) in val.as_object().ok_or("encyclopedia not a json object")?.into_iter() { - items.insert(k.clone(), Assemblage::from_json(v)?); + items.insert(EntityType(k.clone()), Assemblage::from_json(v)?); } Ok(Encyclopedia{items}) } diff --git a/src/main.rs b/src/main.rs index 6a2d863..ed7de08 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,6 +34,7 @@ mod worldloader; mod world; mod sprite; mod attack; +mod timestamp; pub use self::{ pos::Pos, @@ -42,7 +43,8 @@ pub use self::{ util::Result, sprite::Sprite, template::Template, - encyclopedia::Encyclopedia + encyclopedia::Encyclopedia, + timestamp::Timestamp }; use self::{ diff --git a/src/resources/mod.rs b/src/resources/mod.rs index 2f218fe..4ce0e59 100644 --- a/src/resources/mod.rs +++ b/src/resources/mod.rs @@ -14,7 +14,8 @@ use crate::{ worldmessages::WorldMessage, PlayerId, RoomId, - playerstate::RoomPos + playerstate::RoomPos, + Timestamp }; @@ -52,5 +53,5 @@ pub struct Emigration { #[derive(Default)] pub struct TimeStamp { - pub time: i64 + pub time: Timestamp } diff --git a/src/room.rs b/src/room.rs index f3861dc..81b8cd0 100644 --- a/src/room.rs +++ b/src/room.rs @@ -59,7 +59,8 @@ use crate::{ PlayerId, RoomId, aerr, - Result + Result, + Timestamp }; @@ -142,7 +143,7 @@ impl <'a, 'b>Room<'a, 'b> { self.world.fetch::<Output>().output.clone() } - pub fn update(&mut self, timestamp: i64) { + pub fn update(&mut self, timestamp: Timestamp) { self.world.fetch_mut::<TimeStamp>().time = timestamp; self.dispatcher.dispatch(&self.world); self.world.maintain(); @@ -241,7 +242,7 @@ impl <'a, 'b>Room<'a, 'b> { emigrants } - pub fn get_time(&self) -> i64 { + pub fn get_time(&self) -> Timestamp { self.world.fetch::<TimeStamp>().time } } diff --git a/src/template.rs b/src/template.rs index 805a8a3..5539f94 100644 --- a/src/template.rs +++ b/src/template.rs @@ -8,9 +8,12 @@ use crate::{ aerr }; +#[derive(Debug, PartialEq, Eq, Clone, Hash)] +pub struct EntityType(pub String); + #[derive(Debug, Clone, PartialEq)] pub struct Template { - pub name: String, + pub name: EntityType, pub args: Vec<Parameter>, pub kwargs: HashMap<String, Parameter>, pub save: bool @@ -21,7 +24,7 @@ impl Template { pub fn new(name: &str, kwargs: HashMap<String, Parameter>) -> Self { Self { - name: name.to_string(), + name: EntityType(name.to_string()), args: Vec::new(), kwargs, save: true @@ -41,7 +44,7 @@ impl Template { if val.is_string(){ return Ok(Self::empty(val.as_str().ok_or(aerr!("json string is not a string?"))?)); } - let name = val.get("type").ok_or(aerr!("template doesn't have 'type'"))?.as_str().ok_or(aerr!("template type not a string"))?.to_string(); + let name = EntityType(val.get("type").ok_or(aerr!("template doesn't have 'type'"))?.as_str().ok_or(aerr!("template type not a string"))?.to_string()); let mut args = Vec::new(); for arg in val.get("args").unwrap_or(&json!([])).as_array().ok_or(aerr!("template args not an array"))? { args.push(Parameter::guess_from_json(arg).ok_or(aerr!("template arg not a parameter"))?); @@ -55,12 +58,12 @@ impl Template { pub fn to_json(&self) -> Value { if self.args.is_empty() && self.kwargs.is_empty() { - return json!(self.name); + return json!(self.name.0); } let jsonargs: Vec<Value> = self.args.iter().map(|a| a.to_json()).collect(); let jsonkwargs: HashMap<&String, Value> = self.kwargs.iter().map(|(k, a)| (k, a.to_json())).collect(); json!({ - "type": self.name, + "type": self.name.0, "args": jsonargs, "kwargs": jsonkwargs }) diff --git a/src/timestamp.rs b/src/timestamp.rs new file mode 100644 index 0000000..645fb0d --- /dev/null +++ b/src/timestamp.rs @@ -0,0 +1,19 @@ + +use std::ops::{Add, Sub}; + +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +pub struct Timestamp(pub i64); + +impl Add<i64> for Timestamp { + type Output = Self; + fn add(self, other: i64) -> Self { + Self(self.0 + other) + } +} + +impl Sub<i64> for Timestamp { + type Output = Self; + fn sub(self, other: i64) -> Self { + Self(self.0 - other) + } +} diff --git a/src/world.rs b/src/world.rs index aed60ff..4152889 100644 --- a/src/world.rs +++ b/src/world.rs @@ -13,6 +13,7 @@ use crate::{ Result, aerr, worldmessages::WorldMessage, + Timestamp }; pub struct World<'a, 'b> { @@ -22,7 +23,7 @@ pub struct World<'a, 'b> { players: HashMap<PlayerId, RoomId>, rooms: HashMap<RoomId, Room<'a, 'b>>, encyclopedia: Encyclopedia, - timestamp: i64 + time: Timestamp } @@ -36,7 +37,7 @@ impl <'a, 'b>World<'a, 'b> { encyclopedia, players: HashMap::new(), rooms: HashMap::new(), - timestamp: 0 + time: Timestamp(0) } } @@ -47,7 +48,7 @@ impl <'a, 'b>World<'a, 'b> { if let Ok(state) = self.persistence.load_room(id.clone()){ room.load_saved(&state); } - let last_time = self.timestamp - 1; + let last_time = self.time - 1; if room.get_time() < last_time { room.update(last_time); } @@ -103,9 +104,9 @@ impl <'a, 'b>World<'a, 'b> { pub fn update(&mut self) { self.migrate(); for room in self.rooms.values_mut() { - room.update(self.timestamp); + room.update(self.time); } - self.timestamp += 1; + self.time.0 += 1; } fn migrate(&mut self) { |
