2019-12-20 13:30:37 +00:00
|
|
|
use crate::{
|
|
|
|
comp::{
|
2020-02-24 18:17:16 +00:00
|
|
|
AbilityPool, Body, CharacterState, Controller, EcsStateData, Energy, Mounting, Ori,
|
|
|
|
PhysicsState, Pos, Stats, Vel,
|
2019-12-20 13:30:37 +00:00
|
|
|
},
|
2019-12-26 14:43:59 +00:00
|
|
|
event::{EventBus, LocalEvent, ServerEvent},
|
2019-12-20 13:30:37 +00:00
|
|
|
state::DeltaTime,
|
2020-01-16 13:28:45 +00:00
|
|
|
sync::{Uid, UidAllocator},
|
2019-12-20 13:30:37 +00:00
|
|
|
};
|
|
|
|
|
2019-12-29 23:47:42 +00:00
|
|
|
use specs::{Entities, Join, LazyUpdate, Read, ReadStorage, System, WriteStorage};
|
2019-12-22 16:08:48 +00:00
|
|
|
|
2020-01-07 15:49:08 +00:00
|
|
|
/// ## Character State System
|
2020-02-24 18:17:16 +00:00
|
|
|
/// #### Calls updates to `CharacterState`s. Acts on tuples of (
|
|
|
|
/// `CharacterState`, `Pos`, `Vel`, and `Ori` ).
|
2019-12-29 16:36:59 +00:00
|
|
|
///
|
2020-02-24 18:17:16 +00:00
|
|
|
/// _System forms `EcsStateData` tuples and passes those to `ActionState`
|
|
|
|
/// `update()` fn, then does the same for `MoveState` `update`_
|
2019-12-20 13:30:37 +00:00
|
|
|
pub struct Sys;
|
|
|
|
|
|
|
|
impl<'a> System<'a> for Sys {
|
|
|
|
type SystemData = (
|
|
|
|
Entities<'a>,
|
|
|
|
Read<'a, UidAllocator>,
|
|
|
|
Read<'a, EventBus<ServerEvent>>,
|
|
|
|
Read<'a, EventBus<LocalEvent>>,
|
|
|
|
Read<'a, DeltaTime>,
|
2019-12-26 14:43:59 +00:00
|
|
|
Read<'a, LazyUpdate>,
|
2019-12-20 13:30:37 +00:00
|
|
|
WriteStorage<'a, CharacterState>,
|
2019-12-21 15:57:15 +00:00
|
|
|
WriteStorage<'a, Pos>,
|
|
|
|
WriteStorage<'a, Vel>,
|
|
|
|
WriteStorage<'a, Ori>,
|
2020-02-24 18:17:16 +00:00
|
|
|
WriteStorage<'a, Energy>,
|
2019-12-20 13:30:37 +00:00
|
|
|
ReadStorage<'a, Controller>,
|
|
|
|
ReadStorage<'a, Stats>,
|
|
|
|
ReadStorage<'a, Body>,
|
|
|
|
ReadStorage<'a, PhysicsState>,
|
2020-02-03 10:54:50 +00:00
|
|
|
ReadStorage<'a, AbilityPool>,
|
2019-12-20 13:30:37 +00:00
|
|
|
ReadStorage<'a, Uid>,
|
|
|
|
ReadStorage<'a, Mounting>,
|
|
|
|
);
|
2020-02-24 18:17:16 +00:00
|
|
|
|
2019-12-20 13:30:37 +00:00
|
|
|
fn run(
|
|
|
|
&mut self,
|
|
|
|
(
|
|
|
|
entities,
|
2019-12-26 18:01:19 +00:00
|
|
|
_uid_allocator,
|
2019-12-20 13:30:37 +00:00
|
|
|
server_bus,
|
|
|
|
local_bus,
|
|
|
|
dt,
|
2019-12-26 14:43:59 +00:00
|
|
|
updater,
|
2019-12-20 13:30:37 +00:00
|
|
|
mut character_states,
|
2019-12-21 15:57:15 +00:00
|
|
|
mut positions,
|
|
|
|
mut velocities,
|
|
|
|
mut orientations,
|
2020-02-24 18:17:16 +00:00
|
|
|
mut energies,
|
2019-12-20 13:30:37 +00:00
|
|
|
controllers,
|
|
|
|
stats,
|
|
|
|
bodies,
|
|
|
|
physics_states,
|
2020-02-03 10:54:50 +00:00
|
|
|
ability_pools,
|
2019-12-20 13:30:37 +00:00
|
|
|
uids,
|
|
|
|
mountings,
|
|
|
|
): Self::SystemData,
|
|
|
|
) {
|
2020-02-03 10:54:50 +00:00
|
|
|
for (
|
|
|
|
entity,
|
|
|
|
uid,
|
|
|
|
character,
|
|
|
|
pos,
|
|
|
|
vel,
|
|
|
|
ori,
|
2020-02-24 18:17:16 +00:00
|
|
|
energy,
|
2020-02-03 10:54:50 +00:00
|
|
|
controller,
|
|
|
|
stats,
|
|
|
|
body,
|
|
|
|
physics,
|
|
|
|
ability_pool,
|
|
|
|
) in (
|
2019-12-20 13:30:37 +00:00
|
|
|
&entities,
|
|
|
|
&uids,
|
|
|
|
&mut character_states,
|
2019-12-21 15:57:15 +00:00
|
|
|
&mut positions,
|
|
|
|
&mut velocities,
|
|
|
|
&mut orientations,
|
2020-02-24 18:17:16 +00:00
|
|
|
&mut energies,
|
2019-12-20 13:30:37 +00:00
|
|
|
&controllers,
|
|
|
|
&stats,
|
|
|
|
&bodies,
|
|
|
|
&physics_states,
|
2020-02-03 10:54:50 +00:00
|
|
|
&ability_pools,
|
2019-12-20 13:30:37 +00:00
|
|
|
)
|
2019-12-29 23:47:42 +00:00
|
|
|
.join()
|
|
|
|
{
|
|
|
|
let inputs = &controller.inputs;
|
|
|
|
|
|
|
|
// Being dead overrides all other states
|
|
|
|
if stats.is_dead {
|
|
|
|
// Only options: click respawn
|
|
|
|
// prevent instant-respawns (i.e. player was holding attack)
|
|
|
|
// by disallowing while input is held down
|
|
|
|
if inputs.respawn.is_pressed() && !inputs.respawn.is_held_down() {
|
|
|
|
server_bus.emitter().emit(ServerEvent::Respawn(entity));
|
|
|
|
}
|
|
|
|
// Or do nothing
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// If mounted, character state is controlled by mount
|
|
|
|
// TODO: Make mounting a state
|
|
|
|
if let Some(Mounting(_)) = mountings.get(entity) {
|
2020-01-21 22:54:32 +00:00
|
|
|
*character = CharacterState::Sit(None);
|
2019-12-29 23:47:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-03 10:54:50 +00:00
|
|
|
let mut state_update = character.update(&EcsStateData {
|
2020-01-21 22:54:32 +00:00
|
|
|
entity: &entity,
|
|
|
|
uid,
|
|
|
|
character,
|
|
|
|
pos,
|
|
|
|
vel,
|
|
|
|
ori,
|
2020-02-24 18:17:16 +00:00
|
|
|
energy,
|
2020-01-21 22:54:32 +00:00
|
|
|
dt: &dt,
|
|
|
|
inputs,
|
|
|
|
stats,
|
|
|
|
body,
|
|
|
|
physics,
|
|
|
|
updater: &updater,
|
2020-02-03 10:54:50 +00:00
|
|
|
ability_pool,
|
2020-01-21 22:54:32 +00:00
|
|
|
});
|
2019-12-26 14:43:59 +00:00
|
|
|
|
2020-01-21 22:54:32 +00:00
|
|
|
*character = state_update.character;
|
|
|
|
*pos = state_update.pos;
|
|
|
|
*vel = state_update.vel;
|
|
|
|
*ori = state_update.ori;
|
2020-02-24 18:17:16 +00:00
|
|
|
*energy = state_update.energy;
|
2020-02-03 10:54:50 +00:00
|
|
|
local_bus.emitter().append(&mut state_update.local_events);
|
|
|
|
server_bus.emitter().append(&mut state_update.server_events);
|
2019-12-29 23:47:42 +00:00
|
|
|
}
|
2019-12-22 16:08:48 +00:00
|
|
|
}
|
|
|
|
}
|