diff options
Diffstat (limited to 'src/entities')
-rw-r--r-- | src/entities/character.rs | 51 | ||||
-rw-r--r-- | src/entities/creature.rs | 63 | ||||
-rw-r--r-- | src/entities/entity.rs | 125 | ||||
-rw-r--r-- | src/entities/entity_char.rs | 24 | ||||
-rw-r--r-- | src/entities/environment.rs | 36 | ||||
-rw-r--r-- | src/entities/item.rs | 50 | ||||
-rw-r--r-- | src/entities/mod.rs | 20 | ||||
-rw-r--r-- | src/entities/raw_types.rs | 110 | ||||
-rw-r--r-- | src/entities/raws.rs | 38 | ||||
-rw-r--r-- | src/entities/raws/gormlak.toml | 10 | ||||
-rw-r--r-- | src/entities/raws/noodles.json | 15 | ||||
-rw-r--r-- | src/entities/util.rs | 72 |
12 files changed, 0 insertions, 614 deletions
diff --git a/src/entities/character.rs b/src/entities/character.rs deleted file mode 100644 index 3e8336b129ff..000000000000 --- a/src/entities/character.rs +++ /dev/null @@ -1,51 +0,0 @@ -use crate::display; -use crate::entities::item::Item; -use crate::types::{Position, Speed}; -use std::io::{self, Write}; - -const DEFAULT_SPEED: Speed = Speed(100); - -entity! { - pub struct Character { - pub o_name: Option<String>, - pub inventory: Vec<Box<Item>>, - } -} - -static_description!(Character, "yourself"); - -impl Character { - pub fn new() -> Character { - Character { - id: None, - position: Position { x: 0, y: 0 }, - o_name: None, - inventory: Vec::new(), - } - } - - pub fn speed(&self) -> Speed { - Speed(100) - } - - pub fn damage(&self) -> u16 { - // TODO - 1 - } - - pub fn name(&self) -> &str { - self.o_name - .as_ref() - .expect("Character name not initialized") - } - - pub fn set_name(&mut self, name: String) { - self.o_name = Some(name); - } -} - -impl display::Draw for Character { - fn do_draw(&self, out: &mut dyn Write) -> io::Result<()> { - write!(out, "@") - } -} diff --git a/src/entities/creature.rs b/src/entities/creature.rs deleted file mode 100644 index 20071c1d88eb..000000000000 --- a/src/entities/creature.rs +++ /dev/null @@ -1,63 +0,0 @@ -use crate::display; -use crate::entities::raws::CreatureType; -use crate::entities::raws::EntityRaw; -use crate::entities::{raw, Describe, EntityID}; -use crate::types::Position; -use std::io::{self, Write}; - -#[derive(Debug, Clone)] -pub struct Creature { - pub id: Option<EntityID>, - 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 { - id: None, - typ, - position, - hitpoints: typ.max_hitpoints, - } - } - - /// Damage the given creature by the given amount - pub fn damage(&mut self, amount: u16) { - if self.hitpoints <= amount { - self.hitpoints = 0; - } else { - self.hitpoints -= amount; - } - } - - /// Returns true if this creature has died - pub fn dead(&self) -> bool { - self.hitpoints == 0 - } -} - -entity!(Creature); - -impl Describe for Creature { - fn description(&self) -> String { - self.typ.description.to_string() - } -} - -impl display::Draw for Creature { - fn do_draw(&self, out: &mut dyn Write) -> io::Result<()> { - write!(out, "{}", self.typ.chr) - } -} diff --git a/src/entities/entity.rs b/src/entities/entity.rs deleted file mode 100644 index 01075d298f81..000000000000 --- a/src/entities/entity.rs +++ /dev/null @@ -1,125 +0,0 @@ -use crate::display::DrawWithNeighbors; -use crate::entities::EntityID; -use crate::types::Neighbors; -use crate::types::Position; -use crate::types::{Positioned, PositionedMut}; -use downcast_rs::Downcast; -use std::fmt::Debug; -use std::io::{self, Write}; - -pub trait Identified<ID>: Debug { - fn opt_id(&self) -> Option<ID>; - fn set_id(&mut self, id: ID); - - fn id(&self) -> ID { - self.opt_id() - .unwrap_or_else(|| panic!("Entity ({:?}) is not in the game", self)) - } -} - -impl<'a, A, ID> Identified<ID> for &'a mut A -where - A: Identified<ID>, -{ - fn opt_id(&self) -> Option<ID> { - (**self).opt_id() - } - fn set_id(&mut self, id: ID) { - (**self).set_id(id); - } -} - -impl<ID, A: Identified<ID>> Identified<ID> for Box<A> { - fn opt_id(&self) -> Option<ID> { - (**self).opt_id() - } - fn set_id(&mut self, id: ID) { - (**self).set_id(id); - } -} - -pub trait Describe { - fn description(&self) -> String; -} - -ref_impl! { - impl<T: Describe> Describe for &T { - fn description(&self) -> String { - (**self).description() - } - } -} - -#[macro_export] -macro_rules! static_description { - ($name: ident, $description: expr) => { - impl $crate::entities::entity::Describe for $name { - fn description(&self) -> String { - $description.to_string() - } - } - }; -} - -pub trait Entity: - Positioned - + PositionedMut - + Identified<EntityID> - + DrawWithNeighbors - + Downcast - + Describe -{ -} - -impl Identified<EntityID> for Box<dyn Entity> { - fn opt_id(&self) -> Option<EntityID> { - (**self).opt_id() - } - fn set_id(&mut self, id: EntityID) { - (**self).set_id(id); - } -} - -#[macro_export] -macro_rules! identified { - ($name: ident, $typ: path) => { - identified!($name, $typ, id); - }; - ($name: ident, $typ: path, $attr: ident) => { - impl crate::entities::entity::Identified<$typ> for $name { - fn opt_id(&self) -> Option<$typ> { - self.$attr - } - - fn set_id(&mut self, id: $typ) { - self.$attr = Some(id) - } - } - }; -} - -impl_downcast!(Entity); - -impl DrawWithNeighbors for Box<dyn Entity> { - fn do_draw_with_neighbors<'a, 'b>( - &'a self, - out: &'b mut dyn Write, - neighbors: &'a Neighbors<Vec<&'a Box<dyn Entity>>>, - ) -> io::Result<()> { - (**self).do_draw_with_neighbors(out, neighbors) - } -} - -pub type AnEntity = Box<dyn Entity>; - -impl Positioned for AnEntity { - fn position(&self) -> Position { - (**self).position() - } -} - -impl PositionedMut for AnEntity { - fn set_position(&mut self, pos: Position) { - (**self).set_position(pos) - } -} diff --git a/src/entities/entity_char.rs b/src/entities/entity_char.rs deleted file mode 100644 index 70f26bfffdbd..000000000000 --- a/src/entities/entity_char.rs +++ /dev/null @@ -1,24 +0,0 @@ -use crate::display::color::Color; -use std::fmt::{self, Display, Formatter}; -use termion::color; - -#[derive(Debug, Deserialize, PartialEq, Eq)] -pub struct EntityChar { - #[serde(default)] - 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/environment.rs b/src/entities/environment.rs deleted file mode 100644 index 8f8a56706287..000000000000 --- a/src/entities/environment.rs +++ /dev/null @@ -1,36 +0,0 @@ -use crate::display; -use crate::display::draw_box::{BoxStyle, Stylable}; -use crate::entities::Entity; -use crate::types::{Neighbors, Position}; -use std::io::{self, Write}; - -entity! { - pub struct Wall { - pub style: BoxStyle - } -} - -static_description!(Wall, "a wall"); - -impl Wall { - pub fn new(position: Position, style: BoxStyle) -> Self { - new_entity!(Wall { position, style }) - } -} - -impl display::DrawWithNeighbors for Wall { - fn do_draw_with_neighbors<'a, 'b>( - &'a self, - out: &'b mut dyn Write, - neighbors: &'a Neighbors<Vec<&'a Box<dyn Entity>>>, - ) -> io::Result<()> { - let neighbor_styles: Neighbors<Option<BoxStyle>> = - neighbors.map(|es| { - es.iter() - .filter_map(|e| e.downcast_ref::<Wall>()) - .map(|wall| wall.style) - .next() - }); - write!(out, "{}", neighbor_styles.style(self.style)) - } -} diff --git a/src/entities/item.rs b/src/entities/item.rs deleted file mode 100644 index 5f08780d4fb2..000000000000 --- a/src/entities/item.rs +++ /dev/null @@ -1,50 +0,0 @@ -use crate::display; -use crate::entities::raws::{raw, EntityRaw, ItemType}; -use crate::entities::{Describe, EntityID}; -use crate::types::Position; -use std::io::{self, Write}; - -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct Item { - pub id: Option<EntityID>, - pub typ: &'static ItemType<'static>, - pub position: Position, -} - -impl Item { - pub fn new_from_raw(name: &'static str, position: Position) -> Self { - match raw(name) { - EntityRaw::Item(typ) => Self::new_with_type(typ, position), - _ => panic!("Invalid raw type for {:?}, expected Item", name), - } - } - - pub fn new_with_type( - typ: &'static ItemType<'static>, - position: Position, - ) -> Self { - Item { - id: None, - typ, - position, - } - } - - pub fn is_edible(&self) -> bool { - self.typ.is_edible() - } -} - -entity!(Item); - -impl Describe for Item { - fn description(&self) -> String { - self.typ.description.to_string() - } -} - -impl display::Draw for Item { - fn do_draw(&self, out: &mut dyn Write) -> io::Result<()> { - write!(out, "{}", self.typ.chr) - } -} diff --git a/src/entities/mod.rs b/src/entities/mod.rs deleted file mode 100644 index a8c39ed8aa78..000000000000 --- a/src/entities/mod.rs +++ /dev/null @@ -1,20 +0,0 @@ -#[macro_use] -pub mod entity; -#[macro_use] -pub mod util; -pub mod character; -pub mod creature; -pub mod entity_char; -pub mod environment; -pub mod item; -pub mod raw_types; -pub mod raws; - -pub use character::Character; -pub use creature::Creature; -pub use entity::{AnEntity, Describe, Entity, Identified}; -pub use entity_char::EntityChar; -pub use item::Item; -pub use raws::raw; - -pub type EntityID = u32; diff --git a/src/entities/raw_types.rs b/src/entities/raw_types.rs deleted file mode 100644 index 4bc291b69580..000000000000 --- a/src/entities/raw_types.rs +++ /dev/null @@ -1,110 +0,0 @@ -use crate::entities::entity_char::EntityChar; -use crate::messages::Message; -use crate::types::Speed; - -#[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, PartialEq, Eq)] -pub struct EdibleItem<'a> { - #[serde(borrow)] - pub eat_message: Option<Message<'a>>, - - /// The number of hitpoints that eating this item heals - pub hitpoints_healed: u16, -} - -#[derive(Debug, Deserialize, PartialEq, Eq)] -pub struct ItemType<'a> { - pub name: &'a str, - - /// A description of the item, used by the "look" command and when walking - /// over the item on the ground - pub description: &'a str, - - /// A longer description of the item - pub long_description: &'a str, - - pub edible_item: Option<EdibleItem<'a>>, - - #[serde(rename = "char")] - pub chr: EntityChar, -} - -#[cfg(test)] -mod item_type_tests { - use super::*; - - #[test] - fn test_deserialize_item_type() { - let result = serde_json::from_str( - r#"{ - "Item": { - "name": "noodles", - "description": "a big bowl o' noodles", - "long_description": "You know exactly what kind of noodles", - "char": { "char": "n" }, - "edible_item": { - "eat_message": "You slurp up the noodles", - "hitpoints_healed": 2 - } - } - }"#, - ) - .unwrap(); - assert_matches!(result, EntityRaw::Item(_)); - if let EntityRaw::Item(item) = result { - assert_eq!(item.name, "noodles"); - } - - let toml_result = toml::from_str( - r#"[Item] -name = "noodles" -description = "a big bowl o' noodles" -long_description = "You know exactly what kind of noodles" -char = { char = "๐" } -edible_item = { eat_message = "You slurp up the noodles", hitpoints_healed = 2 } -"#, - ) - .unwrap(); - - assert_matches!(toml_result, EntityRaw::Item(_)); - if let EntityRaw::Item(item) = toml_result { - assert_eq!(item.name, "noodles"); - } - } -} - -impl<'a> ItemType<'a> { - pub fn is_edible(&self) -> bool { - self.edible_item.is_some() - } -} - -#[derive(Debug, Deserialize)] -pub enum EntityRaw<'a> { - Creature(#[serde(borrow)] CreatureType<'a>), - Item(#[serde(borrow)] ItemType<'a>), -} - -impl<'a> EntityRaw<'a> { - pub fn name(&self) -> &'a str { - use EntityRaw::*; - match self { - Creature(typ) => typ.name, - Item(typ) => typ.name, - } - } -} diff --git a/src/entities/raws.rs b/src/entities/raws.rs deleted file mode 100644 index 061e29a84037..000000000000 --- a/src/entities/raws.rs +++ /dev/null @@ -1,38 +0,0 @@ -pub use crate::entities::raw_types::{CreatureType, EntityRaw, ItemType}; -use std::collections::HashMap; - -static_cfg! { - static ref RAWS: Vec<EntityRaw<'static>> = cfg_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> { - RAWS_BY_NAME - .get(name) - .copied() - .unwrap_or_else(|| panic!("Raw not found: {}", name)) -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_raws() { - RAWS_BY_NAME.keys(); - assert_eq!(raw("noodles").name(), "noodles"); - } -} diff --git a/src/entities/raws/gormlak.toml b/src/entities/raws/gormlak.toml deleted file mode 100644 index be30362d25bd..000000000000 --- a/src/entities/raws/gormlak.toml +++ /dev/null @@ -1,10 +0,0 @@ -[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 diff --git a/src/entities/raws/noodles.json b/src/entities/raws/noodles.json deleted file mode 100644 index dfa2609f5ecb..000000000000 --- a/src/entities/raws/noodles.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "Item": { - "name": "noodles", - "char": { - "char": "n", - "color": "yellow" - }, - "description": "a big bowl o' noodles", - "long_description": "You know exactly what kind of noodles", - "edible_item": { - "eat_message": "You slurp up the noodles", - "hitpoints_healed": 2 - } - } -} diff --git a/src/entities/util.rs b/src/entities/util.rs deleted file mode 100644 index 6c11ffadf994..000000000000 --- a/src/entities/util.rs +++ /dev/null @@ -1,72 +0,0 @@ -#[macro_export] -macro_rules! new_entity { - ($name: ident) => { - new_entity!($name, {}) - }; - - ($name: ident { position: $position:expr $(, $fields:tt)* }) => { - $name { - id: None, - position: $position, - $($fields)* - } - }; - - ($name: ident { $position:expr $(, $fields:tt)* }) => { - $name { - id: None, - position: $position, - $($fields)* - } - }; -} - -#[macro_export] -macro_rules! boring_entity { - ($name:ident) => { - entity! { - pub struct $name {} - } - - impl $name { - #[allow(dead_code)] - pub fn new(position: $crate::types::Position) -> Self { - $name { id: None, position } - } - } - }; - - ($name:ident, char: $char: expr) => { - boring_entity!($name); - - impl $crate::display::Draw for $name { - fn do_draw(&self, out: &mut Write) -> io::Result<()> { - write!(out, "{}", $char) - } - } - }; -} - -#[macro_export] -macro_rules! entity { - ($name: ident) => { - positioned!($name); - positioned_mut!($name); - identified!($name, $crate::entities::EntityID); - impl $crate::entities::entity::Entity for $name {} - }; - - (pub struct $name:ident { $($struct_contents:tt)* } $($rest:tt)*) => { - #[derive(Debug, PartialEq, Eq, Clone)] - pub struct $name { - pub id: Option<$crate::entities::EntityID>, - pub position: $crate::types::Position, - $($struct_contents)* - } - - entity!($name); - entity!($($rest)*); - }; - - () => {}; -} |