about summary refs log tree commit diff
path: root/src/entities/item.rs
blob: 5f08780d4fb2bfe45b2c0727476fb9f8e68e2e51 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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)
    }
}