|
@@ -1,47 +1,15 @@
|
|
extern crate rltk;
|
|
extern crate rltk;
|
|
-use rltk::{Point, Rltk, VirtualKeyCode};
|
|
+use rltk::{Rltk, VirtualKeyCode};
|
|
extern crate specs;
|
|
extern crate specs;
|
|
-use super::{CombatStats, Map, Position, MoveEvent, RunState, State, Viewshed, WantsToMelee, InputEvent};
|
|
+use super::{CombatStats, Map, Position, MoveEvent, RunState, State, WantsToMelee, InputEvent};
|
|
use specs::prelude::*;
|
|
use specs::prelude::*;
|
|
|
|
|
|
|
|
|
|
-fn clamp<T: PartialOrd>(low: T, high: T, val: T) -> T {
|
|
+/// Handles converting low-level `InputEvent` components (which may
|
|
- if val < low {
|
|
+/// represent one of several actions, or even a completely invalid
|
|
- low
|
|
+/// action such as walking into a wall) and dispatches them to the
|
|
- } else if val > high {
|
|
+/// relevant high-level intention (e.g. moving in a direction or
|
|
- high
|
|
+/// attacking a target)
|
|
- } else {
|
|
|
|
- val
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-pub struct HandleMoveEvent;
|
|
|
|
-
|
|
|
|
-impl<'a> System<'a> for HandleMoveEvent {
|
|
|
|
- type SystemData = (
|
|
|
|
- WriteStorage<'a, MoveEvent>,
|
|
|
|
- WriteStorage<'a, Position>,
|
|
|
|
- WriteStorage<'a, Viewshed>,
|
|
|
|
- WriteExpect<'a, Point>,
|
|
|
|
- ReadExpect<'a, Map>,
|
|
|
|
- );
|
|
|
|
-
|
|
|
|
- fn run(&mut self, (mut move_events, mut pos, mut viewshed, mut point, map): Self::SystemData) {
|
|
|
|
- for (&event, pos, viewshed) in (&move_events, &mut pos, &mut viewshed).join() {
|
|
|
|
- let MoveEvent { destination, tgt_x, tgt_y } = event;
|
|
|
|
- if !map.blocked[destination] {
|
|
|
|
- viewshed.dirty = true;
|
|
|
|
- pos.x = tgt_x;
|
|
|
|
- pos.y = tgt_y;
|
|
|
|
- point.x = clamp(0, 79, tgt_x);
|
|
|
|
- point.y = clamp(0, 49, tgt_y);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- move_events.clear();
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-
|
|
|
|
pub struct HandleInputEvent;
|
|
pub struct HandleInputEvent;
|
|
|
|
|
|
impl<'a> System<'a> for HandleInputEvent {
|
|
impl<'a> System<'a> for HandleInputEvent {
|
|
@@ -78,6 +46,7 @@ impl<'a> System<'a> for HandleInputEvent {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+/// Translates keyboard input into the appropriate `InputEvent` value
|
|
pub fn player_input(gs: &mut State, ctx: &mut Rltk) -> RunState {
|
|
pub fn player_input(gs: &mut State, ctx: &mut Rltk) -> RunState {
|
|
let player = *gs.ecs.fetch::<Entity>();
|
|
let player = *gs.ecs.fetch::<Entity>();
|
|
let mut input = gs.ecs.write_storage::<InputEvent>();
|
|
let mut input = gs.ecs.write_storage::<InputEvent>();
|