aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillaume Pasquet <guillaume.pasquet@eggplant.io>2019-11-13 14:19:45 +0100
committerGuillaume Pasquet <guillaume.pasquet@eggplant.io>2019-11-13 14:19:45 +0100
commit3fb8f5651144ef21338c09c14cd3db03c6416136 (patch)
tree49d6390e20700f922c7ad20b02f24284a13da703
parentd6bda2f1662c082b98ac5f05ac9acb5838c1db67 (diff)
Move the player around!
-rw-r--r--src/entities.rs3
-rw-r--r--src/main.rs21
-rw-r--r--src/state.rs35
-rw-r--r--src/tiling.rs4
-rw-r--r--src/world.rs8
5 files changed, 49 insertions, 22 deletions
diff --git a/src/entities.rs b/src/entities.rs
index d462fa9..80e1235 100644
--- a/src/entities.rs
+++ b/src/entities.rs
@@ -90,7 +90,8 @@ impl Entity for Character {
}
fn move_by(&mut self, movement: Movement) -> Result<(), String> {
- self.previous_location = apply_movement(self.location, movement)?;
+ self.previous_location = self.location;
+ self.location = apply_movement(self.location, movement)?;
Ok(())
}
diff --git a/src/main.rs b/src/main.rs
index 9f5a432..2c9e611 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -8,7 +8,7 @@ mod entities;
mod world;
mod tiling;
-use entities::Player;
+use entities::{Player, Character, Entity};
use pancurses::{
initscr,
endwin,
@@ -16,7 +16,7 @@ use pancurses::{
noecho
};
use state::State;
-use world::{Dungeon};
+use world::{Dungeon, LEFT, RIGHT, UP, DOWN};
fn main() {
@@ -50,10 +50,19 @@ fn main() {
// get input and execute it
match window.getch() {
Some(Input::Character('?')) => { window.addstr("q: quit\n"); },
- Some(Input::Character('j')) => { window.addstr("down\n"); },
- Some(Input::Character('k')) => { window.addstr("up\n"); },
- Some(Input::Character('h')) => { window.addstr("left\n"); },
- Some(Input::Character('l')) => { window.addstr("right\n"); },
+ Some(Input::Character('j')) => {
+ state.player.move_by(DOWN).unwrap();
+ // state.get_player_mut().move_by(DOWN).unwrap();
+ },
+ Some(Input::Character('k')) => {
+ state.get_player_mut().move_by(UP).unwrap();
+ },
+ Some(Input::Character('h')) => {
+ state.get_player_mut().move_by(LEFT).unwrap();
+ },
+ Some(Input::Character('l')) => {
+ state.get_player_mut().move_by(RIGHT).unwrap();
+ },
Some(Input::Character('q')) => break,
Some(_) => (),
None => (),
diff --git a/src/state.rs b/src/state.rs
index 0523442..2f09d1e 100644
--- a/src/state.rs
+++ b/src/state.rs
@@ -1,13 +1,14 @@
use pancurses::Window;
-use crate::tiling::{TileType, tile_to_str};
+use crate::tiling::{TileType, TileGrid, tile_to_str};
use crate::entities::{Character, Entity};
use crate::world::{Dungeon, Generatable, Level};
pub struct State {
pub player: Character,
- pub dungeon: Dungeon,
- pub level: usize,
+ dungeon: Dungeon,
+ level: usize,
+ grid: Option<TileGrid>
}
pub fn draw_block(window: &Window, block: &TileType) {
@@ -15,26 +16,28 @@ pub fn draw_block(window: &Window, block: &TileType) {
}
impl State {
- pub fn new(
- player: Character,
- dungeon: Dungeon,
- ) -> 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.player.place(self.current_level().get_start_point());
+ self.switch_level(0);
}
- pub fn render_level(&self, window: &Window) {
- let grid = self.current_level().to_tilegrid().unwrap();
+ pub fn switch_level(&mut self, num_level: usize) {
+ self.level = num_level;
+ self.grid = Some(self.current_level().to_tilegrid().unwrap());
+ }
- for (linenum, line) in grid.raw_data().iter().enumerate() {
+ pub fn render_level(&self, window: &Window) {
+ for (linenum, line) in self.grid.as_ref().unwrap().raw_data().iter().enumerate() {
for block in line.iter() {
draw_block(&window, &block);
}
@@ -43,6 +46,12 @@ impl State {
}
fn render_entity(&self, entity: &dyn Entity, window: &Window) {
+ if !entity.is_dirty() {
+ return;
+ }
+ let dirt = entity.get_previous_location();
+ window.mv(dirt.1 as i32, dirt.0 as i32);
+ draw_block(window, self.grid.as_ref().unwrap().get_block_at(dirt.0, dirt.1));
window.mv(entity.get_location().1 as i32, entity.get_location().0 as i32);
draw_block(window, entity.get_tiletype());
}
@@ -57,7 +66,11 @@ impl State {
self.render_entity(&self.player, window)
}
- fn current_level(&self) -> &Level {
+ pub fn current_level(&self) -> &Level {
&self.dungeon.levels[self.level]
}
+
+ pub fn get_player_mut(&mut self) -> &mut Character {
+ &mut self.player
+ }
} \ No newline at end of file
diff --git a/src/tiling.rs b/src/tiling.rs
index bc1bd50..3bad119 100644
--- a/src/tiling.rs
+++ b/src/tiling.rs
@@ -36,6 +36,10 @@ impl<'a> TileGrid {
pub fn raw_data(&'a self) -> &'a Vec<Vec<TileType>> {
&self.grid
}
+
+ pub fn get_block_at(&self, x: usize, y: usize) -> &TileType {
+ &self.grid[y + 1][x]
+ }
}
pub fn tile_to_str(tile: &TileType) -> &str {
diff --git a/src/world.rs b/src/world.rs
index 73fc802..c6f0912 100644
--- a/src/world.rs
+++ b/src/world.rs
@@ -17,10 +17,10 @@ enum CorridorType {
Vertical,
}
-const LEFT: Movement = (-1, 0);
-const RIGHT: Movement = (1, 0);
-const UP: Movement = (0, -1);
-const DOWN: Movement = (0, 1);
+pub const LEFT: Movement = (-1, 0);
+pub const RIGHT: Movement = (1, 0);
+pub const UP: Movement = (0, -1);
+pub const DOWN: Movement = (0, 1);
pub fn apply_movement(point: Point, movement: Movement) -> Result<Point, String> {
let x = point.0 as i32 + movement.0 as i32;