veloren/common/src/comp/ability.rs

681 lines
24 KiB
Rust
Raw Normal View History

use crate::{
2020-03-24 19:31:54 +00:00
comp::{
item::{armor::Protection, Item, ItemKind},
Body, CharacterState, EnergySource, Gravity, LightEmitter, Projectile, StateUpdate,
2020-03-24 19:31:54 +00:00
},
states::{utils::StageSection, *},
2020-03-17 14:01:41 +00:00
sys::character_behavior::JoinData,
};
use arraygen::Arraygen;
use serde::{Deserialize, Serialize};
2020-03-22 04:49:32 +00:00
use specs::{Component, FlaggedStorage};
use specs_idvs::IdvStorage;
2020-03-14 15:40:29 +00:00
use std::time::Duration;
use vek::Vec3;
2020-01-01 17:16:29 +00:00
#[derive(Copy, Clone, Hash, Eq, PartialEq, Debug, Serialize, Deserialize)]
pub enum CharacterAbilityType {
BasicMelee,
BasicRanged,
Boost,
2020-10-14 22:30:58 +00:00
ChargedMelee(StageSection),
2020-07-26 03:06:53 +00:00
ChargedRanged,
2020-10-14 22:30:58 +00:00
DashMelee(StageSection),
BasicBlock,
2020-09-13 01:33:46 +00:00
ComboMelee(StageSection, u32),
2020-10-14 22:30:58 +00:00
LeapMelee(StageSection),
SpinMelee(StageSection),
Shockwave,
BasicBeam,
RepeaterRanged,
}
impl From<&CharacterState> for CharacterAbilityType {
fn from(state: &CharacterState) -> Self {
match state {
CharacterState::BasicMelee(_) => Self::BasicMelee,
CharacterState::BasicRanged(_) => Self::BasicRanged,
CharacterState::Boost(_) => Self::Boost,
2020-10-14 22:30:58 +00:00
CharacterState::DashMelee(data) => Self::DashMelee(data.stage_section),
CharacterState::BasicBlock => Self::BasicBlock,
2020-10-14 22:30:58 +00:00
CharacterState::LeapMelee(data) => Self::LeapMelee(data.stage_section),
2020-09-13 01:33:46 +00:00
CharacterState::ComboMelee(data) => Self::ComboMelee(data.stage_section, data.stage),
2020-10-14 22:30:58 +00:00
CharacterState::SpinMelee(data) => Self::SpinMelee(data.stage_section),
CharacterState::ChargedMelee(data) => Self::ChargedMelee(data.stage_section),
2020-07-26 03:06:53 +00:00
CharacterState::ChargedRanged(_) => Self::ChargedRanged,
CharacterState::Shockwave(_) => Self::ChargedRanged,
CharacterState::BasicBeam(_) => Self::BasicBeam,
CharacterState::RepeaterRanged(_) => Self::RepeaterRanged,
_ => Self::BasicMelee,
}
}
}
#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
2020-03-14 15:40:29 +00:00
pub enum CharacterAbility {
BasicMelee {
2020-03-24 21:03:11 +00:00
energy_cost: u32,
2020-03-14 15:40:29 +00:00
buildup_duration: Duration,
recover_duration: Duration,
2020-03-24 21:03:11 +00:00
base_healthchange: i32,
knockback: f32,
range: f32,
max_angle: f32,
2020-03-12 14:36:02 +00:00
},
BasicRanged {
2020-03-24 19:09:23 +00:00
energy_cost: u32,
holdable: bool,
prepare_duration: Duration,
2020-03-22 19:39:50 +00:00
recover_duration: Duration,
projectile: Projectile,
projectile_body: Body,
2020-03-24 19:31:54 +00:00
projectile_light: Option<LightEmitter>,
projectile_gravity: Option<Gravity>,
projectile_speed: f32,
2020-03-22 19:39:50 +00:00
},
RepeaterRanged {
energy_cost: u32,
2020-09-28 02:38:23 +00:00
movement_duration: Duration,
buildup_duration: Duration,
shoot_duration: Duration,
recover_duration: Duration,
2020-09-28 02:38:23 +00:00
leap: Option<f32>,
projectile: Projectile,
projectile_body: Body,
projectile_light: Option<LightEmitter>,
projectile_gravity: Option<Gravity>,
projectile_speed: f32,
reps_remaining: u32,
},
Boost {
duration: Duration,
only_up: bool,
},
2020-03-16 15:34:53 +00:00
DashMelee {
energy_cost: u32,
base_damage: u32,
max_damage: u32,
base_knockback: f32,
max_knockback: f32,
range: f32,
angle: f32,
energy_drain: u32,
forward_speed: f32,
2020-03-16 15:34:53 +00:00
buildup_duration: Duration,
charge_duration: Duration,
swing_duration: Duration,
2020-03-16 15:34:53 +00:00
recover_duration: Duration,
infinite_charge: bool,
is_interruptible: bool,
2020-03-16 15:34:53 +00:00
},
2020-03-07 18:15:02 +00:00
BasicBlock,
Roll,
ComboMelee {
stage_data: Vec<combo_melee::Stage>,
initial_energy_gain: u32,
max_energy_gain: u32,
energy_increase: u32,
speed_increase: f32,
max_speed_increase: f32,
is_interruptible: bool,
2020-03-19 22:40:03 +00:00
},
LeapMelee {
energy_cost: u32,
buildup_duration: Duration,
2020-09-28 23:55:38 +00:00
movement_duration: Duration,
swing_duration: Duration,
recover_duration: Duration,
base_damage: u32,
range: f32,
max_angle: f32,
2020-09-25 05:51:26 +00:00
knockback: f32,
2020-09-28 23:55:38 +00:00
forward_leap_strength: f32,
vertical_leap_strength: f32,
},
2020-07-08 19:58:41 +00:00
SpinMelee {
buildup_duration: Duration,
swing_duration: Duration,
2020-07-08 19:58:41 +00:00
recover_duration: Duration,
base_damage: u32,
knockback: f32,
range: f32,
energy_cost: u32,
is_infinite: bool,
is_helicopter: bool,
is_interruptible: bool,
forward_speed: f32,
num_spins: u32,
2020-07-08 19:58:41 +00:00
},
ChargedMelee {
energy_cost: u32,
energy_drain: u32,
initial_damage: u32,
max_damage: u32,
initial_knockback: f32,
max_knockback: f32,
range: f32,
max_angle: f32,
2020-09-28 01:58:49 +00:00
charge_duration: Duration,
swing_duration: Duration,
recover_duration: Duration,
},
2020-07-26 03:06:53 +00:00
ChargedRanged {
energy_cost: u32,
energy_drain: u32,
initial_damage: u32,
max_damage: u32,
initial_knockback: f32,
max_knockback: f32,
prepare_duration: Duration,
charge_duration: Duration,
recover_duration: Duration,
projectile_body: Body,
projectile_light: Option<LightEmitter>,
projectile_gravity: Option<Gravity>,
initial_projectile_speed: f32,
max_projectile_speed: f32,
2020-07-26 03:06:53 +00:00
},
Shockwave {
2020-08-08 20:53:55 +00:00
energy_cost: u32,
buildup_duration: Duration,
recover_duration: Duration,
damage: u32,
knockback: f32,
shockwave_angle: f32,
shockwave_speed: f32,
shockwave_duration: Duration,
2020-09-19 16:55:31 +00:00
requires_ground: bool,
2020-08-08 20:53:55 +00:00
},
BasicBeam {
energy_cost: u32,
buildup_duration: Duration,
recover_duration: Duration,
beam_duration: Duration,
base_hps: u32,
base_dps: u32,
2020-08-31 21:55:38 +00:00
tick_rate: f32,
range: f32,
max_angle: f32,
lifesteal_eff: f32,
energy_regen: u32,
energy_drain: u32,
},
2020-01-01 17:16:29 +00:00
}
2020-03-17 14:01:41 +00:00
impl CharacterAbility {
2020-03-21 22:55:20 +00:00
/// Attempts to fulfill requirements, mutating `update` (taking energy) if
/// applicable.
pub fn requirements_paid(&self, data: &JoinData, update: &mut StateUpdate) -> bool {
2020-03-17 14:01:41 +00:00
match self {
CharacterAbility::Roll => {
data.physics.on_ground
&& data.body.is_humanoid()
&& data.vel.0.xy().magnitude_squared() > 0.5
2020-03-17 14:01:41 +00:00
&& update
.energy
.try_change_by(-220, EnergySource::Ability)
2020-03-22 19:39:50 +00:00
.is_ok()
},
CharacterAbility::DashMelee { energy_cost, .. } => update
2020-03-26 13:46:08 +00:00
.energy
.try_change_by(-(*energy_cost as i32), EnergySource::Ability)
2020-03-26 13:46:08 +00:00
.is_ok(),
CharacterAbility::BasicMelee { energy_cost, .. } => update
.energy
.try_change_by(-(*energy_cost as i32), EnergySource::Ability)
.is_ok(),
CharacterAbility::BasicRanged { energy_cost, .. } => update
.energy
.try_change_by(-(*energy_cost as i32), EnergySource::Ability)
.is_ok(),
CharacterAbility::LeapMelee { energy_cost, .. } => update
.energy
.try_change_by(-(*energy_cost as i32), EnergySource::Ability)
.is_ok(),
2020-07-08 19:58:41 +00:00
CharacterAbility::SpinMelee { energy_cost, .. } => update
.energy
.try_change_by(-(*energy_cost as i32), EnergySource::Ability)
.is_ok(),
2020-07-26 03:06:53 +00:00
CharacterAbility::ChargedRanged { energy_cost, .. } => update
.energy
.try_change_by(-(*energy_cost as i32), EnergySource::Ability)
.is_ok(),
CharacterAbility::ChargedMelee { energy_cost, .. } => update
.energy
.try_change_by(-(*energy_cost as i32), EnergySource::Ability)
.is_ok(),
CharacterAbility::RepeaterRanged { energy_cost, .. } => update
.energy
.try_change_by(-(*energy_cost as i32), EnergySource::Ability)
.is_ok(),
CharacterAbility::Shockwave { energy_cost, .. } => update
2020-08-08 20:53:55 +00:00
.energy
.try_change_by(-(*energy_cost as i32), EnergySource::Ability)
.is_ok(),
CharacterAbility::BasicBeam { energy_cost, .. } => update
.energy
.try_change_by(-(*energy_cost as i32), EnergySource::Ability)
.is_ok(),
2020-03-17 14:01:41 +00:00
_ => true,
}
}
}
#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
pub struct ItemConfig {
pub item: Item,
pub ability1: Option<CharacterAbility>,
pub ability2: Option<CharacterAbility>,
pub ability3: Option<CharacterAbility>,
pub block_ability: Option<CharacterAbility>,
pub dodge_ability: Option<CharacterAbility>,
}
impl From<Item> for ItemConfig {
fn from(item: Item) -> Self {
if let ItemKind::Tool(tool) = &item.kind() {
let mut abilities = tool.get_abilities();
let mut ability_drain = abilities.drain(..);
return ItemConfig {
item,
ability1: ability_drain.next(),
ability2: ability_drain.next(),
ability3: ability_drain.next(),
block_ability: Some(CharacterAbility::BasicBlock),
dodge_ability: Some(CharacterAbility::Roll),
};
}
unimplemented!("ItemConfig is currently only supported for Tools")
}
}
#[derive(Arraygen, Clone, PartialEq, Default, Debug, Serialize, Deserialize)]
#[gen_array(pub fn get_armor: &Option<Item>)]
pub struct Loadout {
pub active_item: Option<ItemConfig>,
pub second_item: Option<ItemConfig>,
pub lantern: Option<Item>,
2020-08-24 01:31:05 +00:00
pub glider: Option<Item>,
#[in_array(get_armor)]
pub shoulder: Option<Item>,
#[in_array(get_armor)]
pub chest: Option<Item>,
#[in_array(get_armor)]
pub belt: Option<Item>,
#[in_array(get_armor)]
pub hand: Option<Item>,
#[in_array(get_armor)]
pub pants: Option<Item>,
#[in_array(get_armor)]
pub foot: Option<Item>,
#[in_array(get_armor)]
2020-04-06 16:11:05 +00:00
pub back: Option<Item>,
#[in_array(get_armor)]
2020-04-06 20:03:46 +00:00
pub ring: Option<Item>,
#[in_array(get_armor)]
2020-04-06 20:03:46 +00:00
pub neck: Option<Item>,
#[in_array(get_armor)]
2020-04-06 20:03:46 +00:00
pub head: Option<Item>,
#[in_array(get_armor)]
2020-04-06 20:03:46 +00:00
pub tabard: Option<Item>,
2020-01-01 17:16:29 +00:00
}
impl Loadout {
pub fn get_damage_reduction(&self) -> f32 {
let protection = self
.get_armor()
.iter()
.flat_map(|armor| armor.as_ref())
.filter_map(|item| {
if let ItemKind::Armor(armor) = &item.kind() {
Some(armor.get_protection())
} else {
None
}
})
.map(|protection| match protection {
Protection::Normal(protection) => Some(protection),
Protection::Invincible => None,
})
.sum::<Option<f32>>();
match protection {
Some(dr) => dr / (60.0 + dr.abs()),
None => 1.0,
}
}
}
impl From<&CharacterAbility> for CharacterState {
fn from(ability: &CharacterAbility) -> Self {
2020-03-14 15:40:29 +00:00
match ability {
CharacterAbility::BasicMelee {
2020-03-14 15:40:29 +00:00
buildup_duration,
recover_duration,
2020-03-24 21:03:11 +00:00
base_healthchange,
knockback,
range,
max_angle,
2020-03-24 21:03:11 +00:00
energy_cost: _,
} => CharacterState::BasicMelee(basic_melee::Data {
2020-03-14 15:40:29 +00:00
exhausted: false,
buildup_duration: *buildup_duration,
recover_duration: *recover_duration,
2020-03-24 21:03:11 +00:00
base_healthchange: *base_healthchange,
knockback: *knockback,
range: *range,
max_angle: *max_angle,
}),
CharacterAbility::BasicRanged {
holdable,
prepare_duration,
2020-03-14 15:40:29 +00:00
recover_duration,
projectile,
projectile_body,
2020-03-24 19:31:54 +00:00
projectile_light,
projectile_gravity,
projectile_speed,
2020-03-24 19:09:23 +00:00
energy_cost: _,
} => CharacterState::BasicRanged(basic_ranged::Data {
exhausted: false,
prepare_timer: Duration::default(),
holdable: *holdable,
prepare_duration: *prepare_duration,
recover_duration: *recover_duration,
projectile: projectile.clone(),
projectile_body: *projectile_body,
2020-03-24 19:31:54 +00:00
projectile_light: *projectile_light,
projectile_gravity: *projectile_gravity,
projectile_speed: *projectile_speed,
}),
CharacterAbility::Boost { duration, only_up } => CharacterState::Boost(boost::Data {
duration: *duration,
only_up: *only_up,
}),
2020-03-16 15:34:53 +00:00
CharacterAbility::DashMelee {
energy_cost: _,
base_damage,
max_damage,
base_knockback,
max_knockback,
range,
angle,
energy_drain,
forward_speed,
2020-03-16 15:34:53 +00:00
buildup_duration,
charge_duration,
swing_duration,
2020-03-16 15:34:53 +00:00
recover_duration,
infinite_charge,
is_interruptible,
2020-03-16 15:34:53 +00:00
} => CharacterState::DashMelee(dash_melee::Data {
2020-09-11 13:59:45 +00:00
static_data: dash_melee::StaticData {
base_damage: *base_damage,
max_damage: *max_damage,
base_knockback: *base_knockback,
max_knockback: *max_knockback,
range: *range,
angle: *angle,
energy_drain: *energy_drain,
forward_speed: *forward_speed,
infinite_charge: *infinite_charge,
buildup_duration: *buildup_duration,
charge_duration: *charge_duration,
swing_duration: *swing_duration,
2020-09-11 13:59:45 +00:00
recover_duration: *recover_duration,
is_interruptible: *is_interruptible,
2020-09-11 13:59:45 +00:00
},
end_charge: false,
timer: Duration::default(),
stage_section: StageSection::Buildup,
2020-09-11 13:59:45 +00:00
exhausted: false,
2020-03-16 15:34:53 +00:00
}),
CharacterAbility::BasicBlock => CharacterState::BasicBlock,
CharacterAbility::Roll => CharacterState::Roll(roll::Data {
2020-07-21 01:37:13 +00:00
remaining_duration: Duration::from_millis(500),
2020-03-26 13:46:08 +00:00
was_wielded: false, // false by default. utils might set it to true
2020-03-14 21:17:27 +00:00
}),
CharacterAbility::ComboMelee {
stage_data,
initial_energy_gain,
max_energy_gain,
energy_increase,
speed_increase,
max_speed_increase,
is_interruptible,
} => CharacterState::ComboMelee(combo_melee::Data {
2020-09-21 22:38:01 +00:00
static_data: combo_melee::StaticData {
num_stages: stage_data.len() as u32,
stage_data: stage_data.clone(),
initial_energy_gain: *initial_energy_gain,
max_energy_gain: *max_energy_gain,
energy_increase: *energy_increase,
speed_increase: 1.0 - *speed_increase,
max_speed_increase: *max_speed_increase - 1.0,
is_interruptible: *is_interruptible,
},
stage: 1,
combo: 0,
timer: Duration::default(),
stage_section: StageSection::Buildup,
next_stage: false,
2020-03-27 17:40:15 +00:00
}),
CharacterAbility::LeapMelee {
energy_cost: _,
buildup_duration,
2020-09-28 23:55:38 +00:00
movement_duration,
swing_duration,
recover_duration,
base_damage,
2020-09-28 23:55:38 +00:00
knockback,
range,
max_angle,
2020-09-28 23:55:38 +00:00
forward_leap_strength,
vertical_leap_strength,
} => CharacterState::LeapMelee(leap_melee::Data {
2020-09-28 23:55:38 +00:00
static_data: leap_melee::StaticData {
buildup_duration: *buildup_duration,
movement_duration: *movement_duration,
swing_duration: *swing_duration,
recover_duration: *recover_duration,
base_damage: *base_damage,
knockback: *knockback,
range: *range,
max_angle: *max_angle,
forward_leap_strength: *forward_leap_strength,
vertical_leap_strength: *vertical_leap_strength,
},
timer: Duration::default(),
stage_section: StageSection::Buildup,
exhausted: false,
}),
2020-07-08 19:58:41 +00:00
CharacterAbility::SpinMelee {
buildup_duration,
swing_duration,
2020-07-08 19:58:41 +00:00
recover_duration,
base_damage,
knockback,
range,
energy_cost,
is_infinite,
is_helicopter,
is_interruptible,
forward_speed,
num_spins,
2020-07-08 19:58:41 +00:00
} => CharacterState::SpinMelee(spin_melee::Data {
static_data: spin_melee::StaticData {
buildup_duration: *buildup_duration,
swing_duration: *swing_duration,
recover_duration: *recover_duration,
base_damage: *base_damage,
knockback: *knockback,
range: *range,
energy_cost: *energy_cost,
is_infinite: *is_infinite,
is_helicopter: *is_helicopter,
is_interruptible: *is_interruptible,
forward_speed: *forward_speed,
num_spins: *num_spins,
},
timer: Duration::default(),
spins_remaining: *num_spins - 1,
stage_section: StageSection::Buildup,
2020-09-18 19:27:02 +00:00
exhausted: false,
2020-07-08 19:58:41 +00:00
}),
CharacterAbility::ChargedMelee {
2020-10-06 05:21:22 +00:00
energy_cost,
energy_drain,
initial_damage,
max_damage,
initial_knockback,
max_knockback,
charge_duration,
2020-09-28 01:58:49 +00:00
swing_duration,
recover_duration,
range,
max_angle,
} => CharacterState::ChargedMelee(charged_melee::Data {
2020-09-28 01:58:49 +00:00
static_data: charged_melee::StaticData {
2020-10-06 05:21:22 +00:00
energy_cost: *energy_cost,
2020-09-28 01:58:49 +00:00
energy_drain: *energy_drain,
initial_damage: *initial_damage,
max_damage: *max_damage,
initial_knockback: *initial_knockback,
max_knockback: *max_knockback,
range: *range,
max_angle: *max_angle,
charge_duration: *charge_duration,
swing_duration: *swing_duration,
recover_duration: *recover_duration,
},
stage_section: StageSection::Charge,
2020-09-28 01:58:49 +00:00
timer: Duration::default(),
exhausted: false,
2020-09-28 01:58:49 +00:00
charge_amount: 0.0,
}),
2020-07-26 03:06:53 +00:00
CharacterAbility::ChargedRanged {
energy_cost: _,
energy_drain,
initial_damage,
max_damage,
initial_knockback,
max_knockback,
prepare_duration,
charge_duration,
recover_duration,
projectile_body,
projectile_light,
projectile_gravity,
initial_projectile_speed,
max_projectile_speed,
2020-07-26 03:06:53 +00:00
} => CharacterState::ChargedRanged(charged_ranged::Data {
exhausted: false,
energy_drain: *energy_drain,
initial_damage: *initial_damage,
max_damage: *max_damage,
initial_knockback: *initial_knockback,
max_knockback: *max_knockback,
prepare_duration: *prepare_duration,
charge_duration: *charge_duration,
charge_timer: Duration::default(),
recover_duration: *recover_duration,
projectile_body: *projectile_body,
projectile_light: *projectile_light,
projectile_gravity: *projectile_gravity,
initial_projectile_speed: *initial_projectile_speed,
max_projectile_speed: *max_projectile_speed,
2020-07-26 03:06:53 +00:00
}),
CharacterAbility::RepeaterRanged {
energy_cost: _,
movement_duration,
2020-09-28 02:38:23 +00:00
buildup_duration,
shoot_duration,
recover_duration,
2020-09-28 02:38:23 +00:00
leap,
projectile,
projectile_body,
projectile_light,
projectile_gravity,
projectile_speed,
reps_remaining,
} => CharacterState::RepeaterRanged(repeater_ranged::Data {
2020-09-28 02:38:23 +00:00
static_data: repeater_ranged::StaticData {
movement_duration: *movement_duration,
buildup_duration: *buildup_duration,
shoot_duration: *shoot_duration,
recover_duration: *recover_duration,
leap: *leap,
projectile: projectile.clone(),
projectile_body: *projectile_body,
projectile_light: *projectile_light,
projectile_gravity: *projectile_gravity,
projectile_speed: *projectile_speed,
},
timer: Duration::default(),
stage_section: StageSection::Movement,
reps_remaining: *reps_remaining,
}),
CharacterAbility::Shockwave {
2020-08-08 20:53:55 +00:00
energy_cost: _,
buildup_duration,
recover_duration,
damage,
knockback,
shockwave_angle,
shockwave_speed,
shockwave_duration,
2020-09-19 16:55:31 +00:00
requires_ground,
} => CharacterState::Shockwave(shockwave::Data {
2020-08-08 20:53:55 +00:00
exhausted: false,
buildup_duration: *buildup_duration,
recover_duration: *recover_duration,
damage: *damage,
knockback: *knockback,
shockwave_angle: *shockwave_angle,
shockwave_speed: *shockwave_speed,
shockwave_duration: *shockwave_duration,
2020-09-19 16:55:31 +00:00
requires_ground: *requires_ground,
2020-08-08 20:53:55 +00:00
}),
CharacterAbility::BasicBeam {
energy_cost: _,
buildup_duration,
recover_duration,
beam_duration,
base_hps,
base_dps,
2020-08-31 21:55:38 +00:00
tick_rate,
range,
max_angle,
lifesteal_eff,
energy_regen,
energy_drain,
} => CharacterState::BasicBeam(basic_beam::Data {
static_data: basic_beam::StaticData {
buildup_duration: *buildup_duration,
recover_duration: *recover_duration,
beam_duration: *beam_duration,
base_hps: *base_hps,
base_dps: *base_dps,
tick_rate: *tick_rate,
range: *range,
max_angle: *max_angle,
lifesteal_eff: *lifesteal_eff,
energy_regen: *energy_regen,
energy_drain: *energy_drain,
},
timer: Duration::default(),
stage_section: StageSection::Buildup,
particle_ori: None::<Vec3<f32>>,
offset: 0.0,
}),
2020-02-03 10:54:50 +00:00
}
}
}
impl Component for Loadout {
type Storage = FlaggedStorage<Self, IdvStorage<Self>>;
2020-01-01 17:16:29 +00:00
}