From 818f2d01d49e574946d1a704e1445156afc9c2fb Mon Sep 17 00:00:00 2001 From: ShayBox Date: Thu, 30 Oct 2025 12:14:19 -0400 Subject: Add support for mob effects (#269) * Add support for mob effects * Remove Option * MobEffectFlags * jump_boost_power f32 --- azalea-entity/src/effects.rs | 83 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 71 insertions(+), 12 deletions(-) (limited to 'azalea-entity/src/effects.rs') diff --git a/azalea-entity/src/effects.rs b/azalea-entity/src/effects.rs index 9cc750e5..d905414d 100644 --- a/azalea-entity/src/effects.rs +++ b/azalea-entity/src/effects.rs @@ -1,21 +1,80 @@ -// TODO +use std::collections::HashMap; -// pub struct ActiveEffects(HashMap); +use azalea_registry::MobEffect; +use bevy_ecs::component::Component; -/// Returns the level of the given effect, or `None` if the effect is not -/// active. The lowest level is 0. -pub fn get_effect(_effect: azalea_registry::MobEffect) -> Option { - // TODO - None +/// Data about an active mob effect that the client knows about. +#[derive(Clone, Debug, Default)] +pub struct MobEffectData { + pub amplifier: u32, + pub duration_ticks: u32, + pub ambient: bool, + pub show_particles: bool, + pub show_icon: bool, +} +impl MobEffectData { + pub fn new( + amplifier: u32, + duration_ticks: u32, + ambient: bool, + show_particles: bool, + show_icon: bool, + ) -> Self { + Self { + amplifier, + duration_ticks, + ambient, + show_particles, + show_icon, + } + } + + pub fn is_ambient(&self) -> bool { + self.ambient + } + pub fn should_show_particles(&self) -> bool { + self.show_particles + } + pub fn should_show_icon(&self) -> bool { + self.show_icon + } +} + +/// Component storing the active mob effects on an entity. +#[derive(Component, Clone, Debug, Default)] +pub struct ActiveEffects(pub HashMap); +impl ActiveEffects { + pub fn insert(&mut self, effect: MobEffect, data: MobEffectData) { + self.0.insert(effect, data); + } + + pub fn remove(&mut self, effect: MobEffect) -> Option { + self.0.remove(&effect) + } + + pub fn get_level(&self, effect: MobEffect) -> Option { + self.0.get(&effect).map(|data| data.amplifier) + } + + pub fn get(&self, effect: MobEffect) -> Option<&MobEffectData> { + self.0.get(&effect) + } +} + +/// Returns the level (amplifier) of the given effect, or `None` if the effect +/// is not active. The lowest level is 0. +pub fn get_effect(active_effects: &ActiveEffects, effect: MobEffect) -> Option { + active_effects.get_level(effect) } -pub fn get_dig_speed_amplifier() -> Option { +/// Returns the amplifier for dig speed (haste / conduit power), if present. +pub fn get_dig_speed_amplifier(active_effects: &ActiveEffects) -> Option { let effect_plus_one = u32::max( - get_effect(azalea_registry::MobEffect::Haste) - .map(|x| x + 1) + get_effect(active_effects, MobEffect::Haste) + .map(|level| level + 1) .unwrap_or_default(), - get_effect(azalea_registry::MobEffect::ConduitPower) - .map(|x| x + 1) + get_effect(active_effects, MobEffect::ConduitPower) + .map(|level| level + 1) .unwrap_or_default(), ); if effect_plus_one > 0 { -- cgit v1.2.3