From b03d2942e1bef98e13acadde5cbb8856a3f8c74d Mon Sep 17 00:00:00 2001 From: mat Date: Thu, 19 Mar 2026 04:12:20 -0100 Subject: implement speed effect --- azalea-entity/src/plugin/effect_events.rs | 58 +++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 azalea-entity/src/plugin/effect_events.rs (limited to 'azalea-entity/src/plugin/effect_events.rs') diff --git a/azalea-entity/src/plugin/effect_events.rs b/azalea-entity/src/plugin/effect_events.rs new file mode 100644 index 00000000..2d37c463 --- /dev/null +++ b/azalea-entity/src/plugin/effect_events.rs @@ -0,0 +1,58 @@ +use azalea_registry::builtin::MobEffect; +use bevy_ecs::{entity::Entity, event::EntityEvent, observer::On, system::Query}; +use tracing::warn; + +use crate::{ActiveEffects, Attributes, MobEffectData, effects::attribute_modifier_for_effect}; + +#[derive(EntityEvent)] +pub struct AddEffectEvent { + pub entity: Entity, + pub id: MobEffect, + pub data: MobEffectData, +} +pub fn handle_add_effect( + add_effect: On, + mut query: Query<(&mut ActiveEffects, &mut Attributes)>, +) { + let Ok((mut active_effects, mut attributes)) = query.get_mut(add_effect.entity) else { + warn!("got handle_add_effect for an entity without the required components"); + return; + }; + + active_effects.insert(add_effect.id, add_effect.data.clone()); + + if let Some((attribute, modifier)) = attribute_modifier_for_effect(add_effect.id) { + let modifier = modifier.create(add_effect.data.amplifier); + if let Some(attribute) = attributes.get_mut(attribute) { + attribute.insert(modifier); + } + } +} + +#[derive(EntityEvent)] +pub struct RemoveEffectsEvent { + pub entity: Entity, + pub effects: Vec, +} +pub fn handle_remove_effects( + remove_effects: On, + mut query: Query<(&mut ActiveEffects, &mut Attributes)>, +) { + let Ok((mut active_effects, mut attributes)) = query.get_mut(remove_effects.entity) else { + warn!("got handle_remove_effects for an entity without the required components"); + return; + }; + + for &effect in &remove_effects.effects { + active_effects.remove(effect); + + if let Some((attribute, modifier)) = attribute_modifier_for_effect(effect) { + // we're just trying to get the id of the modifier, so the amplifier passed here + // doesn't matter + let modifier = modifier.create(0); + if let Some(attribute) = attributes.get_mut(attribute) { + attribute.remove(&modifier.id); + } + } + } +} -- cgit v1.2.3