2020-08-27 00:08:29 +00:00
|
|
|
use crate::{
|
2021-01-29 01:37:33 +00:00
|
|
|
combat::{
|
2021-05-06 18:50:16 +00:00
|
|
|
Attack, AttackDamage, AttackEffect, CombatEffect, CombatRequirement, Damage, DamageKind,
|
|
|
|
DamageSource, GroupTarget,
|
2021-01-29 01:37:33 +00:00
|
|
|
},
|
2021-08-07 21:18:49 +00:00
|
|
|
comp::{
|
|
|
|
beam, body::biped_large, Body, CharacterState, EnergyChange, EnergySource, Ori, Pos,
|
|
|
|
StateUpdate,
|
|
|
|
},
|
2020-09-05 16:27:36 +00:00
|
|
|
event::ServerEvent,
|
2020-12-01 00:28:00 +00:00
|
|
|
states::{
|
|
|
|
behavior::{CharacterBehavior, JoinData},
|
|
|
|
utils::*,
|
|
|
|
},
|
2020-12-13 17:11:55 +00:00
|
|
|
uid::Uid,
|
2021-08-04 23:14:10 +00:00
|
|
|
util::Dir,
|
2020-08-27 00:08:29 +00:00
|
|
|
};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::time::Duration;
|
2021-01-19 19:38:32 +00:00
|
|
|
use vek::*;
|
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,
|
2021-03-02 19:26:45 +00:00
|
|
|
/// Base damage per tick
|
|
|
|
pub damage: f32,
|
|
|
|
/// Ticks per second
|
2020-08-31 21:55:38 +00:00
|
|
|
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,
|
2021-03-20 17:29:57 +00:00
|
|
|
/// Adds an effect onto the main damage of the attack
|
|
|
|
pub damage_effect: Option<CombatEffect>,
|
2021-03-02 19:26:45 +00:00
|
|
|
/// Energy regenerated per tick
|
2021-02-05 01:39:12 +00:00
|
|
|
pub energy_regen: f32,
|
2021-03-02 19:26:45 +00:00
|
|
|
/// Energy drained per second
|
2021-01-30 14:14:25 +00:00
|
|
|
pub energy_drain: f32,
|
2021-06-18 17:30:12 +00:00
|
|
|
/// How fast enemy can rotate with beam
|
|
|
|
pub ori_rate: f32,
|
2020-10-07 02:32:57 +00:00
|
|
|
/// What key is used to press ability
|
2021-02-14 06:01:53 +00:00
|
|
|
pub ability_info: AbilityInfo,
|
2021-03-03 04:56:09 +00:00
|
|
|
/// Used to specify the beam to the frontend
|
|
|
|
pub specifier: beam::FrontendSpecifier,
|
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,
|
|
|
|
}
|
|
|
|
|
2020-08-27 00:08:29 +00:00
|
|
|
impl CharacterBehavior for Data {
|
|
|
|
fn behavior(&self, data: &JoinData) -> StateUpdate {
|
|
|
|
let mut update = StateUpdate::from(data);
|
|
|
|
|
2021-06-18 17:30:12 +00:00
|
|
|
let ori_rate = self.static_data.ori_rate;
|
2021-01-19 19:38:32 +00:00
|
|
|
|
2021-08-29 23:23:34 +00:00
|
|
|
handle_orientation(data, &mut update, ori_rate, None);
|
2020-08-27 00:08:29 +00:00
|
|
|
handle_move(data, &mut update, 0.4);
|
2021-03-22 22:47:13 +00:00
|
|
|
handle_jump(data, &mut update, 1.0);
|
2020-08-27 00:08:29 +00:00
|
|
|
|
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 {
|
2021-05-30 19:39:30 +00:00
|
|
|
timer: tick_attack_or_default(data, self.timer, None),
|
2020-10-27 22:16:17 +00:00
|
|
|
..*self
|
2020-09-25 20:02:30 +00:00
|
|
|
});
|
|
|
|
} 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(),
|
|
|
|
});
|
|
|
|
// Build up
|
|
|
|
update.character = CharacterState::BasicBeam(Data {
|
|
|
|
timer: Duration::default(),
|
2021-07-01 19:44:24 +00:00
|
|
|
stage_section: StageSection::Action,
|
2020-10-27 22:16:17 +00:00
|
|
|
..*self
|
2020-09-25 20:02:30 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2021-07-01 19:44:24 +00:00
|
|
|
StageSection::Action => {
|
2021-03-14 20:35:28 +00:00
|
|
|
if input_is_pressed(data, self.static_data.ability_info.input)
|
2021-02-05 01:39:12 +00:00
|
|
|
&& (self.static_data.energy_drain <= f32::EPSILON
|
|
|
|
|| update.energy.current() > 0)
|
2020-10-07 02:32:57 +00:00
|
|
|
{
|
2020-09-25 21:06:51 +00:00
|
|
|
let speed =
|
|
|
|
self.static_data.range / self.static_data.beam_duration.as_secs_f32();
|
2021-01-29 01:37:33 +00:00
|
|
|
|
2021-02-02 18:02:40 +00:00
|
|
|
let energy = AttackEffect::new(
|
|
|
|
None,
|
|
|
|
CombatEffect::EnergyReward(self.static_data.energy_regen),
|
|
|
|
)
|
|
|
|
.with_requirement(CombatRequirement::AnyDamage);
|
2021-03-20 17:29:57 +00:00
|
|
|
let mut damage = AttackDamage::new(
|
2021-02-02 18:02:40 +00:00
|
|
|
Damage {
|
|
|
|
source: DamageSource::Energy,
|
2021-05-06 18:50:16 +00:00
|
|
|
kind: DamageKind::Energy,
|
2021-03-02 19:26:45 +00:00
|
|
|
value: self.static_data.damage,
|
2021-02-02 18:02:40 +00:00
|
|
|
},
|
|
|
|
Some(GroupTarget::OutOfGroup),
|
2021-03-20 17:29:57 +00:00
|
|
|
);
|
|
|
|
if let Some(effect) = self.static_data.damage_effect {
|
|
|
|
damage = damage.with_effect(effect);
|
|
|
|
}
|
2021-03-09 03:53:58 +00:00
|
|
|
let (crit_chance, crit_mult) =
|
|
|
|
get_crit_data(data, self.static_data.ability_info);
|
2021-01-30 05:03:23 +00:00
|
|
|
let attack = Attack::default()
|
|
|
|
.with_damage(damage)
|
2021-03-09 03:53:58 +00:00
|
|
|
.with_crit(crit_chance, crit_mult)
|
2021-03-02 22:19:38 +00:00
|
|
|
.with_effect(energy)
|
|
|
|
.with_combo_increment();
|
2021-01-29 01:37:33 +00:00
|
|
|
|
2020-09-25 20:02:30 +00:00
|
|
|
let properties = beam::Properties {
|
2021-01-29 01:37:33 +00:00
|
|
|
attack,
|
2020-09-25 20:02:30 +00:00
|
|
|
angle: self.static_data.max_angle.to_radians(),
|
|
|
|
speed,
|
|
|
|
duration: self.static_data.beam_duration,
|
|
|
|
owner: Some(*data.uid),
|
2021-03-03 04:56:09 +00:00
|
|
|
specifier: self.static_data.specifier,
|
2020-09-25 20:02:30 +00:00
|
|
|
};
|
2021-08-05 18:00:28 +00:00
|
|
|
let beam_ori = {
|
|
|
|
// We want Beam to use Ori of owner.
|
|
|
|
// But we also want beam to use Z part of where owner looks.
|
2021-08-04 23:14:10 +00:00
|
|
|
// This means that we need to merge this data to one Ori.
|
|
|
|
//
|
|
|
|
// This code just gets look_dir without Z part
|
|
|
|
// and normalizes it. This is what `xy_dir is`.
|
|
|
|
//
|
|
|
|
// Then we find rotation between xy_dir and look_dir
|
|
|
|
// which gives us quaternion how of what rotation we need
|
|
|
|
// to do to get Z part we want.
|
|
|
|
//
|
|
|
|
// Then we construct Ori without Z part
|
|
|
|
// and applying `pitch` to get needed orientation.
|
|
|
|
let look_dir = data.inputs.look_dir;
|
|
|
|
let xy_dir = Dir::from_unnormalized(Vec3::new(look_dir.x, look_dir.y, 0.0))
|
|
|
|
.unwrap_or_else(Dir::default);
|
|
|
|
let pitch = xy_dir.rotation_between(look_dir);
|
2021-08-05 18:00:28 +00:00
|
|
|
|
|
|
|
Ori::from(Vec3::new(
|
|
|
|
update.ori.look_vec().x,
|
|
|
|
update.ori.look_vec().y,
|
|
|
|
0.0,
|
|
|
|
))
|
|
|
|
.prerotated(pitch)
|
|
|
|
};
|
|
|
|
// Gets offsets
|
2021-08-07 21:18:49 +00:00
|
|
|
let body_offsets =
|
|
|
|
beam_offsets(data.body, data.inputs.look_dir, update.ori.look_vec());
|
2020-11-15 20:06:07 +00:00
|
|
|
let pos = Pos(data.pos.0 + body_offsets);
|
2021-08-05 18:00:28 +00:00
|
|
|
|
2020-09-25 20:02:30 +00:00
|
|
|
// Create beam segment
|
|
|
|
update.server_events.push_front(ServerEvent::BeamSegment {
|
|
|
|
properties,
|
|
|
|
pos,
|
2021-08-05 18:00:28 +00:00
|
|
|
ori: beam_ori,
|
2020-09-25 20:02:30 +00:00
|
|
|
});
|
|
|
|
update.character = CharacterState::BasicBeam(Data {
|
2021-05-30 19:39:30 +00:00
|
|
|
timer: tick_attack_or_default(data, self.timer, None),
|
2020-10-27 22:16:17 +00:00
|
|
|
..*self
|
2020-09-25 20:02:30 +00:00
|
|
|
});
|
2020-10-07 02:32:57 +00:00
|
|
|
|
|
|
|
// Consumes energy if there's enough left and ability key is held down
|
2020-10-30 21:49:58 +00:00
|
|
|
update.energy.change_by(EnergyChange {
|
|
|
|
amount: -(self.static_data.energy_drain as f32 * data.dt.0) as i32,
|
|
|
|
source: EnergySource::Ability,
|
|
|
|
});
|
2020-09-25 20:02:30 +00:00
|
|
|
} else {
|
|
|
|
update.character = CharacterState::BasicBeam(Data {
|
|
|
|
timer: Duration::default(),
|
|
|
|
stage_section: StageSection::Recover,
|
2020-10-27 22:16:17 +00:00
|
|
|
..*self
|
2020-09-25 20:02:30 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
StageSection::Recover => {
|
|
|
|
if self.timer < self.static_data.recover_duration {
|
|
|
|
update.character = CharacterState::BasicBeam(Data {
|
2021-05-30 19:39:30 +00:00
|
|
|
timer: tick_attack_or_default(data, self.timer, None),
|
2020-10-27 22:16:17 +00:00
|
|
|
..*self
|
2020-09-25 20:02:30 +00:00
|
|
|
});
|
|
|
|
} 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
|
|
|
}
|
|
|
|
|
2021-03-14 20:35:28 +00:00
|
|
|
// At end of state logic so an interrupt isn't overwritten
|
|
|
|
if !input_is_pressed(data, self.static_data.ability_info.input) {
|
|
|
|
handle_state_interrupt(data, &mut update, false);
|
|
|
|
}
|
|
|
|
|
2020-08-27 00:08:29 +00:00
|
|
|
update
|
|
|
|
}
|
|
|
|
}
|
2021-08-07 21:18:49 +00:00
|
|
|
|
|
|
|
fn height_offset(body: &Body, look_dir: Dir) -> f32 {
|
|
|
|
match body {
|
|
|
|
Body::BirdLarge(_) => body.height() * 0.8,
|
|
|
|
Body::Golem(_) => body.height() * 0.9 + look_dir.z * 3.0,
|
|
|
|
Body::BipedLarge(b) => match b.species {
|
|
|
|
biped_large::Species::Mindflayer => body.height() * 0.6,
|
|
|
|
_ => body.height() * 0.5,
|
|
|
|
},
|
|
|
|
_ => body.height() * 0.5,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn beam_offsets(body: &Body, look_dir: Dir, ori: Vec3<f32>) -> Vec3<f32> {
|
2021-09-16 10:42:07 +00:00
|
|
|
let body_radius = body.min_radius();
|
2021-08-07 21:18:49 +00:00
|
|
|
let body_offsets_z = height_offset(body, look_dir);
|
2021-09-16 10:42:07 +00:00
|
|
|
|
2021-08-07 21:18:49 +00:00
|
|
|
Vec3::new(
|
|
|
|
body_radius * ori.x * 1.1,
|
|
|
|
body_radius * ori.y * 1.1,
|
|
|
|
body_offsets_z,
|
|
|
|
)
|
|
|
|
}
|