Add map blocking and diagonal movement
This commit is contained in:
parent
45ab89f795
commit
3009aa1781
@ -32,3 +32,6 @@ pub struct Monster {}
|
|||||||
pub struct Name {
|
pub struct Name {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
pub struct BlocksTile {}
|
||||||
|
10
src/main.rs
10
src/main.rs
@ -8,12 +8,15 @@ pub use map::*;
|
|||||||
mod player;
|
mod player;
|
||||||
use player::*;
|
use player::*;
|
||||||
mod rect;
|
mod rect;
|
||||||
|
pub use rect::Rect;
|
||||||
mod visibility_system;
|
mod visibility_system;
|
||||||
use visibility_system::VisibilitySystem;
|
use visibility_system::VisibilitySystem;
|
||||||
mod monster_ai_system;
|
mod monster_ai_system;
|
||||||
use 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)]
|
#[derive(PartialEq, Copy, Clone)]
|
||||||
pub enum RunState {
|
pub enum RunState {
|
||||||
@ -34,6 +37,9 @@ impl State {
|
|||||||
let mut mob = MonsterAI {};
|
let mut mob = MonsterAI {};
|
||||||
mob.run_now(&self.ecs);
|
mob.run_now(&self.ecs);
|
||||||
|
|
||||||
|
let mut mapindex = MapIndexingSystem {};
|
||||||
|
mapindex.run_now(&self.ecs);
|
||||||
|
|
||||||
self.ecs.maintain();
|
self.ecs.maintain();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -82,6 +88,7 @@ fn main() -> rltk::BError {
|
|||||||
gs.ecs.register::<Viewshed>();
|
gs.ecs.register::<Viewshed>();
|
||||||
gs.ecs.register::<Monster>();
|
gs.ecs.register::<Monster>();
|
||||||
gs.ecs.register::<Name>();
|
gs.ecs.register::<Name>();
|
||||||
|
gs.ecs.register::<BlocksTile>();
|
||||||
|
|
||||||
let map: Map = Map::new_map_rooms_and_corridors();
|
let map: Map = Map::new_map_rooms_and_corridors();
|
||||||
let (player_x, player_y) = map.rooms[0].center();
|
let (player_x, player_y) = map.rooms[0].center();
|
||||||
@ -121,6 +128,7 @@ fn main() -> rltk::BError {
|
|||||||
.with(Name {
|
.with(Name {
|
||||||
name: format!("{} #{}", &name, i),
|
name: format!("{} #{}", &name, i),
|
||||||
})
|
})
|
||||||
|
.with(BlocksTile {})
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
34
src/map.rs
34
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 rltk::{Algorithm2D, BaseMap, Point, RandomNumberGenerator, Rltk, SmallVec, RGB};
|
||||||
use specs::prelude::*;
|
use specs::prelude::*;
|
||||||
use std::cmp::{max, min};
|
use std::cmp::{max, min};
|
||||||
@ -16,6 +16,7 @@ pub struct Map {
|
|||||||
pub height: i32,
|
pub height: i32,
|
||||||
pub revealed_tiles: Vec<bool>,
|
pub revealed_tiles: Vec<bool>,
|
||||||
pub visible_tiles: Vec<bool>,
|
pub visible_tiles: Vec<bool>,
|
||||||
|
pub blocked: Vec<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Map {
|
impl Map {
|
||||||
@ -56,12 +57,13 @@ impl Map {
|
|||||||
/// This gives a handful of random rooms and corridors joining them together
|
/// This gives a handful of random rooms and corridors joining them together
|
||||||
pub fn new_map_rooms_and_corridors() -> Map {
|
pub fn new_map_rooms_and_corridors() -> Map {
|
||||||
let mut map = Map {
|
let mut map = Map {
|
||||||
tiles: vec![TileType::Wall; 80 * 50],
|
tiles: vec![TileType::Wall; MAP_SIZE],
|
||||||
rooms: Vec::new(),
|
rooms: Vec::new(),
|
||||||
width: 80,
|
width: 80,
|
||||||
height: 50,
|
height: 50,
|
||||||
revealed_tiles: vec![false; 80 * 50],
|
revealed_tiles: vec![false; MAP_SIZE],
|
||||||
visible_tiles: vec![false; 80 * 50],
|
visible_tiles: vec![false; MAP_SIZE],
|
||||||
|
blocked: vec![false; MAP_SIZE],
|
||||||
};
|
};
|
||||||
|
|
||||||
const MAX_ROOMS: i32 = 30;
|
const MAX_ROOMS: i32 = 30;
|
||||||
@ -114,7 +116,14 @@ impl Map {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let idx = self.xy_idx(x, y);
|
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 y = idx as i32 / self.width;
|
||||||
let w = self.width as usize;
|
let w = self.width as usize;
|
||||||
|
|
||||||
|
// Cardinal directions
|
||||||
if self.is_exit_valid(x - 1, y) {
|
if self.is_exit_valid(x - 1, y) {
|
||||||
exits.push((idx - 1, 1.0))
|
exits.push((idx - 1, 1.0))
|
||||||
};
|
};
|
||||||
@ -148,6 +158,20 @@ impl BaseMap for Map {
|
|||||||
exits.push((idx + w, 1.0))
|
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
|
exits
|
||||||
}
|
}
|
||||||
|
|
||||||
|
22
src/map_indexing_system.rs
Normal file
22
src/map_indexing_system.rs
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -21,9 +21,15 @@ impl<'a> System<'a> for MonsterAI {
|
|||||||
for (mut viewshed, _monster, name, mut pos) in
|
for (mut viewshed, _monster, name, mut pos) in
|
||||||
(&mut viewshed, &monster, &name, &mut position).join()
|
(&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));
|
console::log(&format!("{} shouts insults", name.name));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if viewshed.visible_tiles.contains(&*player_pos) {
|
||||||
let path = rltk::a_star_search(
|
let path = rltk::a_star_search(
|
||||||
map.xy_idx(pos.x, pos.y) as i32,
|
map.xy_idx(pos.x, pos.y) as i32,
|
||||||
map.xy_idx(player_pos.x, player_pos.y) as i32,
|
map.xy_idx(player_pos.x, player_pos.y) as i32,
|
||||||
|
@ -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() {
|
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);
|
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.x = min(79, max(0, pos.x + delta_x));
|
||||||
pos.y = min(49, max(0, pos.y + delta_y));
|
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 {
|
match ctx.key {
|
||||||
None => return RunState::Paused, // Nothing happened
|
None => return RunState::Paused, // Nothing happened
|
||||||
Some(key) => match key {
|
Some(key) => match key {
|
||||||
VirtualKeyCode::Left | VirtualKeyCode::Numpad4 | VirtualKeyCode::H => {
|
VirtualKeyCode::Left
|
||||||
try_move_player(-1, 0, &mut gs.ecs)
|
| VirtualKeyCode::Numpad4
|
||||||
}
|
| VirtualKeyCode::H
|
||||||
|
| VirtualKeyCode::A => try_move_player(-1, 0, &mut gs.ecs),
|
||||||
|
|
||||||
VirtualKeyCode::Right | VirtualKeyCode::Numpad6 | VirtualKeyCode::L => {
|
VirtualKeyCode::Right
|
||||||
try_move_player(1, 0, &mut gs.ecs)
|
| VirtualKeyCode::Numpad6
|
||||||
}
|
| VirtualKeyCode::L
|
||||||
|
| VirtualKeyCode::D => try_move_player(1, 0, &mut gs.ecs),
|
||||||
|
|
||||||
VirtualKeyCode::Up | VirtualKeyCode::Numpad8 | VirtualKeyCode::K => {
|
VirtualKeyCode::Up
|
||||||
try_move_player(0, -1, &mut gs.ecs)
|
| VirtualKeyCode::Numpad8
|
||||||
}
|
| VirtualKeyCode::K
|
||||||
|
| VirtualKeyCode::W => try_move_player(0, -1, &mut gs.ecs),
|
||||||
|
|
||||||
VirtualKeyCode::Down | VirtualKeyCode::Numpad2 | VirtualKeyCode::J => {
|
VirtualKeyCode::Down
|
||||||
try_move_player(0, 1, &mut gs.ecs)
|
| 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,
|
_ => return RunState::Paused,
|
||||||
|
Loading…
Reference in New Issue
Block a user