2020-08-27 00:08:29 +00:00
|
|
|
use crate::{
|
2020-10-07 02:32:57 +00:00
|
|
|
comp::{beam, humanoid, Body, CharacterState, EnergySource, Ori, Pos, StateUpdate},
|
2020-09-05 16:27:36 +00:00
|
|
|
event::ServerEvent,
|
2020-10-07 02:32:57 +00:00
|
|
|
states::utils::*,
|
2020-09-24 02:02:30 +00:00
|
|
|
sync::Uid,
|
2020-09-30 00:47:11 +00:00
|
|
|
sys::character_behavior::{CharacterBehavior, JoinData},
|
2020-08-27 00:08:29 +00:00
|
|
|
};
|
|
|
|
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
|
|
|
|
2020-09-25 20:02:30 +00:00
|
|
|
/// Separated out to condense update portions of character state
|
2020-08-27 00:08:29 +00:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
|
2020-09-25 20:02:30 +00:00
|
|
|
pub struct StaticData {
|
2020-08-27 00:08:29 +00:00
|
|
|
/// How long until state should deal damage or heal
|
|
|
|
pub buildup_duration: Duration,
|
|
|
|
/// How long the state has until exiting
|
|
|
|
pub recover_duration: Duration,
|
2020-09-05 16:27:36 +00:00
|
|
|
/// How long each beam segment persists for
|
|
|
|
pub beam_duration: Duration,
|
2020-08-27 00:08:29 +00:00
|
|
|
/// 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,
|
2020-09-15 23:39:19 +00:00
|
|
|
/// Energy regened per second for damage ticks
|
2020-08-27 00:08:29 +00:00
|
|
|
pub energy_regen: u32,
|
2020-09-15 23:39:19 +00:00
|
|
|
/// Energy consumed per second for heal ticks
|
2020-10-07 02:32:57 +00:00
|
|
|
pub energy_cost: u32,
|
|
|
|
/// Energy drained per
|
2020-09-15 23:39:19 +00:00
|
|
|
pub energy_drain: u32,
|
2020-10-07 02:32:57 +00:00
|
|
|
/// What key is used to press ability
|
|
|
|
pub ability_key: AbilityKey,
|
2020-08-27 00:08:29 +00:00
|
|
|
}
|
|
|
|
|
2020-09-25 20:02:30 +00:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub struct Data {
|
|
|
|
/// Struct containing data that does not change over the course of the
|
|
|
|
/// character state
|
|
|
|
pub static_data: StaticData,
|
|
|
|
/// Timer for each stage
|
|
|
|
pub timer: Duration,
|
|
|
|
/// What section the character stage is in
|
|
|
|
pub stage_section: StageSection,
|
|
|
|
/// Used for particle stuffs
|
|
|
|
pub particle_ori: Option<Vec3<f32>>,
|
|
|
|
/// Used to offset beam and particles
|
|
|
|
pub offset: f32,
|
|
|
|
}
|
|
|
|
|
2020-08-27 00:08:29 +00:00
|
|
|
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);
|
|
|
|
|
2020-09-14 22:12:09 +00:00
|
|
|
if unwrap_tool_data(data).is_none() {
|
|
|
|
update.character = CharacterState::Idle;
|
|
|
|
return update;
|
|
|
|
}
|
|
|
|
|
2020-09-25 20:02:30 +00:00
|
|
|
match self.stage_section {
|
|
|
|
StageSection::Buildup => {
|
|
|
|
if self.timer < self.static_data.buildup_duration {
|
|
|
|
// Build up
|
|
|
|
update.character = CharacterState::BasicBeam(Data {
|
|
|
|
static_data: self.static_data,
|
|
|
|
timer: self
|
|
|
|
.timer
|
|
|
|
.checked_add(Duration::from_secs_f32(data.dt.0))
|
|
|
|
.unwrap_or_default(),
|
|
|
|
stage_section: self.stage_section,
|
|
|
|
particle_ori: Some(*data.inputs.look_dir),
|
|
|
|
offset: self.offset,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Creates beam
|
|
|
|
data.updater.insert(data.entity, beam::Beam {
|
|
|
|
hit_entities: Vec::<Uid>::new(),
|
|
|
|
tick_dur: Duration::from_secs_f32(1.0 / self.static_data.tick_rate),
|
|
|
|
timer: Duration::default(),
|
|
|
|
});
|
|
|
|
// Gets offset
|
|
|
|
let eye_height = match data.body {
|
|
|
|
Body::Humanoid(body) => body.eye_height(),
|
|
|
|
_ => humanoid::DEFAULT_HUMANOID_EYE_HEIGHT,
|
|
|
|
};
|
|
|
|
// Build up
|
|
|
|
update.character = CharacterState::BasicBeam(Data {
|
|
|
|
static_data: self.static_data,
|
|
|
|
timer: Duration::default(),
|
|
|
|
stage_section: StageSection::Cast,
|
|
|
|
particle_ori: Some(*data.inputs.look_dir),
|
|
|
|
offset: eye_height * 0.9,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
StageSection::Cast => {
|
2020-10-07 02:32:57 +00:00
|
|
|
if ability_key_is_pressed(data, self.static_data.ability_key)
|
|
|
|
&& (self.static_data.energy_drain == 0 || update.energy.current() > 0)
|
|
|
|
{
|
2020-09-25 21:06:51 +00:00
|
|
|
let damage =
|
|
|
|
(self.static_data.base_dps as f32 / self.static_data.tick_rate) as u32;
|
|
|
|
let heal =
|
|
|
|
(self.static_data.base_hps as f32 / self.static_data.tick_rate) as u32;
|
|
|
|
let energy_regen =
|
|
|
|
(self.static_data.energy_regen as f32 / self.static_data.tick_rate) as u32;
|
2020-10-07 02:32:57 +00:00
|
|
|
let energy_cost =
|
|
|
|
(self.static_data.energy_cost as f32 / self.static_data.tick_rate) as u32;
|
2020-09-25 21:06:51 +00:00
|
|
|
let speed =
|
|
|
|
self.static_data.range / self.static_data.beam_duration.as_secs_f32();
|
2020-09-25 20:02:30 +00:00
|
|
|
let properties = beam::Properties {
|
|
|
|
angle: self.static_data.max_angle.to_radians(),
|
|
|
|
speed,
|
|
|
|
damage,
|
|
|
|
heal,
|
|
|
|
lifesteal_eff: self.static_data.lifesteal_eff,
|
|
|
|
energy_regen,
|
2020-10-07 02:32:57 +00:00
|
|
|
energy_cost,
|
2020-09-25 20:02:30 +00:00
|
|
|
duration: self.static_data.beam_duration,
|
|
|
|
owner: Some(*data.uid),
|
|
|
|
};
|
|
|
|
let pos = Pos(data.pos.0 + Vec3::new(0.0, 0.0, self.offset));
|
|
|
|
// Create beam segment
|
|
|
|
update.server_events.push_front(ServerEvent::BeamSegment {
|
|
|
|
properties,
|
|
|
|
pos,
|
|
|
|
ori: Ori(data.inputs.look_dir),
|
|
|
|
});
|
|
|
|
update.character = CharacterState::BasicBeam(Data {
|
|
|
|
static_data: self.static_data,
|
2020-10-11 01:23:35 +00:00
|
|
|
timer: self
|
|
|
|
.timer
|
|
|
|
.checked_add(Duration::from_secs_f32(data.dt.0))
|
|
|
|
.unwrap_or_default(),
|
2020-09-25 20:02:30 +00:00
|
|
|
stage_section: self.stage_section,
|
|
|
|
particle_ori: Some(*data.inputs.look_dir),
|
|
|
|
offset: self.offset,
|
|
|
|
});
|
2020-10-07 02:32:57 +00:00
|
|
|
|
|
|
|
// Consumes energy if there's enough left and ability key is held down
|
|
|
|
update.energy.change_by(
|
|
|
|
-(self.static_data.energy_drain as f32 * data.dt.0) as i32,
|
|
|
|
EnergySource::Ability,
|
|
|
|
);
|
2020-09-25 20:02:30 +00:00
|
|
|
} else {
|
|
|
|
update.character = CharacterState::BasicBeam(Data {
|
|
|
|
static_data: self.static_data,
|
|
|
|
timer: Duration::default(),
|
|
|
|
stage_section: StageSection::Recover,
|
|
|
|
particle_ori: Some(*data.inputs.look_dir),
|
|
|
|
offset: self.offset,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
StageSection::Recover => {
|
|
|
|
if self.timer < self.static_data.recover_duration {
|
|
|
|
update.character = CharacterState::BasicBeam(Data {
|
|
|
|
static_data: self.static_data,
|
|
|
|
timer: self
|
|
|
|
.timer
|
|
|
|
.checked_add(Duration::from_secs_f32(data.dt.0))
|
|
|
|
.unwrap_or_default(),
|
|
|
|
stage_section: self.stage_section,
|
|
|
|
particle_ori: Some(*data.inputs.look_dir),
|
|
|
|
offset: self.offset,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Done
|
|
|
|
update.character = CharacterState::Wielding;
|
|
|
|
// Make sure attack component is removed
|
|
|
|
data.updater.remove::<beam::Beam>(data.entity);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
// If it somehow ends up in an incorrect stage section
|
|
|
|
update.character = CharacterState::Wielding;
|
|
|
|
// Make sure attack component is removed
|
|
|
|
data.updater.remove::<beam::Beam>(data.entity);
|
|
|
|
},
|
2020-08-27 00:08:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
update
|
|
|
|
}
|
|
|
|
}
|