Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions src/dev_tools.rs
Original file line number Diff line number Diff line change
@@ -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::<Screen>);

app.init_state::<DebugMode>();

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::<DebugMode>));
}

#[derive(States, PartialEq, Eq, Debug, Hash, Reflect, Clone, Default)]
pub struct DebugMode(pub bool);

fn toggle_debug_mode(
debug_mode: Res<State<DebugMode>>,
mut next_debug_mode: ResMut<NextState<DebugMode>>,
) {
next_debug_mode.set(DebugMode(!debug_mode.0));
}

fn toggle_debug_ui(mut options: ResMut<UiDebugOptions>) {
options.toggle();
fn sync_ui_debug(mut options: ResMut<UiDebugOptions>, debug_mode: Res<State<DebugMode>>) {
options.enabled = debug_mode.0;
}
37 changes: 17 additions & 20 deletions src/gameplay/hud/hotbar.rs
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -36,10 +36,7 @@ pub fn plugin(app: &mut App) {

app.add_systems(
Update,
(
select_on_keyboard_shortcuts.run_if(on_message::<KeyboardInput>),
highlight_selected_slot,
),
(select_on_keyboard_shortcuts, highlight_selected_slot),
);

app.add_systems(
Expand Down Expand Up @@ -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)]
Expand All @@ -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 {
Expand All @@ -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]),
)
}))),
));
Expand Down Expand Up @@ -252,12 +249,12 @@ fn select_on_click(
}

fn select_on_keyboard_shortcuts(
keys: Res<ButtonInput<KeyCode>>,
hotbar_slots: Query<(Entity, &HotbarShortcut), With<HotbarSlot>>,
input_actions: Res<InputActions>,
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));
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/gameplay/hud/inspect/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)),
);
}

Expand Down
5 changes: 3 additions & 2 deletions src/gameplay/hud/inventory/mod.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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,
),
Expand Down
20 changes: 10 additions & 10 deletions src/gameplay/structure/interactable.rs
Original file line number Diff line number Diff line change
@@ -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::<InteractionAssets>();
Expand All @@ -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)]
Expand Down Expand Up @@ -73,6 +77,7 @@ fn on_hover(
mut commands: Commands,
mut hovered_interactable: ResMut<HoveredInteractable>,
interaction_assets: Res<InteractionAssets>,
input_map: Res<InputMap>,
) {
if !interactables.contains(pointer_over.entity) {
return;
Expand All @@ -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);
Expand All @@ -111,14 +116,9 @@ fn on_leave(
}

fn on_interact_button_click(
keys: Res<ButtonInput<KeyCode>>,
hovered_interactable: Res<HoveredInteractable>,
mut commands: Commands,
) {
if !keys.just_pressed(INTERACTABLE_BUTTON) {
return;
}

let Some(interactable) = hovered_interactable.0 else {
return;
};
Expand Down
58 changes: 26 additions & 32 deletions src/gameplay/world/demolition.rs
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -14,20 +15,21 @@ pub(super) fn plugin(app: &mut App) {

app.add_message::<Demolished>();

app.add_observer(add_to_selection);
app.add_observer(remove_from_selection);

app.add_systems(
FixedUpdate,
Update,
(
add_to_selection.run_if(on_message::<Pointer<Over>>),
remove_from_selection.run_if(on_message::<Pointer<Out>>),
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),
)
Expand Down Expand Up @@ -63,48 +65,40 @@ impl Default for DemolishTimer {

fn tick_demolish_timer(
mut timer: ResMut<DemolishTimer>,
keys: Res<ButtonInput<KeyCode>>,
time: Res<Time>,
input_actions: Res<InputActions>,
) {
if keys.just_released(DEMOLISH_BUTTON) {
timer.reset();
}

if keys.pressed(DEMOLISH_BUTTON) {
if input_actions.pressed.contains(&Action::Demolish) {
timer.tick(time.delta());
} else {
timer.reset();
}
}

fn demolish_timer_finished(timer: Res<DemolishTimer>) -> bool {
timer.just_finished()
timer.is_finished()
}

fn add_to_selection(
mut pointer_overs: MessageReader<Pointer<Over>>,
mut selection: ResMut<DemolishSelection>,
pointer_over: On<Pointer<Over>>,
demolishables: Query<Entity, With<Demolishable>>,
mut selection: ResMut<DemolishSelection>,
) {
for pointer_over in pointer_overs.read() {
if !demolishables.contains(pointer_over.entity) {
continue;
}

if demolishables.contains(pointer_over.entity) {
selection.insert(pointer_over.entity);
}
}

fn remove_from_selection(
mut pointer_outs: MessageReader<Pointer<Out>>,
pointer_out: On<Pointer<Out>>,
input_actions: Res<InputActions>,
mut selection: ResMut<DemolishSelection>,
keys: Res<ButtonInput<KeyCode>>,
) {
for pointer_out in pointer_outs.read() {
if keys.pressed(KeyCode::ShiftLeft) {
continue;
}

selection.remove(&pointer_out.entity);
if input_actions.pressed.contains(&Action::MultiSelect) {
return;
}

selection.remove(&pointer_out.entity);
}

fn clear_selection(mut selection: ResMut<DemolishSelection>) {
Expand Down
Loading