2019-12-20 13:30:37 +00:00
|
|
|
use crate::{
|
|
|
|
comp::{
|
2020-01-01 17:16:29 +00:00
|
|
|
states::*, Body, CharacterState, Controller, EcsStateData, Mounting, MoveState::*, Ori,
|
2019-12-28 16:10:39 +00:00
|
|
|
OverrideAction, OverrideMove, OverrideState, PhysicsState, Pos, SitState, StateHandle,
|
2019-12-26 18:01:19 +00:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2019-12-29 23:47:42 +00:00
|
|
|
use specs::{Entities, Join, LazyUpdate, Read, ReadStorage, System, WriteStorage};
|
2019-12-26 14:43:59 +00:00
|
|
|
use sphynx::{Uid, UidAllocator};
|
2019-12-22 16:08:48 +00:00
|
|
|
|
2019-12-29 16:36:59 +00:00
|
|
|
/// # Character State System
|
|
|
|
/// #### Updates tuples of ( `CharacterState`, `Pos`, `Vel`, and `Ori` ) in parallel.
|
|
|
|
/// _Each update for a single character involves first passing an `EcsStateData` struct of ECS components
|
|
|
|
/// to the character's `MoveState`, then the character's `ActionState`. State update logic is
|
|
|
|
/// is encapsulated in state's `handle()` fn, impl'd by the `StateHandle` trait. `handle()` fn's
|
|
|
|
/// return a `StateUpdate` tuple containing new ( `CharacterState`, `Pos`, `Vel`, and `Ori` ) components.
|
|
|
|
/// Since `handle()` accepts readonly components, component updates are contained within this system and ECS
|
|
|
|
/// behavior constraints are satisfied._
|
|
|
|
///
|
|
|
|
/// _This mimics the typical OOP style state machine pattern, but remains performant
|
|
|
|
/// under ECS since trait fn's are syntactic sugar for static fn's that accept their implementor's
|
|
|
|
/// object type as its first parameter. See `StateHandle` for more information._
|
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>,
|
2019-12-20 13:30:37 +00:00
|
|
|
ReadStorage<'a, Controller>,
|
|
|
|
ReadStorage<'a, Stats>,
|
|
|
|
ReadStorage<'a, Body>,
|
|
|
|
ReadStorage<'a, PhysicsState>,
|
|
|
|
ReadStorage<'a, Uid>,
|
|
|
|
ReadStorage<'a, Mounting>,
|
2019-12-26 14:43:59 +00:00
|
|
|
ReadStorage<'a, OverrideState>,
|
|
|
|
ReadStorage<'a, OverrideMove>,
|
|
|
|
ReadStorage<'a, OverrideAction>,
|
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,
|
2019-12-20 13:30:37 +00:00
|
|
|
controllers,
|
|
|
|
stats,
|
|
|
|
bodies,
|
|
|
|
physics_states,
|
|
|
|
uids,
|
|
|
|
mountings,
|
2019-12-26 14:43:59 +00:00
|
|
|
state_overrides,
|
|
|
|
move_overrides,
|
|
|
|
action_overrides,
|
2019-12-20 13:30:37 +00:00
|
|
|
): Self::SystemData,
|
|
|
|
) {
|
2019-12-29 23:47:42 +00:00
|
|
|
for (entity, uid, mut character, pos, vel, ori, controller, stats, body, physics, ()) 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,
|
2019-12-20 13:30:37 +00:00
|
|
|
&controllers,
|
|
|
|
&stats,
|
|
|
|
&bodies,
|
|
|
|
&physics_states,
|
2019-12-26 14:43:59 +00:00
|
|
|
!&state_overrides,
|
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) {
|
|
|
|
character.move_state = Sit(SitState);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-01 17:16:29 +00:00
|
|
|
// Determine new action if character can act
|
2019-12-31 13:19:23 +00:00
|
|
|
if let (None, false) = (
|
2020-01-01 17:16:29 +00:00
|
|
|
action_overrides.get(entity),
|
|
|
|
character.action_state.overrides_move_state(),
|
2019-12-31 13:19:23 +00:00
|
|
|
) {
|
2020-01-01 17:16:29 +00:00
|
|
|
let state_update = character.action_state.handle(&EcsStateData {
|
2019-12-29 23:47:42 +00:00
|
|
|
entity: &entity,
|
2019-12-26 14:43:59 +00:00
|
|
|
uid,
|
2019-12-29 23:47:42 +00:00
|
|
|
character,
|
2019-12-22 16:08:48 +00:00
|
|
|
pos,
|
|
|
|
vel,
|
|
|
|
ori,
|
2019-12-29 23:47:42 +00:00
|
|
|
dt: &dt,
|
|
|
|
inputs,
|
2019-12-22 16:08:48 +00:00
|
|
|
stats,
|
|
|
|
body,
|
|
|
|
physics,
|
2019-12-29 23:47:42 +00:00
|
|
|
updater: &updater,
|
|
|
|
server_bus: &server_bus,
|
|
|
|
local_bus: &local_bus,
|
|
|
|
});
|
2019-12-26 14:43:59 +00:00
|
|
|
|
2019-12-29 23:47:42 +00:00
|
|
|
*character = state_update.character;
|
|
|
|
*pos = state_update.pos;
|
|
|
|
*vel = state_update.vel;
|
|
|
|
*ori = state_update.ori;
|
|
|
|
}
|
2019-12-26 14:43:59 +00:00
|
|
|
|
2020-01-01 17:16:29 +00:00
|
|
|
// Determine new move state if character can move
|
2019-12-31 13:19:23 +00:00
|
|
|
if let (None, false) = (
|
2020-01-01 17:16:29 +00:00
|
|
|
move_overrides.get(entity),
|
|
|
|
character.move_state.overrides_action_state(),
|
2019-12-31 13:19:23 +00:00
|
|
|
) {
|
2020-01-01 17:16:29 +00:00
|
|
|
let state_update = character.move_state.handle(&EcsStateData {
|
2019-12-29 23:47:42 +00:00
|
|
|
entity: &entity,
|
|
|
|
uid,
|
|
|
|
character,
|
|
|
|
pos,
|
|
|
|
vel,
|
|
|
|
ori,
|
|
|
|
dt: &dt,
|
|
|
|
inputs,
|
|
|
|
stats,
|
|
|
|
body,
|
|
|
|
physics,
|
|
|
|
updater: &updater,
|
|
|
|
server_bus: &server_bus,
|
|
|
|
local_bus: &local_bus,
|
|
|
|
});
|
2019-12-26 14:43:59 +00:00
|
|
|
|
2019-12-29 23:47:42 +00:00
|
|
|
*character = state_update.character;
|
|
|
|
*pos = state_update.pos;
|
|
|
|
*vel = state_update.vel;
|
|
|
|
*ori = state_update.ori;
|
|
|
|
}
|
|
|
|
}
|
2019-12-22 16:08:48 +00:00
|
|
|
}
|
|
|
|
}
|