2020-03-16 15:34:53 +00:00
|
|
|
use crate::{
|
2020-09-10 02:58:28 +00:00
|
|
|
comp::{Attacking, CharacterState, EnergySource, StateUpdate},
|
2020-10-07 02:32:57 +00:00
|
|
|
states::utils::*,
|
2020-09-30 00:47:11 +00:00
|
|
|
sys::character_behavior::{CharacterBehavior, JoinData},
|
2020-03-16 15:34:53 +00:00
|
|
|
};
|
2020-07-06 14:23:08 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2020-03-21 21:16:26 +00:00
|
|
|
use std::time::Duration;
|
2020-09-12 16:46:21 +00:00
|
|
|
use vek::Vec3;
|
2020-03-16 15:34:53 +00:00
|
|
|
|
2020-09-11 13:59:45 +00:00
|
|
|
/// Separated out to condense update portions of character state
|
2020-09-10 02:58:28 +00:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
|
2020-09-11 13:59:45 +00:00
|
|
|
pub struct StaticData {
|
2020-09-10 02:58:28 +00:00
|
|
|
/// How much damage the attack initially does
|
|
|
|
pub base_damage: u32,
|
|
|
|
/// How much damage the attack does at max charge distance
|
|
|
|
pub max_damage: u32,
|
|
|
|
/// How much the attack knocks the target back initially
|
|
|
|
pub base_knockback: f32,
|
|
|
|
/// How much knockback happens at max charge distance
|
|
|
|
pub max_knockback: f32,
|
|
|
|
/// Range of the attack
|
|
|
|
pub range: f32,
|
|
|
|
/// Angle of the attack
|
|
|
|
pub angle: f32,
|
|
|
|
/// Rate of energy drain
|
|
|
|
pub energy_drain: u32,
|
|
|
|
/// How quickly dasher moves forward
|
|
|
|
pub forward_speed: f32,
|
2020-09-11 13:59:45 +00:00
|
|
|
/// Whether state keeps charging after reaching max charge duration
|
|
|
|
pub infinite_charge: bool,
|
2020-03-16 15:34:53 +00:00
|
|
|
/// How long until state should deal damage
|
|
|
|
pub buildup_duration: Duration,
|
2020-09-10 02:58:28 +00:00
|
|
|
/// How long the state charges for until it reaches max damage
|
|
|
|
pub charge_duration: Duration,
|
2020-09-11 19:56:04 +00:00
|
|
|
/// Suration of state spent in swing
|
|
|
|
pub swing_duration: Duration,
|
2020-03-16 15:34:53 +00:00
|
|
|
/// How long the state has until exiting
|
|
|
|
pub recover_duration: Duration,
|
2020-09-12 16:46:21 +00:00
|
|
|
/// Whether the state can be interrupted by other abilities
|
|
|
|
pub is_interruptible: bool,
|
2020-09-11 13:59:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub struct Data {
|
2020-09-12 16:46:21 +00:00
|
|
|
/// Struct containing data that does not change over the course of the
|
|
|
|
/// character state
|
2020-09-11 13:59:45 +00:00
|
|
|
pub static_data: StaticData,
|
|
|
|
/// Whether the charge should end
|
|
|
|
pub end_charge: bool,
|
2020-09-10 02:58:28 +00:00
|
|
|
/// Timer for each stage
|
|
|
|
pub timer: Duration,
|
|
|
|
/// What section the character stage is in
|
|
|
|
pub stage_section: StageSection,
|
2020-09-11 13:59:45 +00:00
|
|
|
/// Whether the state should attempt attacking again
|
|
|
|
pub exhausted: bool,
|
2020-03-16 15:34:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CharacterBehavior for Data {
|
|
|
|
fn behavior(&self, data: &JoinData) -> StateUpdate {
|
2020-03-21 21:16:26 +00:00
|
|
|
let mut update = StateUpdate::from(data);
|
2020-03-16 15:34:53 +00:00
|
|
|
|
2020-09-10 02:58:28 +00:00
|
|
|
handle_orientation(data, &mut update, 1.0);
|
|
|
|
handle_move(data, &mut update, 0.1);
|
2020-03-16 15:34:53 +00:00
|
|
|
|
2020-09-12 16:46:21 +00:00
|
|
|
// Allows for other states to interrupt this state
|
|
|
|
if self.static_data.is_interruptible && !data.inputs.secondary.is_pressed() {
|
2020-09-20 17:23:33 +00:00
|
|
|
handle_interrupt(data, &mut update);
|
2020-09-13 01:33:46 +00:00
|
|
|
match update.character {
|
|
|
|
CharacterState::DashMelee(_) => {},
|
|
|
|
_ => {
|
|
|
|
return update;
|
|
|
|
},
|
2020-09-12 16:46:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-21 22:38:01 +00:00
|
|
|
match self.stage_section {
|
|
|
|
StageSection::Buildup => {
|
|
|
|
if self.timer < self.static_data.buildup_duration {
|
|
|
|
// Build up
|
2020-09-12 16:46:21 +00:00
|
|
|
update.character = CharacterState::DashMelee(Data {
|
|
|
|
static_data: self.static_data,
|
2020-09-21 22:38:01 +00:00
|
|
|
end_charge: self.end_charge,
|
2020-09-12 16:46:21 +00:00
|
|
|
timer: self
|
|
|
|
.timer
|
|
|
|
.checked_add(Duration::from_secs_f32(data.dt.0))
|
|
|
|
.unwrap_or_default(),
|
2020-09-21 22:38:01 +00:00
|
|
|
stage_section: self.stage_section,
|
|
|
|
exhausted: self.exhausted,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Transitions to charge section of stage
|
|
|
|
update.character = CharacterState::DashMelee(Data {
|
|
|
|
static_data: self.static_data,
|
|
|
|
end_charge: self.end_charge,
|
|
|
|
timer: Duration::default(),
|
2020-09-12 16:46:21 +00:00
|
|
|
stage_section: StageSection::Charge,
|
2020-09-21 22:38:01 +00:00
|
|
|
exhausted: self.exhausted,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
StageSection::Charge => {
|
|
|
|
if (self.timer < self.static_data.charge_duration
|
|
|
|
|| (self.static_data.infinite_charge && data.inputs.secondary.is_pressed()))
|
|
|
|
&& update.energy.current() > 0
|
|
|
|
&& !self.end_charge
|
|
|
|
{
|
|
|
|
// Forward movement
|
|
|
|
forward_move(data, &mut update, 0.1, self.static_data.forward_speed);
|
|
|
|
|
|
|
|
// Hit attempt (also checks if player is moving)
|
|
|
|
if !self.exhausted && update.vel.0.distance_squared(Vec3::zero()) > 1.0 {
|
|
|
|
let charge_frac = (self.timer.as_secs_f32()
|
|
|
|
/ self.static_data.charge_duration.as_secs_f32())
|
|
|
|
.min(1.0);
|
|
|
|
let damage = (self.static_data.max_damage as f32
|
|
|
|
- self.static_data.base_damage as f32)
|
|
|
|
* charge_frac
|
|
|
|
+ self.static_data.base_damage as f32;
|
|
|
|
let knockback = (self.static_data.max_knockback
|
|
|
|
- self.static_data.base_knockback)
|
|
|
|
* charge_frac
|
|
|
|
+ self.static_data.base_knockback;
|
|
|
|
data.updater.insert(data.entity, Attacking {
|
2020-09-22 23:37:17 +00:00
|
|
|
base_damage: damage as u32,
|
|
|
|
base_heal: 0,
|
2020-09-21 22:38:01 +00:00
|
|
|
range: self.static_data.range,
|
|
|
|
max_angle: self.static_data.angle.to_radians(),
|
|
|
|
applied: false,
|
|
|
|
hit_count: 0,
|
|
|
|
knockback,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// This logic basically just decides if a charge should end, and prevents the
|
|
|
|
// character state spamming attacks while checking if it has hit something
|
|
|
|
if !self.exhausted {
|
|
|
|
update.character = CharacterState::DashMelee(Data {
|
|
|
|
static_data: self.static_data,
|
|
|
|
end_charge: self.end_charge,
|
|
|
|
timer: self
|
|
|
|
.timer
|
|
|
|
.checked_add(Duration::from_secs_f32(data.dt.0))
|
|
|
|
.unwrap_or_default(),
|
|
|
|
stage_section: StageSection::Charge,
|
|
|
|
exhausted: true,
|
|
|
|
})
|
|
|
|
} else if let Some(attack) = data.attacking {
|
|
|
|
if attack.applied && attack.hit_count > 0 {
|
|
|
|
update.character = CharacterState::DashMelee(Data {
|
|
|
|
static_data: self.static_data,
|
|
|
|
end_charge: !self.static_data.infinite_charge,
|
|
|
|
timer: self
|
|
|
|
.timer
|
|
|
|
.checked_add(Duration::from_secs_f32(data.dt.0))
|
|
|
|
.unwrap_or_default(),
|
|
|
|
stage_section: StageSection::Charge,
|
|
|
|
exhausted: false,
|
|
|
|
})
|
|
|
|
} else if attack.applied {
|
|
|
|
update.character = CharacterState::DashMelee(Data {
|
|
|
|
static_data: self.static_data,
|
|
|
|
end_charge: self.end_charge,
|
|
|
|
timer: self
|
|
|
|
.timer
|
|
|
|
.checked_add(Duration::from_secs_f32(data.dt.0))
|
|
|
|
.unwrap_or_default(),
|
|
|
|
stage_section: StageSection::Charge,
|
|
|
|
exhausted: false,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
update.character = CharacterState::DashMelee(Data {
|
|
|
|
static_data: self.static_data,
|
|
|
|
end_charge: !self.static_data.infinite_charge,
|
|
|
|
timer: self
|
|
|
|
.timer
|
|
|
|
.checked_add(Duration::from_secs_f32(data.dt.0))
|
|
|
|
.unwrap_or_default(),
|
|
|
|
stage_section: StageSection::Charge,
|
|
|
|
exhausted: self.exhausted,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
update.character = CharacterState::DashMelee(Data {
|
|
|
|
static_data: self.static_data,
|
|
|
|
end_charge: !self.static_data.infinite_charge,
|
|
|
|
timer: self
|
|
|
|
.timer
|
|
|
|
.checked_add(Duration::from_secs_f32(data.dt.0))
|
|
|
|
.unwrap_or_default(),
|
|
|
|
stage_section: StageSection::Charge,
|
|
|
|
exhausted: self.exhausted,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Consumes energy if there's enough left and charge has not stopped
|
|
|
|
update.energy.change_by(
|
|
|
|
-(self.static_data.energy_drain as f32 * data.dt.0) as i32,
|
|
|
|
EnergySource::Ability,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// Transitions to swing section of stage
|
|
|
|
update.character = CharacterState::DashMelee(Data {
|
|
|
|
static_data: self.static_data,
|
|
|
|
end_charge: self.end_charge,
|
|
|
|
timer: Duration::default(),
|
|
|
|
stage_section: StageSection::Swing,
|
|
|
|
exhausted: self.exhausted,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
StageSection::Swing => {
|
|
|
|
if self.timer < self.static_data.swing_duration {
|
|
|
|
// Swings
|
2020-09-12 16:46:21 +00:00
|
|
|
update.character = CharacterState::DashMelee(Data {
|
|
|
|
static_data: self.static_data,
|
|
|
|
end_charge: self.end_charge,
|
|
|
|
timer: self
|
|
|
|
.timer
|
|
|
|
.checked_add(Duration::from_secs_f32(data.dt.0))
|
|
|
|
.unwrap_or_default(),
|
2020-09-21 22:38:01 +00:00
|
|
|
stage_section: self.stage_section,
|
|
|
|
exhausted: self.exhausted,
|
|
|
|
});
|
2020-09-11 02:17:33 +00:00
|
|
|
} else {
|
2020-09-21 22:38:01 +00:00
|
|
|
// Transitions to recover section of stage
|
|
|
|
update.character = CharacterState::DashMelee(Data {
|
|
|
|
static_data: self.static_data,
|
|
|
|
end_charge: self.end_charge,
|
|
|
|
timer: Duration::default(),
|
|
|
|
stage_section: StageSection::Recover,
|
|
|
|
exhausted: self.exhausted,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
StageSection::Recover => {
|
|
|
|
if self.timer < self.static_data.recover_duration {
|
|
|
|
// Recover
|
2020-09-11 13:59:45 +00:00
|
|
|
update.character = CharacterState::DashMelee(Data {
|
|
|
|
static_data: self.static_data,
|
2020-09-21 22:38:01 +00:00
|
|
|
end_charge: self.end_charge,
|
2020-09-11 13:59:45 +00:00
|
|
|
timer: self
|
|
|
|
.timer
|
|
|
|
.checked_add(Duration::from_secs_f32(data.dt.0))
|
|
|
|
.unwrap_or_default(),
|
2020-09-21 22:38:01 +00:00
|
|
|
stage_section: self.stage_section,
|
2020-09-11 13:59:45 +00:00
|
|
|
exhausted: self.exhausted,
|
2020-09-21 22:38:01 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Done
|
|
|
|
update.character = CharacterState::Wielding;
|
|
|
|
// Make sure attack component is removed
|
|
|
|
data.updater.remove::<Attacking>(data.entity);
|
2020-09-11 02:17:33 +00:00
|
|
|
}
|
2020-09-21 22:38:01 +00:00
|
|
|
},
|
2020-09-25 20:02:30 +00:00
|
|
|
_ => {
|
|
|
|
// If it somehow ends up in an incorrect stage section
|
|
|
|
update.character = CharacterState::Wielding;
|
|
|
|
// Make sure attack component is removed
|
|
|
|
data.updater.remove::<Attacking>(data.entity);
|
|
|
|
},
|
2020-03-16 15:34:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
update
|
|
|
|
}
|
|
|
|
}
|