diff --git a/src/game/mod.rs b/src/game/mod.rs index 7086b1d..98a3403 100644 --- a/src/game/mod.rs +++ b/src/game/mod.rs @@ -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), diff --git a/src/systems/item.rs b/src/systems/item.rs new file mode 100644 index 0000000..d2cd91a --- /dev/null +++ b/src/systems/item.rs @@ -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, + mut score: ResMut, + pacman_query: Query>, + item_query: Query<(Entity, &EntityType), With>, +) { + 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(); + } + } + } +} diff --git a/src/systems/mod.rs b/src/systems/mod.rs index 0c4fa03..87530f1 100644 --- a/src/systems/mod.rs +++ b/src/systems/mod.rs @@ -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;