diff --git a/src/dev_tools.rs b/src/dev_tools.rs index 2056ecb..e5a124f 100644 --- a/src/dev_tools.rs +++ b/src/dev_tools.rs @@ -1,29 +1,41 @@ use bevy::dev_tools::fps_overlay::FpsOverlayPlugin; use bevy::dev_tools::states::log_transitions; -use bevy::input::common_conditions::{input_just_pressed, input_toggle_active}; use bevy::prelude::*; use bevy_inspector_egui::bevy_egui::EguiPlugin; use bevy_inspector_egui::quick::WorldInspectorPlugin; +use crate::input::input_map::{Action, action_just_pressed}; use crate::screens::Screen; -const TOGGLE_KEY: KeyCode = KeyCode::Backquote; - pub fn plugin(app: &mut App) { app.add_plugins(( FpsOverlayPlugin::default(), EguiPlugin::default(), - WorldInspectorPlugin::new().run_if(input_toggle_active(false, TOGGLE_KEY)), + WorldInspectorPlugin::new().run_if(in_state(DebugMode(true))), )); app.add_systems(Update, log_transitions::); + app.init_state::(); + app.add_systems( Update, - toggle_debug_ui.run_if(input_just_pressed(TOGGLE_KEY)), + toggle_debug_mode.run_if(action_just_pressed(Action::DebugMode)), ); + + app.add_systems(Update, sync_ui_debug.run_if(state_changed::)); +} + +#[derive(States, PartialEq, Eq, Debug, Hash, Reflect, Clone, Default)] +pub struct DebugMode(pub bool); + +fn toggle_debug_mode( + debug_mode: Res>, + mut next_debug_mode: ResMut>, +) { + next_debug_mode.set(DebugMode(!debug_mode.0)); } -fn toggle_debug_ui(mut options: ResMut) { - options.toggle(); +fn sync_ui_debug(mut options: ResMut, debug_mode: Res>) { + options.enabled = debug_mode.0; } diff --git a/src/gameplay/hud/hotbar.rs b/src/gameplay/hud/hotbar.rs index 2f705cf..3fe48b2 100644 --- a/src/gameplay/hud/hotbar.rs +++ b/src/gameplay/hud/hotbar.rs @@ -1,25 +1,25 @@ use bevy::{ ecs::{spawn::SpawnIter, system::SystemParam}, - input::keyboard::KeyboardInput, prelude::*, }; use bevy_aseprite_ultra::prelude::*; use crate::{ gameplay::{FactorySystems, structure::assets::StructureDef, world::tilemap::TileClicked}, + input::input_map::{Action, InputActions}, screens::Screen, }; -const DIGIT_KEYS: [KeyCode; 9] = [ - KeyCode::Digit1, - KeyCode::Digit2, - KeyCode::Digit3, - KeyCode::Digit4, - KeyCode::Digit5, - KeyCode::Digit6, - KeyCode::Digit7, - KeyCode::Digit8, - KeyCode::Digit9, +const HOTBAR_ACTIONS: [Action; 9] = [ + Action::Hotbar1, + Action::Hotbar2, + Action::Hotbar3, + Action::Hotbar4, + Action::Hotbar5, + Action::Hotbar6, + Action::Hotbar7, + Action::Hotbar8, + Action::Hotbar9, ]; pub fn plugin(app: &mut App) { @@ -36,10 +36,7 @@ pub fn plugin(app: &mut App) { app.add_systems( Update, - ( - select_on_keyboard_shortcuts.run_if(on_message::), - highlight_selected_slot, - ), + (select_on_keyboard_shortcuts, highlight_selected_slot), ); app.add_systems( @@ -124,7 +121,7 @@ pub enum HotbarActionKind { #[derive(Component, Reflect, Debug)] #[reflect(Component)] -struct HotbarShortcut(KeyCode); +struct HotbarShortcut(Action); #[derive(Component, Reflect, Debug)] #[reflect(Component)] @@ -147,7 +144,7 @@ fn spawn_hotbar(mut commands: Commands) { ..default() }, Pickable::IGNORE, - Children::spawn(SpawnIter((0..DIGIT_KEYS.len()).map(|i| { + Children::spawn(SpawnIter((0..HOTBAR_ACTIONS.len()).map(|i| { ( Name::new(format!("Hotbar Slot {}", i + 1)), Node { @@ -162,7 +159,7 @@ fn spawn_hotbar(mut commands: Commands) { Pickable::default(), BorderColor::all(Color::WHITE), HotbarSlot, - HotbarShortcut(DIGIT_KEYS[i]), + HotbarShortcut(HOTBAR_ACTIONS[i]), ) }))), )); @@ -252,12 +249,12 @@ fn select_on_click( } fn select_on_keyboard_shortcuts( - keys: Res>, hotbar_slots: Query<(Entity, &HotbarShortcut), With>, + input_actions: Res, mut hotbar: HotbarSelector, ) { for (entity, shortcut) in hotbar_slots { - if keys.just_pressed(shortcut.0) { + if input_actions.just_pressed.contains(&shortcut.0) { hotbar.select(Some(entity)); } } diff --git a/src/gameplay/hud/inspect/mod.rs b/src/gameplay/hud/inspect/mod.rs index 6f984c3..9d03a5a 100644 --- a/src/gameplay/hud/inspect/mod.rs +++ b/src/gameplay/hud/inspect/mod.rs @@ -1,6 +1,9 @@ -use bevy::{input::common_conditions::input_just_pressed, prelude::*}; +use bevy::prelude::*; -use crate::gameplay::recipe::select::SelectedRecipe; +use crate::{ + gameplay::recipe::select::SelectedRecipe, + input::input_map::{Action, action_just_pressed}, +}; mod info; mod select; @@ -15,7 +18,7 @@ pub fn plugin(app: &mut App) { app.add_systems( Update, - close_menu.run_if(input_just_pressed(KeyCode::Escape)), + close_menu.run_if(action_just_pressed(Action::Dismiss)), ); } diff --git a/src/gameplay/hud/inventory/mod.rs b/src/gameplay/hud/inventory/mod.rs index 13fd7f8..67ecc16 100644 --- a/src/gameplay/hud/inventory/mod.rs +++ b/src/gameplay/hud/inventory/mod.rs @@ -1,10 +1,11 @@ use bevy::{ - input::common_conditions::input_just_pressed, prelude::*, ui::Checked, ui_widgets::{RadioButton, RadioGroup, ValueChange, observe}, }; +use crate::input::input_map::{Action, action_just_pressed}; + pub mod items; pub mod people; @@ -25,7 +26,7 @@ pub(super) fn plugin(app: &mut App) { app.add_systems( Update, ( - toggle_inventory_ui.run_if(input_just_pressed(KeyCode::KeyT)), + toggle_inventory_ui.run_if(action_just_pressed(Action::OpenTome)), update_tab_color, update_entry_color, ), diff --git a/src/gameplay/structure/interactable.rs b/src/gameplay/structure/interactable.rs index 2720201..0170ba0 100644 --- a/src/gameplay/structure/interactable.rs +++ b/src/gameplay/structure/interactable.rs @@ -1,9 +1,10 @@ use bevy::prelude::*; use bevy_aseprite_ultra::prelude::*; -use crate::assets::tracking::LoadResource; - -const INTERACTABLE_BUTTON: KeyCode = KeyCode::KeyE; +use crate::{ + assets::tracking::LoadResource, + input::input_map::{Action, InputMap, action_just_pressed}, +}; pub fn plugin(app: &mut App) { app.load_resource::(); @@ -13,7 +14,10 @@ pub fn plugin(app: &mut App) { app.add_observer(on_hover); app.add_observer(on_leave); - app.add_systems(Update, on_interact_button_click); + app.add_systems( + Update, + on_interact_button_click.run_if(action_just_pressed(Action::Interact)), + ); } #[derive(Component, Reflect, Default)] @@ -73,6 +77,7 @@ fn on_hover( mut commands: Commands, mut hovered_interactable: ResMut, interaction_assets: Res, + input_map: Res, ) { if !interactables.contains(pointer_over.entity) { return; @@ -85,7 +90,7 @@ fn on_hover( Transform::from_xyz(0.0, 0.0, 1.0), ChildOf(pointer_over.entity), ButtonIndicatorOf(pointer_over.entity), - interaction_assets.sprite(INTERACTABLE_BUTTON), + interaction_assets.sprite(*input_map.keymap.get(&Action::Interact).unwrap()), )); pointer_over.propagate(false); @@ -111,14 +116,9 @@ fn on_leave( } fn on_interact_button_click( - keys: Res>, hovered_interactable: Res, mut commands: Commands, ) { - if !keys.just_pressed(INTERACTABLE_BUTTON) { - return; - } - let Some(interactable) = hovered_interactable.0 else { return; }; diff --git a/src/gameplay/world/demolition.rs b/src/gameplay/world/demolition.rs index e46c113..0cf0c81 100644 --- a/src/gameplay/world/demolition.rs +++ b/src/gameplay/world/demolition.rs @@ -1,11 +1,12 @@ use std::collections::HashSet; -use bevy::{input::common_conditions::input_just_pressed, prelude::*}; +use bevy::prelude::*; -use crate::gameplay::{FactorySystems, world::tilemap::coord::Coord}; +use crate::{ + gameplay::{FactorySystems, world::tilemap::coord::Coord}, + input::input_map::{Action, InputActions, action_just_pressed}, +}; -pub const DEMOLISH_BUTTON: KeyCode = KeyCode::KeyF; -pub const DEMOLISH_CANCEL_BUTTON: KeyCode = KeyCode::Escape; pub const DEMOLISH_DURATION_SECS: f32 = 1.0; pub(super) fn plugin(app: &mut App) { @@ -14,20 +15,21 @@ pub(super) fn plugin(app: &mut App) { app.add_message::(); + app.add_observer(add_to_selection); + app.add_observer(remove_from_selection); + app.add_systems( - FixedUpdate, + Update, ( - add_to_selection.run_if(on_message::>), - remove_from_selection.run_if(on_message::>), - clear_selection.run_if(input_just_pressed(DEMOLISH_CANCEL_BUTTON)), + clear_selection.run_if(action_just_pressed(Action::Dismiss)), + tick_demolish_timer, ) - .in_set(FactorySystems::Demolish), + .chain(), ); app.add_systems( FixedUpdate, ( - tick_demolish_timer, highlight_demolition, demolish_selection.run_if(demolish_timer_finished), ) @@ -63,48 +65,40 @@ impl Default for DemolishTimer { fn tick_demolish_timer( mut timer: ResMut, - keys: Res>, time: Res