Add WantsToAttack component

- Add player intent to attack when bumping an enemy
This commit is contained in:
Daniel Lynn 2021-07-07 21:39:47 -05:00
parent 4aa39bfd6a
commit e203c6000d
2 changed files with 40 additions and 10 deletions

@ -29,3 +29,9 @@ pub struct Health {
#[derive(Clone, PartialEq)] #[derive(Clone, PartialEq)]
pub struct Name(pub String); pub struct Name(pub String);
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct WantsToAttack {
pub attacker: Entity,
pub victim: Entity,
}

@ -19,17 +19,41 @@ pub fn player_input(
_ => Point::new(0, 0), _ => Point::new(0, 0),
}; };
players.iter_mut(ecs).for_each(|(entity, pos)| { let (player_entity, destination) = players
let destination = *pos + delta; .iter(ecs)
.find_map(|(entity, pos)| Some((*entity, *pos + delta)))
.unwrap();
let mut enemies = <(Entity, &Point)>::query().filter(component::<Enemy>());
if delta.x != 0 || delta.y != 0 {
let mut hit_something = false;
enemies
.iter(ecs)
.filter(|(_, pos)| **pos == destination)
.for_each(|(entity, _)| {
hit_something = true;
commands.push(( commands.push((
(), (),
WantsToMove { WantsToAttack {
entity: *entity, attacker: player_entity,
destination, victim: *entity,
}, },
)); ));
}); });
if !hit_something {
commands.push((
(),
WantsToMove {
entity: player_entity,
destination,
},
));
}
}
*turn_state = TurnState::PlayerTurn; *turn_state = TurnState::PlayerTurn;
} }
} }