Skip to content

Commit

Permalink
Use Timer as property of DiplopodHead for immunity
Browse files Browse the repository at this point in the history
  • Loading branch information
tehlers committed Feb 1, 2025
1 parent 42ba1f9 commit c63bca4
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 26 deletions.
3 changes: 1 addition & 2 deletions src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down Expand Up @@ -92,7 +92,6 @@ impl Plugin for GamePlugin {
.add_systems(OnExit(GameState::Game), despawn_screen::<OnGameScreen>)
.insert_resource(DiplopodSegments::default())
.insert_resource(LastSpecialSpawn::default())
.insert_resource(ImmunityTime::default())
.insert_resource(FreePositions::new(CONSUMABLE_WIDTH, CONSUMABLE_HEIGHT))
.insert_resource(Time::<Fixed>::from_seconds(0.075))
.add_event::<GameOver>()
Expand Down
10 changes: 10 additions & 0 deletions src/game/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
3 changes: 0 additions & 3 deletions src/game/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AudioSource>,
Expand Down
45 changes: 28 additions & 17 deletions src/game/systems/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,17 @@ impl Command for SpawnDiplopodSegment {
.unwrap()
};

let color = if world.resource::<ImmunityTime>().0 > 0 {
let immune = if is_head {
false
} else {
!world
.get::<DiplopodHead>(*segments.first().unwrap())
.unwrap()
.immunity
.finished()
};

let color = if immune {
ANTIDOTE_COLOR
} else {
DIPLOPOD_COLOR
Expand All @@ -59,9 +69,7 @@ impl Command for SpawnDiplopodSegment {
));

if is_head {
segment.insert(DiplopodHead {
direction: Vec2::ZERO,
});
segment.insert(DiplopodHead::default());
}
}
}
Expand Down Expand Up @@ -417,13 +425,12 @@ pub fn eat(
superfood_positions: Query<(Entity, &Position), With<Superfood>>,
poison_positions: Query<(Entity, &Position), With<Poison>>,
antidote_positions: Query<(Entity, &Position), With<Antidote>>,
head_positions: Query<&DiplopodPosition, With<DiplopodHead>>,
mut heads: Query<(&mut DiplopodHead, &DiplopodPosition)>,
wall_positions: Query<(Entity, &Position), With<Wall>>,
mut free_positions: ResMut<FreePositions>,
mut immunity_time: ResMut<ImmunityTime>,
sounds: Res<Sounds>,
) {
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();
Expand Down Expand Up @@ -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()),
Expand All @@ -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();
Expand Down Expand Up @@ -550,20 +559,24 @@ pub fn move_antidote(
}
}

pub fn limit_immunity(mut immunity_time: ResMut<ImmunityTime>) {
if immunity_time.0 > 0 {
immunity_time.0 -= 1;
pub fn limit_immunity(mut heads: Query<&mut DiplopodHead>, time: Res<Time>) {
let mut head = heads.single_mut();

if !head.immunity.finished() {
head.immunity.tick(time.delta());
}
}

pub fn control_antidote_sound(
mut commands: Commands,
immunity_time: Res<ImmunityTime>,
heads: Query<&DiplopodHead>,
antidote_sound: Query<(&AudioSink, Entity), With<AntidoteSound>>,
) {
if immunity_time.0 > 2 {
let head = heads.single();

if head.immunity.remaining_secs() > 2.0 {
// keep the sound
} else if immunity_time.0 > 0 {
} else if !head.immunity.finished() {
if let Ok(sound) = antidote_sound.get_single() {
sound.0.toggle();
}
Expand All @@ -580,7 +593,6 @@ pub fn game_over(
mut segments: ResMut<DiplopodSegments>,
mut free_positions: ResMut<FreePositions>,
mut last_special_spawn: ResMut<LastSpecialSpawn>,
mut immunity_time: ResMut<ImmunityTime>,
sounds: Res<Sounds>,
mut game_state: ResMut<NextState<GameState>>,
mut lastscore: ResMut<Lastscore>,
Expand All @@ -601,7 +613,6 @@ pub fn game_over(
free_positions.reset();

last_special_spawn.0 = 0;
immunity_time.0 = 0;

segments.0 = Vec::new();

Expand Down
9 changes: 5 additions & 4 deletions src/game/systems/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use bevy_prototype_lyon::prelude::*;

use crate::game::components::*;
use crate::game::events::ShowMessage;
use crate::game::resources::ImmunityTime;
use crate::game::OnGameScreen;
use crate::prelude::*;

Expand Down Expand Up @@ -49,14 +48,16 @@ pub fn rotate_superfood(mut query: Query<&mut Transform, With<Superfood>>, time:

pub fn change_color(
mut query: Query<(&mut Fill, &mut Stroke), With<DiplopodSegment>>,
immunity_time: Res<ImmunityTime>,
heads: Query<&DiplopodHead>,
) {
if immunity_time.0 > 2 {
let head = heads.single();

if head.immunity.remaining_secs() > 2.0 {
for (mut fill, mut stroke) in query.iter_mut() {
fill.color = DIPLOPOD_IMMUNE_COLOR;
stroke.color = DIPLOPOD_IMMUNE_COLOR;
}
} else if immunity_time.0 > 0 {
} else if !head.immunity.finished() {
for (mut fill, mut stroke) in query.iter_mut() {
if fill.color == DIPLOPOD_IMMUNE_COLOR {
fill.color = DIPLOPOD_COLOR;
Expand Down

0 comments on commit c63bca4

Please sign in to comment.