diff --git a/client/src/lib.rs b/client/src/lib.rs index 7b4bb8dd8b..4e906bb765 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -307,11 +307,49 @@ impl Client { } } - pub fn swap_loadout(&mut self) { self.control_action(ControlAction::SwapLoadout); } + pub fn swap_loadout(&mut self) { + let can_swap = self + .state + .ecs() + .read_storage::() + .get(self.entity) + .map(|cs| cs.can_swap()); + match can_swap { + Some(true) => self.control_action(ControlAction::SwapLoadout), + Some(false) => {}, + None => warn!("Can't swap, client entity doesn't have a `CharacterState`"), + } + } - pub fn toggle_wield(&mut self) { self.control_action(ControlAction::ToggleWield); } + pub fn toggle_wield(&mut self) { + let is_wielding = self + .state + .ecs() + .read_storage::() + .get(self.entity) + .map(|cs| cs.is_wield()); - pub fn toggle_sit(&mut self) { self.control_action(ControlAction::ToggleSit); } + match is_wielding { + Some(true) => self.control_action(ControlAction::Unwield), + Some(false) => self.control_action(ControlAction::Wield), + None => warn!("Can't toggle wield, client entity doesn't have a `CharacterState`"), + } + } + + pub fn toggle_sit(&mut self) { + let is_sitting = self + .state + .ecs() + .read_storage::() + .get(self.entity) + .map(|cs| matches!(cs, comp::CharacterState::Sit)); + + match is_sitting { + Some(true) => self.control_action(ControlAction::Stand), + Some(false) => self.control_action(ControlAction::Sit), + None => warn!("Can't toggle sit, client entity doesn't have a `CharacterState`"), + } + } fn control_action(&mut self, control_action: ControlAction) { if let Some(controller) = self diff --git a/common/src/comp/controller.rs b/common/src/comp/controller.rs index 87d9e1577f..ef955fa0bd 100644 --- a/common/src/comp/controller.rs +++ b/common/src/comp/controller.rs @@ -18,8 +18,10 @@ pub enum ControlEvent { #[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] pub enum ControlAction { SwapLoadout, - ToggleWield, - ToggleSit, + Wield, + Unwield, + Sit, + Stand, } #[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)] diff --git a/common/src/states/idle.rs b/common/src/states/idle.rs index eb58dccf55..c403d43115 100644 --- a/common/src/states/idle.rs +++ b/common/src/states/idle.rs @@ -20,13 +20,13 @@ impl CharacterBehavior for Data { update } - fn toggle_wield(&self, data: &JoinData) -> StateUpdate { + fn wield(&self, data: &JoinData) -> StateUpdate { let mut update = StateUpdate::from(data); attempt_wield(data, &mut update); update } - fn toggle_sit(&self, data: &JoinData) -> StateUpdate { + fn sit(&self, data: &JoinData) -> StateUpdate { let mut update = StateUpdate::from(data); attempt_sit(data, &mut update); update diff --git a/common/src/states/sit.rs b/common/src/states/sit.rs index ad919a3993..8dd7f9471f 100644 --- a/common/src/states/sit.rs +++ b/common/src/states/sit.rs @@ -21,13 +21,13 @@ impl CharacterBehavior for Data { update } - fn toggle_wield(&self, data: &JoinData) -> StateUpdate { + fn wield(&self, data: &JoinData) -> StateUpdate { let mut update = StateUpdate::from(data); attempt_wield(data, &mut update); update } - fn toggle_sit(&self, data: &JoinData) -> StateUpdate { + fn stand(&self, data: &JoinData) -> StateUpdate { let mut update = StateUpdate::from(data); // Try to Fall/Stand up/Move update.character = CharacterState::Idle; diff --git a/common/src/states/wielding.rs b/common/src/states/wielding.rs index 9381c185e5..6babe4f85d 100644 --- a/common/src/states/wielding.rs +++ b/common/src/states/wielding.rs @@ -22,13 +22,13 @@ impl CharacterBehavior for Data { update } - fn toggle_sit(&self, data: &JoinData) -> StateUpdate { + fn sit(&self, data: &JoinData) -> StateUpdate { let mut update = StateUpdate::from(data); attempt_sit(data, &mut update); update } - fn toggle_wield(&self, data: &JoinData) -> StateUpdate { + fn unwield(&self, data: &JoinData) -> StateUpdate { let mut update = StateUpdate::from(data); update.character = CharacterState::Idle; update diff --git a/common/src/sys/character_behavior.rs b/common/src/sys/character_behavior.rs index e46fd63242..2dff358eae 100644 --- a/common/src/sys/character_behavior.rs +++ b/common/src/sys/character_behavior.rs @@ -17,13 +17,17 @@ 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 toggle_wield(&self, data: &JoinData) -> StateUpdate { StateUpdate::from(data) } - fn toggle_sit(&self, data: &JoinData) -> StateUpdate { StateUpdate::from(data) } + fn wield(&self, data: &JoinData) -> StateUpdate { StateUpdate::from(data) } + fn unwield(&self, data: &JoinData) -> StateUpdate { StateUpdate::from(data) } + fn sit(&self, data: &JoinData) -> StateUpdate { StateUpdate::from(data) } + fn stand(&self, data: &JoinData) -> StateUpdate { StateUpdate::from(data) } fn handle_event(&self, data: &JoinData, event: ControlAction) -> StateUpdate { match event { ControlAction::SwapLoadout => self.swap_loadout(data), - ControlAction::ToggleWield => self.toggle_wield(data), - ControlAction::ToggleSit => self.toggle_sit(data), + ControlAction::Wield => self.wield(data), + ControlAction::Unwield => self.unwield(data), + ControlAction::Sit => self.sit(data), + ControlAction::Stand => self.stand(data), } } // fn init(data: &JoinData) -> CharacterState;