Skip to content

Commit

Permalink
Migrate PR to Bevy 0.11
Browse files Browse the repository at this point in the history
  • Loading branch information
Zakru committed Jul 27, 2023
1 parent 8cac0dc commit e65c2a9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 20 deletions.
17 changes: 13 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 11 additions & 14 deletions crates/audio/src/spatial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use bevy_kira_audio::{
AudioControl, AudioInstance,
};
use de_camera::CameraFocus;
use de_core::{baseset::GameSet, gamestate::GameState, state::AppState};
use de_core::{gamestate::GameState, state::AppState};
use enum_map::{enum_map, Enum, EnumMap};
use iyes_progress::{Progress, ProgressSystem};

Expand All @@ -20,18 +20,14 @@ pub(crate) struct SpatialSoundPlugin;
impl Plugin for SpatialSoundPlugin {
fn build(&self, app: &mut App) {
app.add_event::<PlaySpatialAudioEvent>()
.add_system(setup.in_schedule(OnEnter(AppState::AppLoading)))
.add_system(load.track_progress().run_if(in_state(AppState::AppLoading)))
.add_system(
play.in_base_set(GameSet::PostUpdate)
.run_if(in_state(GameState::Playing))
.run_if(on_event::<PlaySpatialAudioEvent>()),
)
.add_system(
update_spatial
.after(play)
.in_base_set(GameSet::PostUpdate)
.run_if(in_state(GameState::Playing)),
.add_systems(OnEnter(AppState::AppLoading), setup)
.add_systems(Update, load.track_progress().run_if(in_state(AppState::AppLoading)))
.add_systems(
PostUpdate,
(
play.run_if(on_event::<PlaySpatialAudioEvent>()),
update_spatial.after(play),
).run_if(in_state(GameState::Playing)),
);
}
}
Expand All @@ -45,6 +41,7 @@ pub enum Sound {
LaserFire,
}

#[derive(Event)]
pub struct PlaySpatialAudioEvent {
pub sound: Sound,
pub position: Vec3,
Expand Down Expand Up @@ -170,7 +167,7 @@ fn update_spatial(

for (entity, audio, transform) in &spatial_audios {
let Some(audio_instance) = audio_instances.get_mut(audio) else {
commands.entity(entity).despawn();
commands.entity(entity).despawn_recursive();
continue;
};

Expand Down
15 changes: 13 additions & 2 deletions crates/spawner/src/despawner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::marker::PhantomData;

use bevy::ecs::query::{ReadOnlyWorldQuery, WorldQuery};
use bevy::prelude::*;
use de_audio::spatial::{PlaySpatialAudioEvent, Sound};
use de_core::objects::ActiveObjectType;
use de_core::{objects::ObjectType, player::Player, state::AppState};
use de_objects::Health;

Expand Down Expand Up @@ -43,13 +45,22 @@ pub struct DespawnEvent(Entity);
/// Find all entities with low health and mark them for despawning
fn find_dead(
mut counter: ResMut<ObjectCounter>,
entities: Query<(Entity, &Player, &ObjectType, &Health), Changed<Health>>,
entities: Query<(Entity, &Player, &ObjectType, &Health, &Transform), Changed<Health>>,
mut event_writer: EventWriter<DespawnEvent>,
mut play_audio: EventWriter<PlaySpatialAudioEvent>,
) {
for (entity, &player, &object_type, health) in entities.iter() {
for (entity, &player, &object_type, health, transform) in entities.iter() {
if health.destroyed() {
if let ObjectType::Active(active_type) = object_type {
counter.player_mut(player).unwrap().update(active_type, -1);

play_audio.send(PlaySpatialAudioEvent::new(
match active_type {
ActiveObjectType::Building(_) => Sound::DestroyBuilding,
ActiveObjectType::Unit(_) => Sound::DestroyUnit,
},
transform.translation,
));
}
event_writer.send(DespawnEvent(entity));
}
Expand Down

0 comments on commit e65c2a9

Please sign in to comment.