veloren/common/src/states/triple_strike.rs

228 lines
7.9 KiB
Rust
Raw Normal View History

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::time::Duration;
use vek::vec::Vec3;
2020-04-01 15:39:18 +00:00
use HoldingState::*;
use TimingState::*;
use TransitionStyle::*;
2020-03-12 14:25:06 +00:00
2020-03-12 15:22:42 +00:00
// In millis
const STAGE_DURATION: u64 = 700;
const TIMING_DELAY: u64 = 350;
2020-03-26 13:46:08 +00:00
const INITIAL_ACCEL: f32 = 90.0;
2020-03-20 22:03:29 +00:00
const BASE_SPEED: f32 = 25.0;
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize, Eq, Hash)]
pub enum Stage {
First,
Second,
Third,
}
2020-04-01 15:39:18 +00:00
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize, Eq, Hash)]
pub enum TimingState {
NotPressed,
PressedEarly,
Success,
}
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize, Eq, Hash)]
pub enum HoldingState {
Holding,
Released,
}
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize, Eq, Hash)]
pub enum TransitionStyle {
/// Player must time a button press properly to transition
Timed(TimingState),
/// Player must hold button for whole move
Hold(HoldingState),
}
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-04-01 15:39:18 +00:00
/// What stage (of 3) the attack is in
pub stage: Stage,
2020-03-14 21:17:27 +00:00
/// 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-21 21:55:04 +00:00
/// Whether state has performed intialization logic
pub initialized: bool,
2020-04-01 15:39:18 +00:00
/// What this instance's current transition stat is
pub transition_style: TransitionStyle,
2020-03-14 21:17:27 +00:00
}
impl CharacterBehavior for Data {
fn behavior(&self, data: &JoinData) -> StateUpdate {
2020-03-21 21:55:04 +00:00
let mut update = StateUpdate::from(data);
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());
if !self.initialized {
2020-03-21 22:06:42 +00:00
update.vel.0 = Vec3::zero();
if let Some(dir) = data.inputs.look_dir.try_normalized() {
update.ori.0 = dir.into();
}
2020-03-21 21:55:04 +00:00
}
let initialized = true;
2020-03-21 21:55:04 +00:00
2020-04-01 15:39:18 +00:00
// Update transition
let transition_style = match self.transition_style {
Timed(state) => match state {
NotPressed => {
if data.inputs.primary.is_just_pressed() {
if stage_time_active > Duration::from_millis(TIMING_DELAY) {
2020-04-01 15:39:18 +00:00
Timed(Success)
} else {
Timed(PressedEarly)
}
} else {
self.transition_style
}
},
_ => self.transition_style,
},
Hold(_) => {
if !data.inputs.primary.is_pressed() {
Hold(Released)
} else {
2020-04-01 15:39:18 +00:00
self.transition_style
}
2020-04-01 15:39:18 +00:00
},
};
2020-03-27 17:40:15 +00:00
2020-03-26 21:44:39 +00:00
// Handle hit applied
if let Some(attack) = data.attacking {
if attack.applied && attack.hit_count > 0 {
// Take energy on successful hit
update.energy.change_by(100, EnergySource::HitEnemy);
2020-03-26 21:44:39 +00:00
// Always remove component
data.updater.remove::<Attacking>(data.entity);
}
}
2020-03-22 19:50:52 +00:00
// Handling movement
if stage_time_active < Duration::from_millis(STAGE_DURATION / 3) {
let adjusted_accel = match (self.stage, data.physics.touch_entity.is_none()) {
(Stage::First, true) => INITIAL_ACCEL,
(Stage::Second, true) => INITIAL_ACCEL * 0.75,
(Stage::Third, true) => INITIAL_ACCEL * 0.75,
(_, _) => 0.0,
};
// Move player forward while in first third of each stage
if update.vel.0.magnitude_squared() < BASE_SPEED.powf(2.0) {
update.vel.0 += data.dt.0
* (if data.physics.on_ground {
Vec3::new(0.0, 0.0, 500.0) // Jump upwards if on ground
} else {
Vec3::one()
} + adjusted_accel * Vec3::from(data.ori.0.xy()));
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-22 19:50:52 +00:00
} else {
2020-03-28 20:50:42 +00:00
handle_orientation(data, &mut update, 50.0);
2020-03-22 19:50:52 +00:00
}
2020-03-19 22:40:03 +00:00
2020-03-22 19:50:52 +00:00
// Handling attacking
update.character = if stage_time_active > Duration::from_millis(STAGE_DURATION / 2)
&& !self.stage_exhausted
{
2020-03-22 19:50:52 +00:00
let dmg = match self.stage {
Stage::First => self.base_damage / 2,
Stage::Second => self.base_damage,
Stage::Third => (self.base_damage as f32 * 1.5) as u32,
2020-03-22 19:50:52 +00:00
};
// Try to deal damage in second half of stage
data.updater.insert(data.entity, Attacking {
2020-03-24 21:03:11 +00:00
base_healthchange: -(dmg as i32),
range: 3.5,
2020-03-22 19:50:52 +00:00
max_angle: 180_f32.to_radians(),
applied: false,
hit_count: 0,
2020-03-27 15:26:53 +00:00
knockback: 16.0,
2020-03-22 19:50:52 +00:00
});
2020-03-19 22:40:03 +00:00
CharacterState::TripleStrike(Data {
2020-03-22 19:50:52 +00:00
base_damage: self.base_damage,
stage: self.stage,
stage_time_active,
stage_exhausted: true,
initialized,
2020-04-01 15:39:18 +00:00
transition_style,
})
2020-03-22 19:50:52 +00:00
} else if stage_time_active > Duration::from_millis(STAGE_DURATION) {
2020-04-01 15:52:44 +00:00
let next_stage =
2020-04-01 15:39:18 +00:00
// Determine whether stage can transition based on TransitionStyle
if let Hold(Holding) | Timed(Success) = transition_style {
// Determine what stage to transition to
match self.stage {
Stage::First => Some(Stage::Second),
Stage::Second => Some(Stage::Third),
Stage::Third => None,
}
}
// Player messed up inputs, don't transition
else { None };
if let Some(stage) = next_stage {
CharacterState::TripleStrike(Data {
base_damage: self.base_damage,
stage,
stage_time_active: Duration::default(),
stage_exhausted: false,
initialized,
2020-04-01 15:39:18 +00:00
transition_style: match transition_style {
Hold(_) => Hold(Holding),
Timed(_) => Timed(NotPressed),
2020-04-01 15:52:44 +00:00
},
})
2020-03-19 22:40:03 +00:00
} else {
2020-03-22 19:50:52 +00:00
// Make sure attack component is removed
data.updater.remove::<Attacking>(data.entity);
// Done
CharacterState::Wielding
2020-03-19 22:40:03 +00:00
}
} else {
CharacterState::TripleStrike(Data {
2020-03-22 19:50:52 +00:00
base_damage: self.base_damage,
stage: self.stage,
stage_time_active,
stage_exhausted: self.stage_exhausted,
initialized,
2020-04-01 15:39:18 +00:00
transition_style,
})
};
2020-03-19 22:40:03 +00:00
// 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
}