mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-16 14:12:40 -06:00
feat: allow freezing of blinking entities, lightly refactor game.rs structure
This commit is contained in:
@@ -7,15 +7,21 @@ use bevy_ecs::{
|
||||
|
||||
use crate::systems::{
|
||||
components::{DeltaTime, Renderable},
|
||||
Hidden,
|
||||
Frozen, Hidden,
|
||||
};
|
||||
|
||||
#[derive(Component)]
|
||||
#[derive(Component, Debug)]
|
||||
pub struct Blinking {
|
||||
pub timer: f32,
|
||||
pub interval: f32,
|
||||
}
|
||||
|
||||
impl Blinking {
|
||||
pub fn new(interval: f32) -> Self {
|
||||
Self { timer: 0.0, interval }
|
||||
}
|
||||
}
|
||||
|
||||
/// Updates blinking entities by toggling their visibility at regular intervals.
|
||||
///
|
||||
/// This system manages entities that have both `Blinking` and `Renderable` components,
|
||||
@@ -23,20 +29,34 @@ pub struct Blinking {
|
||||
pub fn blinking_system(
|
||||
mut commands: Commands,
|
||||
time: Res<DeltaTime>,
|
||||
mut query: Query<(Entity, &mut Blinking, Has<Hidden>), With<Renderable>>,
|
||||
mut query: Query<(Entity, &mut Blinking, Has<Hidden>, Has<Frozen>), With<Renderable>>,
|
||||
) {
|
||||
for (entity, mut blinking, hidden) in query.iter_mut() {
|
||||
blinking.timer += time.0;
|
||||
|
||||
if blinking.timer >= blinking.interval {
|
||||
blinking.timer = 0.0;
|
||||
|
||||
// Add or remove the Visible component based on whether it is currently in the query
|
||||
for (entity, mut blinking, hidden, frozen) in query.iter_mut() {
|
||||
// If the entity is frozen, blinking is disabled and the entity is unhidden (if it was hidden)
|
||||
if frozen {
|
||||
if hidden {
|
||||
commands.entity(entity).remove::<Hidden>();
|
||||
} else {
|
||||
commands.entity(entity).insert(Hidden);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
// Increase the timer by the delta time
|
||||
blinking.timer += time.0;
|
||||
|
||||
// If the timer is less than the interval, there's nothing to do yet
|
||||
if blinking.timer < blinking.interval {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Subtract the interval (allows for the timer to retain partial interval progress)
|
||||
blinking.timer -= blinking.interval;
|
||||
|
||||
// Toggle the Hidden component
|
||||
if hidden {
|
||||
commands.entity(entity).remove::<Hidden>();
|
||||
} else {
|
||||
commands.entity(entity).insert(Hidden);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ use bevy_ecs::{
|
||||
};
|
||||
use tracing::debug;
|
||||
|
||||
use crate::systems::{Frozen, GhostCollider, Hidden, PlayerControlled};
|
||||
use crate::systems::{Blinking, Frozen, GhostCollider, Hidden, PlayerControlled};
|
||||
|
||||
#[derive(Resource, Debug, Clone, Copy)]
|
||||
pub enum StartupSequence {
|
||||
@@ -72,6 +72,7 @@ impl StartupSequence {
|
||||
pub fn startup_stage_system(
|
||||
mut startup: ResMut<StartupSequence>,
|
||||
mut commands: Commands,
|
||||
mut blinking_query: Query<Entity, With<Blinking>>,
|
||||
mut player_query: Query<Entity, With<PlayerControlled>>,
|
||||
mut ghost_query: Query<Entity, With<GhostCollider>>,
|
||||
) {
|
||||
@@ -80,10 +81,15 @@ pub fn startup_stage_system(
|
||||
match (from, to) {
|
||||
// (StartupSequence::TextOnly { .. }, StartupSequence::CharactersVisible { .. }) => {}
|
||||
(StartupSequence::CharactersVisible { .. }, StartupSequence::GameActive) => {
|
||||
// Remove Frozen/Hidden tags from all entities and enable player input
|
||||
// Unfreeze and unhide the player & ghosts
|
||||
for entity in player_query.iter_mut().chain(ghost_query.iter_mut()) {
|
||||
commands.entity(entity).remove::<(Frozen, Hidden)>();
|
||||
}
|
||||
|
||||
// Unfreeze pellet blinking
|
||||
for entity in blinking_query.iter_mut() {
|
||||
commands.entity(entity).remove::<Frozen>();
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user