Skip to content

Commit 5dfa346

Browse files
Fixing bug despawning pickable after collected
1 parent 4e65226 commit 5dfa346

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

src/character.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ use bevy::{
1111
use bevy_rapier2d::prelude::*;
1212

1313
use crate::{
14-
control::CharacterControlInput, pickables::PlacedPickable, GameWorld, BLOCK_SIZE,
15-
CHARACTER_JUMP_SPEED, CHARACTER_MOVEMENT_SPEED, CHARACTER_SIZE, GRAVITY, PIXEL_PERFECT_LAYERS,
16-
WORLD_BOTTOM_OFFSET_IN_PIXELS, WORLD_CENTER_COL,
14+
control::CharacterControlInput,
15+
pickables::{PlacedPickable, PlacedPickableCollected},
16+
GameWorld, BLOCK_SIZE, CHARACTER_JUMP_SPEED, CHARACTER_MOVEMENT_SPEED, CHARACTER_SIZE, GRAVITY,
17+
PIXEL_PERFECT_LAYERS, WORLD_BOTTOM_OFFSET_IN_PIXELS, WORLD_CENTER_COL,
1718
};
1819

1920
const GROUND_TIMER: f32 = 0.5;
@@ -278,7 +279,9 @@ fn handle_collision(
278279
for collision in &controller_output.collisions {
279280
if let Ok(placed_pickable) = placed_pickables.get(collision.entity) {
280281
coin_pouch.0 += placed_pickable.item_type.get_coins();
281-
commands.entity(placed_pickable.entity).despawn();
282+
commands.trigger(PlacedPickableCollected {
283+
entity: placed_pickable.entity,
284+
});
282285
commands.entity(collision.entity).despawn();
283286
}
284287
}

src/pickables.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ impl Plugin for PickablesPlugin {
1515
fn build(&self, app: &mut bevy::prelude::App) {
1616
app.observe(on_new_day)
1717
.observe(on_new_chunk)
18-
.add_systems(Startup, (initialize, load_textures, startup).chain());
18+
.observe(on_pickable_collected)
19+
.add_systems(Startup, (load_textures, startup).chain());
1920
}
2021
}
2122

@@ -64,25 +65,21 @@ impl Distribution<PickableItemType> for Standard {
6465
}
6566
}
6667

67-
#[derive(Resource)]
68-
pub struct PickableCount(u8);
69-
7068
#[derive(Component)]
7169
pub struct Pickable {
72-
pub id: u8,
7370
pub item_type: PickableItemType,
7471
pub x: usize,
7572
}
7673

7774
#[derive(Component)]
7875
pub struct PlacedPickable {
79-
pub id: u8,
8076
pub entity: Entity,
8177
pub item_type: PickableItemType,
8278
}
8379

84-
fn initialize(mut commands: Commands) {
85-
commands.insert_resource(PickableCount(0));
80+
#[derive(Event)]
81+
pub struct PlacedPickableCollected {
82+
pub entity: Entity,
8683
}
8784

8885
fn load_textures(
@@ -95,13 +92,11 @@ fn load_textures(
9592
commands.insert_resource(Tiles(asset_server.load("purple-valley-icon-set/icons.png")));
9693
}
9794

98-
fn startup(mut commands: Commands, game_world: Res<GameWorld>, mut counter: ResMut<PickableCount>) {
95+
fn startup(mut commands: Commands, game_world: Res<GameWorld>) {
9996
let mut rng = thread_rng();
10097
let pickables_count = rng.gen_range(8..64) as usize;
10198
for _ in 0..pickables_count {
102-
counter.0 += 1;
10399
commands.spawn(Pickable {
104-
id: counter.0,
105100
item_type: random(),
106101
x: game_world.get_random_x_block(),
107102
});
@@ -130,7 +125,6 @@ fn on_new_chunk(
130125
for (entity, item) in items {
131126
parent.spawn((
132127
PlacedPickable {
133-
id: item.id,
134128
entity,
135129
item_type: item.item_type.clone(),
136130
},
@@ -157,3 +151,15 @@ fn on_new_chunk(
157151
}
158152
});
159153
}
154+
155+
fn on_pickable_collected(
156+
trigger: Trigger<PlacedPickableCollected>,
157+
query: Query<(Entity, &Pickable)>,
158+
mut commands: Commands,
159+
) {
160+
if let Ok((entity, _)) = query.get(trigger.event().entity) {
161+
commands.entity(entity).despawn();
162+
} else {
163+
println!("Pickable not found: {:?}", trigger.event().entity);
164+
}
165+
}

0 commit comments

Comments
 (0)