From c63bca47b478217c9838a7634fddabffed3fe522 Mon Sep 17 00:00:00 2001 From: Thorsten Ehlers Date: Sat, 1 Feb 2025 16:13:41 +0100 Subject: [PATCH] Use `Timer` as property of `DiplopodHead` for immunity --- src/game.rs | 3 +-- src/game/components.rs | 10 ++++++++ src/game/resources.rs | 3 --- src/game/systems/control.rs | 45 ++++++++++++++++++++++-------------- src/game/systems/graphics.rs | 9 ++++---- 5 files changed, 44 insertions(+), 26 deletions(-) diff --git a/src/game.rs b/src/game.rs index d66f71a..1e4acd2 100644 --- a/src/game.rs +++ b/src/game.rs @@ -62,7 +62,7 @@ impl Plugin for GamePlugin { .after(Phase::Movement) .run_if(in_state(GameState::Game)), ( - control::limit_immunity.run_if(on_timer(Duration::from_secs(1))), + control::limit_immunity, graphics::fade_text.run_if(on_timer(Duration::from_millis(200))), ) .run_if(in_state(GameState::Game)), @@ -92,7 +92,6 @@ impl Plugin for GamePlugin { .add_systems(OnExit(GameState::Game), despawn_screen::) .insert_resource(DiplopodSegments::default()) .insert_resource(LastSpecialSpawn::default()) - .insert_resource(ImmunityTime::default()) .insert_resource(FreePositions::new(CONSUMABLE_WIDTH, CONSUMABLE_HEIGHT)) .insert_resource(Time::::from_seconds(0.075)) .add_event::() diff --git a/src/game/components.rs b/src/game/components.rs index 5b19f4e..59b10f2 100644 --- a/src/game/components.rs +++ b/src/game/components.rs @@ -10,6 +10,16 @@ use super::DiplopodSegments; #[derive(Component)] pub struct DiplopodHead { pub direction: Vec2, + pub immunity: Timer, +} + +impl Default for DiplopodHead { + fn default() -> Self { + Self { + direction: Vec2::ZERO, + immunity: Timer::from_seconds(0.0, TimerMode::Once), + } + } } #[derive(Component)] diff --git a/src/game/resources.rs b/src/game/resources.rs index 9718f32..016f6fd 100644 --- a/src/game/resources.rs +++ b/src/game/resources.rs @@ -60,9 +60,6 @@ impl FreePositions { #[derive(Default, Resource)] pub struct LastSpecialSpawn(pub u32); -#[derive(Default, Resource)] -pub struct ImmunityTime(pub u8); - #[derive(Resource)] pub struct Sounds { pub eat_food: Handle, diff --git a/src/game/systems/control.rs b/src/game/systems/control.rs index af88f55..7642aec 100644 --- a/src/game/systems/control.rs +++ b/src/game/systems/control.rs @@ -39,7 +39,17 @@ impl Command for SpawnDiplopodSegment { .unwrap() }; - let color = if world.resource::().0 > 0 { + let immune = if is_head { + false + } else { + !world + .get::(*segments.first().unwrap()) + .unwrap() + .immunity + .finished() + }; + + let color = if immune { ANTIDOTE_COLOR } else { DIPLOPOD_COLOR @@ -59,9 +69,7 @@ impl Command for SpawnDiplopodSegment { )); if is_head { - segment.insert(DiplopodHead { - direction: Vec2::ZERO, - }); + segment.insert(DiplopodHead::default()); } } } @@ -417,13 +425,12 @@ pub fn eat( superfood_positions: Query<(Entity, &Position), With>, poison_positions: Query<(Entity, &Position), With>, antidote_positions: Query<(Entity, &Position), With>, - head_positions: Query<&DiplopodPosition, With>, + mut heads: Query<(&mut DiplopodHead, &DiplopodPosition)>, wall_positions: Query<(Entity, &Position), With>, mut free_positions: ResMut, - mut immunity_time: ResMut, sounds: Res, ) { - for head_pos in head_positions.iter() { + for (mut head, head_pos) in heads.iter_mut() { for (ent, food_pos) in food_positions.iter() { if *food_pos == head_pos.to_position() { commands.entity(ent).despawn(); @@ -472,7 +479,9 @@ pub fn eat( if *antidote_pos == head_pos.to_position() { commands.entity(ent).despawn(); free_positions.positions.push(*antidote_pos); - immunity_time.0 += 10; + + let remaining = head.immunity.remaining_secs(); + head.immunity = Timer::from_seconds(10.0 + remaining, TimerMode::Once); commands.spawn(( AudioPlayer(sounds.antidote.clone()), @@ -485,7 +494,7 @@ pub fn eat( for (ent, poison_pos) in poison_positions.iter() { if *poison_pos == head_pos.to_position() { - if immunity_time.0 > 0 { + if !head.immunity.finished() { commands.entity(ent).despawn(); free_positions.positions.push(*poison_pos); free_positions.shuffle(); @@ -550,20 +559,24 @@ pub fn move_antidote( } } -pub fn limit_immunity(mut immunity_time: ResMut) { - if immunity_time.0 > 0 { - immunity_time.0 -= 1; +pub fn limit_immunity(mut heads: Query<&mut DiplopodHead>, time: Res