summaryrefslogtreecommitdiff
path: root/src/engine/command.rs
blob: e6dbedf1f8ac4a401064dde9dbc3bee704123c45 (plain)
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
use std::fmt;
use super::geometry::Point;

#[derive(Debug, Clone, Copy)]
pub enum Command {
    Nothing,
    Build(Point, BuildingType),
    Deconstruct(Point),
    IronCurtain
}

impl fmt::Display for Command {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Command::Nothing => write!(f, ""),
            Command::Build(p, b) => write!(f, "{},{},{}", p.x(), p.y(), b as u8),
            Command::Deconstruct(p) => write!(f, "{},{},3", p.x(), p.y()),
            Command::IronCurtain => write!(f, "0,0,5")
        }
    }
}

#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BuildingType {
    Defence = 0,
    Attack = 1,
    Energy = 2,
    Tesla = 4,
}

impl BuildingType {
    pub fn all() -> [BuildingType; 4] {
        use self::BuildingType::*;
        [Defence, Attack, Energy, Tesla]
    }

    pub fn from_u8(id: u8) -> Option<BuildingType> {
        use std::mem;
        if id <= 4 && id != 3 { Some(unsafe { mem::transmute(id) }) } else { None }
    }

}