Files
pomomon-garden/src/features/status/mod.rs
2025-11-26 19:59:53 +01:00

105 lines
3.0 KiB
Rust

use crate::features::savegame::messages::SavegameDumpMessage;
use crate::prelude::*;
pub struct StatusPlugin;
impl Plugin for StatusPlugin {
fn build(&self, app: &mut App) {
app.add_systems(OnEnter(AppState::GameScreen), setup);
app.add_systems(OnExit(AppState::GameScreen), cleanup);
app.add_systems(Update, update_status.run_if(in_state(AppState::GameScreen)));
app.add_systems(
Update,
status_buttons.run_if(in_state(AppState::GameScreen)),
);
}
}
fn setup(mut commands: Commands) {
commands.spawn((
UiStatusRootContainer,
Node {
position_type: PositionType::Absolute,
bottom: px(0),
left: px(0),
width: percent(100),
height: px(50),
justify_content: JustifyContent::SpaceAround,
align_items: AlignItems::Center,
flex_direction: FlexDirection::Row,
..default()
},
BackgroundColor(Color::srgba(0.0, 0.0, 0.0, 0.8)),
children![
(
UiPhaseText,
Text::new("..."),
TextFont::from_font_size(16.0),
TextColor(Color::WHITE)
),
(
UiTimerText,
Text::new("--:--"),
TextFont::from_font_size(16.0),
TextColor(Color::WHITE)
),
(
Button,
UiStatusButton::SavegameDump,
Node::default(),
children![
Text::new("Save"),
TextFont::from_font_size(16.0),
TextColor(Color::WHITE)
]
)
],
));
}
fn update_status(
phase_res: Res<CurrentPhase>,
mut phase_query: Query<&mut Text, (With<UiPhaseText>, Without<UiTimerText>)>,
mut timer_query: Query<&mut Text, (With<UiTimerText>, Without<UiPhaseText>)>,
) {
if !phase_res.is_changed() {
return;
}
let current_phase = &phase_res.0;
if let Ok(mut phase_text) = phase_query.single_mut() {
phase_text.0 = current_phase.display_name().to_string();
}
if let Ok(mut timer_text) = timer_query.single_mut() {
timer_text.0 = current_phase.format_duration();
}
}
fn status_buttons(
mut interaction_query: Query<
(&Interaction, &UiStatusButton),
(Changed<Interaction>, With<Button>),
>,
mut savegamedump_messages: MessageWriter<SavegameDumpMessage>,
) {
for (interaction, button_type) in &mut interaction_query {
match *interaction {
Interaction::Pressed => match button_type {
UiStatusButton::SavegameDump => {
savegamedump_messages.write(SavegameDumpMessage);
}
},
_ => {}
}
}
}
fn cleanup(mut commands: Commands, query: Query<Entity, With<UiStatusRootContainer>>) {
for entity in query.iter() {
commands.entity(entity).despawn();
}
}