forked from Leafwing-Studios/leafwing-input-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvirtual_dpad.rs
More file actions
58 lines (53 loc) · 2.17 KB
/
Copy pathvirtual_dpad.rs
File metadata and controls
58 lines (53 loc) · 2.17 KB
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
57
58
use bevy::prelude::*;
use leafwing_input_manager::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
// This plugin maps inputs to an input-type agnostic action-state
// We need to provide it with an enum which stores the possible actions a player could take
.add_plugin(InputManagerPlugin::<Action>::default())
// Spawn an entity with Player, InputMap, and ActionState components
.add_startup_system(spawn_player)
// Read the ActionState in your systems using queries!
.add_system(move_player)
.run();
}
#[derive(Actionlike, PartialEq, Eq, Clone, Copy, Hash, Debug)]
enum Action {
Move,
}
#[derive(Component)]
struct Player;
fn spawn_player(mut commands: Commands) {
commands
.spawn(InputManagerBundle::<Action> {
// Stores "which actions are currently activated"
// Map some arbitrary keys into a virtual direction pad that triggers our move action
input_map: InputMap::new([(
VirtualDPad {
up: KeyCode::W.into(),
down: KeyCode::S.into(),
left: KeyCode::A.into(),
right: KeyCode::D.into(),
},
Action::Move,
)])
.build(),
..default()
})
.insert(Player);
}
// Query for the `ActionState` component in your game logic systems!
fn move_player(query: Query<&ActionState<Action>, With<Player>>) {
let action_state = query.single();
// If any button in a virtual direction pad is pressed, then the action state is "pressed"
if action_state.pressed(Action::Move) {
// Virtual direction pads are one of the types which return an AxisPair. The values will be
// represented as `-1.0`, `0.0`, or `1.0` depending on the combination of buttons pressed.
let axis_pair = action_state.axis_pair(Action::Move).unwrap();
println!("Move:");
println!(" distance: {}", axis_pair.length());
println!(" x: {}", axis_pair.x());
println!(" y: {}", axis_pair.y());
}
}