diff options
author | Guillaume Pasquet <dev@etenil.net> | 2019-11-19 04:56:57 +0000 |
---|---|---|
committer | Guillaume Pasquet <dev@etenil.net> | 2019-11-19 04:56:57 +0000 |
commit | 138b22aec818572e88e1cc254301dc4a844784b0 (patch) | |
tree | 1331cdecbe63630d897e8de4867fd358832ac913 | |
parent | 649e209c64a86e951bf24805e369143888f33c9a (diff) |
Let's set boundaries.
-rw-r--r-- | Cargo.lock | 7 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/main.rs | 20 | ||||
-rw-r--r-- | src/state.rs | 27 |
4 files changed, 44 insertions, 11 deletions
@@ -68,6 +68,11 @@ dependencies = [ ] [[package]] +name = "ignore-result" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] name = "iovec" version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -191,6 +196,7 @@ name = "roguelike" version = "0.1.0" dependencies = [ "crossterm 0.13.3 (registry+https://github.com/rust-lang/crates.io-index)", + "ignore-result 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "text_io 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -258,6 +264,7 @@ dependencies = [ "checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" "checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" "checksum getrandom 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "e65cce4e5084b14874c4e7097f38cab54f47ee554f9194673456ea379dcc4c55" +"checksum ignore-result 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "665ff4dce8edd10d490641ccb78949832f1ddbff02c584fb1f85ab888fe0e50c" "checksum iovec 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "b2b3ea6ff95e175473f8ffe6a7eb7c00d054240321b84c57051175fe3c1e075e" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" "checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" @@ -8,3 +8,4 @@ edition = "2018" rand = "0.7.0" text_io = "0.1.7" crossterm = "0.13.3" +ignore-result = "0.2.0" diff --git a/src/main.rs b/src/main.rs index c8e3133..0351ffb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ extern crate crossterm; +extern crate ignore_result; extern crate rand; extern crate text_io; @@ -12,7 +13,8 @@ use crossterm::input::{input, InputEvent, KeyEvent}; use crossterm::screen::{EnterAlternateScreen, LeaveAlternateScreen, RawScreen}; use crossterm::terminal; use crossterm::{execute, Output}; -use entities::{Entity, Player}; +use entities::Player; +use ignore_result::Ignore; use state::State; use std::env; use std::io::{stdout, Write}; @@ -59,15 +61,15 @@ fn main() { InputEvent::Keyboard(KeyEvent::Char('?')) => { execute!(stdout(), Output("q: quit")).unwrap() } - InputEvent::Keyboard(KeyEvent::Char('j')) => state.player.move_by(DOWN).unwrap(), - InputEvent::Keyboard(KeyEvent::Char('k')) => state.player.move_by(UP).unwrap(), - InputEvent::Keyboard(KeyEvent::Char('h')) => state.player.move_by(LEFT).unwrap(), - InputEvent::Keyboard(KeyEvent::Char('l')) => state.player.move_by(RIGHT).unwrap(), + InputEvent::Keyboard(KeyEvent::Char('j')) => state.move_player(DOWN).ignore(), + InputEvent::Keyboard(KeyEvent::Char('k')) => state.move_player(UP).ignore(), + InputEvent::Keyboard(KeyEvent::Char('h')) => state.move_player(LEFT).ignore(), + InputEvent::Keyboard(KeyEvent::Char('l')) => state.move_player(RIGHT).ignore(), // Arrow keys for noobs - InputEvent::Keyboard(KeyEvent::Down) => state.player.move_by(DOWN).unwrap(), - InputEvent::Keyboard(KeyEvent::Up) => state.player.move_by(UP).unwrap(), - InputEvent::Keyboard(KeyEvent::Left) => state.player.move_by(LEFT).unwrap(), - InputEvent::Keyboard(KeyEvent::Right) => state.player.move_by(RIGHT).unwrap(), + InputEvent::Keyboard(KeyEvent::Down) => state.move_player(DOWN).ignore(), + InputEvent::Keyboard(KeyEvent::Up) => state.move_player(UP).ignore(), + InputEvent::Keyboard(KeyEvent::Left) => state.move_player(LEFT).ignore(), + InputEvent::Keyboard(KeyEvent::Right) => state.move_player(RIGHT).ignore(), _ => (), } } diff --git a/src/state.rs b/src/state.rs index 68d5250..d377784 100644 --- a/src/state.rs +++ b/src/state.rs @@ -3,8 +3,8 @@ 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}; +use crate::tiling::{tile_to_str, TileGrid, TileType}; +use crate::world::{apply_movement, Dungeon, Generatable, Level, Movement}; pub struct State { pub player: Character, @@ -89,4 +89,27 @@ impl State { pub fn current_level(&self) -> &Level { &self.dungeon.levels[self.level] } + + fn can_step_on(tile: &TileType) -> bool { + match tile { + TileType::Floor => true, + TileType::StairsDown => true, + TileType::StairsUp => true, + _ => false, + } + } + + pub fn move_player(&mut self, dir: Movement) -> Result<(), String> { + match &self.grid { + None => Err(String::from("No level loaded!")), + Some(grid) => { + let loc = apply_movement(*self.player.location(), dir)?; + // Is the new location colliding with anything? + if !State::can_step_on(grid.block_at(loc.0, loc.1)) { + return Err(String::from("Can't move entity!")); + } + self.player.move_by(dir) + } + } + } } |