use crossterm::cursor::MoveTo; use crossterm::{queue, Output}; use std::io::{stdout, Write}; use crate::entities::{Character, Entity, Player}; use crate::tiling::{tile_to_str, TileGrid}; use crate::world::{Dungeon, Generatable, Level}; pub struct State { pub player: Character, dungeon: Dungeon, level: usize, grid: Option, } impl State { pub fn new(player: Character, dungeon: Dungeon) -> State { State { player, dungeon, level: 0, grid: None, } } pub fn init(&mut self) { self.dungeon.generate(); self.switch_level(0); self.player.place(self.current_level().get_start_point()); } pub fn switch_level(&mut self, num_level: usize) { self.level = num_level; self.grid = Some(self.current_level().to_tilegrid().unwrap()); } pub fn render_level(&self) { let mut sout = stdout(); for (linenum, line) in self.grid.as_ref().unwrap().raw_data().iter().enumerate() { let linestr = line.iter().map(tile_to_str).collect::>(); let mut linestr2 = String::from(""); for chr in linestr { linestr2.push_str(chr); } queue!(sout, Output(linestr2), MoveTo(0, linenum as u16)).unwrap(); sout.flush().unwrap(); } } fn render_entity(&self, entity: &dyn Entity) { if !entity.is_dirty() { return; } let dirt = entity.get_previous_location(); let background = self.grid.as_ref().unwrap().get_block_at(dirt.0, dirt.1); let mut sout = stdout(); queue!( sout, MoveTo(dirt.0 as u16, dirt.1 as u16), Output(tile_to_str(background)), MoveTo( entity.get_location().0 as u16, entity.get_location().1 as u16 ), Output(tile_to_str(entity.get_tiletype())) ) .unwrap(); sout.flush().unwrap(); } pub fn render_entities(&self) { for e in self.current_level().entities.iter() { self.render_entity(&**e); } } pub fn render_player(&self) { self.render_entity(&self.player) } pub fn render_ui(&self) { let mut sout = stdout(); queue!( sout, MoveTo(0, (self.dungeon.get_ysize() + 1) as u16), Output(self.player.stats()) ) .unwrap(); sout.flush().unwrap(); } pub fn current_level(&self) -> &Level { &self.dungeon.levels[self.level] } }