feat(health): added damage event and vulnerable structs
This commit is contained in:
parent
6b2d5d8af1
commit
48c13347fe
2 changed files with 27 additions and 1 deletions
|
|
@ -0,0 +1,22 @@
|
|||
use bevy::prelude::*;
|
||||
|
||||
use crate::health::Health;
|
||||
|
||||
/// Event that is called on entities when they should recieve damage
|
||||
#[derive(EntityEvent)]
|
||||
pub struct Damage {
|
||||
entity: Entity,
|
||||
damage: usize
|
||||
}
|
||||
|
||||
/// Marker-component that marks entity for an automatic damage handling
|
||||
#[derive(Component)]
|
||||
#[require(Health)]
|
||||
pub struct Vulnerable;
|
||||
|
||||
/// Damages every vulnerable health components.
|
||||
pub(super) fn damage_vulnerable_system(event: On<Damage>, mut health_query: Query<&mut Health,With<Vulnerable>>) {
|
||||
if let Ok(mut health) = health_query.get_mut(event.entity) {
|
||||
health.damage(event.damage);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,9 @@
|
|||
use bevy::prelude::*;
|
||||
|
||||
use event::damage_vulnerable_system;
|
||||
|
||||
pub use event::{Damage,Vulnerable};
|
||||
|
||||
pub mod event;
|
||||
|
||||
/// Health plugin provides systems to handle health modifications and events
|
||||
|
|
@ -7,7 +11,7 @@ pub struct HealthPlugin;
|
|||
|
||||
impl Plugin for HealthPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(PostUpdate,cleanup_killed);
|
||||
app.add_systems(PostUpdate,cleanup_killed).add_observer(damage_vulnerable_system);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue