refactor: Move status to hud

This commit is contained in:
demenik
2025-11-27 17:33:37 +01:00
parent 3e04c71e9d
commit 2a17d161f3
8 changed files with 48 additions and 45 deletions

View File

@@ -0,0 +1,20 @@
use crate::prelude::*;
#[derive(Component)]
pub enum RootMarker {
Status,
Settings,
}
#[derive(Component)]
pub enum TextType {
Phase,
Timer,
}
#[derive(Component)]
pub enum ButtonType {
SavegameDump,
SettingsOpen,
SettingsClose,
}

View File

@@ -1,23 +1,24 @@
use crate::features::savegame::messages::SavegameDumpMessage; use crate::features::savegame::messages::SavegameDumpMessage;
use crate::prelude::*; use crate::prelude::*;
use components::*;
pub struct StatusPlugin; pub mod components;
pub mod ui;
impl Plugin for StatusPlugin { pub struct HudPlugin;
impl Plugin for HudPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(OnEnter(AppState::GameScreen), setup); app.add_systems(OnEnter(AppState::GameScreen), setup);
app.add_systems(OnExit(AppState::GameScreen), cleanup); app.add_systems(OnExit(AppState::GameScreen), cleanup);
app.add_systems(Update, update_status.run_if(in_state(AppState::GameScreen))); app.add_systems(Update, update_status.run_if(in_state(AppState::GameScreen)));
app.add_systems( app.add_systems(Update, buttons.run_if(in_state(AppState::GameScreen)));
Update,
status_buttons.run_if(in_state(AppState::GameScreen)),
);
} }
} }
fn setup(mut commands: Commands) { fn setup(mut commands: Commands) {
commands.spawn(( commands.spawn((
UiStatusRootContainer, RootMarker::Status,
Node { Node {
position_type: PositionType::Absolute, position_type: PositionType::Absolute,
bottom: px(0), bottom: px(0),
@@ -34,23 +35,23 @@ fn setup(mut commands: Commands) {
BackgroundColor(Color::srgba(0.0, 0.0, 0.0, 0.8)), BackgroundColor(Color::srgba(0.0, 0.0, 0.0, 0.8)),
children![ children![
( (
UiStatusText::Phase, TextType::Phase,
Text::new("..."), Text::new("..."),
TextFont::from_font_size(16.0), TextFont::from_font_size(16.0),
TextColor(Color::WHITE) TextColor(Color::WHITE)
), ),
( (
UiStatusText::Timer, TextType::Timer,
Text::new("--:--"), Text::new("--:--"),
TextFont::from_font_size(16.0), TextFont::from_font_size(16.0),
TextColor(Color::WHITE) TextColor(Color::WHITE)
), ),
( (
Button, Button,
UiStatusButton::SavegameDump, ButtonType::SettingsOpen,
Node::default(), Node::default(),
children![ children![
Text::new("Save"), Text::new("Einstellungen"),
TextFont::from_font_size(16.0), TextFont::from_font_size(16.0),
TextColor(Color::WHITE) TextColor(Color::WHITE)
] ]
@@ -59,7 +60,7 @@ fn setup(mut commands: Commands) {
)); ));
} }
fn update_status(phase_res: Res<CurrentPhase>, mut text_query: Query<(&mut Text, &UiStatusText)>) { fn update_status(phase_res: Res<CurrentPhase>, mut text_query: Query<(&mut Text, &TextType)>) {
if !phase_res.is_changed() { if !phase_res.is_changed() {
return; return;
} }
@@ -67,32 +68,27 @@ fn update_status(phase_res: Res<CurrentPhase>, mut text_query: Query<(&mut Text,
for (mut text, status_type) in text_query.iter_mut() { for (mut text, status_type) in text_query.iter_mut() {
text.0 = match status_type { text.0 = match status_type {
UiStatusText::Phase => current_phase.display_name().into(), TextType::Phase => current_phase.display_name().into(),
UiStatusText::Timer => current_phase.format_duration(), TextType::Timer => current_phase.format_duration(),
}; };
} }
} }
fn status_buttons( fn buttons(
mut interaction_query: Query< mut interaction_query: Query<(&Interaction, &ButtonType), (Changed<Interaction>, With<Button>)>,
(&Interaction, &UiStatusButton),
(Changed<Interaction>, With<Button>),
>,
mut savegamedump_messages: MessageWriter<SavegameDumpMessage>,
) { ) {
for (interaction, button_type) in &mut interaction_query { for (interaction, button_type) in &mut interaction_query {
match *interaction { match *interaction {
Interaction::Pressed => match button_type { Interaction::Pressed => match button_type {
UiStatusButton::SavegameDump => { ButtonType::SettingsOpen => {}
savegamedump_messages.write(SavegameDumpMessage); _ => {}
}
}, },
_ => {} _ => {}
} }
} }
} }
fn cleanup(mut commands: Commands, query: Query<Entity, With<UiStatusRootContainer>>) { fn cleanup(mut commands: Commands, query: Query<Entity, With<RootMarker>>) {
for entity in query.iter() { for entity in query.iter() {
commands.entity(entity).despawn(); commands.entity(entity).despawn();
} }

View File

@@ -0,0 +1 @@
pub mod settings;

View File

@@ -0,0 +1,3 @@
use crate::prelude::*;
pub fn open_settings(commands: &mut Commands) {}

View File

@@ -2,21 +2,21 @@ pub mod config;
pub mod core; pub mod core;
pub mod game_screen; pub mod game_screen;
pub mod grid; pub mod grid;
pub mod hud;
pub mod input; pub mod input;
pub mod phase; pub mod phase;
pub mod pom; pub mod pom;
pub mod savegame; pub mod savegame;
pub mod start_screen; pub mod start_screen;
pub mod status;
pub mod ui; pub mod ui;
pub use core::CorePlugin; pub use core::CorePlugin;
pub use game_screen::GameScreenPlugin; pub use game_screen::GameScreenPlugin;
pub use grid::GridPlugin; pub use grid::GridPlugin;
pub use hud::HudPlugin;
pub use input::InputPlugin; pub use input::InputPlugin;
pub use phase::PhasePlugin; pub use phase::PhasePlugin;
pub use pom::PomPlugin; pub use pom::PomPlugin;
pub use savegame::SavegamePlugin; pub use savegame::SavegamePlugin;
pub use start_screen::StartScreenPlugin; pub use start_screen::StartScreenPlugin;
pub use status::StatusPlugin;
pub use ui::UiPlugin; pub use ui::UiPlugin;

View File

@@ -6,17 +6,3 @@ pub struct Scroll {
pub entity: Entity, pub entity: Entity,
pub delta: Vec2, pub delta: Vec2,
} }
#[derive(Component)]
pub struct UiStatusRootContainer;
#[derive(Component)]
pub enum UiStatusText {
Phase,
Timer,
}
#[derive(Component)]
pub enum UiStatusButton {
SavegameDump,
}

View File

@@ -29,7 +29,7 @@ fn main() {
features::PomPlugin, features::PomPlugin,
features::InputPlugin, features::InputPlugin,
features::PhasePlugin, features::PhasePlugin,
features::StatusPlugin, features::HudPlugin,
features::SavegamePlugin, features::SavegamePlugin,
features::UiPlugin, features::UiPlugin,
)) ))

View File

@@ -13,10 +13,7 @@ pub use crate::features::{
messages::{InteractStartMessage, MoveMessage}, messages::{InteractStartMessage, MoveMessage},
}, },
savegame::components::SavegamePath, savegame::components::SavegamePath,
ui::{ ui::consts::*,
components::{UiStatusButton, UiStatusRootContainer, UiStatusText},
consts::*,
},
}; };
pub use crate::utils::path::get_internal_path; pub use crate::utils::path::get_internal_path;
pub use bevy::prelude::*; pub use bevy::prelude::*;