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

View File

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

View File

@ -19,17 +19,41 @@ pub fn player_input(
_ => Point::new(0, 0),
};
players.iter_mut(ecs).for_each(|(entity, pos)| {
let destination = *pos + delta;
let (player_entity, destination) = players
.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((
(),
WantsToAttack {
attacker: player_entity,
victim: *entity,
},
));
});
if !hit_something {
commands.push((
(),
WantsToMove {
entity: player_entity,
destination,
},
));
}
}
commands.push((
(),
WantsToMove {
entity: *entity,
destination,
},
));
});
*turn_state = TurnState::PlayerTurn;
}
}