2021-10-22 10:05:06 -04:00
|
|
|
use rltk::{VirtualKeyCode, Rltk};
|
|
|
|
use specs::prelude::*;
|
2021-10-25 14:23:19 -04:00
|
|
|
use super::{Position, Player, TileType, State, Map};
|
2021-10-22 10:05:06 -04:00
|
|
|
use std::cmp::{min, max};
|
|
|
|
|
|
|
|
pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
|
|
|
|
let mut positions = ecs.write_storage::<Position>();
|
|
|
|
let mut players = ecs.write_storage::<Player>();
|
2021-10-25 14:23:19 -04:00
|
|
|
let map = ecs.fetch::<Map>();
|
2021-10-22 10:05:06 -04:00
|
|
|
|
|
|
|
for (_player, pos) in (&mut players, &mut positions).join() {
|
2021-10-25 14:23:19 -04:00
|
|
|
let destination_idx = map.xy_idx(pos.x + delta_x, pos.y + delta_y);
|
|
|
|
if map.tiles[destination_idx] != TileType::Wall {
|
2021-10-22 10:05:06 -04:00
|
|
|
pos.x = min(79, max(0, pos.x + delta_x));
|
|
|
|
pos.y = min(49, max(0, pos.y + delta_y));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn player_input(gs: &mut State, ctx: &mut Rltk) {
|
|
|
|
// Player movement
|
|
|
|
match ctx.key {
|
|
|
|
None => {} // Nothing happened
|
|
|
|
Some(key) => match key {
|
2021-10-22 14:50:04 -04:00
|
|
|
VirtualKeyCode::Left |
|
|
|
|
VirtualKeyCode::Numpad4 |
|
|
|
|
VirtualKeyCode::H => try_move_player(-1, 0, &mut gs.ecs),
|
|
|
|
|
|
|
|
VirtualKeyCode::Right |
|
|
|
|
VirtualKeyCode::Numpad6 |
|
|
|
|
VirtualKeyCode::L=> try_move_player(1, 0, &mut gs.ecs),
|
|
|
|
|
|
|
|
VirtualKeyCode::Up |
|
|
|
|
VirtualKeyCode::Numpad8 |
|
|
|
|
VirtualKeyCode::K => try_move_player(0, -1, &mut gs.ecs),
|
|
|
|
|
|
|
|
VirtualKeyCode::Down |
|
|
|
|
VirtualKeyCode::Numpad2 |
|
|
|
|
VirtualKeyCode::J => try_move_player(0, 1, &mut gs.ecs),
|
|
|
|
|
2021-10-22 10:05:06 -04:00
|
|
|
_ => {}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|