use bevy::prelude::*;
use leafwing_input_manager::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(InputManagerPlugin::<PlayerAction>::default())
.add_systems(Startup, spawn_player)
.add_systems(Update, use_actions)
.run();
}
#[derive(Actionlike, PartialEq, Eq, Clone, Copy, Hash, Debug, Reflect)]
enum PlayerAction {
#[actionlike(DualAxis)]
Run,
Jump,
UseItem,
}
impl PlayerAction {
fn default_input_map() -> InputMap<Self> {
let mut input_map = InputMap::default();
input_map.insert_dual_axis(Self::Run, GamepadStick::LEFT);
input_map.insert(Self::Jump, GamepadButton::South);
input_map.insert(Self::UseItem, GamepadButton::RightTrigger2);
input_map.insert_dual_axis(Self::Run, VirtualDPad::wasd());
input_map.insert(Self::Jump, KeyCode::Space);
input_map.insert(Self::UseItem, MouseButton::Left);
input_map
}
}
#[derive(Component)]
struct Player;
fn spawn_player(mut commands: Commands) {
commands
.spawn(PlayerAction::default_input_map())
.insert(Player);
}
fn use_actions(query: Query<&ActionState<PlayerAction>, With<Player>>) {
let action_state = query.single().expect("Player actions not found");
if action_state.axis_pair(&PlayerAction::Run) != Vec2::ZERO {
println!(
"Moving in direction {}",
action_state.clamped_axis_pair(&PlayerAction::Run)
);
}
if action_state.just_pressed(&PlayerAction::Jump) {
println!("Jumped!");
}
if action_state.just_pressed(&PlayerAction::UseItem) {
println!("Used an Item!");
}
}