Now with resize
This commit is contained in:
35
src/player.rs
Normal file
35
src/player.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
use crate::{
|
||||
components::{Player, Position},
|
||||
map::{xy_idx, TileType},
|
||||
State,
|
||||
};
|
||||
use rltk::Rltk;
|
||||
use specs::prelude::*;
|
||||
use std::cmp::{max, min};
|
||||
|
||||
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>();
|
||||
let map = ecs.fetch::<Vec<TileType>>();
|
||||
|
||||
for (_player, pos) in (&mut players, &mut positions).join() {
|
||||
let destination_idx = xy_idx(pos.x + delta_x, pos.y + delta_y);
|
||||
if map[destination_idx] != TileType::Wall {
|
||||
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) {
|
||||
match ctx.key {
|
||||
None => {}
|
||||
Some(key) => match key {
|
||||
rltk::VirtualKeyCode::Left => try_move_player(-1, 0, &mut gs.ecs),
|
||||
rltk::VirtualKeyCode::Right => try_move_player(1, 0, &mut gs.ecs),
|
||||
rltk::VirtualKeyCode::Up => try_move_player(0, -1, &mut gs.ecs),
|
||||
rltk::VirtualKeyCode::Down => try_move_player(0, 1, &mut gs.ecs),
|
||||
_ => {}
|
||||
},
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user