2019-12-20 13:30:37 +00:00
|
|
|
use crate::{
|
|
|
|
comp::{
|
2019-12-26 14:43:59 +00:00
|
|
|
Body, CharacterState, Controller, ControllerInputs, ECSStateData, Mounting, MoveState::*,
|
|
|
|
Ori, OverrideAction, OverrideMove, OverrideState, PhysicsState, Pos, SitHandler,
|
|
|
|
StateHandle, 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-26 14:43:59 +00:00
|
|
|
use specs::{Entities, Join, LazyUpdate, Read, ReadStorage, System, WriteStorage};
|
|
|
|
use sphynx::{Uid, UidAllocator};
|
2019-12-22 16:08:48 +00:00
|
|
|
|
2019-12-26 14:43:59 +00:00
|
|
|
/// # Character StateHandle System
|
2019-12-20 13:30:37 +00:00
|
|
|
/// #### Updates then detemrines next Character States based on ControllerInputs
|
|
|
|
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,
|
|
|
|
uid_allocator,
|
|
|
|
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-21 15:57:15 +00:00
|
|
|
for (
|
|
|
|
entity,
|
|
|
|
uid,
|
|
|
|
mut character,
|
|
|
|
mut pos,
|
|
|
|
mut vel,
|
|
|
|
mut ori,
|
|
|
|
controller,
|
|
|
|
stats,
|
|
|
|
body,
|
|
|
|
physics,
|
2019-12-26 14:43:59 +00:00
|
|
|
maybe_mount,
|
|
|
|
maybe_move_override,
|
|
|
|
maybe_action_override,
|
|
|
|
(),
|
2019-12-21 15:57:15 +00:00
|
|
|
) 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,
|
|
|
|
mountings.maybe(),
|
2019-12-26 14:43:59 +00:00
|
|
|
move_overrides.maybe(),
|
|
|
|
action_overrides.maybe(),
|
|
|
|
!&state_overrides,
|
2019-12-20 13:30:37 +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() {
|
2019-12-22 16:08:48 +00:00
|
|
|
server_bus.emitter().emit(ServerEvent::Respawn(entity));
|
2019-12-20 13:30:37 +00:00
|
|
|
}
|
|
|
|
// Or do nothing
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// If mounted, character state is controlled by mount
|
2019-12-26 14:43:59 +00:00
|
|
|
// TODO: Make mounting a stater
|
|
|
|
if maybe_mount.is_some() {
|
|
|
|
character.move_state = Sit(SitHandler);
|
2019-12-20 13:30:37 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-12-26 14:43:59 +00:00
|
|
|
// Determine new move state if can move
|
|
|
|
if !maybe_move_override.is_some() {
|
|
|
|
let state_update = character.move_state.handle(&ECSStateData {
|
|
|
|
entity: &entity,
|
|
|
|
uid,
|
2019-12-22 16:08:48 +00:00
|
|
|
character,
|
|
|
|
pos,
|
|
|
|
vel,
|
|
|
|
ori,
|
2019-12-26 14:43:59 +00:00
|
|
|
dt: &dt,
|
2019-12-22 16:08:48 +00:00
|
|
|
inputs,
|
|
|
|
stats,
|
|
|
|
body,
|
|
|
|
physics,
|
2019-12-26 14:43:59 +00:00
|
|
|
updater: &updater,
|
|
|
|
server_bus: &server_bus,
|
|
|
|
local_bus: &local_bus,
|
|
|
|
});
|
|
|
|
|
|
|
|
*character = state_update.character;
|
|
|
|
*pos = state_update.pos;
|
|
|
|
*vel = state_update.vel;
|
|
|
|
*ori = state_update.ori;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine new action if can_act
|
|
|
|
if !maybe_action_override.is_some() {
|
|
|
|
let state_update = character.action_state.handle(&ECSStateData {
|
|
|
|
entity: &entity,
|
|
|
|
uid,
|
2019-12-22 16:08:48 +00:00
|
|
|
character,
|
|
|
|
pos,
|
|
|
|
vel,
|
|
|
|
ori,
|
2019-12-26 14:43:59 +00:00
|
|
|
dt: &dt,
|
2019-12-22 16:08:48 +00:00
|
|
|
inputs,
|
|
|
|
stats,
|
|
|
|
body,
|
|
|
|
physics,
|
2019-12-26 14:43:59 +00:00
|
|
|
updater: &updater,
|
|
|
|
server_bus: &server_bus,
|
|
|
|
local_bus: &local_bus,
|
|
|
|
});
|
|
|
|
|
|
|
|
*character = state_update.character;
|
|
|
|
*pos = state_update.pos;
|
|
|
|
*vel = state_update.vel;
|
|
|
|
*ori = state_update.ori;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rolling + Any Movement, prioritizes finishing charge
|
|
|
|
// over move_state states
|
|
|
|
// (
|
|
|
|
// Roll {
|
|
|
|
// time_left,
|
|
|
|
// was_wielding,
|
|
|
|
// },
|
|
|
|
// _,
|
|
|
|
// ) => {
|
|
|
|
// if time_left == Duration::default() {
|
|
|
|
// if was_wielding {
|
|
|
|
// character.action = try_wield(stats);
|
|
|
|
// } else {
|
|
|
|
// character.action = Idle;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
2019-12-22 16:08:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|