From 45be7dd64d1c5980498a1ea69e2286edb48ad22c Mon Sep 17 00:00:00 2001 From: Sam Date: Sun, 7 Feb 2021 11:57:41 -0500 Subject: [PATCH] Eradicated clones --- client/src/lib.rs | 3 +-- common/src/comp/character_state.rs | 4 ++-- common/src/comp/controller.rs | 29 ++++++++++++++++++++++++++-- common/src/comp/mod.rs | 2 +- common/src/states/behavior.rs | 6 +++--- common/src/states/dance.rs | 6 +++--- common/src/states/glide_wield.rs | 6 +++--- common/src/states/idle.rs | 6 +++--- common/src/states/sit.rs | 6 +++--- common/src/states/sneak.rs | 6 +++--- common/src/states/talk.rs | 6 +++--- common/src/states/utils.rs | 6 +++--- common/src/states/wielding.rs | 6 +++--- common/sys/src/character_behavior.rs | 6 +++--- common/sys/src/controller.rs | 4 +++- 15 files changed, 64 insertions(+), 38 deletions(-) diff --git a/client/src/lib.rs b/client/src/lib.rs index 6fd280eeea..6d5502add7 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -896,8 +896,7 @@ impl Client { .write_storage::() .get_mut(self.entity) { - // TODO: Undo this clone, somehow... - controller.actions.push(control_action.clone()); + controller.actions.push(control_action); } self.send_msg(ClientGeneral::ControlAction(control_action)); } diff --git a/common/src/comp/character_state.rs b/common/src/comp/character_state.rs index f034304034..4cc2304526 100644 --- a/common/src/comp/character_state.rs +++ b/common/src/comp/character_state.rs @@ -1,6 +1,6 @@ use crate::{ combat::Attack, - comp::{Energy, InventoryManip, Ori, Pos, Vel}, + comp::{Energy, LoadoutManip, Ori, Pos, Vel}, event::{LocalEvent, ServerEvent}, states::{behavior::JoinData, *}, }; @@ -17,7 +17,7 @@ pub struct StateUpdate { pub ori: Ori, pub energy: Energy, pub swap_loadout: bool, - pub modify_loadout: Option, + pub modify_loadout: Option, pub local_events: VecDeque, pub server_events: VecDeque, } diff --git a/common/src/comp/controller.rs b/common/src/comp/controller.rs index 4549f2e244..21c3929c1b 100644 --- a/common/src/comp/controller.rs +++ b/common/src/comp/controller.rs @@ -22,6 +22,31 @@ pub enum InventoryManip { CraftRecipe(String), } +#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] +pub enum LoadoutManip { + Swap(Slot, Slot), + Drop(Slot), +} + +impl From for InventoryManip { + fn from(loadout_manip: LoadoutManip) -> Self { + match loadout_manip { + LoadoutManip::Swap(slot1, slot2) => Self::Swap(slot1, slot2), + LoadoutManip::Drop(slot) => Self::Drop(slot), + } + } +} + +impl From for Option { + fn from(inv_manip: InventoryManip) -> Self { + match inv_manip { + InventoryManip::Swap(slot1, slot2) => Some(LoadoutManip::Swap(slot1, slot2)), + InventoryManip::Drop(slot) => Some(LoadoutManip::Drop(slot)), + _ => None, + } + } +} + #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub enum GroupManip { Invite(Uid), @@ -46,10 +71,10 @@ pub enum ControlEvent { Respawn, } -#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] pub enum ControlAction { SwapLoadout, - ModifyLoadout(InventoryManip), + ModifyLoadout(Option), Wield, GlideWield, Unwield, diff --git a/common/src/comp/mod.rs b/common/src/comp/mod.rs index 72dad9fa85..b36ce811fb 100644 --- a/common/src/comp/mod.rs +++ b/common/src/comp/mod.rs @@ -46,7 +46,7 @@ pub use chat::{ }; pub use controller::{ Climb, ControlAction, ControlEvent, Controller, ControllerInputs, GroupManip, Input, - InventoryManip, MountState, Mounting, + InventoryManip, LoadoutManip, MountState, Mounting, }; pub use energy::{Energy, EnergyChange, EnergySource}; pub use group::Group; diff --git a/common/src/states/behavior.rs b/common/src/states/behavior.rs index 62734a72d8..260a5a4aa6 100644 --- a/common/src/states/behavior.rs +++ b/common/src/states/behavior.rs @@ -1,7 +1,7 @@ use crate::{ comp::{ Beam, Body, CharacterState, ControlAction, Controller, ControllerInputs, Energy, Health, - Inventory, InventoryManip, Melee, Ori, PhysicsState, Pos, StateUpdate, Stats, Vel, + Inventory, LoadoutManip, Melee, Ori, PhysicsState, Pos, StateUpdate, Stats, Vel, }, resources::DeltaTime, uid::Uid, @@ -17,7 +17,7 @@ pub trait CharacterBehavior { fn behavior(&self, data: &JoinData) -> StateUpdate; // Impl these to provide behavior for these inputs fn swap_loadout(&self, data: &JoinData) -> StateUpdate { StateUpdate::from(data) } - fn modify_loadout(&self, data: &JoinData, _inv_manip: InventoryManip) -> StateUpdate { + fn modify_loadout(&self, data: &JoinData, _loadout_manip: Option) -> StateUpdate { StateUpdate::from(data) } fn wield(&self, data: &JoinData) -> StateUpdate { StateUpdate::from(data) } @@ -31,7 +31,7 @@ pub trait CharacterBehavior { fn handle_event(&self, data: &JoinData, event: ControlAction) -> StateUpdate { match event { ControlAction::SwapLoadout => self.swap_loadout(data), - ControlAction::ModifyLoadout(inv_manip) => self.modify_loadout(data, inv_manip), + ControlAction::ModifyLoadout(loadout_manip) => self.modify_loadout(data, loadout_manip), ControlAction::Wield => self.wield(data), ControlAction::GlideWield => self.glide_wield(data), ControlAction::Unwield => self.unwield(data), diff --git a/common/src/states/dance.rs b/common/src/states/dance.rs index 8ff7ed6f0c..3c888c79a9 100644 --- a/common/src/states/dance.rs +++ b/common/src/states/dance.rs @@ -1,6 +1,6 @@ use super::utils::*; use crate::{ - comp::{CharacterState, InventoryManip, StateUpdate}, + comp::{CharacterState, LoadoutManip, StateUpdate}, states::behavior::{CharacterBehavior, JoinData}, }; use serde::{Deserialize, Serialize}; @@ -42,9 +42,9 @@ impl CharacterBehavior for Data { update } - fn modify_loadout(&self, data: &JoinData, inv_manip: InventoryManip) -> StateUpdate { + fn modify_loadout(&self, data: &JoinData, loadout_manip: Option) -> StateUpdate { let mut update = StateUpdate::from(data); - handle_modify_loadout(&mut update, inv_manip); + handle_modify_loadout(&mut update, loadout_manip); update } } diff --git a/common/src/states/glide_wield.rs b/common/src/states/glide_wield.rs index fc14ca5f9a..13c39b56f9 100644 --- a/common/src/states/glide_wield.rs +++ b/common/src/states/glide_wield.rs @@ -1,6 +1,6 @@ use super::utils::*; use crate::{ - comp::{slot::EquipSlot, CharacterState, EnergySource, InventoryManip, StateUpdate}, + comp::{slot::EquipSlot, CharacterState, EnergySource, LoadoutManip, StateUpdate}, states::behavior::{CharacterBehavior, JoinData}, }; @@ -69,9 +69,9 @@ impl CharacterBehavior for Data { update } - fn modify_loadout(&self, data: &JoinData, inv_manip: InventoryManip) -> StateUpdate { + fn modify_loadout(&self, data: &JoinData, loadout_manip: Option) -> StateUpdate { let mut update = StateUpdate::from(data); - handle_modify_loadout(&mut update, inv_manip); + handle_modify_loadout(&mut update, loadout_manip); update } } diff --git a/common/src/states/idle.rs b/common/src/states/idle.rs index 328e9ecdc0..e62b44455e 100644 --- a/common/src/states/idle.rs +++ b/common/src/states/idle.rs @@ -1,6 +1,6 @@ use super::utils::*; use crate::{ - comp::{InventoryManip, StateUpdate}, + comp::{LoadoutManip, StateUpdate}, states::behavior::{CharacterBehavior, JoinData}, }; @@ -61,9 +61,9 @@ impl CharacterBehavior for Data { update } - fn modify_loadout(&self, data: &JoinData, inv_manip: InventoryManip) -> StateUpdate { + fn modify_loadout(&self, data: &JoinData, loadout_manip: Option) -> StateUpdate { let mut update = StateUpdate::from(data); - handle_modify_loadout(&mut update, inv_manip); + handle_modify_loadout(&mut update, loadout_manip); update } } diff --git a/common/src/states/sit.rs b/common/src/states/sit.rs index e89aa1f571..e4476272c2 100644 --- a/common/src/states/sit.rs +++ b/common/src/states/sit.rs @@ -1,6 +1,6 @@ use super::utils::*; use crate::{ - comp::{CharacterState, InventoryManip, StateUpdate}, + comp::{CharacterState, LoadoutManip, StateUpdate}, states::behavior::{CharacterBehavior, JoinData}, }; use serde::{Deserialize, Serialize}; @@ -42,9 +42,9 @@ impl CharacterBehavior for Data { update } - fn modify_loadout(&self, data: &JoinData, inv_manip: InventoryManip) -> StateUpdate { + fn modify_loadout(&self, data: &JoinData, loadout_manip: Option) -> StateUpdate { let mut update = StateUpdate::from(data); - handle_modify_loadout(&mut update, inv_manip); + handle_modify_loadout(&mut update, loadout_manip); update } } diff --git a/common/src/states/sneak.rs b/common/src/states/sneak.rs index 50d3d18c58..dae6d72313 100644 --- a/common/src/states/sneak.rs +++ b/common/src/states/sneak.rs @@ -1,6 +1,6 @@ use super::utils::*; use crate::{ - comp::{CharacterState, InventoryManip, StateUpdate}, + comp::{CharacterState, LoadoutManip, StateUpdate}, states::behavior::{CharacterBehavior, JoinData}, }; @@ -60,9 +60,9 @@ impl CharacterBehavior for Data { update } - fn modify_loadout(&self, data: &JoinData, inv_manip: InventoryManip) -> StateUpdate { + fn modify_loadout(&self, data: &JoinData, loadout_manip: Option) -> StateUpdate { let mut update = StateUpdate::from(data); - handle_modify_loadout(&mut update, inv_manip); + handle_modify_loadout(&mut update, loadout_manip); update } } diff --git a/common/src/states/talk.rs b/common/src/states/talk.rs index e31e6447e5..ed4ebad006 100644 --- a/common/src/states/talk.rs +++ b/common/src/states/talk.rs @@ -1,6 +1,6 @@ use super::utils::*; use crate::{ - comp::{CharacterState, InventoryManip, StateUpdate}, + comp::{CharacterState, LoadoutManip, StateUpdate}, states::behavior::{CharacterBehavior, JoinData}, }; use serde::{Deserialize, Serialize}; @@ -47,9 +47,9 @@ impl CharacterBehavior for Data { update } - fn modify_loadout(&self, data: &JoinData, inv_manip: InventoryManip) -> StateUpdate { + fn modify_loadout(&self, data: &JoinData, loadout_manip: Option) -> StateUpdate { let mut update = StateUpdate::from(data); - handle_modify_loadout(&mut update, inv_manip); + handle_modify_loadout(&mut update, loadout_manip); update } } diff --git a/common/src/states/utils.rs b/common/src/states/utils.rs index 447c78286e..d743e1195c 100644 --- a/common/src/states/utils.rs +++ b/common/src/states/utils.rs @@ -4,7 +4,7 @@ use crate::{ item::{Hands, ItemKind, Tool, ToolKind}, quadruped_low, quadruped_medium, skills::{AxeSkill, BowSkill, HammerSkill, Skill, StaffSkill, SwordSkill}, - theropod, Body, CharacterState, InventoryManip, StateUpdate, + theropod, Body, CharacterState, LoadoutManip, StateUpdate, }, consts::{FRIC_GROUND, GRAVITY}, event::LocalEvent, @@ -360,8 +360,8 @@ pub fn attempt_swap_loadout(data: &JoinData, update: &mut StateUpdate) { } /// Handles inventory manipulations that affect the loadout -pub fn handle_modify_loadout(update: &mut StateUpdate, inv_manip: InventoryManip) { - update.modify_loadout = Some(inv_manip); +pub fn handle_modify_loadout(update: &mut StateUpdate, loadout_manip: Option) { + update.modify_loadout = loadout_manip; } /// Checks that player can wield the glider and updates `CharacterState` if so diff --git a/common/src/states/wielding.rs b/common/src/states/wielding.rs index a5aa3c1f92..0529a49df7 100644 --- a/common/src/states/wielding.rs +++ b/common/src/states/wielding.rs @@ -1,6 +1,6 @@ use super::utils::*; use crate::{ - comp::{CharacterState, InventoryManip, StateUpdate}, + comp::{CharacterState, LoadoutManip, StateUpdate}, states::behavior::{CharacterBehavior, JoinData}, }; @@ -57,9 +57,9 @@ impl CharacterBehavior for Data { update } - fn modify_loadout(&self, data: &JoinData, inv_manip: InventoryManip) -> StateUpdate { + fn modify_loadout(&self, data: &JoinData, loadout_manip: Option) -> StateUpdate { let mut update = StateUpdate::from(data); - handle_modify_loadout(&mut update, inv_manip); + handle_modify_loadout(&mut update, loadout_manip); update } } diff --git a/common/sys/src/character_behavior.rs b/common/sys/src/character_behavior.rs index 5ec6f5d6fb..50d77d9452 100644 --- a/common/sys/src/character_behavior.rs +++ b/common/sys/src/character_behavior.rs @@ -268,9 +268,9 @@ impl<'a> System<'a> for Sys { }; local_emitter.append(&mut state_update.local_events); server_emitter.append(&mut state_update.server_events); - if let Some(ref inv_manip) = state_update.modify_loadout { - // TODO: Delete this clone, somehow... - server_emitter.emit(ServerEvent::InventoryManip(j.entity, inv_manip.clone())); + if let Some(loadout_manip) = state_update.modify_loadout { + server_emitter + .emit(ServerEvent::InventoryManip(j.entity, loadout_manip.into())); } incorporate_update(&mut tuple, state_update); } diff --git a/common/sys/src/controller.rs b/common/sys/src/controller.rs index 6f550e60e9..334e9b6b03 100644 --- a/common/sys/src/controller.rs +++ b/common/sys/src/controller.rs @@ -105,7 +105,9 @@ impl<'a> System<'a> for Sys { InventoryManip::Drop(Slot::Equip(_)) | InventoryManip::Swap(_, Slot::Equip(_)) | InventoryManip::Swap(Slot::Equip(_), _) => { - controller.actions.push(ControlAction::ModifyLoadout(manip)); + controller + .actions + .push(ControlAction::ModifyLoadout(manip.into())); }, _ => { server_emitter.emit(ServerEvent::InventoryManip(entity, manip));