Add combat system

- Remove collisions system
- Add attack intent to random move system
This commit is contained in:
Daniel Lynn 2021-07-07 23:04:26 -05:00
parent e203c6000d
commit 1be2cf3907
4 changed files with 71 additions and 29 deletions

View File

@ -1,17 +0,0 @@
use crate::prelude::*;
#[system]
#[read_component(Point)]
#[read_component(Player)]
#[read_component(Enemy)]
pub fn collisions(ecs: &mut SubWorld, commands: &mut CommandBuffer) {
let mut player_pos = Point::zero();
let mut players = <&Point>::query().filter(component::<Player>());
players.iter(ecs).for_each(|pos| player_pos = *pos);
let mut enemies = <(Entity, &Point)>::query().filter(component::<Enemy>());
enemies
.iter(ecs)
.filter(|(_, pos)| **pos == player_pos)
.for_each(|(entity, _)| commands.remove(*entity));
}

29
src/systems/combat.rs Normal file
View File

@ -0,0 +1,29 @@
use crate::prelude::*;
#[system]
#[read_component(WantsToAttack)]
#[write_component(Health)]
pub fn combat(ecs: &mut SubWorld, commands: &mut CommandBuffer) {
let mut attackers = <(Entity, &WantsToAttack)>::query();
let victims: Vec<(Entity, Entity)> = attackers
.iter(ecs)
.map(|(entity, attack)| (*entity, attack.victim))
.collect();
victims.iter().for_each(|(message, victim)| {
if let Ok(mut health) = ecs
.entry_mut(*victim)
.unwrap()
.get_component_mut::<Health>()
{
println!("Health before attack: {}", health.current);
health.current -= 1;
if health.current < 1 {
commands.remove(*victim);
}
println!("Health after attack: {}", health.current);
}
commands.remove(*message);
});
}

View File

@ -1,4 +1,4 @@
mod collisions;
mod combat;
mod end_turn;
mod entity_render;
mod hud;
@ -23,9 +23,9 @@ pub fn build_input_scheduler() -> Schedule {
pub fn build_player_scheduler() -> Schedule {
Schedule::builder()
.add_system(movement::movement_system())
.add_system(combat::combat_system())
.flush()
.add_system(collisions::collisions_system())
.add_system(movement::movement_system())
.flush()
.add_system(map_render::map_render_system())
.add_system(entity_render::entity_render_system())
@ -39,6 +39,8 @@ pub fn build_monster_scheduler() -> Schedule {
Schedule::builder()
.add_system(random_move::random_move_system())
.flush()
.add_system(combat::combat_system())
.flush()
.add_system(movement::movement_system())
.flush()
.add_system(map_render::map_render_system())

View File

@ -3,9 +3,13 @@ use crate::prelude::*;
#[system]
#[read_component(Point)]
#[read_component(MovingRandomly)]
pub fn random_move(ecs: &mut SubWorld, commands: &mut CommandBuffer) {
#[read_component(Health)]
#[read_component(Player)]
pub fn random_move(ecs: &SubWorld, commands: &mut CommandBuffer) {
let mut movers = <(Entity, &Point, &MovingRandomly)>::query();
movers.iter_mut(ecs).for_each(|(entity, pos, _)| {
let mut positions = <(Entity, &Point, &Health)>::query();
movers.iter(ecs).for_each(|(entity, pos, _)| {
let mut rng = RandomNumberGenerator::new();
let destination = match rng.range(0, 4) {
0 => Point::new(-1, 0),
@ -14,12 +18,36 @@ pub fn random_move(ecs: &mut SubWorld, commands: &mut CommandBuffer) {
_ => Point::new(0, 1),
} + *pos;
commands.push((
(),
WantsToMove {
entity: *entity,
destination,
},
));
let mut attacked = false;
positions
.iter(ecs)
.filter(|(_, target_pos, _)| **target_pos == destination)
.for_each(|(victim, _, _)| {
if ecs
.entry_ref(*victim)
.unwrap()
.get_component::<Player>()
.is_ok()
{
commands.push((
(),
WantsToAttack {
attacker: *entity,
victim: *victim,
},
));
attacked = true;
}
});
if !attacked {
commands.push((
(),
WantsToMove {
entity: *entity,
destination,
},
));
}
});
}