From 3009aa17813f6ea89fc4b0faefef13b87c41cc2e Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Fri, 29 Oct 2021 11:11:17 -0400 Subject: [PATCH] Add map blocking and diagonal movement --- src/components.rs | 3 +++ src/main.rs | 10 ++++++++- src/map.rs | 34 +++++++++++++++++++++++++----- src/map_indexing_system.rs | 22 +++++++++++++++++++ src/monster_ai_system.rs | 8 ++++++- src/player.rs | 43 +++++++++++++++++++++++++++----------- 6 files changed, 101 insertions(+), 19 deletions(-) create mode 100644 src/map_indexing_system.rs diff --git a/src/components.rs b/src/components.rs index 9842a96..0e40c75 100644 --- a/src/components.rs +++ b/src/components.rs @@ -32,3 +32,6 @@ pub struct Monster {} pub struct Name { pub name: String, } + +#[derive(Component)] +pub struct BlocksTile {} diff --git a/src/main.rs b/src/main.rs index 7026337..1c16e46 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,12 +8,15 @@ pub use map::*; mod player; use player::*; mod rect; +pub use rect::Rect; mod visibility_system; use visibility_system::VisibilitySystem; mod monster_ai_system; use monster_ai_system::*; +mod map_indexing_system; +use map_indexing_system::*; -pub use rect::Rect; +pub const MAP_SIZE: usize = 80 * 50; #[derive(PartialEq, Copy, Clone)] pub enum RunState { @@ -34,6 +37,9 @@ impl State { let mut mob = MonsterAI {}; mob.run_now(&self.ecs); + let mut mapindex = MapIndexingSystem {}; + mapindex.run_now(&self.ecs); + self.ecs.maintain(); } } @@ -82,6 +88,7 @@ fn main() -> rltk::BError { gs.ecs.register::(); gs.ecs.register::(); gs.ecs.register::(); + gs.ecs.register::(); let map: Map = Map::new_map_rooms_and_corridors(); let (player_x, player_y) = map.rooms[0].center(); @@ -121,6 +128,7 @@ fn main() -> rltk::BError { .with(Name { name: format!("{} #{}", &name, i), }) + .with(BlocksTile {}) .build(); } diff --git a/src/map.rs b/src/map.rs index f4b0127..828ab92 100644 --- a/src/map.rs +++ b/src/map.rs @@ -1,4 +1,4 @@ -use super::Rect; +use super::{Rect, MAP_SIZE}; use rltk::{Algorithm2D, BaseMap, Point, RandomNumberGenerator, Rltk, SmallVec, RGB}; use specs::prelude::*; use std::cmp::{max, min}; @@ -16,6 +16,7 @@ pub struct Map { pub height: i32, pub revealed_tiles: Vec, pub visible_tiles: Vec, + pub blocked: Vec, } impl Map { @@ -56,12 +57,13 @@ impl Map { /// This gives a handful of random rooms and corridors joining them together pub fn new_map_rooms_and_corridors() -> Map { let mut map = Map { - tiles: vec![TileType::Wall; 80 * 50], + tiles: vec![TileType::Wall; MAP_SIZE], rooms: Vec::new(), width: 80, height: 50, - revealed_tiles: vec![false; 80 * 50], - visible_tiles: vec![false; 80 * 50], + revealed_tiles: vec![false; MAP_SIZE], + visible_tiles: vec![false; MAP_SIZE], + blocked: vec![false; MAP_SIZE], }; const MAX_ROOMS: i32 = 30; @@ -114,7 +116,14 @@ impl Map { } let idx = self.xy_idx(x, y); - self.tiles[idx as usize] != TileType::Wall + + !self.blocked[idx] + } + + pub fn populate_blocked(&mut self) { + for (i, tile) in self.tiles.iter_mut().enumerate() { + self.blocked[i] = *tile == TileType::Wall; + } } } @@ -135,6 +144,7 @@ impl BaseMap for Map { let y = idx as i32 / self.width; let w = self.width as usize; + // Cardinal directions if self.is_exit_valid(x - 1, y) { exits.push((idx - 1, 1.0)) }; @@ -148,6 +158,20 @@ impl BaseMap for Map { exits.push((idx + w, 1.0)) }; + // Diagonals + if self.is_exit_valid(x - 1, y - 1) { + exits.push(((idx - w) - 1, 1.45)); + } + if self.is_exit_valid(x + 1, y - 1) { + exits.push(((idx - w) + 1, 1.45)); + } + if self.is_exit_valid(x - 1, y + 1) { + exits.push(((idx + w) - 1, 1.45)); + } + if self.is_exit_valid(x + 1, y + 1) { + exits.push(((idx + w) + 1, 1.45)); + } + exits } diff --git a/src/map_indexing_system.rs b/src/map_indexing_system.rs new file mode 100644 index 0000000..4c8ba36 --- /dev/null +++ b/src/map_indexing_system.rs @@ -0,0 +1,22 @@ +use super::{BlocksTile, Map, Position}; +use specs::prelude::*; + +pub struct MapIndexingSystem {} + +impl<'a> System<'a> for MapIndexingSystem { + type SystemData = ( + WriteExpect<'a, Map>, + ReadStorage<'a, Position>, + ReadStorage<'a, BlocksTile>, + ); + + fn run(&mut self, data: Self::SystemData) { + let (mut map, position, blockers) = data; + + map.populate_blocked(); + for (position, _blocks) in (&position, &blockers).join() { + let idx = map.xy_idx(position.x, position.y); + map.blocked[idx] = true; + } + } +} diff --git a/src/monster_ai_system.rs b/src/monster_ai_system.rs index eb136e9..68b4ec3 100644 --- a/src/monster_ai_system.rs +++ b/src/monster_ai_system.rs @@ -21,9 +21,15 @@ impl<'a> System<'a> for MonsterAI { for (mut viewshed, _monster, name, mut pos) in (&mut viewshed, &monster, &name, &mut position).join() { - if viewshed.visible_tiles.contains(&*player_pos) { + let distance = + rltk::DistanceAlg::Pythagoras.distance2d(Point::new(pos.x, pos.y), *player_pos); + if distance < 1.5 { + // Attack goes here console::log(&format!("{} shouts insults", name.name)); + return; + } + if viewshed.visible_tiles.contains(&*player_pos) { let path = rltk::a_star_search( map.xy_idx(pos.x, pos.y) as i32, map.xy_idx(player_pos.x, player_pos.y) as i32, diff --git a/src/player.rs b/src/player.rs index f8ef15a..e2a5dad 100644 --- a/src/player.rs +++ b/src/player.rs @@ -12,7 +12,7 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) { for (_player, pos, viewshed) in (&mut players, &mut positions, &mut viewsheds).join() { let destination_idx = map.xy_idx(pos.x + delta_x, pos.y + delta_y); - if map.tiles[destination_idx] != TileType::Wall { + if !map.blocked[destination_idx] { pos.x = min(79, max(0, pos.x + delta_x)); pos.y = min(49, max(0, pos.y + delta_y)); @@ -30,20 +30,39 @@ pub fn player_input(gs: &mut State, ctx: &mut Rltk) -> RunState { match ctx.key { None => return RunState::Paused, // Nothing happened Some(key) => match key { - VirtualKeyCode::Left | VirtualKeyCode::Numpad4 | VirtualKeyCode::H => { - try_move_player(-1, 0, &mut gs.ecs) - } + VirtualKeyCode::Left + | VirtualKeyCode::Numpad4 + | VirtualKeyCode::H + | VirtualKeyCode::A => try_move_player(-1, 0, &mut gs.ecs), - VirtualKeyCode::Right | VirtualKeyCode::Numpad6 | VirtualKeyCode::L => { - try_move_player(1, 0, &mut gs.ecs) - } + VirtualKeyCode::Right + | VirtualKeyCode::Numpad6 + | VirtualKeyCode::L + | VirtualKeyCode::D => try_move_player(1, 0, &mut gs.ecs), - VirtualKeyCode::Up | VirtualKeyCode::Numpad8 | VirtualKeyCode::K => { - try_move_player(0, -1, &mut gs.ecs) - } + VirtualKeyCode::Up + | VirtualKeyCode::Numpad8 + | VirtualKeyCode::K + | VirtualKeyCode::W => try_move_player(0, -1, &mut gs.ecs), - VirtualKeyCode::Down | VirtualKeyCode::Numpad2 | VirtualKeyCode::J => { - try_move_player(0, 1, &mut gs.ecs) + VirtualKeyCode::Down + | VirtualKeyCode::Numpad2 + | VirtualKeyCode::J + | VirtualKeyCode::S + | VirtualKeyCode::X => try_move_player(0, 1, &mut gs.ecs), + + // Diagonals + VirtualKeyCode::Numpad9 | VirtualKeyCode::Y | VirtualKeyCode::E => { + try_move_player(1, -1, &mut gs.ecs) + } + VirtualKeyCode::Numpad7 | VirtualKeyCode::U | VirtualKeyCode::Q => { + try_move_player(-1, -1, &mut gs.ecs) + } + VirtualKeyCode::Numpad3 | VirtualKeyCode::N | VirtualKeyCode::C => { + try_move_player(1, 1, &mut gs.ecs) + } + VirtualKeyCode::Numpad1 | VirtualKeyCode::B | VirtualKeyCode::Z => { + try_move_player(-1, 1, &mut gs.ecs) } _ => return RunState::Paused,