azalea_entity/plugin/
effect_events.rs1use azalea_registry::builtin::MobEffect;
2use bevy_ecs::{entity::Entity, event::EntityEvent, observer::On, system::Query};
3use tracing::warn;
4
5use crate::{ActiveEffects, Attributes, MobEffectData, effects::attribute_modifier_for_effect};
6
7#[derive(EntityEvent)]
8pub struct AddEffectEvent {
9 pub entity: Entity,
10 pub id: MobEffect,
11 pub data: MobEffectData,
12}
13pub fn handle_add_effect(
14 add_effect: On<AddEffectEvent>,
15 mut query: Query<(&mut ActiveEffects, &mut Attributes)>,
16) {
17 let Ok((mut active_effects, mut attributes)) = query.get_mut(add_effect.entity) else {
18 warn!("got handle_add_effect for an entity without the required components");
19 return;
20 };
21
22 active_effects.insert(add_effect.id, add_effect.data.clone());
23
24 if let Some((attribute, modifier)) = attribute_modifier_for_effect(add_effect.id) {
25 let modifier = modifier.create(add_effect.data.amplifier);
26 if let Some(attribute) = attributes.get_mut(attribute) {
27 attribute.insert(modifier);
28 }
29 }
30}
31
32#[derive(EntityEvent)]
33pub struct RemoveEffectsEvent {
34 pub entity: Entity,
35 pub effects: Vec<MobEffect>,
36}
37pub fn handle_remove_effects(
38 remove_effects: On<RemoveEffectsEvent>,
39 mut query: Query<(&mut ActiveEffects, &mut Attributes)>,
40) {
41 let Ok((mut active_effects, mut attributes)) = query.get_mut(remove_effects.entity) else {
42 warn!("got handle_remove_effects for an entity without the required components");
43 return;
44 };
45
46 for &effect in &remove_effects.effects {
47 active_effects.remove(effect);
48
49 if let Some((attribute, modifier)) = attribute_modifier_for_effect(effect) {
50 let modifier = modifier.create(0);
53 if let Some(attribute) = attributes.get_mut(attribute) {
54 attribute.remove(&modifier.id);
55 }
56 }
57 }
58}