Hey, I'm pretty new to bevy and wanted to know if I'm doing something correctly or overcomplicating things.
In my game I want to handle Death of enemies and bosses and basically anything that has the Health component in the same generic Observer that listens to DeathEvents
#[derive(Event)]
pub struct DeathEvent {
pub target: Entity,
}
if health.get_current() <= 0.0 {
println!("entity {target} has 0 health, sending death event");
commands.trigger(DeathEvent { target });
}
But if I'm understanding correctly if I have an observer like so
pub(super) fn plugin(app: &mut App) {
app.add_observer(death_event_trigger::<Enemy>).
add_observer(death_event_trigger::<Boss>);
}
pub trait OnDeath {
fn on_death(&self) {
println!("Default");
}
}
pub trait DeathComponent: Component + OnDeath {}
impl<T: Component + OnDeath> DeathComponent for T {}
fn death_event_trigger<T: DeathComponent>(
trigger: Trigger<DeathEvent>,
mut transform_query: Query<(&mut Transform, &T)>,
) {
let entity = trigger.event().target;
if let Ok((mut transform, mut death_component)) = transform_query.get_mut(entity) {
death_component.on_death()
}
}
Will it even work, or is it going to create a race condition as there are going to be multiple observers reading from the same events being sent,
Thanks in advance!