clean up based on pr review comment

This commit is contained in:
Adam Whitehurst 2020-03-21 15:55:20 -07:00
parent 72278d0c07
commit 650f53eaeb
9 changed files with 46 additions and 74 deletions

@ -3,7 +3,8 @@ use crate::{
states::*, states::*,
sys::character_behavior::JoinData, sys::character_behavior::JoinData,
}; };
use specs::{Component, DenseVecStorage, FlaggedStorage, HashMapStorage}; use specs::{Component, FlaggedStorage, HashMapStorage};
use specs_idvs::IDVStorage;
use std::time::Duration; use std::time::Duration;
#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)] #[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
@ -40,7 +41,9 @@ pub enum CharacterAbility {
} }
impl CharacterAbility { impl CharacterAbility {
pub fn test_requirements(&self, data: &JoinData, update: &mut StateUpdate) -> bool { /// Attempts to fulfill requirements, mutating `update` (taking energy) if
/// applicable.
pub fn requirements_paid(&self, data: &JoinData, update: &mut StateUpdate) -> bool {
match self { match self {
CharacterAbility::Roll => { CharacterAbility::Roll => {
data.physics.on_ground data.physics.on_ground
@ -64,7 +67,7 @@ impl CharacterAbility {
} }
impl Component for CharacterAbility { impl Component for CharacterAbility {
type Storage = DenseVecStorage<Self>; type Storage = IDVStorage<Self>;
} }
#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)] #[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
@ -159,5 +162,5 @@ impl From<&CharacterAbility> for CharacterState {
} }
impl Component for Loadout { impl Component for Loadout {
type Storage = FlaggedStorage<Self, HashMapStorage<Self>>; type Storage = FlaggedStorage<Self, IDVStorage<Self>>;
} }

@ -112,8 +112,6 @@ impl Default for Input {
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] #[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct ControllerInputs { pub struct ControllerInputs {
// When adding new inputs:
// 1. Add to tick() update
pub primary: Input, pub primary: Input,
pub secondary: Input, pub secondary: Input,
pub sit: Input, pub sit: Input,
@ -155,24 +153,6 @@ impl ControllerInputs {
self.swap_loadout.tick(dt); self.swap_loadout.tick(dt);
self.charge.tick(dt); self.charge.tick(dt);
} }
/*
/// Updates `inputs.move_dir`.
pub fn update_move_dir(&mut self) {
self.move_dir = if self.move_dir.magnitude_squared() > 1.0 {
// Cap move_dir to 1
self.move_dir.normalized()
} else {
self.move_dir
};
}
/// Updates `inputs.look_dir`
pub fn update_look_dir(&mut self) {
self.look_dir
.try_normalized()
.unwrap_or(self.move_dir.into());
}*/
} }
impl Controller { impl Controller {

@ -46,16 +46,28 @@ pub enum SfxEvent {
} }
pub enum LocalEvent { pub enum LocalEvent {
/// Applies upward force to entity's `Vel`
Jump(EcsEntity), Jump(EcsEntity),
Knockback(EcsEntity), /// Applies the `force` + implicit upward force, in `dir` direction to
/// `entity`'s `Vel`
KnockUp {
entity: EcsEntity,
dir: Vec3<f32>,
force: f32,
},
/// Applies the `force`, in `dir` direction to `entity`'s `Vel`
ApplyForce {
entity: EcsEntity,
dir: Vec3<f32>,
force: f32,
},
/// Applies leaping force to `entity`'s `Vel` away from `wall_dir` direction
WallLeap { WallLeap {
entity: EcsEntity, entity: EcsEntity,
wall_dir: Vec3<f32>, wall_dir: Vec3<f32>,
}, },
Boost { /// Applies `vel` velocity to `entity`
entity: EcsEntity, Boost { entity: EcsEntity, vel: Vec3<f32> },
vel: Vec3<f32>,
},
} }
pub enum ServerEvent { pub enum ServerEvent {

@ -351,7 +351,6 @@ impl State {
let events = self.ecs.read_resource::<EventBus<LocalEvent>>().recv_all(); let events = self.ecs.read_resource::<EventBus<LocalEvent>>().recv_all();
for event in events { for event in events {
let mut velocities = self.ecs.write_storage::<comp::Vel>(); let mut velocities = self.ecs.write_storage::<comp::Vel>();
let mut orientations = self.ecs.write_storage::<comp::Ori>();
let mut controllers = self.ecs.write_storage::<comp::Controller>(); let mut controllers = self.ecs.write_storage::<comp::Controller>();
match event { match event {
LocalEvent::Jump(entity) => { LocalEvent::Jump(entity) => {
@ -359,12 +358,15 @@ impl State {
vel.0.z = HUMANOID_JUMP_ACCEL; vel.0.z = HUMANOID_JUMP_ACCEL;
} }
}, },
LocalEvent::Knockback(entity) => { LocalEvent::KnockUp { entity, dir, force } => {
if let Some(vel) = velocities.get_mut(entity) { if let Some(vel) = velocities.get_mut(entity) {
if let Some(ori) = orientations.get_mut(entity) { vel.0 = dir * force;
vel.0 = -ori.0 * 10.0; vel.0.z = HUMANOID_JUMP_ACCEL;
vel.0.z = HUMANOID_JUMP_ACCEL; }
} },
LocalEvent::ApplyForce { entity, dir, force } => {
if let Some(vel) = velocities.get_mut(entity) {
vel.0 = dir * force;
} }
}, },
LocalEvent::WallLeap { entity, wall_dir } => { LocalEvent::WallLeap { entity, wall_dir } => {

@ -48,12 +48,6 @@ impl CharacterBehavior for Data {
// If player stops holding input, // If player stops holding input,
if !data.inputs.primary.is_pressed() { if !data.inputs.primary.is_pressed() {
// // Done
// update.character = CharacterState::Wielding;
// // Make sure attack component is removed
// data.updater.remove::<Attacking>(data.entity);
// return update;
should_transition = false; should_transition = false;
} }

@ -105,14 +105,14 @@ pub fn attempt_wield(data: &JoinData, update: &mut StateUpdate) {
time_left: tool.equip_time(), time_left: tool.equip_time(),
}); });
} else { } else {
update.character = CharacterState::Idle {}; update.character = CharacterState::Idle;
}; };
} }
/// Checks that player can `Sit` and updates `CharacterState` if so /// Checks that player can `Sit` and updates `CharacterState` if so
pub fn handle_sit(data: &JoinData, update: &mut StateUpdate) { pub fn handle_sit(data: &JoinData, update: &mut StateUpdate) {
if data.inputs.sit.is_pressed() && data.physics.on_ground && data.body.is_humanoid() { if data.inputs.sit.is_pressed() && data.physics.on_ground && data.body.is_humanoid() {
update.character = CharacterState::Sit {}; update.character = CharacterState::Sit;
} }
} }
@ -125,7 +125,7 @@ pub fn handle_climb(data: &JoinData, update: &mut StateUpdate) {
&& data.body.is_humanoid() && data.body.is_humanoid()
&& update.energy.current() > 100 && update.energy.current() > 100
{ {
update.character = CharacterState::Climb {}; update.character = CharacterState::Climb;
} }
} }
@ -133,7 +133,7 @@ pub fn handle_climb(data: &JoinData, update: &mut StateUpdate) {
pub fn handle_unwield(data: &JoinData, update: &mut StateUpdate) { pub fn handle_unwield(data: &JoinData, update: &mut StateUpdate) {
if let CharacterState::Wielding { .. } = update.character { if let CharacterState::Wielding { .. } = update.character {
if data.inputs.toggle_wield.is_pressed() { if data.inputs.toggle_wield.is_pressed() {
update.character = CharacterState::Idle {}; update.character = CharacterState::Idle;
} }
} }
} }
@ -158,7 +158,7 @@ pub fn handle_glide(data: &JoinData, update: &mut StateUpdate) {
&& !data.physics.in_fluid && !data.physics.in_fluid
&& data.body.is_humanoid() && data.body.is_humanoid()
{ {
update.character = CharacterState::Glide {}; update.character = CharacterState::Glide;
} }
} }
} }
@ -181,7 +181,7 @@ pub fn handle_primary_input(data: &JoinData, update: &mut StateUpdate) {
.active_item .active_item
.as_ref() .as_ref()
.and_then(|i| i.primary_ability.as_ref()) .and_then(|i| i.primary_ability.as_ref())
.filter(|ability| ability.test_requirements(data, update)) .filter(|ability| ability.requirements_paid(data, update))
{ {
update.character = ability.into(); update.character = ability.into();
} }
@ -197,7 +197,7 @@ pub fn handle_secondary_input(data: &JoinData, update: &mut StateUpdate) {
.active_item .active_item
.as_ref() .as_ref()
.and_then(|i| i.secondary_ability.as_ref()) .and_then(|i| i.secondary_ability.as_ref())
.filter(|ability| ability.test_requirements(data, update)) .filter(|ability| ability.requirements_paid(data, update))
{ {
update.character = ability.into(); update.character = ability.into();
} }
@ -213,7 +213,7 @@ pub fn handle_dodge_input(data: &JoinData, update: &mut StateUpdate) {
.active_item .active_item
.as_ref() .as_ref()
.and_then(|i| i.dodge_ability.as_ref()) .and_then(|i| i.dodge_ability.as_ref())
.filter(|ability| ability.test_requirements(data, update)) .filter(|ability| ability.requirements_paid(data, update))
{ {
update.character = ability.into(); update.character = ability.into();
} }

@ -70,6 +70,7 @@ impl<'a> System<'a> for Sys {
controller.reset(); controller.reset();
//TODO: Make npcs have valid `look_dir` during all activities
let mut inputs = &mut controller.inputs; let mut inputs = &mut controller.inputs;
const AVG_FOLLOW_DIST: f32 = 6.0; const AVG_FOLLOW_DIST: f32 = 6.0;

@ -77,12 +77,9 @@ impl<'a> JoinData<'a> {
} }
} }
/// /// ## Character State System /// ## Character Behavior System
/// #### Calls updates to `CharacterState`s. Acts on tuples of ( /// Passes `JoinData` to `CharacterState`'s `behavior` handler fn's. Recieves a
/// `CharacterState`, `Pos`, `Vel`, and `Ori` ). /// `StateUpdate` in return and performs updates to ECS Components from that.
///
/// _System forms `CharacterEntityData` tuples and passes those to `ActionState`
/// `update()` fn, then does the same for `MoveState` `update`_
pub struct Sys; pub struct Sys;
impl<'a> System<'a> for Sys { impl<'a> System<'a> for Sys {
@ -176,7 +173,7 @@ impl<'a> System<'a> for Sys {
CharacterState::Climb => states::climb::Data.behavior(&j), CharacterState::Climb => states::climb::Data.behavior(&j),
CharacterState::Glide => states::glide::Data.behavior(&j), CharacterState::Glide => states::glide::Data.behavior(&j),
CharacterState::Sit => states::sit::Data::behavior(&states::sit::Data, &j), CharacterState::Sit => states::sit::Data::behavior(&states::sit::Data, &j),
CharacterState::BasicBlock => states::basic_block::Data.behavior(&j), CharacterState::BasicBlock => states::basic_block::Data.behavior(&j),
CharacterState::Roll(data) => data.behavior(&j), CharacterState::Roll(data) => data.behavior(&j),
CharacterState::Wielding => states::wielding::Data.behavior(&j), CharacterState::Wielding => states::wielding::Data.behavior(&j),
CharacterState::Equipping(data) => data.behavior(&j), CharacterState::Equipping(data) => data.behavior(&j),
@ -186,17 +183,6 @@ impl<'a> System<'a> for Sys {
CharacterState::Boost(data) => data.behavior(&j), CharacterState::Boost(data) => data.behavior(&j),
CharacterState::DashMelee(data) => data.behavior(&j), CharacterState::DashMelee(data) => data.behavior(&j),
CharacterState::TimedCombo(data) => data.behavior(&j), CharacterState::TimedCombo(data) => data.behavior(&j),
// Do not use default match.
// _ => StateUpdate {
// character: *j.character,
// pos: *j.pos,
// vel: *j.vel,
// ori: *j.ori,
// energy: *j.energy,
// local_events: VecDeque::new(),
// server_events: VecDeque::new(),
// },
}; };
*tuple.2 = state_update.character; *tuple.2 = state_update.character;

@ -3,12 +3,10 @@ use crate::{
Attacking, Body, CharacterState, Controller, HealthChange, HealthSource, Ori, Pos, Scale, Attacking, Body, CharacterState, Controller, HealthChange, HealthSource, Ori, Pos, Scale,
Stats, Stats,
}, },
event::{EventBus, LocalEvent, ServerEvent}, event::{EventBus, ServerEvent},
state::DeltaTime,
sync::Uid, sync::Uid,
}; };
use specs::{Entities, Join, Read, ReadStorage, System, WriteStorage}; use specs::{Entities, Join, Read, ReadStorage, System, WriteStorage};
// use std::time::Duration;
use vek::*; use vek::*;
const BLOCK_EFFICIENCY: f32 = 0.9; const BLOCK_EFFICIENCY: f32 = 0.9;
@ -23,8 +21,6 @@ impl<'a> System<'a> for Sys {
type SystemData = ( type SystemData = (
Entities<'a>, Entities<'a>,
Read<'a, EventBus<ServerEvent>>, Read<'a, EventBus<ServerEvent>>,
Read<'a, EventBus<LocalEvent>>,
Read<'a, DeltaTime>,
ReadStorage<'a, Uid>, ReadStorage<'a, Uid>,
ReadStorage<'a, Pos>, ReadStorage<'a, Pos>,
ReadStorage<'a, Ori>, ReadStorage<'a, Ori>,
@ -41,8 +37,6 @@ impl<'a> System<'a> for Sys {
( (
entities, entities,
server_bus, server_bus,
_local_bus,
_dt,
uids, uids,
positions, positions,
orientations, orientations,