Skip to content

Commit

Permalink
despawn jellyfish when its last flappy explodes
Browse files Browse the repository at this point in the history
  • Loading branch information
nelson137 committed Jan 11, 2024
1 parent a5a0e6f commit 6a1ea85
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 9 deletions.
27 changes: 20 additions & 7 deletions src/core/elements/flappy_jellyfish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ fn explode_flappy_jellyfish(
mut animated_sprites: CompMut<AnimatedSprite>,
mut damage_regions: CompMut<DamageRegion>,
mut lifetimes: CompMut<Lifetime>,
mut dehydrate_jellyfish: CompMut<DehydrateJellyfish>,
) {
// Collect the hitboxes of all players
let mut player_hitboxes = SmallVec::<[Rect; MAX_PLAYERS]>::with_capacity(MAX_PLAYERS);
Expand Down Expand Up @@ -223,18 +224,19 @@ fn explode_flappy_jellyfish(
}
}

for flappy in explode_flappy_entities {
let Some(jellyfish_ent) = flappy_jellyfishes.get(flappy).map(|f| f.jellyfish) else {
for flappy_ent in explode_flappy_entities {
let Some(flappy) = flappy_jellyfishes.get(flappy_ent) else {
continue;
};
if let Some(jellyfish) = jellyfishes.get_mut(jellyfish_ent) {
jellyfish.flappy.take();
let Some(jellyfish) = jellyfishes.get_mut(flappy.jellyfish) else {
continue;
};
jellyfish.flappy.take();

// Get data for the explosion

let Some(flappy_meta) = element_handles
.get(jellyfish_ent)
.get(flappy.jellyfish)
.map(|element_h| assets.get(element_h.0))
.map(|element_meta| assets.get(element_meta.data))
.as_deref()
Expand All @@ -244,14 +246,14 @@ fn explode_flappy_jellyfish(
return;
};

let Some(mut explosion_transform) = transforms.get(flappy).copied() else {
let Some(mut explosion_transform) = transforms.get(flappy_ent).copied() else {
return;
};
explosion_transform.translation.z = -10.0;

// Despawn the flappy

entities.kill(flappy);
entities.kill(flappy_ent);

// Setup the explosion

Expand Down Expand Up @@ -297,6 +299,17 @@ fn explode_flappy_jellyfish(
Lifetime::new(flappy_meta.damage_region_lifetime),
);
}

// Despawn the jellyfish if out of ammo

if jellyfish.ammo == 0 {
dehydrate_jellyfish.insert(
flappy.jellyfish,
DehydrateJellyfish {
owner: flappy.owner,
},
);
}
}
}

Expand Down
32 changes: 30 additions & 2 deletions src/core/elements/jellyfish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub fn session_plugin(session: &mut Session) {
session
.stages
.add_system_to_stage(CoreStage::PreUpdate, hydrate)
.add_system_to_stage(CoreStage::PreUpdate, dehydrate)
.add_system_to_stage(CoreStage::PostUpdate, update_jellyfish_driving);
flappy_jellyfish::session_plugin(session);
}
Expand Down Expand Up @@ -65,7 +66,7 @@ fn hydrate(
assets: Res<AssetServer>,
mut jellyfishes: CompMut<Jellyfish>,
mut atlas_sprites: CompMut<AtlasSprite>,
mut respawn_points: CompMut<DehydrateOutOfBounds>,
mut spawners: CompMut<DehydrateOutOfBounds>,
mut items: CompMut<Item>,
mut item_grabs: CompMut<ItemGrab>,
mut item_throws: CompMut<ItemThrow>,
Expand Down Expand Up @@ -100,6 +101,7 @@ fn hydrate(
hydrated.insert(spawner_ent, MapElementHydrated);

let entity = entities.create();
spawners.insert(entity, DehydrateOutOfBounds(spawner_ent));
hydrated.insert(entity, MapElementHydrated);
jellyfishes.insert(entity, Jellyfish::new(*max_ammo));
items.insert(entity, Item);
Expand All @@ -119,7 +121,6 @@ fn hydrate(
);
element_handles.insert(entity, element_handle);
atlas_sprites.insert(entity, AtlasSprite::new(*atlas));
respawn_points.insert(entity, DehydrateOutOfBounds(spawner_ent));
transforms.insert(entity, transform);
bodies.insert(
entity,
Expand All @@ -136,6 +137,33 @@ fn hydrate(
}
}

/// A marker component for jellyfish items to indicate that it has been used and
/// should dehydrate. This will also remove and despawn the jellyfish from the
/// player's inventory.
#[derive(Clone, Debug, Default, HasSchema)]
pub struct DehydrateJellyfish {
pub owner: Entity,
}

fn dehydrate(
entities: Res<Entities>,
dehydrate_jellyfish: Comp<DehydrateJellyfish>,
spawners: Comp<DehydrateOutOfBounds>,
mut player_driving: CompMut<PlayerDrivingJellyfish>,
mut player_inventories: CompMut<Inventory>,
mut commands: Commands,
mut hydrated: CompMut<MapElementHydrated>,
) {
for (jellyfish_ent, (dehydrate, spawner)) in
entities.iter_with((&dehydrate_jellyfish, &spawners))
{
player_driving.remove(dehydrate.owner);
player_inventories.insert(dehydrate.owner, Inventory(None));
commands.add(move |mut entities: ResMut<Entities>| entities.kill(jellyfish_ent));
hydrated.remove(**spawner);
}
}

fn on_jellyfish_drop(entity: Entity, max_ammo: u32) -> StaticSystem<(), ()> {
(move |mut jellyfishes: CompMut<Jellyfish>| {
// Reload
Expand Down

0 comments on commit 6a1ea85

Please sign in to comment.