2019-08-23 10:11:37 +00:00
|
|
|
use crate::{
|
2020-07-07 15:50:26 +00:00
|
|
|
comp::{
|
|
|
|
slot::{EquipSlot, Slot},
|
|
|
|
CharacterState, ControlEvent, Controller, InventoryManip,
|
|
|
|
},
|
2020-03-07 21:03:10 +00:00
|
|
|
event::{EventBus, LocalEvent, ServerEvent},
|
2020-08-26 08:11:31 +00:00
|
|
|
span,
|
2019-12-03 06:30:08 +00:00
|
|
|
state::DeltaTime,
|
2019-11-24 20:12:03 +00:00
|
|
|
sync::{Uid, UidAllocator},
|
2019-06-09 14:20:20 +00:00
|
|
|
};
|
2019-09-09 19:11:40 +00:00
|
|
|
use specs::{
|
|
|
|
saveload::{Marker, MarkerAllocator},
|
2019-12-26 14:43:59 +00:00
|
|
|
Entities, Join, Read, ReadStorage, System, WriteStorage,
|
2019-09-09 19:11:40 +00:00
|
|
|
};
|
2019-06-09 14:20:20 +00:00
|
|
|
|
2020-03-07 21:03:10 +00:00
|
|
|
// const CHARGE_COST: i32 = 200;
|
|
|
|
// const ROLL_COST: i32 = 30;
|
2019-11-22 00:53:28 +00:00
|
|
|
|
2019-06-09 14:20:20 +00:00
|
|
|
pub struct Sys;
|
2019-12-03 06:30:08 +00:00
|
|
|
|
2019-06-09 14:20:20 +00:00
|
|
|
impl<'a> System<'a> for Sys {
|
2020-06-10 19:47:36 +00:00
|
|
|
#[allow(clippy::type_complexity)]
|
2019-06-09 14:20:20 +00:00
|
|
|
type SystemData = (
|
|
|
|
Entities<'a>,
|
2019-12-03 06:30:08 +00:00
|
|
|
Read<'a, UidAllocator>,
|
2019-08-25 14:49:54 +00:00
|
|
|
Read<'a, EventBus<ServerEvent>>,
|
|
|
|
Read<'a, EventBus<LocalEvent>>,
|
2019-12-03 06:30:08 +00:00
|
|
|
Read<'a, DeltaTime>,
|
2019-07-21 16:50:13 +00:00
|
|
|
WriteStorage<'a, Controller>,
|
2020-03-20 16:15:09 +00:00
|
|
|
WriteStorage<'a, CharacterState>,
|
2019-10-11 04:30:34 +00:00
|
|
|
ReadStorage<'a, Uid>,
|
2019-06-09 14:20:20 +00:00
|
|
|
);
|
2020-02-01 20:39:39 +00:00
|
|
|
|
2019-06-09 14:20:20 +00:00
|
|
|
fn run(
|
|
|
|
&mut self,
|
2020-03-20 16:15:09 +00:00
|
|
|
(
|
|
|
|
entities,
|
|
|
|
uid_allocator,
|
|
|
|
server_bus,
|
|
|
|
_local_bus,
|
2020-03-24 07:38:16 +00:00
|
|
|
_dt,
|
2020-03-20 16:15:09 +00:00
|
|
|
mut controllers,
|
|
|
|
mut character_states,
|
|
|
|
uids,
|
|
|
|
): Self::SystemData,
|
2019-06-09 14:20:20 +00:00
|
|
|
) {
|
2020-09-07 04:59:16 +00:00
|
|
|
span!(_guard, "run", "controller::Sys::run");
|
2019-08-25 14:49:54 +00:00
|
|
|
let mut server_emitter = server_bus.emitter();
|
2020-03-23 17:13:44 +00:00
|
|
|
|
2020-03-24 07:38:16 +00:00
|
|
|
for (entity, _uid, controller, character_state) in
|
|
|
|
(&entities, &uids, &mut controllers, &mut character_states).join()
|
2020-03-20 16:15:09 +00:00
|
|
|
{
|
2020-03-24 07:38:16 +00:00
|
|
|
let mut inputs = &mut controller.inputs;
|
|
|
|
|
|
|
|
// Note(imbris): I avoided incrementing the duration with inputs.tick() because
|
|
|
|
// this is being done manually in voxygen right now so it would double up on
|
|
|
|
// speed of time.
|
|
|
|
// Perhaphs the duration aspects of inputs could be
|
|
|
|
// calculated exclusively on the server (since the client can't be
|
|
|
|
// trusted anyway). It needs to be considered if these calculations
|
|
|
|
// being on the client are critical for responsiveness/client-side prediction.
|
|
|
|
inputs.tick_freshness();
|
2019-10-15 04:06:14 +00:00
|
|
|
|
2019-12-20 13:30:37 +00:00
|
|
|
// Update `inputs.move_dir`.
|
|
|
|
inputs.move_dir = if inputs.move_dir.magnitude_squared() > 1.0 {
|
|
|
|
// Cap move_dir to 1
|
|
|
|
inputs.move_dir.normalized()
|
|
|
|
} else {
|
|
|
|
inputs.move_dir
|
2019-12-03 06:30:08 +00:00
|
|
|
};
|
2019-06-09 14:20:20 +00:00
|
|
|
|
2019-12-03 06:30:08 +00:00
|
|
|
// Process other controller events
|
2019-10-21 00:59:53 +00:00
|
|
|
for event in controller.events.drain(..) {
|
2019-09-09 19:11:40 +00:00
|
|
|
match event {
|
|
|
|
ControlEvent::Mount(mountee_uid) => {
|
|
|
|
if let Some(mountee_entity) =
|
|
|
|
uid_allocator.retrieve_entity_internal(mountee_uid.id())
|
|
|
|
{
|
|
|
|
server_emitter.emit(ServerEvent::Mount(entity, mountee_entity));
|
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-09-09 19:11:40 +00:00
|
|
|
ControlEvent::Unmount => server_emitter.emit(ServerEvent::Unmount(entity)),
|
2020-05-04 15:15:31 +00:00
|
|
|
ControlEvent::ToggleLantern => {
|
|
|
|
server_emitter.emit(ServerEvent::ToggleLantern(entity))
|
|
|
|
},
|
2019-10-15 04:06:14 +00:00
|
|
|
ControlEvent::InventoryManip(manip) => {
|
2020-07-07 15:50:26 +00:00
|
|
|
// Unwield if a wielded equipment slot is being modified, to avoid entering
|
|
|
|
// a barehanded wielding state.
|
|
|
|
if character_state.is_wield() {
|
|
|
|
match manip {
|
|
|
|
InventoryManip::Drop(Slot::Equip(EquipSlot::Mainhand))
|
|
|
|
| InventoryManip::Swap(_, Slot::Equip(EquipSlot::Mainhand))
|
|
|
|
| InventoryManip::Swap(Slot::Equip(EquipSlot::Mainhand), _) => {
|
|
|
|
*character_state = CharacterState::Idle;
|
|
|
|
},
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
2019-10-15 04:06:14 +00:00
|
|
|
server_emitter.emit(ServerEvent::InventoryManip(entity, manip))
|
2020-03-24 07:38:16 +00:00
|
|
|
},
|
2020-04-26 17:03:19 +00:00
|
|
|
ControlEvent::GroupManip(manip) => {
|
|
|
|
server_emitter.emit(ServerEvent::GroupManip(entity, manip))
|
|
|
|
},
|
2020-03-24 07:38:16 +00:00
|
|
|
ControlEvent::Respawn => server_emitter.emit(ServerEvent::Respawn(entity)),
|
2019-09-09 19:11:40 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-09 14:20:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|