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
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//! contains types that represent the topology of a level

use std::collections::HashMap;

use crate::unit::{Unit, UnitType};

/// The `Floor::tile` method constructs a conceptual representation of the
/// floor using the `Tile` enum.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Tile {
    Wall,
    Empty,
    Stairs,
    Unit(UnitType),
}

impl Tile {
    /// A character (`&str` for convenience) representation of the tile
    pub fn draw(self) -> &'static str {
        match self {
            Tile::Wall => panic!("attempted to draw a wall"),
            Tile::Empty => " ",
            Tile::Stairs => ">",
            Tile::Unit(unit_type) => unit_type.draw(),
        }
    }
}

/// Each level has a `Floor` with a predefined `width` and `height`,
/// `stairs` positioned at the exit, and one or more `units`. There
/// is a player-controlled [`Warrior`](crate::warrior::Warrior) unit
/// for every level.
#[derive(Clone, Debug)]
pub struct Floor {
    /// which level
    pub level: usize,
    /// the east/west count of tiles
    pub width: usize,
    /// the north/south count of tiles
    pub height: usize,
    /// the position (x, y) of the exit
    pub stairs: (i32, i32),
    /// all of the units that the level contains
    pub units: Vec<Unit>,
}

impl Floor {
    /// Returns the predefined configuration for a given `level` number.
    pub fn load(level: usize) -> Floor {
        match Floor::get(level) {
            Some(level) => level,
            None => unimplemented!(),
        }
    }

    /// Returns `true` if a configuration exists for a given `level` number.
    pub fn exists(level: usize) -> bool {
        Floor::get(level).is_some()
    }

    /// Returns a `Tile` representing the current state of a tile
    /// of the floor at `position`.
    pub fn tile(&self, position: (i32, i32)) -> Tile {
        if position == self.stairs {
            return Tile::Stairs;
        }

        let unit_positions: HashMap<(i32, i32), UnitType> = self
            .units
            .iter()
            .map(|u| (u.position, u.unit_type))
            .collect();

        if let Some(unit_type) = unit_positions.get(&position) {
            return Tile::Unit(*unit_type);
        }

        Tile::Empty
    }

    /// Prints a textual representation of the floor and all
    /// of its units.
    pub fn draw(&self) {
        println!(" {}", "-".repeat(self.width));

        let tiles: Vec<&str> = (0..self.width)
            .map(|x| self.tile((x as i32, 0)).draw())
            .collect();
        println!("|{}|", tiles.join(""));

        println!(" {}", "-".repeat(self.width));
    }

    fn get(level: usize) -> Option<Floor> {
        match level {
            1 => Some(Floor {
                level,
                width: 8,
                height: 1,
                stairs: (7, 0),
                units: vec![Unit::warrior((0, 0))],
            }),
            2 => Some(Floor {
                level,
                width: 8,
                height: 1,
                stairs: (7, 0),
                units: vec![Unit::warrior((0, 0)), Unit::sludge((4, 0))],
            }),
            3 => Some(Floor {
                level,
                width: 9,
                height: 1,
                stairs: (8, 0),
                units: vec![
                    Unit::warrior((0, 0)),
                    Unit::sludge((2, 0)),
                    Unit::sludge((4, 0)),
                    Unit::sludge((5, 0)),
                    Unit::sludge((7, 0)),
                ],
            }),
            4 => Some(Floor {
                level,
                width: 8,
                height: 1,
                stairs: (7, 0),
                units: vec![
                    Unit::warrior((0, 0)),
                    Unit::thick_sludge((3, 0)),
                    Unit::archer((4, 0)),
                    Unit::thick_sludge((5, 0)),
                ],
            }),
            5 => Some(Floor {
                level,
                width: 8,
                height: 1,
                stairs: (7, 0),
                units: vec![
                    Unit::warrior((0, 0)),
                    Unit::captive((2, 0)),
                    Unit::archer((3, 0)),
                    Unit::archer((4, 0)),
                    Unit::thick_sludge((5, 0)),
                    Unit::captive((6, 0)),
                ],
            }),
            6 => Some(Floor {
                level,
                width: 9,
                height: 1,
                stairs: (8, 0),
                units: vec![
                    Unit::captive((0, 0)),
                    Unit::warrior((2, 0)),
                    Unit::thick_sludge((4, 0)),
                    Unit::archer((6, 0)),
                    Unit::archer((7, 0)),
                ],
            }),
            7 => Some(Floor {
                level,
                width: 6,
                height: 1,
                stairs: (0, 0),
                units: vec![
                    Unit::archer((1, 0)),
                    Unit::thick_sludge((3, 0)),
                    Unit::warrior((5, 0)),
                ],
            }),
            8 => Some(Floor {
                level,
                width: 6,
                height: 1,
                stairs: (5, 0),
                units: vec![
                    Unit::warrior((0, 0)),
                    Unit::captive((2, 0)),
                    Unit::wizard((3, 0)),
                    Unit::wizard((4, 0)),
                ],
            }),
            9 => Some(Floor {
                level,
                width: 11,
                height: 1,
                stairs: (0, 0),
                units: vec![
                    Unit::captive((1, 0)),
                    Unit::archer((2, 0)),
                    Unit::warrior((5, 0)),
                    Unit::thick_sludge((7, 0)),
                    Unit::wizard((9, 0)),
                    Unit::captive((10, 0)),
                ],
            }),
            _ => None,
        }
    }
}