* move swap loadout to handle_swap_loadout util fn

* impl From<JoinData> for StateUpdate
This commit is contained in:
Adam Whitehurst 2020-03-21 14:16:26 -07:00
parent 5361705ca7
commit 819c2767bc
19 changed files with 67 additions and 168 deletions

View File

@ -1,7 +1,8 @@
use crate::{
comp::{Energy, Ori, Pos, Vel},
comp::{Energy, Loadout, Ori, Pos, Vel},
event::{LocalEvent, ServerEvent},
states::*,
sys::character_behavior::JoinData,
};
use serde::{Deserialize, Serialize};
use specs::{Component, FlaggedStorage, HashMapStorage, VecStorage};
@ -14,10 +15,25 @@ pub struct StateUpdate {
pub vel: Vel,
pub ori: Ori,
pub energy: Energy,
pub loadout: Loadout,
pub local_events: VecDeque<LocalEvent>,
pub server_events: VecDeque<ServerEvent>,
}
impl From<&JoinData<'_>> for StateUpdate {
fn from(data: &JoinData) -> Self {
StateUpdate {
pos: *data.pos,
vel: *data.vel,
ori: *data.ori,
energy: *data.energy,
loadout: data.loadout.clone(),
character: data.character.clone(),
local_events: VecDeque::new(),
server_events: VecDeque::new(),
}
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum CharacterState {
Idle,

View File

@ -3,7 +3,6 @@ use crate::{
comp::StateUpdate,
sys::character_behavior::{CharacterBehavior, JoinData},
};
use std::collections::VecDeque;
// const BLOCK_ACCEL: f32 = 30.0;
// const BLOCK_SPEED: f32 = 75.0;
@ -13,15 +12,7 @@ pub struct Data;
impl CharacterBehavior for Data {
fn behavior(&self, data: &JoinData) -> StateUpdate {
let mut update = StateUpdate {
pos: *data.pos,
vel: *data.vel,
ori: *data.ori,
energy: *data.energy,
character: data.character.clone(),
local_events: VecDeque::new(),
server_events: VecDeque::new(),
};
let mut update = StateUpdate::from(data);
handle_move(&data, &mut update);

View File

@ -3,7 +3,7 @@ use crate::{
states::utils::*,
sys::character_behavior::*,
};
use std::{collections::VecDeque, time::Duration};
use std::time::Duration;
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize, Eq, Hash)]
pub struct Data {
@ -19,15 +19,7 @@ pub struct Data {
impl CharacterBehavior for Data {
fn behavior(&self, data: &JoinData) -> StateUpdate {
let mut update = StateUpdate {
pos: *data.pos,
vel: *data.vel,
ori: *data.ori,
energy: *data.energy,
character: data.character.clone(),
local_events: VecDeque::new(),
server_events: VecDeque::new(),
};
let mut update = StateUpdate::from(data);
handle_move(data, &mut update);

View File

@ -4,7 +4,7 @@ use crate::{
states::utils::*,
sys::character_behavior::*,
};
use std::{collections::VecDeque, time::Duration};
use std::time::Duration;
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Data {
@ -22,15 +22,7 @@ pub struct Data {
impl CharacterBehavior for Data {
fn behavior(&self, data: &JoinData) -> StateUpdate {
let mut update = StateUpdate {
pos: *data.pos,
vel: *data.vel,
ori: *data.ori,
energy: *data.energy,
character: data.character.clone(),
local_events: VecDeque::new(),
server_events: VecDeque::new(),
};
let mut update = StateUpdate::from(data);
handle_move(data, &mut update);
handle_jump(data, &mut update);

View File

@ -3,7 +3,7 @@ use crate::{
states::utils::*,
sys::character_behavior::*,
};
use std::{collections::VecDeque, time::Duration};
use std::time::Duration;
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Data {
@ -14,15 +14,7 @@ pub struct Data {
impl CharacterBehavior for Data {
fn behavior(&self, data: &JoinData) -> StateUpdate {
let mut update = StateUpdate {
pos: *data.pos,
vel: *data.vel,
ori: *data.ori,
energy: *data.energy,
character: data.character.clone(),
local_events: VecDeque::new(),
server_events: VecDeque::new(),
};
let mut update = StateUpdate::from(data);
handle_move(data, &mut update);

View File

@ -7,7 +7,6 @@ use crate::{
},
util::safe_slerp,
};
use std::collections::VecDeque;
use vek::{
vec::{Vec2, Vec3},
Lerp,
@ -21,15 +20,7 @@ pub struct Data;
impl CharacterBehavior for Data {
fn behavior(&self, data: &JoinData) -> StateUpdate {
let mut update = StateUpdate {
pos: *data.pos,
vel: *data.vel,
ori: *data.ori,
character: data.character.clone(),
energy: *data.energy,
local_events: VecDeque::new(),
server_events: VecDeque::new(),
};
let mut update = StateUpdate::from(data);
if let Err(_) = update.energy.try_change_by(-5, EnergySource::Climb) {
update.character = CharacterState::Idle {};

View File

@ -3,7 +3,7 @@ use crate::{
sys::character_behavior::*,
util::safe_slerp,
};
use std::{collections::VecDeque, time::Duration};
use std::time::Duration;
use vek::Vec3;
const DASH_SPEED: f32 = 19.0;
@ -23,15 +23,7 @@ pub struct Data {
impl CharacterBehavior for Data {
fn behavior(&self, data: &JoinData) -> StateUpdate {
let mut update = StateUpdate {
pos: *data.pos,
vel: *data.vel,
ori: *data.ori,
energy: *data.energy,
character: data.character.clone(),
local_events: VecDeque::new(),
server_events: VecDeque::new(),
};
let mut update = StateUpdate::from(data);
if self.initialize {
update.vel.0 = data.inputs.look_dir * 20.0;

View File

@ -3,7 +3,7 @@ use crate::{
comp::{CharacterState, StateUpdate},
sys::character_behavior::{CharacterBehavior, JoinData},
};
use std::{collections::VecDeque, time::Duration};
use std::time::Duration;
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize, Eq, Hash)]
pub struct Data {
@ -13,15 +13,7 @@ pub struct Data {
impl CharacterBehavior for Data {
fn behavior(&self, data: &JoinData) -> StateUpdate {
let mut update = StateUpdate {
character: data.character.clone(),
pos: *data.pos,
vel: *data.vel,
ori: *data.ori,
energy: *data.energy,
local_events: VecDeque::new(),
server_events: VecDeque::new(),
};
let mut update = StateUpdate::from(data);
handle_move(&data, &mut update);
handle_jump(&data, &mut update);

View File

@ -3,7 +3,6 @@ use crate::{
sys::character_behavior::{CharacterBehavior, JoinData},
util::safe_slerp,
};
use std::collections::VecDeque;
use vek::Vec2;
// Gravity is 9.81 * 4, so this makes gravity equal to .15
@ -16,15 +15,7 @@ pub struct Data;
impl CharacterBehavior for Data {
fn behavior(&self, data: &JoinData) -> StateUpdate {
let mut update = StateUpdate {
pos: *data.pos,
vel: *data.vel,
ori: *data.ori,
energy: *data.energy,
character: data.character.clone(),
local_events: VecDeque::new(),
server_events: VecDeque::new(),
};
let mut update = StateUpdate::from(data);
// If glide button isn't held or player is on ground, end glide
if !data.inputs.glide.is_pressed() || data.physics.on_ground {

View File

@ -3,21 +3,12 @@ use crate::{
comp::StateUpdate,
sys::character_behavior::{CharacterBehavior, JoinData},
};
use std::collections::VecDeque;
pub struct Data;
impl CharacterBehavior for Data {
fn behavior(&self, data: &JoinData) -> StateUpdate {
let mut update = StateUpdate {
character: data.character.clone(),
pos: *data.pos,
vel: *data.vel,
ori: *data.ori,
energy: *data.energy,
local_events: VecDeque::new(),
server_events: VecDeque::new(),
};
let mut update = StateUpdate::from(data);
handle_move(data, &mut update);
handle_jump(data, &mut update);

View File

@ -3,7 +3,7 @@ use crate::{
sys::character_behavior::{CharacterBehavior, JoinData},
util::safe_slerp,
};
use std::{collections::VecDeque, time::Duration};
use std::time::Duration;
use vek::Vec3;
const ROLL_SPEED: f32 = 17.0;
@ -15,15 +15,7 @@ pub struct Data {
impl CharacterBehavior for Data {
fn behavior(&self, data: &JoinData) -> StateUpdate {
let mut update = StateUpdate {
character: data.character.clone(),
pos: *data.pos,
vel: *data.vel,
ori: *data.ori,
energy: *data.energy,
local_events: VecDeque::new(),
server_events: VecDeque::new(),
};
let mut update = StateUpdate::from(data);
// Update velocity
update.vel.0 = Vec3::new(0.0, 0.0, update.vel.0.z)

View File

@ -3,22 +3,13 @@ use crate::{
comp::{CharacterState, StateUpdate},
sys::character_behavior::{CharacterBehavior, JoinData},
};
use std::collections::VecDeque;
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize, Eq, Hash)]
pub struct Data;
impl CharacterBehavior for Data {
fn behavior(&self, data: &JoinData) -> StateUpdate {
let mut update = StateUpdate {
character: data.character.clone(),
pos: *data.pos,
vel: *data.vel,
ori: *data.ori,
energy: *data.energy,
local_events: VecDeque::new(),
server_events: VecDeque::new(),
};
let mut update = StateUpdate::from(data);
handle_wield(data, &mut update);

View File

@ -2,7 +2,7 @@ use crate::{
comp::{Attacking, CharacterState, EnergySource, StateUpdate},
sys::character_behavior::{CharacterBehavior, JoinData},
};
use std::{collections::VecDeque, time::Duration};
use std::time::Duration;
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize, Eq, Hash)]
pub struct Data {
/// Denotes what stage (of 3) the attack is in
@ -21,15 +21,7 @@ pub struct Data {
impl CharacterBehavior for Data {
fn behavior(&self, data: &JoinData) -> StateUpdate {
let mut update = StateUpdate {
pos: *data.pos,
vel: *data.vel,
ori: *data.ori,
energy: *data.energy,
character: data.character.clone(),
local_events: VecDeque::new(),
server_events: VecDeque::new(),
};
let mut update = StateUpdate::from(data);
let new_stage_time_active = self
.stage_time_active

View File

@ -3,7 +3,7 @@ use crate::{
states::utils::*,
sys::character_behavior::{CharacterBehavior, JoinData},
};
use std::{collections::VecDeque, time::Duration};
use std::time::Duration;
use vek::vec::Vec2;
// In millis
@ -34,15 +34,7 @@ pub struct Data {
impl CharacterBehavior for Data {
fn behavior(&self, data: &JoinData) -> StateUpdate {
let mut update = StateUpdate {
pos: *data.pos,
vel: *data.vel,
ori: *data.ori,
energy: *data.energy,
character: data.character.clone(),
local_events: VecDeque::new(),
server_events: VecDeque::new(),
};
let mut update= StateUpdate::from(data);
let stage_time_active = self
.stage_time_active

View File

@ -138,6 +138,18 @@ pub fn handle_unwield(data: &JoinData, update: &mut StateUpdate) {
}
}
/// Checks that player can Swap Weapons and updates `Loadout` if so
pub fn handle_swap_loadout(data: &JoinData, update: &mut StateUpdate) {
if let CharacterState::Wielding { .. } = update.character {
if data.inputs.swap_loadout.is_just_pressed() {
let mut new_loadout = data.loadout.clone();
new_loadout.active_item = data.loadout.second_item.clone();
new_loadout.second_item = data.loadout.active_item.clone();
update.loadout = new_loadout;
}
}
}
/// Checks that player can glide and updates `CharacterState` if so
pub fn handle_glide(data: &JoinData, update: &mut StateUpdate) {
if let CharacterState::Idle { .. } | CharacterState::Wielding { .. } = update.character {

View File

@ -3,21 +3,12 @@ use crate::{
comp::StateUpdate,
sys::character_behavior::{CharacterBehavior, JoinData},
};
use std::collections::VecDeque;
pub struct Data;
impl CharacterBehavior for Data {
fn behavior(&self, data: &JoinData) -> StateUpdate {
let mut update = StateUpdate {
character: data.character.clone(),
pos: *data.pos,
vel: *data.vel,
ori: *data.ori,
energy: *data.energy,
local_events: VecDeque::new(),
server_events: VecDeque::new(),
};
let mut update = StateUpdate::from(data);
handle_move(&data, &mut update);
handle_jump(&data, &mut update);
@ -25,6 +16,7 @@ impl CharacterBehavior for Data {
handle_climb(&data, &mut update);
handle_glide(&data, &mut update);
handle_unwield(&data, &mut update);
handle_swap_loadout(&data, &mut update);
handle_primary_input(&data, &mut update);
handle_secondary_input(&data, &mut update);
handle_dodge_input(&data, &mut update);

View File

@ -31,9 +31,9 @@ pub struct JoinData<'a> {
pub inputs: &'a ControllerInputs,
pub stats: &'a Stats,
pub energy: &'a Energy,
pub loadout: &'a Loadout,
pub body: &'a Body,
pub physics: &'a PhysicsState,
pub loadout: &'a Loadout,
pub attacking: Option<&'a Attacking>,
pub updater: &'a LazyUpdate,
}
@ -46,11 +46,11 @@ pub type JoinTuple<'a> = (
&'a mut Vel,
&'a mut Ori,
&'a mut Energy,
&'a mut Loadout,
&'a Controller,
&'a Stats,
&'a Body,
&'a PhysicsState,
&'a Loadout,
Option<&'a Attacking>,
);
@ -64,12 +64,12 @@ impl<'a> JoinData<'a> {
vel: j.4,
ori: j.5,
energy: j.6,
controller: j.7,
inputs: &j.7.inputs,
stats: j.8,
body: j.9,
physics: j.10,
loadout: j.11,
loadout: j.7,
controller: j.8,
inputs: &j.8.inputs,
stats: j.9,
body: j.10,
physics: j.11,
attacking: j.12,
updater,
dt,
@ -98,11 +98,11 @@ impl<'a> System<'a> for Sys {
WriteStorage<'a, Vel>,
WriteStorage<'a, Ori>,
WriteStorage<'a, Energy>,
WriteStorage<'a, Loadout>,
ReadStorage<'a, Controller>,
ReadStorage<'a, Stats>,
ReadStorage<'a, Body>,
ReadStorage<'a, PhysicsState>,
ReadStorage<'a, Loadout>,
ReadStorage<'a, Attacking>,
ReadStorage<'a, Uid>,
ReadStorage<'a, Mounting>,
@ -122,11 +122,11 @@ impl<'a> System<'a> for Sys {
mut velocities,
mut orientations,
mut energies,
mut loadouts,
controllers,
stats,
bodies,
physics_states,
loadouts,
attacking_storage,
uids,
mountings,
@ -140,11 +140,11 @@ impl<'a> System<'a> for Sys {
&mut velocities,
&mut orientations,
&mut energies,
&mut loadouts,
&controllers,
&stats,
&bodies,
&physics_states,
&loadouts,
attacking_storage.maybe(),
)
.join();
@ -204,6 +204,7 @@ impl<'a> System<'a> for Sys {
*tuple.4 = state_update.vel;
*tuple.5 = state_update.ori;
*tuple.6 = state_update.energy;
*tuple.7 = state_update.loadout;
local_bus.emitter().append(&mut state_update.local_events);
server_bus.emitter().append(&mut state_update.server_events);
}

View File

@ -323,21 +323,8 @@ impl PlayState for SessionState {
self.inputs.toggle_wield.set_state(state);
},
Event::InputUpdate(GameInput::SwapLoadout, state) => {
let mut client = self.client.borrow_mut();
let entity = client.entity();
let loadout = client
.state()
.read_storage::<comp::Loadout>()
.get(entity)
.cloned();
if let Some(loadout) = loadout {
let mut new_loadout = loadout.clone();
new_loadout.second_item = loadout.active_item;
new_loadout.active_item = loadout.second_item;
client.state_mut().write_component(entity, new_loadout);
}
println!("{:?}", state);
self.inputs.swap_loadout.set_state(state);
},
Event::InputUpdate(GameInput::Mount, true) => {
let mut client = self.client.borrow_mut();

View File

@ -101,7 +101,7 @@ impl Default for ControlSettings {
respawn: KeyMouse::Key(VirtualKeyCode::Space),
interact: KeyMouse::Mouse(MouseButton::Right),
toggle_wield: KeyMouse::Key(VirtualKeyCode::T),
swap_loadout: KeyMouse::Key(VirtualKeyCode::LAlt),
swap_loadout: KeyMouse::Key(VirtualKeyCode::Q),
charge: KeyMouse::Key(VirtualKeyCode::Key1),
}
}