2020-08-27 00:08:29 +00:00
|
|
|
use crate::{
|
|
|
|
comp::{Attacking, CharacterState, EnergySource, StateUpdate},
|
|
|
|
states::utils::*,
|
|
|
|
sys::character_behavior::*,
|
|
|
|
};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::time::Duration;
|
2020-08-28 02:13:57 +00:00
|
|
|
use vek::Vec3;
|
2020-08-27 00:08:29 +00:00
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub struct Data {
|
|
|
|
/// Whether the attack can currently deal damage
|
|
|
|
pub exhausted: bool,
|
2020-08-28 02:13:57 +00:00
|
|
|
/// Used for particle stuffs
|
|
|
|
pub particle_ori: Option<Vec3<f32>>,
|
2020-08-27 00:08:29 +00:00
|
|
|
/// How long until state should deal damage or heal
|
|
|
|
pub buildup_duration: Duration,
|
|
|
|
/// How long until weapon can deal another tick of damage
|
|
|
|
pub cooldown_duration: Duration,
|
|
|
|
/// How long the state has until exiting
|
|
|
|
pub recover_duration: Duration,
|
|
|
|
/// Base healing per second
|
|
|
|
pub base_hps: u32,
|
|
|
|
/// Base damage per second
|
|
|
|
pub base_dps: u32,
|
2020-08-31 21:55:38 +00:00
|
|
|
/// Ticks of damage/healing per second
|
|
|
|
pub tick_rate: f32,
|
2020-08-27 00:08:29 +00:00
|
|
|
/// Max range
|
|
|
|
pub range: f32,
|
|
|
|
/// Max angle (45.0 will give you a 90.0 angle window)
|
|
|
|
pub max_angle: f32,
|
|
|
|
/// Lifesteal efficiency (0 gives 0% conversion of damage to health, 1 gives
|
|
|
|
/// 100% conversion of damage to health)
|
|
|
|
pub lifesteal_eff: f32,
|
|
|
|
/// Energy regened per second
|
|
|
|
pub energy_regen: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CharacterBehavior for Data {
|
|
|
|
fn behavior(&self, data: &JoinData) -> StateUpdate {
|
|
|
|
let mut update = StateUpdate::from(data);
|
|
|
|
|
|
|
|
handle_move(data, &mut update, 0.4);
|
|
|
|
handle_jump(data, &mut update);
|
|
|
|
|
|
|
|
if self.buildup_duration != Duration::default() {
|
|
|
|
// Build up
|
|
|
|
update.character = CharacterState::BasicBeam(Data {
|
|
|
|
exhausted: self.exhausted,
|
2020-08-28 02:13:57 +00:00
|
|
|
particle_ori: Some(*data.inputs.look_dir),
|
2020-08-27 00:08:29 +00:00
|
|
|
buildup_duration: self
|
|
|
|
.buildup_duration
|
|
|
|
.checked_sub(Duration::from_secs_f32(data.dt.0))
|
|
|
|
.unwrap_or_default(),
|
|
|
|
cooldown_duration: self.cooldown_duration,
|
|
|
|
recover_duration: self.recover_duration,
|
|
|
|
base_hps: self.base_hps,
|
|
|
|
base_dps: self.base_dps,
|
2020-08-31 21:55:38 +00:00
|
|
|
tick_rate: self.tick_rate,
|
2020-08-27 00:08:29 +00:00
|
|
|
range: self.range,
|
|
|
|
max_angle: self.max_angle,
|
|
|
|
lifesteal_eff: self.lifesteal_eff,
|
|
|
|
energy_regen: self.energy_regen,
|
|
|
|
});
|
|
|
|
} else if data.inputs.primary.is_pressed() && !self.exhausted {
|
2020-08-31 21:55:38 +00:00
|
|
|
let damage = (self.base_dps as f32 / self.tick_rate) as u32;
|
|
|
|
let heal = (self.base_hps as f32 / self.tick_rate) as u32;
|
2020-08-27 00:08:29 +00:00
|
|
|
// 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: 0.0,
|
|
|
|
is_melee: false,
|
2020-08-28 02:13:57 +00:00
|
|
|
lifesteal_eff: self.lifesteal_eff,
|
2020-08-27 00:08:29 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
update.character = CharacterState::BasicBeam(Data {
|
|
|
|
exhausted: true,
|
2020-08-28 02:13:57 +00:00
|
|
|
particle_ori: Some(*data.inputs.look_dir),
|
2020-08-27 00:08:29 +00:00
|
|
|
buildup_duration: self.buildup_duration,
|
|
|
|
recover_duration: self.recover_duration,
|
2020-08-31 21:55:38 +00:00
|
|
|
cooldown_duration: Duration::from_secs_f32(1.0 / self.tick_rate),
|
2020-08-27 00:08:29 +00:00
|
|
|
base_hps: self.base_hps,
|
|
|
|
base_dps: self.base_dps,
|
2020-08-31 21:55:38 +00:00
|
|
|
tick_rate: self.tick_rate,
|
2020-08-27 00:08:29 +00:00
|
|
|
range: self.range,
|
|
|
|
max_angle: self.max_angle,
|
|
|
|
lifesteal_eff: self.lifesteal_eff,
|
|
|
|
energy_regen: self.energy_regen,
|
|
|
|
});
|
2020-08-29 20:44:46 +00:00
|
|
|
} else if data.inputs.primary.is_pressed() && self.cooldown_duration != Duration::default()
|
|
|
|
{
|
2020-08-27 00:08:29 +00:00
|
|
|
// Cooldown until next tick of damage
|
|
|
|
update.character = CharacterState::BasicBeam(Data {
|
|
|
|
exhausted: self.exhausted,
|
2020-08-28 02:13:57 +00:00
|
|
|
particle_ori: Some(*data.inputs.look_dir),
|
2020-08-27 00:08:29 +00:00
|
|
|
buildup_duration: self.buildup_duration,
|
|
|
|
cooldown_duration: self
|
|
|
|
.cooldown_duration
|
|
|
|
.checked_sub(Duration::from_secs_f32(data.dt.0))
|
|
|
|
.unwrap_or_default(),
|
|
|
|
recover_duration: self.recover_duration,
|
|
|
|
base_hps: self.base_hps,
|
|
|
|
base_dps: self.base_dps,
|
2020-08-31 21:55:38 +00:00
|
|
|
tick_rate: self.tick_rate,
|
2020-08-27 00:08:29 +00:00
|
|
|
range: self.range,
|
|
|
|
max_angle: self.max_angle,
|
|
|
|
lifesteal_eff: self.lifesteal_eff,
|
|
|
|
energy_regen: self.energy_regen,
|
|
|
|
});
|
|
|
|
} else if data.inputs.primary.is_pressed() {
|
|
|
|
update.character = CharacterState::BasicBeam(Data {
|
|
|
|
exhausted: false,
|
2020-08-28 02:13:57 +00:00
|
|
|
particle_ori: Some(*data.inputs.look_dir),
|
2020-08-27 00:08:29 +00:00
|
|
|
buildup_duration: self.buildup_duration,
|
|
|
|
recover_duration: self.recover_duration,
|
2020-08-31 21:55:38 +00:00
|
|
|
cooldown_duration: self.cooldown_duration,
|
2020-08-27 00:08:29 +00:00
|
|
|
base_hps: self.base_hps,
|
|
|
|
base_dps: self.base_dps,
|
2020-08-31 21:55:38 +00:00
|
|
|
tick_rate: self.tick_rate,
|
2020-08-27 00:08:29 +00:00
|
|
|
range: self.range,
|
|
|
|
max_angle: self.max_angle,
|
|
|
|
lifesteal_eff: self.lifesteal_eff,
|
|
|
|
energy_regen: self.energy_regen,
|
|
|
|
});
|
2020-08-28 02:13:57 +00:00
|
|
|
|
|
|
|
// Grant energy on successful hit
|
2020-08-31 21:55:38 +00:00
|
|
|
let energy = (self.energy_regen as f32 / self.tick_rate) as i32;
|
2020-08-28 02:13:57 +00:00
|
|
|
update.energy.change_by(energy, EnergySource::HitEnemy);
|
2020-08-27 00:08:29 +00:00
|
|
|
} else if self.recover_duration != Duration::default() {
|
|
|
|
// Recovery
|
|
|
|
update.character = CharacterState::BasicBeam(Data {
|
|
|
|
exhausted: self.exhausted,
|
2020-08-28 02:13:57 +00:00
|
|
|
particle_ori: Some(*data.inputs.look_dir),
|
2020-08-27 00:08:29 +00:00
|
|
|
buildup_duration: self.buildup_duration,
|
|
|
|
cooldown_duration: self.cooldown_duration,
|
|
|
|
recover_duration: self
|
|
|
|
.recover_duration
|
|
|
|
.checked_sub(Duration::from_secs_f32(data.dt.0))
|
|
|
|
.unwrap_or_default(),
|
|
|
|
base_hps: self.base_hps,
|
|
|
|
base_dps: self.base_dps,
|
2020-08-31 21:55:38 +00:00
|
|
|
tick_rate: self.tick_rate,
|
2020-08-27 00:08:29 +00:00
|
|
|
range: self.range,
|
|
|
|
max_angle: self.max_angle,
|
|
|
|
lifesteal_eff: self.lifesteal_eff,
|
|
|
|
energy_regen: self.energy_regen,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Done
|
|
|
|
update.character = CharacterState::Wielding;
|
|
|
|
// Make sure attack component is removed
|
|
|
|
data.updater.remove::<Attacking>(data.entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
update
|
|
|
|
}
|
|
|
|
}
|