split toggle events

This commit is contained in:
Imbris 2020-03-26 11:05:17 -04:00
parent 7d1f779c28
commit df5a7ef0e3
6 changed files with 59 additions and 15 deletions

View File

@ -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::<comp::CharacterState>()
.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::<comp::CharacterState>()
.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::<comp::CharacterState>()
.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

View File

@ -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)]

View File

@ -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

View File

@ -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;

View File

@ -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

View File

@ -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;