1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use crate::{
action_state::{ActionState, ActionStateDriver},
input_map::InputMap,
Actionlike,
};
use bevy::prelude::*;
pub fn tick_action_state<A: Actionlike>(mut query: Query<&mut ActionState<A>>, time: Res<Time>) {
for mut action_state in query.iter_mut() {
action_state.tick(
time.last_update()
.expect("The `Time` resource has never been updated!"),
);
}
}
pub fn update_action_state<A: Actionlike>(
gamepad_input_stream: Res<Input<GamepadButton>>,
keyboard_input_stream: Res<Input<KeyCode>>,
mouse_input_stream: Res<Input<MouseButton>>,
mut query: Query<(&mut ActionState<A>, &InputMap<A>)>,
) {
for (mut action_state, input_map) in query.iter_mut() {
action_state.update(
input_map,
&*gamepad_input_stream,
&*keyboard_input_stream,
&*mouse_input_stream,
);
}
}
pub fn update_action_state_from_interaction<A: Actionlike>(
ui_query: Query<(&Interaction, &ActionStateDriver<A>)>,
mut action_state_query: Query<&mut ActionState<A>>,
) {
for (&interaction, action_state_driver) in ui_query.iter() {
if interaction == Interaction::Clicked {
let mut action_state = action_state_query
.get_mut(action_state_driver.entity)
.expect("Entity does not exist, or does not have an `ActionState` component.");
action_state.press(action_state_driver.action);
}
}
}