Skip to content

Commit

Permalink
♻️ health and hit and minion unspawn
Browse files Browse the repository at this point in the history
  • Loading branch information
fabienjuif committed Nov 3, 2023
1 parent 48212ae commit 9190478
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
15 changes: 15 additions & 0 deletions src/health_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@ impl Health {
pub fn new(max: f32) -> Self {
Self { value: max, max }
}

pub fn hit(&mut self, value: f32) -> &Self {
if value < 0. {
return self;
}
self.value -= value;
if self.value < 0. {
self.value = 0.;
}
self
}

pub fn is_dead(&self) -> bool {
self.value <= 0.
}
}

#[derive(Component)]
Expand Down
39 changes: 23 additions & 16 deletions src/minions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,29 @@ fn update_move_minions(
fn destroy_minions(
time: Res<Time>,
mut commands: Commands,
mut query: Query<(&mut Minion, &Transform, Entity)>,
mut query: Query<(&mut Minion, &Transform, &Health, Entity)>,
) {
for (mut minion, transform, entity) in &mut query {
let escape_edges_of_the_world = transform.translation.x.abs() >= GAME_MAX_WIDTH / 2.
|| transform.translation.y.abs() >= GAME_MAX_HEIGHT / 2.;
let too_old = minion.destroy_timer.tick(time.delta()).just_finished();
let mut kill = |entity| {
trace!("Unspawning Minion: {:?}", entity);
commands.entity(entity).despawn_recursive();
};

if escape_edges_of_the_world || too_old {
trace!("Unspawning Minion: {:?}", entity);
commands.entity(entity).despawn_recursive();
continue;
for (mut minion, transform, health, entity) in &mut query {
// edge of the world
if transform.translation.x.abs() >= GAME_MAX_WIDTH / 2.
|| transform.translation.y.abs() >= GAME_MAX_HEIGHT / 2.
{
kill(entity);
}

// too old
if minion.destroy_timer.tick(time.delta()).just_finished() {
kill(entity);
}

// just not enough health
if health.is_dead() {
kill(entity);
}
}
}
Expand Down Expand Up @@ -198,10 +210,7 @@ fn check_collisions_players(
}

// hurt the player
player.2.value -= 1.;
if player.2.value < 0. {
player.2.value = 0.
}
player.2.hit(1.);

trace!(
"minion {} collision with the player {}",
Expand Down Expand Up @@ -265,9 +274,7 @@ fn decay_life(
}

fn hurt_minion(commands: &mut Commands, entity: Entity, health: &mut Health, value: f32) {
health.value -= value;
if health.value < 0. {
health.value = 0.;
if health.hit(value).is_dead() {
commands.entity(entity).despawn_recursive();
}
}
3 changes: 1 addition & 2 deletions src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,7 @@ fn check_collisions_sword(
};

// hurt
// TODO: health should have a function to remove some offset
health.value -= 20.;
health.hit(20.);

// player attached to this sword receive gold
if let Ok((mut player, team)) = query_player.get_mut(sword.0) {
Expand Down

0 comments on commit 9190478

Please sign in to comment.