Skip to content

Commit

Permalink
Fixing bug despawning pickable after collected
Browse files Browse the repository at this point in the history
  • Loading branch information
moreirayokoyama committed Jul 29, 2024
1 parent 4e65226 commit 5dfa346
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
11 changes: 7 additions & 4 deletions src/character.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ use bevy::{
use bevy_rapier2d::prelude::*;

use crate::{
control::CharacterControlInput, pickables::PlacedPickable, GameWorld, BLOCK_SIZE,
CHARACTER_JUMP_SPEED, CHARACTER_MOVEMENT_SPEED, CHARACTER_SIZE, GRAVITY, PIXEL_PERFECT_LAYERS,
WORLD_BOTTOM_OFFSET_IN_PIXELS, WORLD_CENTER_COL,
control::CharacterControlInput,
pickables::{PlacedPickable, PlacedPickableCollected},
GameWorld, BLOCK_SIZE, CHARACTER_JUMP_SPEED, CHARACTER_MOVEMENT_SPEED, CHARACTER_SIZE, GRAVITY,
PIXEL_PERFECT_LAYERS, WORLD_BOTTOM_OFFSET_IN_PIXELS, WORLD_CENTER_COL,
};

const GROUND_TIMER: f32 = 0.5;
Expand Down Expand Up @@ -278,7 +279,9 @@ fn handle_collision(
for collision in &controller_output.collisions {
if let Ok(placed_pickable) = placed_pickables.get(collision.entity) {
coin_pouch.0 += placed_pickable.item_type.get_coins();
commands.entity(placed_pickable.entity).despawn();
commands.trigger(PlacedPickableCollected {
entity: placed_pickable.entity,
});
commands.entity(collision.entity).despawn();
}
}
Expand Down
30 changes: 18 additions & 12 deletions src/pickables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ impl Plugin for PickablesPlugin {
fn build(&self, app: &mut bevy::prelude::App) {
app.observe(on_new_day)
.observe(on_new_chunk)
.add_systems(Startup, (initialize, load_textures, startup).chain());
.observe(on_pickable_collected)
.add_systems(Startup, (load_textures, startup).chain());
}
}

Expand Down Expand Up @@ -64,25 +65,21 @@ impl Distribution<PickableItemType> for Standard {
}
}

#[derive(Resource)]
pub struct PickableCount(u8);

#[derive(Component)]
pub struct Pickable {
pub id: u8,
pub item_type: PickableItemType,
pub x: usize,
}

#[derive(Component)]
pub struct PlacedPickable {
pub id: u8,
pub entity: Entity,
pub item_type: PickableItemType,
}

fn initialize(mut commands: Commands) {
commands.insert_resource(PickableCount(0));
#[derive(Event)]
pub struct PlacedPickableCollected {
pub entity: Entity,
}

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

fn startup(mut commands: Commands, game_world: Res<GameWorld>, mut counter: ResMut<PickableCount>) {
fn startup(mut commands: Commands, game_world: Res<GameWorld>) {
let mut rng = thread_rng();
let pickables_count = rng.gen_range(8..64) as usize;
for _ in 0..pickables_count {
counter.0 += 1;
commands.spawn(Pickable {
id: counter.0,
item_type: random(),
x: game_world.get_random_x_block(),
});
Expand Down Expand Up @@ -130,7 +125,6 @@ fn on_new_chunk(
for (entity, item) in items {
parent.spawn((
PlacedPickable {
id: item.id,
entity,
item_type: item.item_type.clone(),
},
Expand All @@ -157,3 +151,15 @@ fn on_new_chunk(
}
});
}

fn on_pickable_collected(
trigger: Trigger<PlacedPickableCollected>,
query: Query<(Entity, &Pickable)>,
mut commands: Commands,
) {
if let Ok((entity, _)) = query.get(trigger.event().entity) {
commands.entity(entity).despawn();
} else {
println!("Pickable not found: {:?}", trigger.event().entity);
}
}

0 comments on commit 5dfa346

Please sign in to comment.