about summary refs log tree commit diff
path: root/src/entities/raws.rs
diff options
context:
space:
mode:
authorGriffin Smith <root@gws.fyi>2019-07-14T18·29-0400
committerGriffin Smith <root@gws.fyi>2019-07-14T18·29-0400
commite7ad87c7301f266dece36e7558c0f212e370aac6 (patch)
tree7da150d5648cc0b17d973bf4a30673f36b20be82 /src/entities/raws.rs
parent081146da30bcf1a17d9533c3dc9c735a3a558165 (diff)
Add (statically-included) entity raws
Add a system for statically-included entity raws (which necessitated
making a deserializable existential Color struct) and test it out by
initializing the game (for now) with a single on-screen gormlak.
Diffstat (limited to 'src/entities/raws.rs')
-rw-r--r--src/entities/raws.rs57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/entities/raws.rs b/src/entities/raws.rs
new file mode 100644
index 000000000000..beeb90a40cea
--- /dev/null
+++ b/src/entities/raws.rs
@@ -0,0 +1,57 @@
+use crate::entities::entity_char::EntityChar;
+use crate::types::Speed;
+use std::collections::HashMap;
+
+#[derive(Debug, Deserialize)]
+pub struct CreatureType<'a> {
+    /// The name of the creature. Used in raw lookups.
+    pub name: &'a str,
+
+    /// A description of the entity, used by the "look" command
+    pub description: &'a str,
+
+    #[serde(rename = "char")]
+    pub chr: EntityChar,
+    pub max_hitpoints: u16,
+    pub speed: Speed,
+    pub friendly: bool,
+}
+
+#[derive(Debug, Deserialize)]
+pub enum EntityRaw<'a> {
+    Creature(#[serde(borrow)] CreatureType<'a>),
+}
+
+impl<'a> EntityRaw<'a> {
+    pub fn name(&self) -> &'a str {
+        match self {
+            EntityRaw::Creature(typ) => typ.name,
+        }
+    }
+}
+
+static_cfg! {
+    static ref RAWS: Vec<EntityRaw<'static>> = toml_dir("src/entities/raws");
+}
+
+lazy_static! {
+    static ref RAWS_BY_NAME: HashMap<&'static str, &'static EntityRaw<'static>> = {
+        let mut hm = HashMap::new();
+        for er in RAWS.iter() {
+            if hm.contains_key(er.name()) {
+                panic!("Duplicate entity: {}", er.name())
+            }
+
+            hm.insert(er.name(), er);
+        }
+        hm
+    };
+}
+
+pub fn raw(name: &'static str) -> &'static EntityRaw<'static> {
+    debug!("{:?}", RAWS_BY_NAME.keys().collect::<Vec<&&'static str>>());
+    RAWS_BY_NAME
+        .get(name)
+        .map(|e| *e)
+        .expect(format!("Raw not found: {}", name).as_str())
+}