|
@@ -1,9 +1,8 @@
|
|
|
extern crate rltk;
|
|
|
use rltk::{Point, Rltk, VirtualKeyCode};
|
|
|
extern crate specs;
|
|
|
-use super::{CombatStats, Map, Player, Position, RunState, State, Viewshed, WantsToMelee};
|
|
|
+use super::{CombatStats, Map, Position, RunState, State, Viewshed, WantsToMelee};
|
|
|
use specs::prelude::*;
|
|
|
-use std::cmp::{max, min};
|
|
|
|
|
|
|
|
|
fn clamp<T: PartialOrd>(low: T, high: T, val: T) -> T {
|
|
@@ -16,51 +15,40 @@ fn clamp<T: PartialOrd>(low: T, high: T, val: T) -> T {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-#[derive(Component)]
|
|
|
-pub enum GameEvent {
|
|
|
- MoveTo {
|
|
|
- destination: usize,
|
|
|
- tgt_x: i32,
|
|
|
- tgt_y: i32,
|
|
|
- },
|
|
|
- Attack { target: Entity },
|
|
|
+#[derive(Component, Copy, Clone)]
|
|
|
+pub struct MoveEvent {
|
|
|
+ destination: usize,
|
|
|
+ tgt_x: i32,
|
|
|
+ tgt_y: i32,
|
|
|
}
|
|
|
|
|
|
-pub struct HandleGameEvent;
|
|
|
+pub struct HandleMoveEvent;
|
|
|
|
|
|
-impl<'a> System<'a> for HandleGameEvent {
|
|
|
+impl<'a> System<'a> for HandleMoveEvent {
|
|
|
type SystemData = (
|
|
|
- Entities<'a>,
|
|
|
- WriteStorage<'a, GameEvent>,
|
|
|
- WriteStorage<'a, WantsToMelee>,
|
|
|
+ WriteStorage<'a, MoveEvent>,
|
|
|
WriteStorage<'a, Position>,
|
|
|
WriteStorage<'a, Viewshed>,
|
|
|
WriteExpect<'a, Point>,
|
|
|
ReadExpect<'a, Map>,
|
|
|
);
|
|
|
|
|
|
- fn run(&mut self, (entities, mut game_events, mut melee, mut pos, mut viewshed, mut point, map): Self::SystemData) {
|
|
|
- for (e, event) in (&entities, &game_events).join() {
|
|
|
- match event {
|
|
|
- GameEvent::Attack { target } => {
|
|
|
- melee.insert(e, WantsToMelee { target: *target })
|
|
|
- .expect("Unable to add melee intention for player");
|
|
|
- }
|
|
|
- GameEvent::MoveTo { destination, tgt_x, tgt_y } => {
|
|
|
- if !map.blocked[*destination] {
|
|
|
- viewshed.get_mut(e).expect("Unable to find viewshed").dirty = true;
|
|
|
- pos.get_mut(e).expect("Unable to find position").x = *tgt_x;
|
|
|
- pos.get_mut(e).expect("Unable to find position").y = *tgt_y;
|
|
|
- point.x = clamp(0, 79, *tgt_x);
|
|
|
- point.y = clamp(0, 49, *tgt_y);
|
|
|
- }
|
|
|
- }
|
|
|
+ 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);
|
|
|
}
|
|
|
}
|
|
|
- game_events.clear();
|
|
|
+ move_events.clear();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
pub struct HandleInputEvent;
|
|
|
|
|
|
impl<'a> System<'a> for HandleInputEvent {
|
|
@@ -70,10 +58,11 @@ impl<'a> System<'a> for HandleInputEvent {
|
|
|
Entities<'a>,
|
|
|
ReadStorage<'a, Position>,
|
|
|
ReadStorage<'a, CombatStats>,
|
|
|
- WriteStorage<'a, GameEvent>,
|
|
|
+ WriteStorage<'a, WantsToMelee>,
|
|
|
+ WriteStorage<'a, MoveEvent>,
|
|
|
);
|
|
|
|
|
|
- fn run(&mut self, (mut event, map, entities, position, stats, mut game_events): Self::SystemData) {
|
|
|
+ fn run(&mut self, (mut event, map, entities, position, stats, mut attack, mut mov): Self::SystemData) {
|
|
|
for (e, ev, pos) in (&entities, &event, &position).join() {
|
|
|
match *ev {
|
|
|
InputEvent::PlayerMovement { delta_x, delta_y } => {
|
|
@@ -83,11 +72,11 @@ impl<'a> System<'a> for HandleInputEvent {
|
|
|
let tgt_y = pos.y + delta_y;
|
|
|
let destination = map.xy_idx(tgt_x, tgt_y);
|
|
|
if let Some(target) = map.get_attackable(destination, &stats) {
|
|
|
- game_events.insert(e, GameEvent::Attack { target })
|
|
|
- .expect("Unable to add GameEvent");
|
|
|
+ attack.insert(e, WantsToMelee { target })
|
|
|
+ .expect("Unable to add WantsToAttack");
|
|
|
} else {
|
|
|
- game_events.insert(e, GameEvent::MoveTo { destination, tgt_x, tgt_y })
|
|
|
- .expect("Unable to add GameEvent");
|
|
|
+ mov.insert(e, MoveEvent { destination, tgt_x, tgt_y })
|
|
|
+ .expect("Unable to add MoveEvent");
|
|
|
}
|
|
|
}
|
|
|
}
|