Skip to content

Commit

Permalink
Store only a single Hashmap in ActionState
Browse files Browse the repository at this point in the history
  • Loading branch information
alice-i-cecile committed Aug 6, 2024
1 parent acff1a9 commit b808d68
Show file tree
Hide file tree
Showing 6 changed files with 279 additions and 254 deletions.
15 changes: 8 additions & 7 deletions benches/action_state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use bevy::{prelude::Reflect, utils::HashMap};
use criterion::{criterion_group, criterion_main, Criterion};
use leafwing_input_manager::{input_map::UpdatedActions, prelude::ActionState, Actionlike};
use leafwing_input_manager::{
input_map::{UpdatedActions, UpdatedValue},
prelude::ActionState,
Actionlike,
};

#[derive(Actionlike, Debug, Clone, Copy, PartialEq, Eq, Hash, Reflect)]
enum TestAction {
Expand Down Expand Up @@ -54,14 +58,11 @@ fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("released", |b| b.iter(|| released(&action_state)));
c.bench_function("just_released", |b| b.iter(|| just_released(&action_state)));

let button_actions: HashMap<TestAction, bool> = TestAction::variants()
.map(|action| (action, true))
let button_actions: HashMap<TestAction, UpdatedValue> = TestAction::variants()
.map(|action| (action, UpdatedValue::Button(true)))
.collect();

let updated_actions = UpdatedActions {
button_actions,
..Default::default()
};
let updated_actions = UpdatedActions(button_actions);

c.bench_function("update", |b| {
b.iter(|| update(action_state.clone(), updated_actions.clone()))
Expand Down
59 changes: 59 additions & 0 deletions src/action_state/action_data.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Contains types used to store the state of the actions held in an [`ActionState`](super::ActionState).

use std::time::Instant;

use bevy::{math::Vec2, reflect::Reflect};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -33,6 +35,23 @@ impl ActionData {
},
}
}

/// Ticks the action data, updating the state of the action.
pub fn tick(&mut self, _current_instant: Instant, _previous_instant: Instant) {
match self.kind_data {
ActionKindData::Button(ref mut data) => {
data.state.tick();

#[cfg(feature = "timing")]
// Durations should not advance while actions are consumed
if !data.consumed {
data.timing.tick(_current_instant, _previous_instant);
}
}
ActionKindData::Axis(ref mut _data) => {}
ActionKindData::DualAxis(ref mut _data) => {}
}
}
}

/// A wrapper over the various forms of data that an action can take.
Expand All @@ -46,6 +65,46 @@ pub enum ActionKindData {
DualAxis(DualAxisData),
}

impl ActionKindData {
pub(super) fn swap_to_update_state(&mut self) {
// save the changes applied to `state` into `fixed_update_state`
// switch to loading the `update_state` into `state`
match self {
Self::Button(data) => {
data.fixed_update_state = data.state;
data.state = data.update_state;
}
Self::Axis(data) => {
data.fixed_update_value = data.value;
data.value = data.update_value;
}
Self::DualAxis(data) => {
data.fixed_update_pair = data.pair;
data.pair = data.update_pair;
}
}
}

pub(super) fn swap_to_fixed_update_state(&mut self) {
// save the changes applied to `state` into `update_state`
// switch to loading the `fixed_update_state` into `state`
match self {
Self::Button(data) => {
data.update_state = data.state;
data.state = data.fixed_update_state;
}
Self::Axis(data) => {
data.update_value = data.value;
data.value = data.fixed_update_value;
}
Self::DualAxis(data) => {
data.update_pair = data.pair;
data.pair = data.fixed_update_pair;
}
}
}
}

/// Metadata about an [`Buttonlike`](crate::user_input::Buttonlike) action
///
/// If a button is released, its `reasons_pressed` should be empty.
Expand Down
Loading

0 comments on commit b808d68

Please sign in to comment.