Skip to content

Commit 8eb8d47

Browse files
committed
desktop: add coin despawning
1 parent 5adbeef commit 8eb8d47

File tree

10 files changed

+73
-7
lines changed

10 files changed

+73
-7
lines changed

desktop/src/desktop_systems/player_input.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use bevy::input::keyboard::KeyboardInput;
22
use bevy::prelude::*;
3-
use spooky_core::events::player_events::PlayerInputEvent;
3+
use spooky_core::events::player::PlayerInputEvent;
44

55
/// Reads keyboard input (arrow keys) and sends a PlayerInputEvent.
66
/// A positive dx moves right; a positive dy moves up.

desktop/src/main.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ mod desktop_systems;
22

33
use bevy::prelude::*;
44
use spooky_core::systems;
5-
use spooky_core::events::player_events::PlayerInputEvent;
5+
use spooky_core::events::player::PlayerInputEvent;
66

77
fn main() {
88
let mut app = App::new();
@@ -11,8 +11,11 @@ fn main() {
1111
))
1212
.add_systems(Startup, systems::setup::setup)
1313
.add_event::<PlayerInputEvent>()
14+
.add_event::<spooky_core::events::coin::CoinCollisionEvent>()
1415
.add_systems(Update, crate::desktop_systems::player_input::dispatch_keyboard_input)
1516
.add_systems(Update, systems::process_player_input::process_player_input)
17+
.add_systems(Update, spooky_core::systems::coin_collision::detect_coin_collision)
18+
.add_systems(Update, spooky_core::systems::coin_collision::remove_coin_on_collision)
1619
.add_systems(Update, systems::game_logic::update_game)
1720
.run();
1821
}

spooky-core/src/components.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,10 @@ pub struct Wall;
1313
pub struct Ghost;
1414

1515
#[derive(Component)]
16-
pub struct MainCamera;
16+
pub struct MainCamera;
17+
18+
#[derive(Component)]
19+
pub struct CoinComponent {
20+
pub x: i32,
21+
pub y: i32,
22+
}

spooky-core/src/events/coin.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use bevy::prelude::Event;
2+
3+
/// This event is fired when the player collides with a coin.
4+
/// The event carries the coin's pixel coordinates.
5+
#[derive(Debug, Event)]
6+
pub struct CoinCollisionEvent {
7+
pub coin_x: i32,
8+
pub coin_y: i32,
9+
}

spooky-core/src/events/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
pub mod player_events;
1+
pub mod player;
2+
pub mod coin;
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use bevy::prelude::*;
2+
use crate::components::CoinComponent;
3+
use crate::events::coin::CoinCollisionEvent;
4+
use crate::resources::{PlayerPosition, MazeResource};
5+
use crate::maze::Coin;
6+
7+
/// This system checks the player's current position against all coin positions in the maze.
8+
/// If the player is on the same tile as a coin, it dispatches a `CoinCollisionEvent`.
9+
pub fn detect_coin_collision(
10+
player_pos: Res<PlayerPosition>,
11+
maze_res: Res<MazeResource>,
12+
mut event_writer: EventWriter<CoinCollisionEvent>,
13+
) {
14+
// Assuming the player moves in tile increments, cast the logical position to i32.
15+
let player_tile_x = player_pos.x as i32;
16+
let player_tile_y = player_pos.y as i32;
17+
18+
for coin in maze_res.maze.coins.iter() {
19+
if coin.x == player_tile_x && coin.y == player_tile_y {
20+
event_writer.send(CoinCollisionEvent {
21+
coin_x: coin.x,
22+
coin_y: coin.y,
23+
});
24+
}
25+
}
26+
}
27+
28+
/// This system listens for `CoinCollisionEvent` and removes the collided coin from the maze.
29+
pub fn remove_coin_on_collision(
30+
mut events: EventReader<CoinCollisionEvent>,
31+
mut maze_res: ResMut<MazeResource>,
32+
mut commands: Commands,
33+
query: Query<(Entity, &CoinComponent)>,
34+
) {
35+
for event in events.read() {
36+
// Remove the coin at the collision coordinates.
37+
maze_res.maze.remove_coin(Coin { x: event.coin_x, y: event.coin_y });
38+
// Despawn coin entity with matching coordinates.
39+
for (entity, coin_comp) in query.iter() {
40+
if coin_comp.x == event.coin_x && coin_comp.y == event.coin_y {
41+
commands.entity(entity).despawn_recursive();
42+
}
43+
}
44+
}
45+
}

spooky-core/src/systems/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pub mod setup;
22
pub mod process_player_input;
3-
pub mod game_logic;
3+
pub mod game_logic;
4+
pub mod coin_collision;

spooky-core/src/systems/process_player_input.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use bevy::prelude::*;
22
use crate::components::{Player, MainCamera};
33
use crate::resources::{PlayerPosition, MazeResource};
4-
use crate::events::player_events::PlayerInputEvent;
4+
use crate::events::player::PlayerInputEvent;
55

66
// Use our unified transform type alias.
77
use crate::transform::SpookyTransform;

spooky-core/src/systems/setup.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use bevy_math::Vec3;
66
use bevy_transform::prelude::{Transform, GlobalTransform};
77
use crate::maze::Maze;
88
use crate::resources::{MazeResource, PlayerPosition};
9-
use crate::components::Player;
9+
use crate::components::{CoinComponent, Player};
1010

1111
// When compiling for desktop (std enabled), use Bevy's AssetServer and its Image type.
1212
#[cfg(feature = "std")]
@@ -157,6 +157,7 @@ pub fn setup(mut commands: Commands, #[cfg(feature = "std")] asset_server: Res<A
157157
commands.spawn((
158158
Sprite::from_image(textures.coin.clone()),
159159
Transform::from_translation(Vec3::new(coin.x as f32, coin.y as f32, 2.0)),
160+
CoinComponent { x: coin.x, y: coin.y },
160161
));
161162
}
162163
#[cfg(not(feature = "std"))]

0 commit comments

Comments
 (0)