From 957717671a9e39080eac3c63b0d13283e927239f Mon Sep 17 00:00:00 2001 From: 2ndbeam <2ndbeam@disroot.org> Date: Tue, 3 Mar 2026 16:49:28 +0300 Subject: [PATCH] feat: Player is now using leafwing-input-manager --- src/input.rs | 47 ++++++++++++++++++++++++++++++++++++++--------- src/lib.rs | 25 +++++++++++++++++++++---- src/player.rs | 24 ++++++++++++------------ src/tests.rs | 2 -- 4 files changed, 71 insertions(+), 27 deletions(-) diff --git a/src/input.rs b/src/input.rs index c5cf8de..30052c1 100644 --- a/src/input.rs +++ b/src/input.rs @@ -1,8 +1,8 @@ use std::{any::{Any, TypeId}, collections::HashMap, hash::Hash, marker::PhantomData}; -use bevy::prelude::*; +use bevy::{prelude::*, reflect::GetTypeRegistration}; use bevy_common_assets::toml::TomlAssetPlugin; -use leafwing_input_manager::{Actionlike, prelude::{Axislike, Buttonlike, DualAxislike, InputMap}}; +use leafwing_input_manager::{Actionlike, plugin::InputManagerPlugin, prelude::*}; use serde::{Deserialize, Serialize, de::DeserializeOwned}; const INPUT_ASSET_EXTENSIONS: [&'static str; 1] = ["input.toml"]; @@ -205,13 +205,42 @@ impl From> for InputAsset } } -#[derive(Debug, Default)] -pub struct InputAssetPlugin (PhantomData); +#[derive(Resource, Deref)] +pub struct InputAssetHandle (Option>>); -impl Plugin for InputAssetPlugin -where T: Sized + Hash + Eq + Reflect + TypePath + Actionlike + DeserializeOwned -{ - fn build(&self, app: &mut App) { - app.add_plugins(TomlAssetPlugin::>::new(&INPUT_ASSET_EXTENSIONS)); +#[derive(Debug)] +pub struct InputAssetPlugin { + _phantom: PhantomData, + extensions: &'static [&'static str], +} + +impl InputAssetPlugin + where T: Sized + Hash + Eq + Reflect + TypePath + Actionlike + DeserializeOwned + GetTypeRegistration { + pub fn new(extensions: &'static [&'static str]) -> Self { + Self { + _phantom: PhantomData, + extensions, + } + } +} + +impl Default for InputAssetPlugin + where T: Sized + Hash + Eq + Reflect + TypePath + Actionlike + DeserializeOwned + GetTypeRegistration { + fn default() -> Self { + Self { + _phantom: PhantomData, + extensions: &INPUT_ASSET_EXTENSIONS, + } + } +} + +impl Plugin for InputAssetPlugin + where T: Sized + Hash + Eq + Reflect + TypePath + Actionlike + DeserializeOwned + GetTypeRegistration +{ + fn build(&self, app: &mut App) { + app.add_plugins(( + TomlAssetPlugin::>::new(&self.extensions), + InputManagerPlugin::::default() + )); } } diff --git a/src/lib.rs b/src/lib.rs index 1110136..bc30edc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,16 +5,32 @@ pub mod input; mod tests; use bevy::prelude::*; +use leafwing_input_manager::prelude::*; +use serde::{Deserialize, Serialize}; pub struct ExpeditionPlugin; -pub enum InputActions { - MoveLeft, - MoveRight, +#[derive(Actionlike, PartialEq, Eq, Hash, Debug, Clone, Reflect, Serialize, Deserialize)] +pub enum InputAction { + #[actionlike(Axis)] + Move, ToggleInventory, Interact, } +impl InputAction { + pub fn default_input_map() -> InputMap { + let input_map = InputMap::default() + .with_axis(Self::Move, VirtualAxis::ad()) + .with_axis(Self::Move, GamepadAxis::LeftStickX) + .with(Self::ToggleInventory, KeyCode::KeyI) + .with(Self::ToggleInventory, GamepadButton::Select) + .with(Self::Interact, KeyCode::KeyE) + .with(Self::Interact, GamepadButton::East); + input_map + } +} + fn camera_bundle() -> impl Bundle { ( Camera2d, @@ -38,7 +54,8 @@ fn setup_global(mut commands: Commands) { impl Plugin for ExpeditionPlugin { fn build(&self, app: &mut App) { - app.add_systems(Startup, (player::setup_player, setup_global)) + app.add_plugins(input::InputAssetPlugin::::default()) + .add_systems(Startup, (player::setup_player, setup_global)) .add_systems(Update, player::handle_input); } } diff --git a/src/player.rs b/src/player.rs index 42cb445..724d6a6 100644 --- a/src/player.rs +++ b/src/player.rs @@ -1,4 +1,7 @@ use bevy::prelude::*; +use leafwing_input_manager::prelude::*; + +use crate::InputAction as Action; #[derive(Component)] pub struct Player { @@ -14,6 +17,7 @@ fn player_bundle(asset_server: &Res) -> impl Bundle { }, Sprite::from_image(image), Transform::from_xyz(0f32, 0f32, 1f32), + Action::default_input_map(), ) } @@ -22,20 +26,16 @@ pub fn setup_player(mut commands: Commands, asset_server: Res) { } pub fn handle_input( - key_input: Res>, time: Res