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
#[derive(Clone, Copy, Debug)] pub struct Floor { pub width: usize, pub height: usize, pub warrior: (i32, i32), pub stairs: (i32, i32), pub sludge: Option<(i32, i32)>, } impl Floor { pub fn load(level: usize) -> Floor { match Floor::get(level) { Some(level) => level, None => unimplemented!(), } } pub fn exists(level: usize) -> bool { Floor::get(level).is_some() } pub fn draw(&self) { println!(" {}", "-".repeat(self.width)); let tiles: Vec<&str> = (0..self.width) .map(|x| { if (x as i32, 0) == self.warrior { "@" } else if (x as i32, 0) == self.stairs { ">" } else { match self.sludge { Some(sludge) if (x as i32, 0) == sludge => "s", _ => " ", } } }) .collect(); println!("|{}|", tiles.join("")); println!(" {}", "-".repeat(self.width)); } fn get(level: usize) -> Option<Floor> { match level { 1 => Some(Floor { ..Floor::default() }), 2 => Some(Floor { sludge: Some((4, 0)), ..Floor::default() }), _ => None, } } fn default() -> Floor { Floor { width: 8, height: 1, warrior: (0, 0), stairs: (7, 0), sludge: None, } } }