veloren/common/src/states/dash_melee.rs

239 lines
10 KiB
Rust
Raw Normal View History

2020-03-16 15:34:53 +00:00
use crate::{
2020-10-30 21:49:58 +00:00
comp::{Attacking, CharacterState, EnergyChange, EnergySource, StateUpdate},
states::utils::*,
2020-09-30 00:47:11 +00:00
sys::character_behavior::{CharacterBehavior, JoinData},
Damage, DamageSource, GroupTarget, Knockback,
2020-03-16 15:34:53 +00:00
};
use serde::{Deserialize, Serialize};
use std::time::Duration;
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
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
2020-09-11 13:59:45 +00:00
pub struct StaticData {
/// 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,
/// How long the state charges for until it reaches max damage
pub charge_duration: Duration,
/// 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,
/// Whether the state can be interrupted by other abilities
pub is_interruptible: bool,
2020-10-31 18:44:00 +00:00
/// What key is used to press ability
pub ability_key: AbilityKey,
2020-09-11 13:59:45 +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
2020-09-11 13:59:45 +00:00
pub static_data: StaticData,
/// Whether the charge should end
2020-10-18 16:46:28 +00:00
pub auto_charge: bool,
/// Timer for each stage
pub timer: Duration,
2020-10-18 16:46:28 +00:00
/// Timer used to limit how often another attack will be applied
pub refresh_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 {
let mut update = StateUpdate::from(data);
2020-03-16 15:34:53 +00:00
handle_orientation(data, &mut update, 1.0);
handle_move(data, &mut update, 0.1);
2020-11-20 17:30:48 +00:00
if !ability_key_is_pressed(data, self.static_data.ability_key) {
handle_interrupt(data, &mut update, self.static_data.is_interruptible);
2020-09-13 01:33:46 +00:00
match update.character {
CharacterState::DashMelee(_) => {},
_ => {
return update;
},
}
}
2020-09-21 22:38:01 +00:00
match self.stage_section {
StageSection::Buildup => {
if self.timer < self.static_data.buildup_duration {
// Build up
update.character = CharacterState::DashMelee(Data {
timer: self
.timer
.checked_add(Duration::from_secs_f32(data.dt.0))
.unwrap_or_default(),
2020-10-27 22:16:17 +00:00
..*self
2020-09-21 22:38:01 +00:00
});
} else {
// Transitions to charge section of stage
update.character = CharacterState::DashMelee(Data {
2020-10-31 18:44:00 +00:00
auto_charge: !ability_key_is_pressed(data, self.static_data.ability_key),
2020-09-21 22:38:01 +00:00
timer: Duration::default(),
stage_section: StageSection::Charge,
2020-10-27 22:16:17 +00:00
..*self
2020-09-21 22:38:01 +00:00
});
}
},
StageSection::Charge => {
2020-10-18 16:46:28 +00:00
if (self.static_data.infinite_charge
|| self.timer < self.static_data.charge_duration)
2020-10-31 18:44:00 +00:00
&& (ability_key_is_pressed(data, self.static_data.ability_key)
2020-10-18 16:46:28 +00:00
|| (self.auto_charge && self.timer < self.static_data.charge_duration))
2020-09-21 22:38:01 +00:00
&& update.energy.current() > 0
{
// Forward movement
handle_forced_movement(
data,
&mut update,
ForcedMovement::Forward {
strength: self.static_data.forward_speed,
},
0.1,
);
2020-09-21 22:38:01 +00:00
// 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 {
2020-10-18 16:46:28 +00:00
// Hit attempt (also checks if player is moving)
if 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 mut damage = Damage {
source: DamageSource::Melee,
value: self.static_data.max_damage as f32,
};
damage.interpolate_damage(
charge_frac,
self.static_data.base_damage as f32,
);
2020-10-18 16:46:28 +00:00
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 {
damages: vec![(Some(GroupTarget::OutOfGroup), damage)],
2020-10-18 16:46:28 +00:00
range: self.static_data.range,
max_angle: self.static_data.angle.to_radians(),
applied: false,
hit_count: 0,
2020-10-18 18:21:58 +00:00
knockback: Knockback::Away(knockback),
2020-10-18 16:46:28 +00:00
});
}
2020-09-21 22:38:01 +00:00
update.character = CharacterState::DashMelee(Data {
timer: self
.timer
.checked_add(Duration::from_secs_f32(data.dt.0))
.unwrap_or_default(),
exhausted: true,
2020-10-27 22:16:17 +00:00
..*self
2020-09-21 22:38:01 +00:00
})
2020-10-18 16:46:28 +00:00
} else if self.refresh_timer < Duration::from_millis(50) {
2020-09-21 22:38:01 +00:00
update.character = CharacterState::DashMelee(Data {
timer: self
.timer
.checked_add(Duration::from_secs_f32(data.dt.0))
.unwrap_or_default(),
2020-10-18 16:46:28 +00:00
refresh_timer: self
.refresh_timer
.checked_add(Duration::from_secs_f32(data.dt.0))
.unwrap_or_default(),
2020-10-27 22:16:17 +00:00
..*self
2020-09-21 22:38:01 +00:00
})
2020-10-18 16:46:28 +00:00
} else {
update.character = CharacterState::DashMelee(Data {
timer: self
.timer
.checked_add(Duration::from_secs_f32(data.dt.0))
.unwrap_or_default(),
refresh_timer: Duration::default(),
exhausted: false,
2020-10-27 22:16:17 +00:00
..*self
2020-10-18 16:46:28 +00:00
})
2020-09-21 22:38:01 +00:00
}
// Consumes energy if there's enough left and charge has not stopped
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-21 22:38:01 +00:00
} else {
// Transitions to swing section of stage
update.character = CharacterState::DashMelee(Data {
timer: Duration::default(),
stage_section: StageSection::Swing,
2020-10-27 22:16:17 +00:00
..*self
2020-09-21 22:38:01 +00:00
});
}
},
StageSection::Swing => {
if self.timer < self.static_data.swing_duration {
// Swings
update.character = CharacterState::DashMelee(Data {
timer: self
.timer
.checked_add(Duration::from_secs_f32(data.dt.0))
.unwrap_or_default(),
2020-10-27 22:16:17 +00:00
..*self
2020-09-21 22:38:01 +00:00
});
} else {
2020-09-21 22:38:01 +00:00
// Transitions to recover section of stage
update.character = CharacterState::DashMelee(Data {
timer: Duration::default(),
stage_section: StageSection::Recover,
2020-10-27 22:16:17 +00:00
..*self
2020-09-21 22:38:01 +00:00
});
}
},
StageSection::Recover => {
if self.timer < self.static_data.recover_duration {
// Recover
2020-09-11 13:59:45 +00:00
update.character = CharacterState::DashMelee(Data {
timer: self
.timer
.checked_add(Duration::from_secs_f32(data.dt.0))
.unwrap_or_default(),
2020-10-27 22:16:17 +00:00
..*self
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-21 22:38:01 +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
}
}