feat: item collection system, score mutations

This commit is contained in:
2025-08-15 18:41:08 -05:00
parent 80930ddd35
commit 4d397bba5f
3 changed files with 46 additions and 0 deletions

View File

@@ -19,6 +19,7 @@ use crate::systems::{
control::player_system,
debug::{debug_render_system, DebugState, DebugTextureResource},
input::input_system,
item::item_system,
movement::movement_system,
profiling::{profile, SystemTimings},
render::{directional_render_system, dirty_render_system, render_system, BackbufferResource, MapTextureResource},
@@ -211,6 +212,7 @@ impl Game {
profile("player", player_system),
profile("movement", movement_system),
profile("collision", collision_system),
profile("item", item_system),
profile("blinking", blinking_system),
profile("directional_render", directional_render_system),
profile("dirty_render", dirty_render_system),

43
src/systems/item.rs Normal file
View File

@@ -0,0 +1,43 @@
use bevy_ecs::{event::EventReader, prelude::*, query::With, system::Query};
use crate::{
events::GameEvent,
systems::components::{EntityType, ItemCollider, PacmanCollider, ScoreResource},
};
pub fn item_system(
mut commands: Commands,
mut collision_events: EventReader<GameEvent>,
mut score: ResMut<ScoreResource>,
pacman_query: Query<Entity, With<PacmanCollider>>,
item_query: Query<(Entity, &EntityType), With<ItemCollider>>,
) {
for event in collision_events.read() {
if let GameEvent::Collision(entity1, entity2) = event {
// Check if one is Pacman and the other is an item
let (_pacman_entity, item_entity) = if pacman_query.get(*entity1).is_ok() && item_query.get(*entity2).is_ok() {
(*entity1, *entity2)
} else if pacman_query.get(*entity2).is_ok() && item_query.get(*entity1).is_ok() {
(*entity2, *entity1)
} else {
continue;
};
// Get the item type and update score
if let Ok((item_ent, entity_type)) = item_query.get(item_entity) {
match entity_type {
EntityType::Pellet => {
score.0 += 10;
}
EntityType::PowerPellet => {
score.0 += 50;
}
_ => continue,
}
// Remove the collected item
commands.entity(item_ent).despawn();
}
}
}
}

View File

@@ -10,6 +10,7 @@ pub mod control;
pub mod debug;
pub mod formatting;
pub mod input;
pub mod item;
pub mod movement;
pub mod profiling;
pub mod render;