About

A straightforward but robust input-action manager for Bevy.

Inputs from various input sources (keyboard, mouse and gamepad) are collected into a common ActionState on your player entity, which can be conveniently used in your game logic.

The mapping between inputs and actions is many-to-many, and easily configured and extended with the InputMap components on each player entity. A single action can be triggered by multiple inputs (or set directly by UI elements or gameplay logic), and a single input can result in multiple actions being triggered, which can be handled contextually.

Features

Limitations

Instructions

Development occurs on the dev branch, which is merged into main on each release. This ensures the examples are in-sync with the latest release.

Getting started

  1. Add leafwing-input-manager to your Cargo.toml.
  2. Create an enum of the logical actions you want to represent, and derive the Actionlike trait for it.
  3. Add the InputManagerPlugin to your App.
  4. Add the InputManagerBundle to your player entity (or entities!).
  5. Configure a mapping between your inputs and your actions by modifying the InputMap component on your player entity.
  6. Read the ActionState component on your player entity to check the collected input state!

```rust, ignore use bevy::prelude::; use leafwing_input_manager::prelude::;

fn main() { App::new() .addplugins(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 .addplugin(InputManagerPlugin::::default()) // The InputMap and ActionState components will be added to any entity with the Player component .addstartupsystem(spawnplayer) // Read the ActionState in your systems using queries! .addsystem(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)]

enum Action { Run, Jump, }

[derive(Component)]

struct Player;

fn spawnplayer(mut commands: Commands) { commands .spawn() .insert(Player) .insertbundle(InputManagerBundle:: { // Stores "which actions are currently pressed" actionstate: ActionState::default(), // Describes how to convert from player inputs into those actions inputmap: InputMap::new([(KeyCode::Space, Action::Jump)]), }); }

// Query for the ActionState component in your game logic systems! fn jump(query: Query<&ActionState, With>) { let actionstate = query.single(); // Each action has a button-like state of its own that you can check if actionstate.just_pressed(Action::Jump) { println!("I'm jumping!"); } } ```

This snippet is the minimal.rs example from the examples folder: check there for more in-depth learning materials!