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
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_plugins(InputManagerPlugin::<Action>::default())
// The InputMap and ActionState components will be added to any entity with the Player component
.add_systems(Startup, spawn_player)
// Read the ActionState in your systems using queries!
.add_systems(Update, jump)
.run();
}
// This is the list of "things in the game I want to be able to do based on input"
#[derive(Actionlike, PartialEq, Eq, Clone, Copy, Hash, Debug, Reflect)]
enum Action {
Run,
Jump,
}
#[derive(Component)]
struct Player;
fn spawn_player(mut commands: Commands) {
// Describes how to convert from player inputs into those actions
let input_map = InputMap::new([(Action::Jump, KeyCode::Space)]);
commands.spawn(input_map).insert(Player);
}
// Query for the `ActionState` component in your game logic systems!
fn jump(action_state: Single<&ActionState<Action>, With<Player>>) {
// Each action has a button-like state of its own that you can check
if action_state.just_pressed(&Action::Jump) {
println!("I'm jumping!");
}
}