2019-08-26 18:05:30 +00:00
|
|
|
use super::{
|
|
|
|
combat::{ATTACK_DURATION, WIELD_DURATION},
|
|
|
|
movement::ROLL_DURATION,
|
|
|
|
};
|
2019-08-23 10:11:37 +00:00
|
|
|
use crate::{
|
|
|
|
comp::{
|
2019-10-11 04:30:34 +00:00
|
|
|
self, item, projectile, ActionState::*, Body, CharacterState, ControlEvent, Controller,
|
2019-10-17 20:59:36 +00:00
|
|
|
HealthChange, HealthSource, Item, MovementState::*, PhysicsState, Projectile, Stats, Vel,
|
2019-08-23 10:11:37 +00:00
|
|
|
},
|
2019-08-25 16:48:12 +00:00
|
|
|
event::{EventBus, LocalEvent, ServerEvent},
|
2019-06-09 14:20:20 +00:00
|
|
|
};
|
2019-09-09 19:11:40 +00:00
|
|
|
use specs::{
|
|
|
|
saveload::{Marker, MarkerAllocator},
|
|
|
|
Entities, Join, Read, ReadStorage, System, WriteStorage,
|
|
|
|
};
|
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-06-09 19:33:20 +00:00
|
|
|
/// This system is responsible for validating controller inputs
|
2019-06-09 14:20:20 +00:00
|
|
|
pub struct Sys;
|
|
|
|
impl<'a> System<'a> for Sys {
|
|
|
|
type SystemData = (
|
2019-09-09 19:11:40 +00:00
|
|
|
Read<'a, UidAllocator>,
|
2019-06-09 14:20:20 +00:00
|
|
|
Entities<'a>,
|
2019-08-25 14:49:54 +00:00
|
|
|
Read<'a, EventBus<ServerEvent>>,
|
|
|
|
Read<'a, EventBus<LocalEvent>>,
|
2019-07-21 16:50:13 +00:00
|
|
|
WriteStorage<'a, Controller>,
|
2019-06-09 14:20:20 +00:00
|
|
|
ReadStorage<'a, Stats>,
|
2019-08-04 08:21:29 +00:00
|
|
|
ReadStorage<'a, Body>,
|
2019-06-13 18:09:50 +00:00
|
|
|
ReadStorage<'a, Vel>,
|
2019-08-23 10:11:37 +00:00
|
|
|
ReadStorage<'a, PhysicsState>,
|
2019-10-11 04:30:34 +00:00
|
|
|
ReadStorage<'a, Uid>,
|
2019-08-23 10:11:37 +00:00
|
|
|
WriteStorage<'a, CharacterState>,
|
2019-06-09 14:20:20 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&mut self,
|
|
|
|
(
|
2019-09-09 19:11:40 +00:00
|
|
|
uid_allocator,
|
2019-06-09 14:20:20 +00:00
|
|
|
entities,
|
2019-08-25 14:49:54 +00:00
|
|
|
server_bus,
|
|
|
|
local_bus,
|
2019-07-21 16:50:13 +00:00
|
|
|
mut controllers,
|
2019-06-09 14:20:20 +00:00
|
|
|
stats,
|
2019-08-04 08:21:29 +00:00
|
|
|
bodies,
|
2019-06-13 18:09:50 +00:00
|
|
|
velocities,
|
2019-08-23 10:11:37 +00:00
|
|
|
physics_states,
|
2019-10-17 20:59:36 +00:00
|
|
|
uids,
|
2019-08-23 10:11:37 +00:00
|
|
|
mut character_states,
|
2019-06-09 14:20:20 +00:00
|
|
|
): Self::SystemData,
|
|
|
|
) {
|
2019-08-25 14:49:54 +00:00
|
|
|
let mut server_emitter = server_bus.emitter();
|
|
|
|
let mut local_emitter = local_bus.emitter();
|
2019-08-23 10:11:37 +00:00
|
|
|
|
2019-10-17 20:59:36 +00:00
|
|
|
for (entity, uid, controller, stats, body, vel, physics, mut character) in (
|
2019-06-09 14:20:20 +00:00
|
|
|
&entities,
|
2019-10-17 20:59:36 +00:00
|
|
|
&uids,
|
2019-07-21 16:50:13 +00:00
|
|
|
&mut controllers,
|
2019-06-09 14:20:20 +00:00
|
|
|
&stats,
|
2019-08-04 08:21:29 +00:00
|
|
|
&bodies,
|
2019-06-13 18:09:50 +00:00
|
|
|
&velocities,
|
2019-08-23 10:11:37 +00:00
|
|
|
&physics_states,
|
|
|
|
&mut character_states,
|
2019-06-09 14:20:20 +00:00
|
|
|
)
|
|
|
|
.join()
|
|
|
|
{
|
2019-10-15 04:06:14 +00:00
|
|
|
let mut inputs = &mut controller.inputs;
|
|
|
|
|
2019-06-09 14:20:20 +00:00
|
|
|
if stats.is_dead {
|
2019-06-09 19:33:20 +00:00
|
|
|
// Respawn
|
2019-10-15 04:06:14 +00:00
|
|
|
if inputs.respawn {
|
2019-08-25 14:49:54 +00:00
|
|
|
server_emitter.emit(ServerEvent::Respawn(entity));
|
2019-06-09 19:33:20 +00:00
|
|
|
}
|
2019-06-09 14:20:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-08-23 10:11:37 +00:00
|
|
|
// Move
|
2019-10-15 04:06:14 +00:00
|
|
|
inputs.move_dir = if inputs.move_dir.magnitude_squared() > 1.0 {
|
|
|
|
inputs.move_dir.normalized()
|
2019-08-23 10:11:37 +00:00
|
|
|
} else {
|
2019-10-15 04:06:14 +00:00
|
|
|
inputs.move_dir
|
2019-08-23 10:11:37 +00:00
|
|
|
};
|
|
|
|
|
2019-10-15 04:06:14 +00:00
|
|
|
if character.movement == Stand && inputs.move_dir.magnitude_squared() > 0.0 {
|
2019-08-23 10:11:37 +00:00
|
|
|
character.movement = Run;
|
2019-10-15 04:06:14 +00:00
|
|
|
} else if character.movement == Run && inputs.move_dir.magnitude_squared() == 0.0 {
|
2019-08-23 10:11:37 +00:00
|
|
|
character.movement = Stand;
|
2019-06-16 15:40:47 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 19:12:54 +00:00
|
|
|
// Look
|
2019-10-15 04:06:14 +00:00
|
|
|
inputs.look_dir = inputs
|
2019-08-24 19:12:54 +00:00
|
|
|
.look_dir
|
2019-08-25 16:08:00 +00:00
|
|
|
.try_normalized()
|
2019-10-15 04:06:14 +00:00
|
|
|
.unwrap_or(inputs.move_dir.into());
|
2019-08-24 19:12:54 +00:00
|
|
|
|
2019-06-09 19:33:20 +00:00
|
|
|
// Glide
|
2019-08-25 19:14:10 +00:00
|
|
|
// TODO: Check for glide ability/item
|
2019-10-15 04:06:14 +00:00
|
|
|
if inputs.glide
|
2019-08-23 10:11:37 +00:00
|
|
|
&& !physics.on_ground
|
|
|
|
&& (character.action == Idle || character.action.is_wield())
|
|
|
|
&& character.movement == Jump
|
|
|
|
&& body.is_humanoid()
|
2019-08-04 08:21:29 +00:00
|
|
|
{
|
2019-08-23 10:11:37 +00:00
|
|
|
character.movement = Glide;
|
2019-10-15 04:06:14 +00:00
|
|
|
} else if !inputs.glide && character.movement == Glide {
|
2019-08-23 10:11:37 +00:00
|
|
|
character.movement = Jump;
|
2019-06-09 14:20:20 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 19:11:40 +00:00
|
|
|
// Sit
|
2019-10-15 04:06:14 +00:00
|
|
|
if inputs.sit
|
2019-09-09 19:11:40 +00:00
|
|
|
&& physics.on_ground
|
|
|
|
&& character.action == Idle
|
|
|
|
&& character.movement != Sit
|
|
|
|
&& body.is_humanoid()
|
|
|
|
{
|
|
|
|
character.movement = Sit;
|
|
|
|
} else if character.movement == Sit
|
2019-10-15 04:06:14 +00:00
|
|
|
&& (inputs.move_dir.magnitude_squared() > 0.0 || !physics.on_ground)
|
2019-09-09 19:11:40 +00:00
|
|
|
{
|
|
|
|
character.movement = Run;
|
|
|
|
}
|
|
|
|
|
2019-07-06 19:00:05 +00:00
|
|
|
// Wield
|
2019-10-15 04:06:14 +00:00
|
|
|
if inputs.primary
|
2019-08-23 10:11:37 +00:00
|
|
|
&& character.action == Idle
|
|
|
|
&& (character.movement == Stand || character.movement == Run)
|
|
|
|
{
|
|
|
|
character.action = Wield {
|
2019-08-26 18:05:30 +00:00
|
|
|
time_left: WIELD_DURATION,
|
2019-08-23 10:11:37 +00:00
|
|
|
};
|
2019-07-06 19:00:05 +00:00
|
|
|
}
|
|
|
|
|
2019-08-28 20:47:52 +00:00
|
|
|
match stats.equipment.main {
|
2019-09-16 15:58:40 +00:00
|
|
|
Some(Item::Tool {
|
|
|
|
kind: item::Tool::Bow,
|
2019-10-17 20:59:36 +00:00
|
|
|
power,
|
2019-09-16 15:58:40 +00:00
|
|
|
..
|
|
|
|
}) => {
|
2019-10-15 04:06:14 +00:00
|
|
|
if inputs.primary
|
2019-09-16 15:58:40 +00:00
|
|
|
&& (character.movement == Stand
|
|
|
|
|| character.movement == Run
|
|
|
|
|| character.movement == Jump)
|
|
|
|
{
|
|
|
|
if let Wield { time_left } = character.action {
|
|
|
|
if time_left == Duration::default() {
|
|
|
|
// Immediately end the wield
|
|
|
|
character.action = Idle;
|
2019-09-28 19:35:28 +00:00
|
|
|
server_emitter.emit(ServerEvent::Shoot {
|
|
|
|
entity,
|
2019-10-15 04:06:14 +00:00
|
|
|
dir: inputs.look_dir,
|
2019-10-11 04:30:34 +00:00
|
|
|
body: comp::Body::Object(comp::object::Body::Arrow),
|
|
|
|
light: None,
|
2019-10-17 20:59:36 +00:00
|
|
|
gravity: Some(comp::Gravity(0.3)),
|
2019-09-28 19:35:28 +00:00
|
|
|
projectile: Projectile {
|
2019-10-17 20:59:36 +00:00
|
|
|
owner: *uid,
|
2019-09-28 19:35:28 +00:00
|
|
|
hit_ground: vec![projectile::Effect::Stick],
|
2019-09-29 08:37:07 +00:00
|
|
|
hit_wall: vec![projectile::Effect::Stick],
|
2019-09-28 19:35:28 +00:00
|
|
|
hit_entity: vec![
|
2019-10-17 20:59:36 +00:00
|
|
|
projectile::Effect::Damage(HealthChange {
|
|
|
|
amount: -(power as i32),
|
|
|
|
cause: HealthSource::Attack { by: *uid },
|
|
|
|
}),
|
2019-09-28 19:35:28 +00:00
|
|
|
projectile::Effect::Vanish,
|
|
|
|
],
|
2019-10-17 20:59:36 +00:00
|
|
|
time_left: Duration::from_secs(30),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(Item::Tool {
|
|
|
|
kind: item::Tool::Staff,
|
|
|
|
power,
|
|
|
|
..
|
|
|
|
}) => {
|
|
|
|
// Melee Attack
|
2019-10-15 04:06:14 +00:00
|
|
|
if inputs.primary
|
2019-10-17 20:59:36 +00:00
|
|
|
&& (character.movement == Stand
|
|
|
|
|| character.movement == Run
|
|
|
|
|| character.movement == Jump)
|
|
|
|
{
|
|
|
|
if let Wield { time_left } = character.action {
|
|
|
|
if time_left == Duration::default() {
|
|
|
|
character.action = Attack {
|
|
|
|
time_left: ATTACK_DURATION,
|
|
|
|
applied: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Magical Bolt
|
2019-10-15 04:06:14 +00:00
|
|
|
if inputs.secondary
|
2019-10-17 20:59:36 +00:00
|
|
|
&& (
|
|
|
|
character.movement == Stand
|
|
|
|
//|| character.movement == Run
|
|
|
|
//|| character.movement == Jump
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if let Wield { time_left } = character.action {
|
|
|
|
if time_left == Duration::default() {
|
|
|
|
character.action = Attack {
|
|
|
|
time_left: ATTACK_DURATION,
|
|
|
|
applied: false,
|
|
|
|
};
|
|
|
|
server_emitter.emit(ServerEvent::Shoot {
|
|
|
|
entity,
|
2019-10-15 04:06:14 +00:00
|
|
|
dir: inputs.look_dir,
|
2019-10-17 20:59:36 +00:00
|
|
|
body: comp::Body::Object(comp::object::Body::BoltFire),
|
|
|
|
gravity: Some(comp::Gravity(0.0)),
|
|
|
|
light: Some(comp::LightEmitter {
|
|
|
|
col: (0.72, 0.11, 0.11).into(),
|
|
|
|
strength: 10.0,
|
|
|
|
offset: Vec3::new(0.0, -5.0, 2.0),
|
|
|
|
}),
|
|
|
|
projectile: Projectile {
|
|
|
|
owner: *uid,
|
|
|
|
hit_ground: vec![projectile::Effect::Vanish],
|
|
|
|
hit_wall: vec![projectile::Effect::Vanish],
|
|
|
|
hit_entity: vec![
|
|
|
|
projectile::Effect::Damage(HealthChange {
|
|
|
|
amount: -(power as i32),
|
|
|
|
cause: HealthSource::Attack { by: *uid },
|
|
|
|
}),
|
|
|
|
projectile::Effect::Vanish,
|
|
|
|
],
|
|
|
|
time_left: Duration::from_secs(5),
|
2019-09-28 19:35:28 +00:00
|
|
|
},
|
|
|
|
});
|
2019-09-16 15:58:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-28 20:47:52 +00:00
|
|
|
Some(Item::Tool { .. }) => {
|
2019-10-23 19:40:45 +00:00
|
|
|
// Melee Attack
|
2019-10-15 04:06:14 +00:00
|
|
|
if inputs.primary
|
2019-08-28 20:47:52 +00:00
|
|
|
&& (character.movement == Stand
|
|
|
|
|| character.movement == Run
|
|
|
|
|| character.movement == Jump)
|
|
|
|
{
|
|
|
|
if let Wield { time_left } = character.action {
|
|
|
|
if time_left == Duration::default() {
|
|
|
|
character.action = Attack {
|
|
|
|
time_left: ATTACK_DURATION,
|
|
|
|
applied: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Block
|
2019-10-15 04:06:14 +00:00
|
|
|
if inputs.secondary
|
2019-08-28 20:47:52 +00:00
|
|
|
&& (character.movement == Stand || character.movement == Run)
|
2019-10-08 19:19:16 +00:00
|
|
|
&& character.action.is_wield()
|
2019-08-28 20:47:52 +00:00
|
|
|
{
|
|
|
|
character.action = Block {
|
|
|
|
time_left: Duration::from_secs(5),
|
2019-08-23 10:11:37 +00:00
|
|
|
};
|
2019-10-15 04:06:14 +00:00
|
|
|
} else if !inputs.secondary && character.action.is_block() {
|
2019-10-08 19:19:16 +00:00
|
|
|
character.action = Wield {
|
|
|
|
time_left: Duration::default(),
|
|
|
|
};
|
2019-08-23 10:11:37 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-29 17:42:25 +00:00
|
|
|
Some(Item::Debug(item::Debug::Boost)) => {
|
2019-10-15 04:06:14 +00:00
|
|
|
if inputs.primary {
|
2019-08-28 20:47:52 +00:00
|
|
|
local_emitter.emit(LocalEvent::Boost {
|
|
|
|
entity,
|
2019-10-15 04:06:14 +00:00
|
|
|
vel: inputs.look_dir * 7.0,
|
2019-08-29 17:39:34 +00:00
|
|
|
});
|
|
|
|
}
|
2019-10-15 04:06:14 +00:00
|
|
|
if inputs.secondary {
|
2019-08-29 17:39:34 +00:00
|
|
|
// Go upward
|
|
|
|
local_emitter.emit(LocalEvent::Boost {
|
|
|
|
entity,
|
2019-08-30 22:08:44 +00:00
|
|
|
vel: Vec3::new(0.0, 0.0, 7.0),
|
2019-08-28 20:47:52 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2019-10-11 04:30:34 +00:00
|
|
|
Some(Item::Debug(item::Debug::Possess)) => {
|
2019-10-15 04:06:14 +00:00
|
|
|
if inputs.primary
|
2019-10-11 04:30:34 +00:00
|
|
|
&& (character.movement == Stand
|
|
|
|
|| character.movement == Run
|
|
|
|
|| character.movement == Jump)
|
|
|
|
{
|
|
|
|
if let Wield { time_left } = character.action {
|
|
|
|
if time_left == Duration::default() {
|
|
|
|
// Immediately end the wield
|
|
|
|
character.action = Idle;
|
|
|
|
server_emitter.emit(ServerEvent::Shoot {
|
|
|
|
entity,
|
2019-10-17 20:59:36 +00:00
|
|
|
gravity: Some(comp::Gravity(0.1)),
|
2019-10-15 04:06:14 +00:00
|
|
|
dir: inputs.look_dir,
|
2019-10-17 20:59:36 +00:00
|
|
|
body: comp::Body::Object(comp::object::Body::ArrowSnake),
|
2019-10-11 04:30:34 +00:00
|
|
|
light: Some(comp::LightEmitter {
|
|
|
|
col: (0.0, 1.0, 0.3).into(),
|
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
projectile: Projectile {
|
2019-10-17 20:59:36 +00:00
|
|
|
owner: *uid,
|
|
|
|
hit_ground: vec![projectile::Effect::Stick],
|
|
|
|
hit_wall: vec![projectile::Effect::Stick],
|
2019-10-11 23:30:05 +00:00
|
|
|
hit_entity: vec![
|
2019-10-17 20:59:36 +00:00
|
|
|
projectile::Effect::Stick,
|
2019-10-11 23:30:05 +00:00
|
|
|
projectile::Effect::Possess,
|
|
|
|
],
|
2019-10-17 20:59:36 +00:00
|
|
|
time_left: Duration::from_secs(10),
|
2019-10-11 04:30:34 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-17 20:59:36 +00:00
|
|
|
// Block
|
2019-10-15 04:06:14 +00:00
|
|
|
if inputs.secondary
|
2019-10-17 20:59:36 +00:00
|
|
|
&& (character.movement == Stand || character.movement == Run)
|
|
|
|
&& character.action.is_wield()
|
|
|
|
{
|
|
|
|
character.action = Block {
|
|
|
|
time_left: Duration::from_secs(5),
|
|
|
|
};
|
2019-10-15 04:06:14 +00:00
|
|
|
} else if !inputs.secondary && character.action.is_block() {
|
2019-10-17 20:59:36 +00:00
|
|
|
character.action = Wield {
|
|
|
|
time_left: Duration::default(),
|
|
|
|
};
|
|
|
|
}
|
2019-10-11 04:30:34 +00:00
|
|
|
}
|
2019-09-07 09:46:30 +00:00
|
|
|
None => {
|
|
|
|
// Attack
|
2019-10-15 04:06:14 +00:00
|
|
|
if inputs.primary
|
2019-09-07 09:46:30 +00:00
|
|
|
&& (character.movement == Stand
|
|
|
|
|| character.movement == Run
|
|
|
|
|| character.movement == Jump)
|
|
|
|
&& !character.action.is_attack()
|
|
|
|
{
|
|
|
|
character.action = Attack {
|
|
|
|
time_left: ATTACK_DURATION,
|
|
|
|
applied: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2019-08-28 20:47:52 +00:00
|
|
|
_ => {}
|
2019-08-24 16:38:59 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 04:08:55 +00:00
|
|
|
// Roll
|
2019-10-15 04:06:14 +00:00
|
|
|
if inputs.roll
|
2019-08-23 10:11:37 +00:00
|
|
|
&& (character.action == Idle || character.action.is_wield())
|
|
|
|
&& character.movement == Run
|
|
|
|
&& physics.on_ground
|
2019-06-29 20:40:40 +00:00
|
|
|
{
|
2019-08-23 10:11:37 +00:00
|
|
|
character.movement = Roll {
|
2019-08-26 18:05:30 +00:00
|
|
|
time_left: ROLL_DURATION,
|
2019-08-23 10:11:37 +00:00
|
|
|
};
|
2019-06-30 17:48:38 +00:00
|
|
|
}
|
2019-08-25 16:48:12 +00:00
|
|
|
|
2019-06-30 17:48:38 +00:00
|
|
|
// Jump
|
2019-10-15 04:06:14 +00:00
|
|
|
if inputs.jump && physics.on_ground && vel.0.z <= 0.0 && !character.movement.is_roll() {
|
2019-08-25 14:49:54 +00:00
|
|
|
local_emitter.emit(LocalEvent::Jump(entity));
|
2019-06-11 04:08:55 +00:00
|
|
|
}
|
2019-09-09 19:11:40 +00:00
|
|
|
|
|
|
|
// Wall leap
|
2019-10-15 04:06:14 +00:00
|
|
|
if inputs.wall_leap {
|
2019-09-09 19:11:40 +00:00
|
|
|
if let (Some(_wall_dir), Climb) = (physics.on_wall, character.movement) {
|
|
|
|
//local_emitter.emit(LocalEvent::WallLeap { entity, wall_dir });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|