From 8351d474150215598241b5aabacb4db863d0cd87 Mon Sep 17 00:00:00 2001 From: Guillaume Pasquet Date: Tue, 12 Nov 2019 16:47:22 +0100 Subject: Some refactor, better render loop --- src/state.rs | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) (limited to 'src/state.rs') diff --git a/src/state.rs b/src/state.rs index 1f50f1a..afc702d 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1,38 +1,58 @@ use pancurses::Window; -use std::env; -use crate::entities::{Character, Entity, Render}; +use crate::tiling::{TileType, tile_to_str}; +use crate::entities::{Character, Entity}; use crate::world::{Dungeon, Generatable, Level}; pub struct State { - pub character: Character, + pub player: Character, pub dungeon: Dungeon, pub level: usize, } +pub fn draw_block(window: &Window, block: &TileType) { + window.printw(tile_to_str(block)); +} + impl State { pub fn new( - character: Character, + player: Character, dungeon: Dungeon, ) -> State { State { - character: character, - dungeon: dungeon, + player, + dungeon, level: 0, } } pub fn init(&mut self) { self.dungeon.generate(); - self.character.place(self.current_level().get_start_point()); + self.player.place(self.current_level().get_start_point()); } pub fn render_level(&self, window: &Window) { - self.current_level().render(window); + let grid = self.current_level().to_tilegrid().unwrap(); + + for (linenum, line) in grid.raw_data().iter().enumerate() { + for block in line.iter() { + draw_block(&window, &block); + } + window.mv(linenum as i32, 0); + } + } + + fn render_entity(&self, entity: &dyn Entity, window: &Window) { + window.mv(entity.get_location().1 as i32, entity.get_location().0 as i32); + draw_block(window, entity.get_tiletype()); + } + + pub fn render_entities(&self, window: &Window) { + // TODO } - pub fn show_character(&self, window: &Window) { - self.character.render(window); + pub fn render_player(&self, window: &Window) { + self.render_entity(&self.player, window) } fn current_level(&self) -> &Level { -- cgit v1.2.3