2019-12-03 06:30:08 +00:00
|
|
|
use super::movement::ROLL_DURATION;
|
2019-08-23 10:11:37 +00:00
|
|
|
use crate::{
|
|
|
|
comp::{
|
2019-12-03 06:30:08 +00:00
|
|
|
self, item, projectile, ActionState, ActionState::*, Body, CharacterState, ControlEvent,
|
|
|
|
Controller, ControllerInputs, HealthChange, HealthSource, ItemKind, Mounting,
|
|
|
|
MovementState, MovementState::*, PhysicsState, Projectile, Stats, Vel,
|
2019-08-23 10:11:37 +00:00
|
|
|
},
|
2019-12-03 06:30:08 +00:00
|
|
|
event::{Emitter, EventBus, LocalEvent, ServerEvent},
|
|
|
|
state::DeltaTime,
|
2019-06-09 14:20:20 +00:00
|
|
|
};
|
2019-09-09 19:11:40 +00:00
|
|
|
use specs::{
|
|
|
|
saveload::{Marker, MarkerAllocator},
|
2019-12-03 06:30:08 +00:00
|
|
|
Entities, Entity, Join, Read, ReadStorage, System, WriteStorage,
|
2019-09-09 19:11:40 +00:00
|
|
|
};
|
2019-10-11 04:30:34 +00:00
|
|
|
use sphynx::{Uid, UidAllocator};
|
2019-08-23 10:11:37 +00:00
|
|
|
use std::time::Duration;
|
2019-08-29 17:39:34 +00:00
|
|
|
use vek::*;
|
2019-06-09 14:20:20 +00:00
|
|
|
|
2019-12-03 06:30:08 +00:00
|
|
|
/// # Controller System
|
2019-12-20 13:30:37 +00:00
|
|
|
/// #### Responsible for validating and updating controller inputs
|
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 {
|
|
|
|
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>,
|
2019-10-11 04:30:34 +00:00
|
|
|
ReadStorage<'a, Uid>,
|
2019-06-09 14:20:20 +00:00
|
|
|
);
|
|
|
|
fn run(
|
|
|
|
&mut self,
|
2019-12-20 13:30:37 +00:00
|
|
|
(entities, uid_allocator, server_bus, local_bus, dt, mut controllers, uids): Self::SystemData,
|
2019-06-09 14:20:20 +00:00
|
|
|
) {
|
2019-08-25 14:49:54 +00:00
|
|
|
let mut server_emitter = server_bus.emitter();
|
|
|
|
let mut local_emitter = local_bus.emitter();
|
2019-12-20 13:30:37 +00:00
|
|
|
for (entity, uid, controller) in (&entities, &uids, &mut controllers).join() {
|
2019-12-03 06:30:08 +00:00
|
|
|
let inputs = &mut controller.inputs;
|
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-20 13:30:37 +00:00
|
|
|
// Update `inputs.look_dir`
|
|
|
|
inputs
|
|
|
|
.look_dir
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(inputs.move_dir.into());
|
2019-11-29 15:20:35 +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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ControlEvent::Unmount => server_emitter.emit(ServerEvent::Unmount(entity)),
|
2019-10-15 04:06:14 +00:00
|
|
|
ControlEvent::InventoryManip(manip) => {
|
|
|
|
server_emitter.emit(ServerEvent::InventoryManip(entity, manip))
|
|
|
|
} //ControlEvent::Respawn => server_emitter.emit(ServerEvent::Unmount(entity)),
|
2019-09-09 19:11:40 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-09 14:20:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|