2020-12-01 00:28:00 +00:00
|
|
|
use common::{
|
2021-06-06 23:48:47 +00:00
|
|
|
combat,
|
2020-11-15 21:05:02 +00:00
|
|
|
comp::{
|
2021-03-04 20:43:58 +00:00
|
|
|
self,
|
2021-01-16 17:01:57 +00:00
|
|
|
skills::{GeneralSkill, Skill},
|
2021-05-21 00:52:29 +00:00
|
|
|
Body, CharacterState, Combo, Energy, EnergyChange, EnergySource, Health, Inventory, Poise,
|
2021-04-14 15:35:34 +00:00
|
|
|
PoiseChange, PoiseSource, Pos, SkillSet, Stats,
|
2020-11-15 21:05:02 +00:00
|
|
|
},
|
2020-01-11 04:08:33 +00:00
|
|
|
event::{EventBus, ServerEvent},
|
2021-01-04 19:29:15 +00:00
|
|
|
outcome::Outcome,
|
2021-05-05 13:54:24 +00:00
|
|
|
resources::{DeltaTime, EntitiesDiedLastTick, Time},
|
2021-01-04 19:29:15 +00:00
|
|
|
uid::Uid,
|
2019-05-17 20:47:58 +00:00
|
|
|
};
|
2021-03-08 22:40:02 +00:00
|
|
|
use common_ecs::{Job, Origin, Phase, System};
|
2020-12-13 17:21:51 +00:00
|
|
|
use hashbrown::HashSet;
|
2021-02-22 19:27:34 +00:00
|
|
|
use specs::{
|
2021-03-08 08:36:53 +00:00
|
|
|
shred::ResourceId, Entities, Join, Read, ReadStorage, SystemData, World, Write, WriteStorage,
|
2021-02-22 19:27:34 +00:00
|
|
|
};
|
2020-12-16 23:30:33 +00:00
|
|
|
use vek::Vec3;
|
2019-05-17 20:47:58 +00:00
|
|
|
|
2020-02-24 19:57:33 +00:00
|
|
|
const ENERGY_REGEN_ACCEL: f32 = 10.0;
|
2020-12-16 23:30:33 +00:00
|
|
|
const POISE_REGEN_ACCEL: f32 = 2.0;
|
2019-05-17 20:47:58 +00:00
|
|
|
|
2021-02-22 19:27:34 +00:00
|
|
|
#[derive(SystemData)]
|
2021-02-22 21:02:37 +00:00
|
|
|
pub struct ReadData<'a> {
|
2021-02-22 19:27:34 +00:00
|
|
|
entities: Entities<'a>,
|
|
|
|
dt: Read<'a, DeltaTime>,
|
2021-02-27 19:55:06 +00:00
|
|
|
time: Read<'a, Time>,
|
2021-02-22 19:27:34 +00:00
|
|
|
server_bus: Read<'a, EventBus<ServerEvent>>,
|
|
|
|
positions: ReadStorage<'a, Pos>,
|
|
|
|
uids: ReadStorage<'a, Uid>,
|
|
|
|
bodies: ReadStorage<'a, Body>,
|
|
|
|
char_states: ReadStorage<'a, CharacterState>,
|
2021-05-21 00:52:29 +00:00
|
|
|
inventories: ReadStorage<'a, Inventory>,
|
2021-02-22 19:27:34 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 00:53:28 +00:00
|
|
|
/// This system kills players, levels them up, and regenerates energy.
|
2021-03-04 14:00:16 +00:00
|
|
|
#[derive(Default)]
|
2019-05-17 20:47:58 +00:00
|
|
|
pub struct Sys;
|
2021-03-08 11:13:59 +00:00
|
|
|
impl<'a> System<'a> for Sys {
|
2020-06-10 19:47:36 +00:00
|
|
|
#[allow(clippy::type_complexity)]
|
2019-05-17 20:47:58 +00:00
|
|
|
type SystemData = (
|
2021-02-22 21:02:37 +00:00
|
|
|
ReadData<'a>,
|
2019-05-19 20:14:18 +00:00
|
|
|
WriteStorage<'a, Stats>,
|
2021-04-14 15:35:34 +00:00
|
|
|
WriteStorage<'a, SkillSet>,
|
2020-10-31 22:34:08 +00:00
|
|
|
WriteStorage<'a, Health>,
|
2020-12-10 00:32:24 +00:00
|
|
|
WriteStorage<'a, Poise>,
|
2019-11-20 18:31:36 +00:00
|
|
|
WriteStorage<'a, Energy>,
|
2021-02-27 19:55:06 +00:00
|
|
|
WriteStorage<'a, Combo>,
|
2021-05-05 13:54:24 +00:00
|
|
|
Write<'a, EntitiesDiedLastTick>,
|
2021-01-04 19:29:15 +00:00
|
|
|
Write<'a, Vec<Outcome>>,
|
2019-05-17 20:47:58 +00:00
|
|
|
);
|
|
|
|
|
2021-03-04 14:00:16 +00:00
|
|
|
const NAME: &'static str = "stats";
|
|
|
|
const ORIGIN: Origin = Origin::Common;
|
|
|
|
const PHASE: Phase = Phase::Create;
|
|
|
|
|
2019-11-20 18:31:36 +00:00
|
|
|
fn run(
|
2021-03-08 11:13:59 +00:00
|
|
|
_job: &mut Job<Self>,
|
2020-10-31 22:34:08 +00:00
|
|
|
(
|
2021-02-22 21:02:37 +00:00
|
|
|
read_data,
|
2021-04-14 15:35:34 +00:00
|
|
|
stats,
|
|
|
|
mut skill_sets,
|
2020-10-31 22:34:08 +00:00
|
|
|
mut healths,
|
2020-12-10 00:32:24 +00:00
|
|
|
mut poises,
|
2020-10-31 22:34:08 +00:00
|
|
|
mut energies,
|
2021-02-27 19:55:06 +00:00
|
|
|
mut combos,
|
2021-05-05 13:54:24 +00:00
|
|
|
mut entities_died_last_tick,
|
2021-01-04 19:29:15 +00:00
|
|
|
mut outcomes,
|
2020-10-31 22:34:08 +00:00
|
|
|
): Self::SystemData,
|
2019-11-20 18:31:36 +00:00
|
|
|
) {
|
2021-05-05 13:54:24 +00:00
|
|
|
entities_died_last_tick.0.clear();
|
2021-02-22 21:02:37 +00:00
|
|
|
let mut server_event_emitter = read_data.server_bus.emitter();
|
|
|
|
let dt = read_data.dt.0;
|
2019-08-23 10:11:37 +00:00
|
|
|
|
2019-12-01 21:54:21 +00:00
|
|
|
// Increment last change timer
|
2020-10-31 22:34:08 +00:00
|
|
|
healths.set_event_emission(false); // avoid unnecessary syncing
|
2020-12-16 23:30:33 +00:00
|
|
|
poises.set_event_emission(false); // avoid unnecessary syncing
|
2021-01-13 03:26:51 +00:00
|
|
|
for mut health in (&mut healths).join() {
|
2021-02-22 19:27:34 +00:00
|
|
|
health.last_change.0 += f64::from(dt);
|
2019-12-01 21:54:21 +00:00
|
|
|
}
|
2021-01-27 03:05:13 +00:00
|
|
|
for mut poise in (&mut poises).join() {
|
2021-02-22 19:27:34 +00:00
|
|
|
poise.last_change.0 += f64::from(dt);
|
2020-12-16 23:30:33 +00:00
|
|
|
}
|
2020-10-31 22:34:08 +00:00
|
|
|
healths.set_event_emission(true);
|
2020-12-16 23:30:33 +00:00
|
|
|
poises.set_event_emission(true);
|
2019-12-01 21:54:21 +00:00
|
|
|
|
2020-07-05 12:39:28 +00:00
|
|
|
// Update stats
|
2021-05-21 00:52:29 +00:00
|
|
|
for (entity, uid, stats, mut skill_set, mut health, pos, mut energy, inventory) in (
|
2021-02-22 21:02:37 +00:00
|
|
|
&read_data.entities,
|
|
|
|
&read_data.uids,
|
2021-04-14 15:35:34 +00:00
|
|
|
&stats,
|
2021-07-27 20:22:39 +00:00
|
|
|
&mut skill_sets,
|
|
|
|
&mut healths,
|
2021-02-22 21:02:37 +00:00
|
|
|
&read_data.positions,
|
2021-07-27 20:22:39 +00:00
|
|
|
&mut energies,
|
2021-05-21 00:52:29 +00:00
|
|
|
read_data.inventories.maybe(),
|
2020-10-31 22:34:08 +00:00
|
|
|
)
|
|
|
|
.join()
|
|
|
|
{
|
2021-07-27 20:22:39 +00:00
|
|
|
let set_dead = { health.should_die() && !health.is_dead };
|
2019-12-01 21:54:21 +00:00
|
|
|
|
|
|
|
if set_dead {
|
2021-05-05 13:54:24 +00:00
|
|
|
let cloned_entity = (entity, *pos);
|
|
|
|
entities_died_last_tick.0.push(cloned_entity);
|
2019-11-23 08:26:39 +00:00
|
|
|
server_event_emitter.emit(ServerEvent::Destroy {
|
2019-05-27 19:41:24 +00:00
|
|
|
entity,
|
2020-10-31 22:34:08 +00:00
|
|
|
cause: health.last_change.1.cause,
|
2019-08-23 10:11:37 +00:00
|
|
|
});
|
|
|
|
|
2020-10-31 22:34:08 +00:00
|
|
|
health.is_dead = true;
|
2019-05-17 20:47:58 +00:00
|
|
|
}
|
2021-04-14 15:35:34 +00:00
|
|
|
let stat = stats;
|
2021-03-20 17:29:57 +00:00
|
|
|
|
|
|
|
let update_max_hp = {
|
|
|
|
(stat.max_health_modifier - 1.0).abs() > f32::EPSILON
|
|
|
|
|| health.base_max() != health.maximum()
|
|
|
|
};
|
|
|
|
|
|
|
|
if update_max_hp {
|
|
|
|
health.scale_maximum(stat.max_health_modifier);
|
|
|
|
}
|
|
|
|
|
2021-06-06 23:48:47 +00:00
|
|
|
let (change_energy, energy_scaling) = {
|
|
|
|
// Calculates energy scaling from stats and inventory
|
|
|
|
let new_energy_scaling =
|
2021-07-27 20:22:39 +00:00
|
|
|
combat::compute_max_energy_mod(&energy, inventory) + stat.max_energy_modifier;
|
2021-06-06 23:48:47 +00:00
|
|
|
let current_energy_scaling = energy.maximum() as f32 / energy.base_max() as f32;
|
|
|
|
// Only changes energy if new modifier different from old modifer
|
|
|
|
// TODO: Look into using wider threshold incase floating point imprecision makes
|
|
|
|
// this always true
|
|
|
|
(
|
|
|
|
(current_energy_scaling - new_energy_scaling).abs() > f32::EPSILON,
|
|
|
|
new_energy_scaling,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
// If modifier sufficiently different, mutably access energy
|
|
|
|
if change_energy {
|
|
|
|
energy.scale_maximum(energy_scaling);
|
|
|
|
}
|
2021-05-21 00:52:29 +00:00
|
|
|
|
2021-07-27 20:22:39 +00:00
|
|
|
let skills_to_level = skill_set
|
2021-01-16 17:01:57 +00:00
|
|
|
.skill_groups
|
|
|
|
.iter()
|
|
|
|
.filter_map(|s_g| {
|
2021-07-27 20:22:39 +00:00
|
|
|
(s_g.exp >= skill_set.skill_point_cost(s_g.skill_group_kind))
|
2021-01-18 19:08:13 +00:00
|
|
|
.then(|| s_g.skill_group_kind)
|
2021-01-16 17:01:57 +00:00
|
|
|
})
|
|
|
|
.collect::<HashSet<_>>();
|
2019-11-23 08:26:39 +00:00
|
|
|
|
2020-11-15 21:05:02 +00:00
|
|
|
if !skills_to_level.is_empty() {
|
2021-01-16 17:01:57 +00:00
|
|
|
for skill_group in skills_to_level {
|
2021-04-14 15:35:34 +00:00
|
|
|
skill_set.earn_skill_point(skill_group);
|
2021-01-04 19:29:15 +00:00
|
|
|
outcomes.push(Outcome::SkillPointGain {
|
|
|
|
uid: *uid,
|
|
|
|
skill_tree: skill_group,
|
2021-04-14 15:35:34 +00:00
|
|
|
total_points: skill_set.earned_sp(skill_group),
|
2021-01-09 16:56:19 +00:00
|
|
|
pos: pos.0,
|
2021-01-04 19:29:15 +00:00
|
|
|
});
|
2020-11-15 21:05:02 +00:00
|
|
|
}
|
2019-08-03 19:30:01 +00:00
|
|
|
}
|
2020-07-05 12:39:28 +00:00
|
|
|
}
|
2019-11-20 18:31:36 +00:00
|
|
|
|
2020-12-31 18:37:25 +00:00
|
|
|
// Apply effects from leveling skills
|
2021-04-14 15:35:34 +00:00
|
|
|
for (mut skill_set, mut health, mut energy, body) in (
|
2021-07-27 20:22:39 +00:00
|
|
|
&mut skill_sets,
|
|
|
|
&mut healths,
|
|
|
|
&mut energies,
|
2021-02-22 21:02:37 +00:00
|
|
|
&read_data.bodies,
|
2020-12-31 18:37:25 +00:00
|
|
|
)
|
|
|
|
.join()
|
|
|
|
{
|
2021-07-27 20:22:39 +00:00
|
|
|
if skill_set.modify_health {
|
|
|
|
let health_level = skill_set
|
2021-01-16 17:01:57 +00:00
|
|
|
.skill_level(Skill::General(GeneralSkill::HealthIncrease))
|
|
|
|
.unwrap_or(None)
|
2020-12-31 18:37:25 +00:00
|
|
|
.unwrap_or(0);
|
2021-01-22 21:12:16 +00:00
|
|
|
health.update_max_hp(Some(*body), health_level);
|
2021-07-27 20:22:39 +00:00
|
|
|
skill_set.modify_health = false;
|
2020-12-31 18:37:25 +00:00
|
|
|
}
|
2021-07-27 20:22:39 +00:00
|
|
|
if skill_set.modify_energy {
|
|
|
|
let energy_level = skill_set
|
2021-01-16 17:01:57 +00:00
|
|
|
.skill_level(Skill::General(GeneralSkill::EnergyIncrease))
|
|
|
|
.unwrap_or(None)
|
2021-01-04 17:16:42 +00:00
|
|
|
.unwrap_or(0);
|
2021-01-22 21:12:16 +00:00
|
|
|
energy.update_max_energy(Some(*body), energy_level);
|
2021-04-14 15:35:34 +00:00
|
|
|
skill_set.modify_energy = false;
|
2020-12-31 18:37:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-10 00:32:24 +00:00
|
|
|
// Update energies and poises
|
2021-07-27 20:22:39 +00:00
|
|
|
for (character_state, mut energy, mut poise) in
|
|
|
|
(&read_data.char_states, &mut energies, &mut poises).join()
|
2020-07-05 12:39:28 +00:00
|
|
|
{
|
2020-02-03 21:02:32 +00:00
|
|
|
match character_state {
|
2020-06-13 11:35:31 +00:00
|
|
|
// Accelerate recharging energy.
|
2020-06-13 03:45:03 +00:00
|
|
|
CharacterState::Idle { .. }
|
2021-01-31 20:29:50 +00:00
|
|
|
| CharacterState::Talk { .. }
|
2020-06-13 03:45:03 +00:00
|
|
|
| CharacterState::Sit { .. }
|
|
|
|
| CharacterState::Dance { .. }
|
2020-08-02 05:09:11 +00:00
|
|
|
| CharacterState::Sneak { .. }
|
2021-04-27 14:41:48 +00:00
|
|
|
| CharacterState::Glide { .. }
|
2020-06-16 21:32:39 +00:00
|
|
|
| CharacterState::GlideWield { .. }
|
2020-06-13 03:45:03 +00:00
|
|
|
| CharacterState::Wielding { .. }
|
|
|
|
| CharacterState::Equipping { .. }
|
|
|
|
| CharacterState::Boost { .. } => {
|
2021-07-27 20:22:39 +00:00
|
|
|
let res = { energy.current() < energy.maximum() };
|
2020-06-08 18:37:41 +00:00
|
|
|
|
|
|
|
if res {
|
2021-01-07 20:25:12 +00:00
|
|
|
let energy = &mut *energy;
|
2020-01-19 19:44:44 +00:00
|
|
|
// Have to account for Calc I differential equations due to acceleration
|
2020-10-30 21:49:58 +00:00
|
|
|
energy.change_by(EnergyChange {
|
2021-02-22 19:27:34 +00:00
|
|
|
amount: (energy.regen_rate * dt + ENERGY_REGEN_ACCEL * dt.powi(2) / 2.0)
|
2020-01-19 21:39:01 +00:00
|
|
|
as i32,
|
2020-10-30 21:49:58 +00:00
|
|
|
source: EnergySource::Regen,
|
|
|
|
});
|
2020-02-24 19:57:33 +00:00
|
|
|
energy.regen_rate =
|
2021-02-22 19:27:34 +00:00
|
|
|
(energy.regen_rate + ENERGY_REGEN_ACCEL * dt).min(100.0);
|
2020-01-19 19:19:44 +00:00
|
|
|
}
|
2020-12-10 00:32:24 +00:00
|
|
|
|
2021-07-27 20:22:39 +00:00
|
|
|
let res_poise = { poise.current() < poise.maximum() };
|
2020-12-10 00:32:24 +00:00
|
|
|
|
2020-12-16 23:30:33 +00:00
|
|
|
if res_poise {
|
2021-01-27 03:05:13 +00:00
|
|
|
let poise = &mut *poise;
|
2020-12-16 23:30:33 +00:00
|
|
|
poise.change_by(
|
|
|
|
PoiseChange {
|
2021-02-22 19:27:34 +00:00
|
|
|
amount: (poise.regen_rate * dt
|
|
|
|
+ POISE_REGEN_ACCEL * dt.powi(2) / 2.0)
|
2020-12-16 23:30:33 +00:00
|
|
|
as i32,
|
|
|
|
source: PoiseSource::Regen,
|
|
|
|
},
|
|
|
|
Vec3::zero(),
|
|
|
|
);
|
2021-02-22 19:27:34 +00:00
|
|
|
poise.regen_rate = (poise.regen_rate + POISE_REGEN_ACCEL * dt).min(10.0);
|
2020-12-16 23:30:33 +00:00
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2021-04-27 14:41:48 +00:00
|
|
|
// Ability use does not regen and sets the rate back to zero.
|
|
|
|
CharacterState::BasicMelee { .. }
|
2020-06-13 03:45:03 +00:00
|
|
|
| CharacterState::DashMelee { .. }
|
2020-07-03 15:40:12 +00:00
|
|
|
| CharacterState::LeapMelee { .. }
|
2020-07-08 19:58:41 +00:00
|
|
|
| CharacterState::SpinMelee { .. }
|
2020-09-04 01:54:59 +00:00
|
|
|
| CharacterState::ComboMelee { .. }
|
2020-07-26 03:06:53 +00:00
|
|
|
| CharacterState::BasicRanged { .. }
|
2020-09-21 04:16:52 +00:00
|
|
|
| CharacterState::ChargedMelee { .. }
|
2020-08-08 20:53:55 +00:00
|
|
|
| CharacterState::ChargedRanged { .. }
|
2020-09-21 04:16:52 +00:00
|
|
|
| CharacterState::RepeaterRanged { .. }
|
2020-10-04 01:24:15 +00:00
|
|
|
| CharacterState::Shockwave { .. }
|
2021-03-01 20:44:29 +00:00
|
|
|
| CharacterState::BasicBeam { .. }
|
2021-03-06 21:29:00 +00:00
|
|
|
| CharacterState::BasicAura { .. }
|
2021-03-21 05:53:39 +00:00
|
|
|
| CharacterState::Blink { .. }
|
2021-04-24 19:01:36 +00:00
|
|
|
| CharacterState::BasicSummon { .. }
|
2021-06-19 18:53:23 +00:00
|
|
|
| CharacterState::SelfBuff { .. }
|
|
|
|
| CharacterState::SpriteSummon { .. } => {
|
2021-07-27 20:22:39 +00:00
|
|
|
if energy.regen_rate != 0.0 {
|
|
|
|
energy.regen_rate = 0.0
|
2020-01-19 19:19:44 +00:00
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2021-04-10 03:40:20 +00:00
|
|
|
// Abilities that temporarily stall energy gain, but preserve regen_rate.
|
2020-12-05 18:23:45 +00:00
|
|
|
CharacterState::Roll { .. }
|
|
|
|
| CharacterState::Climb { .. }
|
2021-04-10 03:40:20 +00:00
|
|
|
| CharacterState::Stunned { .. }
|
2021-06-25 01:38:11 +00:00
|
|
|
| CharacterState::BasicBlock { .. }
|
2021-08-20 04:23:39 +00:00
|
|
|
| CharacterState::UseItem { .. }
|
|
|
|
| CharacterState::SpriteInteract { .. } => {},
|
2019-08-03 19:30:01 +00:00
|
|
|
}
|
2019-05-17 20:47:58 +00:00
|
|
|
}
|
2021-01-15 03:32:12 +00:00
|
|
|
|
2021-02-27 19:55:06 +00:00
|
|
|
// Decay combo
|
|
|
|
for (_, mut combo) in (&read_data.entities, &mut combos).join() {
|
2021-03-04 20:43:58 +00:00
|
|
|
if combo.counter() > 0
|
|
|
|
&& read_data.time.0 - combo.last_increase() > comp::combo::COMBO_DECAY_START
|
|
|
|
{
|
2021-02-27 19:55:06 +00:00
|
|
|
combo.reset();
|
|
|
|
}
|
|
|
|
}
|
2019-05-17 20:47:58 +00:00
|
|
|
}
|
|
|
|
}
|