2020-03-12 14:25:06 +00:00
|
|
|
use crate::{
|
2020-03-19 22:40:03 +00:00
|
|
|
comp::{Attacking, CharacterState, EnergySource, StateUpdate},
|
2020-03-12 14:25:06 +00:00
|
|
|
states::utils::*,
|
2020-03-14 21:17:27 +00:00
|
|
|
sys::character_behavior::{CharacterBehavior, JoinData},
|
2020-03-12 14:25:06 +00:00
|
|
|
};
|
|
|
|
use std::{collections::VecDeque, time::Duration};
|
2020-03-19 22:40:03 +00:00
|
|
|
use vek::vec::Vec2;
|
2020-03-12 14:25:06 +00:00
|
|
|
|
2020-03-12 15:22:42 +00:00
|
|
|
// In millis
|
|
|
|
const STAGE_DURATION: u64 = 600;
|
|
|
|
|
2020-03-20 22:20:14 +00:00
|
|
|
const INITIAL_ACCEL: f32 = 200.0;
|
|
|
|
const SECONDARY_ACCEL: f32 = 100.0;
|
2020-03-20 22:03:29 +00:00
|
|
|
const BASE_SPEED: f32 = 25.0;
|
2020-03-12 15:22:42 +00:00
|
|
|
/// ### A sequence of 3 incrementally increasing attacks.
|
2020-03-12 14:25:06 +00:00
|
|
|
///
|
2020-03-12 15:22:42 +00:00
|
|
|
/// While holding down the `primary` button, perform a series of 3 attacks,
|
|
|
|
/// each one pushes the player forward as the character steps into the swings.
|
2020-03-12 14:25:06 +00:00
|
|
|
/// The player can let go of the left mouse button at any time
|
|
|
|
/// and stop their attacks by interrupting the attack animation.
|
2020-03-14 21:17:27 +00:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize, Eq, Hash)]
|
|
|
|
pub struct Data {
|
|
|
|
/// The tool this state will read to handle damage, etc.
|
2020-03-19 22:40:03 +00:00
|
|
|
pub base_damage: u32,
|
2020-03-14 21:17:27 +00:00
|
|
|
/// `int` denoting what stage (of 3) the attack is in.
|
|
|
|
pub stage: i8,
|
|
|
|
/// How long current stage has been active
|
|
|
|
pub stage_time_active: Duration,
|
|
|
|
/// Whether current stage has exhausted its attack
|
2020-03-19 22:40:03 +00:00
|
|
|
pub stage_exhausted: bool,
|
2020-03-20 22:03:29 +00:00
|
|
|
/// Whether to go to next stage
|
|
|
|
pub should_transition: bool,
|
2020-03-14 21:17:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CharacterBehavior for Data {
|
|
|
|
fn behavior(&self, data: &JoinData) -> StateUpdate {
|
|
|
|
let mut update = StateUpdate {
|
|
|
|
pos: *data.pos,
|
|
|
|
vel: *data.vel,
|
|
|
|
ori: *data.ori,
|
|
|
|
energy: *data.energy,
|
2020-03-16 11:32:57 +00:00
|
|
|
character: data.character.clone(),
|
2020-03-14 21:17:27 +00:00
|
|
|
local_events: VecDeque::new(),
|
|
|
|
server_events: VecDeque::new(),
|
|
|
|
};
|
2020-03-12 14:25:06 +00:00
|
|
|
|
2020-03-20 22:03:29 +00:00
|
|
|
let stage_time_active = self
|
2020-03-14 21:17:27 +00:00
|
|
|
.stage_time_active
|
2020-03-12 14:25:06 +00:00
|
|
|
.checked_add(Duration::from_secs_f32(data.dt.0))
|
|
|
|
.unwrap_or(Duration::default());
|
|
|
|
|
2020-03-20 22:03:29 +00:00
|
|
|
let mut should_transition = self.should_transition;
|
|
|
|
|
2020-03-12 15:22:42 +00:00
|
|
|
// If player stops holding input,
|
2020-03-12 14:25:06 +00:00
|
|
|
if !data.inputs.primary.is_pressed() {
|
2020-03-20 22:03:29 +00:00
|
|
|
// // Done
|
|
|
|
// update.character = CharacterState::Wielding;
|
|
|
|
// // Make sure attack component is removed
|
|
|
|
// data.updater.remove::<Attacking>(data.entity);
|
|
|
|
// return update;
|
2020-03-12 14:25:06 +00:00
|
|
|
|
2020-03-20 22:03:29 +00:00
|
|
|
should_transition = false;
|
|
|
|
}
|
2020-03-20 14:05:40 +00:00
|
|
|
|
2020-03-14 21:17:27 +00:00
|
|
|
if self.stage < 3 {
|
2020-03-20 22:03:29 +00:00
|
|
|
// Handling movement
|
|
|
|
if stage_time_active < Duration::from_millis(STAGE_DURATION / 3) {
|
2020-03-20 22:20:14 +00:00
|
|
|
let adjusted_accel = if self.stage == 0 {
|
|
|
|
INITIAL_ACCEL
|
|
|
|
} else {
|
|
|
|
SECONDARY_ACCEL
|
|
|
|
};
|
|
|
|
|
2020-03-12 15:22:42 +00:00
|
|
|
// Move player forward while in first third of each stage
|
2020-03-19 22:40:03 +00:00
|
|
|
if update.vel.0.magnitude_squared() < BASE_SPEED.powf(2.0) {
|
|
|
|
update.vel.0 =
|
2020-03-20 22:20:14 +00:00
|
|
|
update.vel.0 + Vec2::broadcast(data.dt.0) * data.ori.0 * adjusted_accel;
|
2020-03-19 22:40:03 +00:00
|
|
|
let mag2 = update.vel.0.magnitude_squared();
|
|
|
|
if mag2 > BASE_SPEED.powf(2.0) {
|
|
|
|
update.vel.0 = update.vel.0.normalized() * BASE_SPEED;
|
|
|
|
}
|
|
|
|
};
|
2020-03-20 22:03:29 +00:00
|
|
|
} else {
|
2020-03-21 14:06:35 +00:00
|
|
|
handle_orientation(data, &mut update, 10.0);
|
2020-03-20 22:03:29 +00:00
|
|
|
}
|
2020-03-19 22:40:03 +00:00
|
|
|
|
2020-03-20 22:03:29 +00:00
|
|
|
// Handling attacking
|
|
|
|
if stage_time_active > Duration::from_millis(STAGE_DURATION / 2)
|
2020-03-19 22:40:03 +00:00
|
|
|
&& !self.stage_exhausted
|
2020-03-12 15:22:42 +00:00
|
|
|
{
|
|
|
|
// Try to deal damage in second half of stage
|
2020-03-19 22:40:03 +00:00
|
|
|
data.updater.insert(data.entity, Attacking {
|
|
|
|
base_damage: self.base_damage * (self.stage as u32 + 1),
|
|
|
|
max_angle: 180_f32.to_radians(),
|
|
|
|
applied: false,
|
|
|
|
hit_count: 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
update.character = CharacterState::TripleStrike(Data {
|
|
|
|
base_damage: self.base_damage,
|
|
|
|
stage: self.stage,
|
2020-03-20 22:03:29 +00:00
|
|
|
stage_time_active,
|
2020-03-19 22:40:03 +00:00
|
|
|
stage_exhausted: true,
|
2020-03-20 22:03:29 +00:00
|
|
|
should_transition,
|
2020-03-19 22:40:03 +00:00
|
|
|
});
|
2020-03-20 22:03:29 +00:00
|
|
|
} else if stage_time_active > Duration::from_millis(STAGE_DURATION) {
|
|
|
|
if should_transition {
|
|
|
|
update.character = CharacterState::TripleStrike(Data {
|
|
|
|
base_damage: self.base_damage,
|
|
|
|
stage: self.stage + 1,
|
|
|
|
stage_time_active: Duration::default(),
|
|
|
|
stage_exhausted: false,
|
|
|
|
should_transition,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Done
|
|
|
|
update.character = CharacterState::Wielding;
|
|
|
|
// Make sure attack component is removed
|
|
|
|
data.updater.remove::<Attacking>(data.entity);
|
|
|
|
}
|
2020-03-19 22:40:03 +00:00
|
|
|
} else {
|
|
|
|
update.character = CharacterState::TripleStrike(Data {
|
|
|
|
base_damage: self.base_damage,
|
|
|
|
stage: self.stage,
|
2020-03-20 22:03:29 +00:00
|
|
|
stage_time_active,
|
2020-03-19 22:40:03 +00:00
|
|
|
stage_exhausted: self.stage_exhausted,
|
2020-03-20 22:03:29 +00:00
|
|
|
should_transition,
|
2020-03-19 22:40:03 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Done
|
|
|
|
update.character = CharacterState::Wielding;
|
|
|
|
// Make sure attack component is removed
|
|
|
|
data.updater.remove::<Attacking>(data.entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Grant energy on successful hit
|
|
|
|
if let Some(attack) = data.attacking {
|
|
|
|
if attack.applied && attack.hit_count > 0 {
|
|
|
|
data.updater.remove::<Attacking>(data.entity);
|
|
|
|
update.energy.change_by(100, EnergySource::HitEnemy);
|
2020-03-12 15:22:42 +00:00
|
|
|
}
|
2020-03-12 14:25:06 +00:00
|
|
|
}
|
|
|
|
|
2020-03-14 21:17:27 +00:00
|
|
|
update
|
|
|
|
}
|
2020-03-12 14:25:06 +00:00
|
|
|
}
|