aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillaume Pasquet <guillaume.pasquet@eggplant.io>2019-11-12 16:47:22 +0100
committerGuillaume Pasquet <guillaume.pasquet@eggplant.io>2019-11-12 16:47:22 +0100
commit8351d474150215598241b5aabacb4db863d0cd87 (patch)
treeac5a9f20f2795bca3e5c4208de6d6926bde4eb27
parentdbaeddc419c2d070ce175ef2bcda9fa7c98c6941 (diff)
Some refactor, better render loop
-rw-r--r--src/entities.rs39
-rw-r--r--src/main.rs19
-rw-r--r--src/state.rs40
-rw-r--r--src/tiling.rs9
-rw-r--r--src/world.rs16
5 files changed, 54 insertions, 69 deletions
diff --git a/src/entities.rs b/src/entities.rs
index a01d86a..50841ab 100644
--- a/src/entities.rs
+++ b/src/entities.rs
@@ -7,6 +7,8 @@ use crate::tiling::TileType;
pub trait Entity {
fn info(&self) -> String;
fn place(&mut self, location: Point);
+ fn get_tiletype(&self) -> &TileType;
+ fn get_location(&self) -> &Point;
}
#[derive(Clone)]
@@ -24,10 +26,6 @@ pub struct Character {
tile_type: TileType
}
-pub trait Render {
- fn render(&self, window: &Window);
-}
-
pub trait Enemy {
fn new(
class: String,
@@ -49,8 +47,7 @@ pub trait Player {
attack: i32,
dodge: i32,
luck: i32,
- level: i32,
- location: Point
+ level: i32
) -> Self;
fn damage(&mut self, damage_amount: i32);
fn heal(&mut self, heal_amount: i32);
@@ -59,23 +56,6 @@ pub trait Player {
fn stats(&self) -> String;
}
-impl Render for Character {
- fn render(&self, window: &Window) {
- // window.mv(window.get_max_y() - 2, 0);
- // window.clrtoeol();
-
- // window.refresh();
-
- // window.addstr(self.character.info() + "\n");
-
- // window.mv(self.character.location.1 as i32,self.character.location.0 as i32);
- // window.refresh();
- // draw_block(&window, self.character.tile_type);
- // window.refresh();
-
- }
-}
-
impl Entity for Character {
fn place(&mut self, location: Point) {
self.location = location;
@@ -87,6 +67,14 @@ impl Entity for Character {
self.class, self.health, self.attack, self.dodge, self.luck
)
}
+
+ fn get_tiletype(&self) -> &TileType {
+ &self.tile_type
+ }
+
+ fn get_location(&self) -> &Point {
+ &self.location
+ }
}
impl Enemy for Character {
@@ -126,8 +114,7 @@ impl Player for Character {
attack: i32,
dodge: i32,
luck: i32,
- level: i32,
- location: Point
+ level: i32
) -> Character {
Character {
name: name,
@@ -139,7 +126,7 @@ impl Player for Character {
luck: luck,
xp: 0,
level: 0,
- location: location,
+ location: (0, 0),
tile_type: TileType::Player
}
}
diff --git a/src/main.rs b/src/main.rs
index b59d587..599c65c 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,7 +1,6 @@
extern crate rand;
extern crate pancurses;
-#[macro_use]
extern crate text_io;
mod state;
@@ -31,28 +30,26 @@ fn main() {
10,
10,
20,
- 1,
- (0, 0)
+ 1
),
- Dungeon::new(window.get_max_x() as usize, window.get_max_y() as usize, 5),
+ Dungeon::new(window.get_max_x() as usize, (window.get_max_y() - 2) as usize, 5),
);
state.init();
window.keypad(true);
noecho();
-
+
+ state.render_level(&window);
+
loop {
- // update actors
-
- state.render_level(&window);
+ // update
+ state.render_entities(&window);
- // update character
- state.show_character(&window);
+ state.render_player(&window);
// get input and execute it
match window.getch() {
-
Some(Input::Character('h')) => { window.addstr("q: quit\n"); },
// Some(Input::KeyDown) => { window.addstr("down\n"); },
// Some(Input::KeyUp) => { window.addch('b'); },
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 {
diff --git a/src/tiling.rs b/src/tiling.rs
index d443fb6..bc1bd50 100644
--- a/src/tiling.rs
+++ b/src/tiling.rs
@@ -1,8 +1,5 @@
extern crate pancurses;
-use pancurses::{Window};
-
-
pub struct TileGrid {
grid: Vec<Vec<TileType>>
}
@@ -41,7 +38,7 @@ impl<'a> TileGrid {
}
}
-fn tile_to_str(tile: &TileType) -> &str {
+pub fn tile_to_str(tile: &TileType) -> &str {
match tile {
TileType::Floor => ".",
TileType::Wall => "#",
@@ -53,10 +50,6 @@ fn tile_to_str(tile: &TileType) -> &str {
}
}
-pub fn draw_block(window: &Window, block: &TileType) {
- window.printw(tile_to_str(block));
-}
-
pub trait Tileable {
fn tile(&self, grid: &mut TileGrid) -> Result<(), String>;
}
diff --git a/src/world.rs b/src/world.rs
index 9f58030..7652157 100644
--- a/src/world.rs
+++ b/src/world.rs
@@ -1,7 +1,6 @@
use rand::Rng;
-use pancurses::{Window};
use crate::entities::{Character, Entity, Enemy};
-use crate::tiling::{TileGrid, Tileable, TileType, draw_block};
+use crate::tiling::{TileGrid, Tileable, TileType};
pub type Point = (usize, usize);
@@ -226,7 +225,7 @@ impl Level {
}
}
- fn to_tilegrid(&self) -> Result<TileGrid, String> {
+ pub fn to_tilegrid(&self) -> Result<TileGrid, String> {
let mut grid = TileGrid::new(self.xsize, self.ysize);
for room in &self.rooms {
@@ -250,17 +249,6 @@ impl Level {
return (0,0)
}
- pub fn render(&self, window: &Window) {
- let grid = self.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);
- }
- }
-
pub fn get_entrance(&self) -> Point {
self.entrance
}