about summary refs log tree commit diff
path: root/src/entities
diff options
context:
space:
mode:
Diffstat (limited to 'src/entities')
-rw-r--r--src/entities/creature.rs43
-rw-r--r--src/entities/entity_char.rs22
-rw-r--r--src/entities/mod.rs10
-rw-r--r--src/entities/raws.rs57
-rw-r--r--src/entities/raws/gormlak.toml10
5 files changed, 141 insertions, 1 deletions
diff --git a/src/entities/creature.rs b/src/entities/creature.rs
new file mode 100644
index 000000000000..6ddeade21845
--- /dev/null
+++ b/src/entities/creature.rs
@@ -0,0 +1,43 @@
+use crate::display;
+use crate::entities::raws::CreatureType;
+use crate::entities::raws::EntityRaw;
+use crate::entities::{raw, Entity};
+use crate::types::Position;
+use std::io::{self, Write};
+
+pub struct Creature {
+    pub typ: &'static CreatureType<'static>,
+    pub position: Position,
+    pub hitpoints: u16,
+}
+
+impl Creature {
+    pub fn new_from_raw(name: &'static str, position: Position) -> Self {
+        match raw(name) {
+            EntityRaw::Creature(typ) => Self::new_with_type(typ, position),
+            _ => panic!("Invalid raw type for {:?}, expected Creature", name),
+        }
+    }
+
+    pub fn new_with_type(
+        typ: &'static CreatureType<'static>,
+        position: Position,
+    ) -> Self {
+        Creature {
+            typ,
+            position,
+            hitpoints: typ.max_hitpoints,
+        }
+    }
+}
+
+positioned!(Creature);
+positioned_mut!(Creature);
+
+impl Entity for Creature {}
+
+impl display::Draw for Creature {
+    fn do_draw(&self, out: &mut Write) -> io::Result<()> {
+        write!(out, "{}", self.typ.chr)
+    }
+}
diff --git a/src/entities/entity_char.rs b/src/entities/entity_char.rs
new file mode 100644
index 000000000000..578aaf3da5c3
--- /dev/null
+++ b/src/entities/entity_char.rs
@@ -0,0 +1,22 @@
+use crate::display::color::Color;
+use std::fmt::{self, Display, Formatter};
+use termion::color;
+
+#[derive(Debug, Deserialize)]
+pub struct EntityChar {
+    color: Color,
+    #[serde(rename = "char")]
+    chr: char,
+}
+
+impl Display for EntityChar {
+    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
+        write!(
+            f,
+            "{}{}{}",
+            color::Fg(&self.color),
+            self.chr,
+            color::Fg(color::Reset)
+        )
+    }
+}
diff --git a/src/entities/mod.rs b/src/entities/mod.rs
index a23b15eef34c..c4f46bf4a723 100644
--- a/src/entities/mod.rs
+++ b/src/entities/mod.rs
@@ -1,7 +1,15 @@
 pub mod character;
+pub mod creature;
+pub mod entity_char;
+pub mod raws;
+
+pub use character::Character;
+pub use creature::Creature;
+pub use entity_char::EntityChar;
+pub use raws::raw;
+
 use crate::display::Draw;
 use crate::types::{Positioned, PositionedMut};
-pub use character::Character;
 use downcast_rs::Downcast;
 use std::io::{self, Write};
 
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())
+}
diff --git a/src/entities/raws/gormlak.toml b/src/entities/raws/gormlak.toml
new file mode 100644
index 000000000000..be30362d25bd
--- /dev/null
+++ b/src/entities/raws/gormlak.toml
@@ -0,0 +1,10 @@
+[Creature]
+name = "gormlak"
+description = """
+A chittering imp-like creature with bright yellow horns. It adores shiny objects
+and gathers in swarms.
+"""
+char = { char = "g", color = "red" }
+max_hitpoints = 5
+speed = 120
+friendly = false