use crate::{ comp::{Attacking, CharacterState, EnergySource, StateUpdate}, states::utils::*, sys::character_behavior::*, }; use serde::{Deserialize, Serialize}; use std::time::Duration; #[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)] pub struct Data { /// How long until state should deal damage pub buildup_duration: Duration, /// How long the state has until exiting pub recover_duration: Duration, /// Base damage (negative) or healing (positive) pub base_healthchange: i32, /// Knockback pub knockback: f32, /// Max range pub range: f32, /// Max angle (45.0 will give you a 90.0 angle window) pub max_angle: f32, /// Whether the attack can deal more damage pub exhausted: bool, } impl CharacterBehavior for Data { fn behavior(&self, data: &JoinData) -> StateUpdate { let mut update = StateUpdate::from(data); handle_move(data, &mut update, 0.7); handle_jump(data, &mut update); if self.buildup_duration != Duration::default() { // Build up update.character = CharacterState::BasicMelee(Data { buildup_duration: self .buildup_duration .checked_sub(Duration::from_secs_f32(data.dt.0)) .unwrap_or_default(), recover_duration: self.recover_duration, base_healthchange: self.base_healthchange, knockback: self.knockback, range: self.range, max_angle: self.max_angle, exhausted: false, }); } else if !self.exhausted { let (damage, heal): (u32, u32); if self.base_healthchange > 0 { heal = self.base_healthchange as u32; damage = 0; } else { damage = (-self.base_healthchange) as u32; heal = 0; } // Hit attempt data.updater.insert(data.entity, Attacking { base_damage: damage, base_heal: heal, range: self.range, max_angle: self.max_angle.to_radians(), applied: false, hit_count: 0, knockback: self.knockback, }); update.character = CharacterState::BasicMelee(Data { buildup_duration: self.buildup_duration, recover_duration: self.recover_duration, base_healthchange: self.base_healthchange, knockback: self.knockback, range: self.range, max_angle: self.max_angle, exhausted: true, }); } else if self.recover_duration != Duration::default() { // Recovery update.character = CharacterState::BasicMelee(Data { buildup_duration: self.buildup_duration, recover_duration: self .recover_duration .checked_sub(Duration::from_secs_f32(data.dt.0)) .unwrap_or_default(), base_healthchange: self.base_healthchange, knockback: self.knockback, range: self.range, max_angle: self.max_angle, exhausted: true, }); } else { // Done update.character = CharacterState::Wielding; // Make sure attack component is removed data.updater.remove::(data.entity); } // Grant energy on successful hit if let Some(attack) = data.attacking { if attack.applied && attack.hit_count > 0 { data.updater.remove::(data.entity); update.energy.change_by(50, EnergySource::HitEnemy); } } update } }