Skip to content

Commit

Permalink
Fixed Issue with Skipping Boss Phase (#148)
Browse files Browse the repository at this point in the history
Bug was apparent with the update of Bevy version to 0.12.

* check boss system to check if each mob type in read events is a boss

* clear event reader after checking if boss is destroyed

* check if mob is a boss using the boss tag component
  • Loading branch information
cdsupina authored Dec 19, 2023
1 parent 24a541c commit e8184ca
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 11 deletions.
3 changes: 3 additions & 0 deletions crates/thetawave_interface/src/spawnable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,13 @@ pub enum TextEffectType {
ConsumableCollected(ConsumableType),
}

/// Sends information about destroyed mobs
/// Includes information that is lost when the entity is destroyed
#[derive(Event)]
pub struct MobDestroyedEvent {
pub mob_type: MobType,
pub entity: Entity,
pub is_boss: bool,
}

#[derive(Event)]
Expand Down
1 change: 1 addition & 0 deletions src/game/counters/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ mod test {
events.send(MobDestroyedEvent {
mob_type: MobType::Enemy(EnemyMobType::Drone),
entity,
is_boss: false,
});
app.update();
let got_mob_kills = app
Expand Down
15 changes: 12 additions & 3 deletions src/run/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,13 @@ impl Level {
} => {
if spawn_timer.finished() {
// check if no entities with a BossComponent tag exist
!bosses_destroyed_event_reader.is_empty()

if bosses_destroyed_event_reader.is_empty() {
return false;
} else {
bosses_destroyed_event_reader.clear();
true
}
} else {
spawn_timer.tick(time.delta());
if spawn_timer.just_finished() {
Expand Down Expand Up @@ -236,8 +242,11 @@ impl Level {
self.current_phase = Some(modified_current_phase);

// this will short circuit and not call cycle_phase if !phase_completed
if phase_completed && !self.cycle_phase(cycle_phase_event_writer) {
self.init_phase(change_bg_music_event_writer);
if phase_completed {
info!("Phase completed");
if !self.cycle_phase(cycle_phase_event_writer) {
self.init_phase(change_bg_music_event_writer);
}
}

false
Expand Down
7 changes: 5 additions & 2 deletions src/spawnable/mob/behavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
spawnable::{InitialMotion, SpawnConsumableEvent, SpawnEffectEvent, SpawnProjectileEvent},
};

use super::{MobComponent, SpawnPosition};
use super::{BossComponent, MobComponent, SpawnPosition};

/// Types of behaviors that can be performed by mobs
#[derive(Deserialize, Clone)]
Expand Down Expand Up @@ -75,6 +75,7 @@ pub fn mob_execute_behavior_system(
&Transform,
&Velocity,
&HealthComponent,
Option<&BossComponent>,
)>,
mut player_query: Query<(Entity, &mut PlayerComponent)>,
mut spawn_effect_event_writer: EventWriter<SpawnEffectEvent>,
Expand All @@ -95,7 +96,8 @@ pub fn mob_execute_behavior_system(
}

// Iterate through all spawnable entities and execute their behavior
for (entity, mut mob_component, mob_transform, mob_velocity, mob_health) in mob_query.iter_mut()
for (entity, mut mob_component, mob_transform, mob_velocity, mob_health, boss_tag) in
mob_query.iter_mut()
{
let behaviors = mob_component.behaviors.clone();
for behavior in behaviors {
Expand Down Expand Up @@ -250,6 +252,7 @@ pub fn mob_execute_behavior_system(
mob_destroyed_event_writer.send(MobDestroyedEvent {
entity,
mob_type: mob_component.mob_type.clone(),
is_boss: boss_tag.is_some(),
});
}
}
Expand Down
12 changes: 7 additions & 5 deletions src/spawnable/mob/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,6 @@ pub fn spawn_mob(

let mob_entity = mob.id();
// add mob segment if anchor point

for anchor_point in mob_data.mob_segment_anchor_points.iter() {
// spawn mob_segment

Expand Down Expand Up @@ -473,11 +472,14 @@ pub struct BossesDestroyedEvent;

pub fn check_boss_mobs_system(
boss_mobs_query: Query<&BossComponent>,
mob_destroyed_event_reader: EventReader<MobDestroyedEvent>,
mut mob_destroyed_event_reader: EventReader<MobDestroyedEvent>,
mut bosses_destroyed_event_writer: EventWriter<BossesDestroyedEvent>,
) {
// if a mob was just destroyed, check if there are any boss mobs left
if !mob_destroyed_event_reader.is_empty() && boss_mobs_query.get_single().is_err() {
bosses_destroyed_event_writer.send(BossesDestroyedEvent);
for event in mob_destroyed_event_reader.read() {
// check if the mob that was destroyed was a boss, and check that there are no remaining boss mobs
if event.is_boss && boss_mobs_query.get_single().is_err() {
info!("Sending bosses destroyed event");
bosses_destroyed_event_writer.send(BossesDestroyedEvent);
}
}
}
2 changes: 1 addition & 1 deletion src/spawnable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl Plugin for SpawnablePlugin {
spawn_projectile_system,
spawn_consumable_system, // event generated in mob execute behavior
spawn_mob_system, // event generated in mob execute behavior
check_boss_mobs_system,
check_boss_mobs_system.after(spawn_mob_system),
attract_to_player_system,
)
.run_if(in_state(states::AppStates::Game))
Expand Down

0 comments on commit e8184ca

Please sign in to comment.