use crate::{
combat,
combat::{Attack, AttackDamage, Damage, DamageKind::Crushing, DamageSource, GroupTarget},
comp::{
character_state::OutputEvents, item::Reagent, melee::CustomCombo, tool::Stats,
CharacterState, MeleeConstructor, StateUpdate,
},
event::{ExplosionEvent, LocalEvent},
outcome::Outcome,
states::{
behavior::{CharacterBehavior, JoinData},
combo_melee2,
utils::*,
},
Explosion, RadiusEffect,
};
use serde::{Deserialize, Serialize};
use std::time::Duration;
use vek::*;
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Strike<T> {
pub melee_constructor: MeleeConstructor,
pub buildup_duration: T,
pub swing_duration: T,
pub hit_timing: f32,
pub recover_duration: T,
#[serde(default)]
pub movement: StrikeMovement,
pub ori_modifier: f32,
#[serde(default)]
pub custom_combo: Option<CustomCombo>,
}
impl Strike<f32> {
pub fn to_duration(self) -> Strike<Duration> {
Strike::<Duration> {
melee_constructor: self.melee_constructor,
buildup_duration: Duration::from_secs_f32(self.buildup_duration),
swing_duration: Duration::from_secs_f32(self.swing_duration),
hit_timing: self.hit_timing,
recover_duration: Duration::from_secs_f32(self.recover_duration),
movement: self.movement,
ori_modifier: self.ori_modifier,
custom_combo: self.custom_combo,
}
}
#[must_use]
pub fn adjusted_by_stats(self, stats: Stats) -> Self {
Self {
melee_constructor: self.melee_constructor.adjusted_by_stats(stats),
buildup_duration: self.buildup_duration / stats.speed,
swing_duration: self.swing_duration / stats.speed,
hit_timing: self.hit_timing,
recover_duration: self.recover_duration / stats.speed,
movement: self.movement,
ori_modifier: self.ori_modifier,
custom_combo: self.custom_combo,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize, Default)]
#[serde(deny_unknown_fields)]
pub struct StrikeMovement {
pub buildup: Option<ForcedMovement>,
pub swing: Option<ForcedMovement>,
pub recover: Option<ForcedMovement>,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct StaticData {
pub strikes: Vec<Strike<Duration>>,
pub energy_cost_per_strike: f32,
pub specifier: Option<combo_melee2::FrontendSpecifier>,
pub auto_progress: bool,
pub ability_info: AbilityInfo,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Data {
pub static_data: StaticData,
pub exhausted: bool,
pub start_next_strike: bool,
pub timer: Duration,
pub stage_section: StageSection,
pub completed_strikes: usize,
}
impl CharacterBehavior for Data {
fn behavior(&self, data: &JoinData, output_events: &mut OutputEvents) -> StateUpdate {
let mut update = StateUpdate::from(data);
handle_orientation(data, &mut update, 1.0, None);
handle_move(data, &mut update, 0.7);
handle_interrupts(data, &mut update, output_events);
let strike_data = self.strike_data();
match self.stage_section {
StageSection::Buildup => {
if let Some(movement) = strike_data.movement.buildup {
handle_forced_movement(data, &mut update, movement);
}
if self.timer < strike_data.buildup_duration {
if let CharacterState::ComboMelee2(c) = &mut update.character {
c.timer = tick_attack_or_default(data, self.timer, None);
}
} else {
if let CharacterState::ComboMelee2(c) = &mut update.character {
c.timer = Duration::default();
c.stage_section = StageSection::Action;
}
}
if let Some(FrontendSpecifier::ClayGolemDash) = self.static_data.specifier {
output_events.emit_local(LocalEvent::CreateOutcome(Outcome::ClayGolemDash {
pos: data.pos.0,
}));
}
},
StageSection::Action => {
if let Some(movement) = strike_data.movement.swing {
handle_forced_movement(data, &mut update, movement);
}
if input_is_pressed(data, self.static_data.ability_info.input)
|| self.static_data.auto_progress
{
if let CharacterState::ComboMelee2(c) = &mut update.character {
c.start_next_strike =
(c.completed_strikes + 1) < c.static_data.strikes.len();
}
}
if self.timer.as_secs_f32()
> strike_data.hit_timing * strike_data.swing_duration.as_secs_f32()
&& !self.exhausted
{
if let CharacterState::ComboMelee2(c) = &mut update.character {
c.timer = tick_attack_or_default(data, self.timer, None);
c.exhausted = true;
}
let precision_mult = combat::compute_precision_mult(data.inventory, data.msm);
let tool_stats = get_tool_stats(data, self.static_data.ability_info);
data.updater.insert(
data.entity,
strike_data
.melee_constructor
.custom_combo(strike_data.custom_combo)
.create_melee(precision_mult, tool_stats),
);
} else if self.timer < strike_data.swing_duration {
if let CharacterState::ComboMelee2(c) = &mut update.character {
c.timer = tick_attack_or_default(data, self.timer, None);
}
if self.static_data.specifier == Some(FrontendSpecifier::IronGolemFist) {
let damage = AttackDamage::new(
Damage {
source: DamageSource::Explosion,
kind: Crushing,
value: 10.0,
},
Some(GroupTarget::OutOfGroup),
rand::random(),
);
let attack = Attack::default().with_damage(damage);
let explosion = Explosion {
effects: vec![RadiusEffect::Attack(attack)],
radius: data.body.max_radius() * 10.0,
reagent: Some(Reagent::Yellow),
min_falloff: 0.5,
};
let pos =
data.pos.0 + (*data.ori.look_dir() * (data.body.max_radius() * 3.0));
let explosition =
Vec3::new(pos.x, pos.y, pos.z + (data.body.height() / 2.0));
output_events.emit_server(ExplosionEvent {
pos: explosition,
explosion,
owner: Some(*data.uid),
});
}
} else if self.start_next_strike {
if let CharacterState::ComboMelee2(c) = &mut update.character {
c.completed_strikes += 1;
}
next_strike(data, &mut update);
} else {
if let CharacterState::ComboMelee2(c) = &mut update.character {
c.timer = Duration::default();
c.stage_section = StageSection::Recover;
}
}
},
StageSection::Recover => {
if let Some(movement) = strike_data.movement.recover {
handle_forced_movement(data, &mut update, movement);
}
if self.timer < strike_data.recover_duration {
if let CharacterState::ComboMelee2(c) = &mut update.character {
c.timer = tick_attack_or_default(
data,
self.timer,
Some(data.stats.recovery_speed_modifier),
);
}
} else {
end_melee_ability(data, &mut update);
}
},
_ => {
end_melee_ability(data, &mut update);
},
}
update
}
}
impl Data {
pub fn strike_data(&self) -> &Strike<Duration> {
&self.static_data.strikes[self.completed_strikes % self.static_data.strikes.len()]
}
}
fn next_strike(data: &JoinData, update: &mut StateUpdate) {
let revert_to_wield = if let CharacterState::ComboMelee2(c) = &mut update.character {
if update
.energy
.try_change_by(-c.static_data.energy_cost_per_strike)
.is_ok()
{
c.exhausted = false;
c.start_next_strike = false;
c.timer = Duration::default();
c.stage_section = StageSection::Buildup;
false
} else {
true
}
} else {
false
};
if revert_to_wield {
end_melee_ability(data, update)
}
}
#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub enum FrontendSpecifier {
ClayGolemDash,
IronGolemFist,
}