1234567891011121314151617181920212223242526272829303132 |
- use rltk::{VirtualKeyCode, Rltk};
- use specs::prelude::*;
- use super::{Position, Player, TileType, xy_idx, State};
- 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>();
- 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) {
- // Player movement
- match ctx.key {
- None => {} // Nothing happened
- Some(key) => match key {
- VirtualKeyCode::A => try_move_player(-1, 0, &mut gs.ecs),
- VirtualKeyCode::D => try_move_player(1, 0, &mut gs.ecs),
- VirtualKeyCode::W => try_move_player(0, -1, &mut gs.ecs),
- VirtualKeyCode::S => try_move_player(0, 1, &mut gs.ecs),
- _ => {}
- },
- }
- }
|