clean up based on pr review comment

This commit is contained in:
Adam Whitehurst 2020-03-21 15:55:20 -07:00
parent b96d71a8c7
commit 862efb147c
9 changed files with 46 additions and 74 deletions

View File

@ -3,7 +3,8 @@ use crate::{
states::*,
sys::character_behavior::JoinData,
};
use specs::{Component, DenseVecStorage, FlaggedStorage, HashMapStorage};
use specs::{Component, FlaggedStorage, HashMapStorage};
use specs_idvs::IDVStorage;
use std::time::Duration;
#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
@ -40,7 +41,9 @@ pub enum 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 {
CharacterAbility::Roll => {
data.physics.on_ground
@ -64,7 +67,7 @@ impl CharacterAbility {
}
impl Component for CharacterAbility {
type Storage = DenseVecStorage<Self>;
type Storage = IDVStorage<Self>;
}
#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
@ -159,5 +162,5 @@ impl From<&CharacterAbility> for CharacterState {
}
impl Component for Loadout {
type Storage = FlaggedStorage<Self, HashMapStorage<Self>>;
type Storage = FlaggedStorage<Self, IDVStorage<Self>>;
}

View File

@ -112,8 +112,6 @@ impl Default for Input {
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct ControllerInputs {
// When adding new inputs:
// 1. Add to tick() update
pub primary: Input,
pub secondary: Input,
pub sit: Input,
@ -155,24 +153,6 @@ impl ControllerInputs {
self.swap_loadout.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 {

View File

@ -46,16 +46,28 @@ pub enum SfxEvent {
}
pub enum LocalEvent {
/// Applies upward force to entity's `Vel`
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 {
entity: EcsEntity,
wall_dir: Vec3<f32>,
},
Boost {
entity: EcsEntity,
vel: Vec3<f32>,
},
/// Applies `vel` velocity to `entity`
Boost { entity: EcsEntity, vel: Vec3<f32> },
}
pub enum ServerEvent {

View File

@ -351,7 +351,6 @@ impl State {
let events = self.ecs.read_resource::<EventBus<LocalEvent>>().recv_all();
for event in events {
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>();
match event {
LocalEvent::Jump(entity) => {
@ -359,12 +358,15 @@ impl State {
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(ori) = orientations.get_mut(entity) {
vel.0 = -ori.0 * 10.0;
vel.0.z = HUMANOID_JUMP_ACCEL;
}
vel.0 = dir * force;
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 } => {

View File

@ -48,12 +48,6 @@ impl CharacterBehavior for Data {
// If player stops holding input,
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;
}

View File

@ -105,14 +105,14 @@ pub fn attempt_wield(data: &JoinData, update: &mut StateUpdate) {
time_left: tool.equip_time(),
});
} else {
update.character = CharacterState::Idle {};
update.character = CharacterState::Idle;
};
}
/// Checks that player can `Sit` and updates `CharacterState` if so
pub fn handle_sit(data: &JoinData, update: &mut StateUpdate) {
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()
&& 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) {
if let CharacterState::Wielding { .. } = update.character {
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.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
.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();
}
@ -197,7 +197,7 @@ pub fn handle_secondary_input(data: &JoinData, update: &mut StateUpdate) {
.active_item
.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();
}
@ -213,7 +213,7 @@ pub fn handle_dodge_input(data: &JoinData, update: &mut StateUpdate) {
.active_item
.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();
}

View File

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

View File

@ -77,12 +77,9 @@ impl<'a> JoinData<'a> {
}
}
/// /// ## Character State System
/// #### Calls updates to `CharacterState`s. Acts on tuples of (
/// `CharacterState`, `Pos`, `Vel`, and `Ori` ).
///
/// _System forms `CharacterEntityData` tuples and passes those to `ActionState`
/// `update()` fn, then does the same for `MoveState` `update`_
/// ## Character Behavior System
/// Passes `JoinData` to `CharacterState`'s `behavior` handler fn's. Recieves a
/// `StateUpdate` in return and performs updates to ECS Components from that.
pub struct 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::Glide => states::glide::Data.behavior(&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::Wielding => states::wielding::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::DashMelee(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;

View File

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