mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-06 11:15:46 -06:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4d397bba5f | |||
| 80930ddd35 |
@@ -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
43
src/systems/item.rs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -6,6 +6,9 @@ use parking_lot::{Mutex, RwLock};
|
||||
use std::time::Duration;
|
||||
use thousands::Separable;
|
||||
|
||||
/// The maximum number of systems that can be profiled. Must not be exceeded, or it will panic.
|
||||
const MAX_SYSTEMS: usize = 11;
|
||||
/// The number of durations to keep in the circular buffer.
|
||||
const TIMING_WINDOW_SIZE: usize = 30;
|
||||
|
||||
#[derive(Resource, Default, Debug)]
|
||||
@@ -17,7 +20,7 @@ pub struct SystemTimings {
|
||||
///
|
||||
/// Also, we use a micromap::Map as the number of systems is generally quite small.
|
||||
/// Just make sure to set the capacity appropriately, or it will panic.
|
||||
pub timings: RwLock<Map<&'static str, Mutex<CircularBuffer<TIMING_WINDOW_SIZE, Duration>>, 10>>,
|
||||
pub timings: RwLock<Map<&'static str, Mutex<CircularBuffer<TIMING_WINDOW_SIZE, Duration>>, MAX_SYSTEMS>>,
|
||||
}
|
||||
|
||||
impl SystemTimings {
|
||||
@@ -43,7 +46,7 @@ impl SystemTimings {
|
||||
});
|
||||
}
|
||||
|
||||
pub fn get_stats(&self) -> Map<&'static str, (Duration, Duration), 10> {
|
||||
pub fn get_stats(&self) -> Map<&'static str, (Duration, Duration), MAX_SYSTEMS> {
|
||||
let timings = self.timings.read();
|
||||
let mut stats = Map::new();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user