use std::collections::HashMap;
use crate::unit::{Unit, UnitType};
#[derive(Clone, Debug)]
pub enum Tile {
Empty,
Stairs,
Unit(UnitType),
}
impl Tile {
pub fn draw(self) -> &'static str {
match self {
Tile::Empty => " ",
Tile::Stairs => ">",
Tile::Unit(unit_type) => unit_type.draw(),
}
}
}
#[derive(Clone, Debug)]
pub struct Floor {
pub width: usize,
pub height: usize,
pub stairs: (i32, i32),
pub units: Vec<Unit>,
}
impl Floor {
pub fn load(level: usize, name: String) -> Floor {
match Floor::get(level, name) {
Some(level) => level,
None => unimplemented!(),
}
}
pub fn exists(level: usize) -> bool {
Floor::get(level, "check".to_string()).is_some()
}
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.clone()))
.collect();
if let Some(unit_type) = unit_positions.get(&position) {
return Tile::Unit(unit_type.clone());
}
Tile::Empty
}
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, name: String) -> Option<Floor> {
match level {
1 => Some(Floor {
units: vec![Unit::warrior((0, 0), name)],
..Floor::default()
}),
2 => Some(Floor {
units: vec![Unit::warrior((0, 0), name), Unit::sludge((4, 0))],
..Floor::default()
}),
3 => Some(Floor {
width: 9,
height: 1,
stairs: (8, 0),
units: vec![
Unit::warrior((0, 0), name),
Unit::sludge((2, 0)),
Unit::sludge((4, 0)),
Unit::sludge((5, 0)),
Unit::sludge((7, 0)),
],
}),
4 => Some(Floor {
width: 7,
height: 1,
stairs: (6, 0),
units: vec![
Unit::warrior((0, 0), name),
Unit::thick_sludge((2, 0)),
Unit::archer((3, 0)),
Unit::thick_sludge((5, 0)),
],
}),
5 => Some(Floor {
width: 8,
height: 1,
stairs: (7, 0),
units: vec![
Unit::warrior((0, 0), name),
Unit::captive((2, 0)),
Unit::archer((3, 0)),
Unit::archer((4, 0)),
Unit::thick_sludge((5, 0)),
Unit::captive((6, 0)),
],
}),
_ => None,
}
}
fn default() -> Floor {
Floor {
width: 8,
height: 1,
stairs: (7, 0),
units: Vec::new(),
}
}
}