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
//! contains types that represent the topology of a level

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

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

impl Tile {
    /// A character (`&str` for convenience) representation of the tile
    pub fn draw(self) -> &'static str {
        match self {
            Tile::Empty => " ",
            Tile::Sludge => "s",
            Tile::Stairs => ">",
            Tile::Warrior => "@",
        }
    }
}

/// 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 {
    /// 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()
    }

    /// Extracts the warrior from the `units`
    pub fn warrior(&self) -> &Unit {
        self.units
            .iter()
            .find(|u| u.unit_type == UnitType::Warrior)
            .unwrap()
    }

    /// Returns all of the sludges (if there are any) in `units`.
    /// The `UnitType::Sludge` unit type is introduced in level 2.
    pub fn sludges(&self) -> Vec<&Unit> {
        self.units
            .iter()
            .filter(|u| u.unit_type == UnitType::Sludge)
            .collect()
    }

    /// 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;
        }

        if self.warrior().position == position {
            return Tile::Warrior;
        }

        let sludge_positions: Vec<(i32, i32)> = self
            .units
            .iter()
            .filter(|u| u.unit_type == UnitType::Sludge)
            .map(|s| s.position)
            .collect();
        if sludge_positions.contains(&position) {
            Tile::Sludge
        } else {
            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 {
                units: vec![Unit::warrior((0, 0))],
                ..Floor::default()
            }),
            2 => Some(Floor {
                units: vec![Unit::warrior((0, 0)), Unit::sludge((4, 0))],
                ..Floor::default()
            }),
            3 => Some(Floor {
                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)),
                ],
            }),
            _ => None,
        }
    }

    fn default() -> Floor {
        Floor {
            width: 8,
            height: 1,
            stairs: (7, 0),
            units: Vec::new(),
        }
    }
}