phase -> execute loop

This commit is contained in:
Jim 2025-09-01 12:08:39 -04:00
parent 83256bfbe5
commit 6c0ac7f66d
Signed by: jim
GPG key ID: 3D7D94BA53088BF4
7 changed files with 273 additions and 35 deletions

View file

@ -2,6 +2,8 @@
#![allow(clippy::type_complexity)]
#![allow(clippy::too_many_arguments)]
use std::time::Duration;
use bevy::prelude::*;
use bevy_egui::{
EguiContexts, EguiPrimaryContextPass,
@ -11,6 +13,7 @@ use building::BuildingInfo;
use building_placement::SelectBuildingPreview;
use input::{SelectEntity, Selected};
use resources::{AvailableWorkers, Cost, ResourceDeposit, Resources, WorkedResourceDeposit};
use state::{BasePhaseState, Phase};
use ui_state::GameUIState;
use upgrade::{UnlockUpgrade, Upgradable, Upgrade};
@ -18,14 +21,161 @@ pub struct UiIngamePlugin;
impl Plugin for UiIngamePlugin {
fn build(&self, app: &mut App) {
app.add_systems(
EguiPrimaryContextPass,
resource_ui.run_if(in_state(GameUIState::Default)),
);
app.init_state::<PhaseTransitionState>()
.init_resource::<TransitionTimer>()
.add_systems(
EguiPrimaryContextPass,
(
side_panel.run_if(in_state(GameUIState::Default)),
plan_ui.run_if(in_state(GameUIState::Default).and(in_state(Phase::Plan))),
execute_ui.run_if(in_state(GameUIState::Default).and(in_state(Phase::Execute))),
transition_ui.run_if(
in_state(GameUIState::Default).and(
not(in_state(PhaseTransitionState::None))
.and(state_exists::<BasePhaseState>),
),
),
),
)
//doing ontransition since this is a substate. otherwise, shows up every time you unpause
.add_systems(
OnTransition {
exited: Phase::Plan,
entered: Phase::Execute,
},
execute_transition,
)
.add_systems(
OnTransition {
exited: Phase::Execute,
entered: Phase::Plan,
},
plan_transition,
);
}
}
fn resource_ui(
const LARGE_FONT_SIZE: f32 = 24.;
const FONT_SIZE: f32 = 16.;
#[derive(States, Default, Debug, Clone, PartialEq, Eq, Hash)]
enum PhaseTransitionState {
#[default]
None,
ToExecute,
ToPlan,
}
fn plan_ui(
mut contexts: EguiContexts,
curr_phase: Res<State<BasePhaseState>>,
mut next_phase: ResMut<NextState<BasePhaseState>>,
) {
let ctx = contexts.ctx_mut().unwrap();
egui::Window::new("PlanUI")
.resizable(false)
.collapsible(false)
.title_bar(false)
.anchor(Align2::CENTER_BOTTOM, [0., 0.])
.show(ctx, |ui| {
if ui
.button(RichText::new("EXECUTE").size(LARGE_FONT_SIZE))
.clicked()
{
next_phase.set(BasePhaseState::Execute {
phase: curr_phase.get().phase_count(),
});
}
});
}
fn execute_ui(
mut contexts: EguiContexts,
curr_phase: Res<State<BasePhaseState>>,
mut next_phase: ResMut<NextState<BasePhaseState>>,
) {
let ctx = contexts.ctx_mut().unwrap();
egui::Window::new("ExecuteUI")
.resizable(false)
.collapsible(false)
.title_bar(false)
.anchor(Align2::CENTER_TOP, [0., 0.])
.show(ctx, |ui| {
// todo - remove this
if ui
.button(RichText::new("DEBUG - PLAN").size(LARGE_FONT_SIZE))
.clicked()
{
next_phase.set(BasePhaseState::Plan {
phase: curr_phase.get().phase_count() + 1,
});
}
});
}
#[derive(Resource, DerefMut, Deref)]
struct TransitionTimer(Timer);
impl Default for TransitionTimer {
fn default() -> Self {
Self(Timer::new(Duration::from_secs(1), TimerMode::Once))
}
}
fn execute_transition(
mut next_transition: ResMut<NextState<PhaseTransitionState>>,
mut timer: ResMut<TransitionTimer>,
) {
next_transition.set(PhaseTransitionState::ToExecute);
timer.reset();
}
fn plan_transition(
mut next_transition: ResMut<NextState<PhaseTransitionState>>,
mut timer: ResMut<TransitionTimer>,
) {
next_transition.set(PhaseTransitionState::ToPlan);
timer.reset();
}
fn transition_ui(
mut contexts: EguiContexts,
curr_transition: Res<State<PhaseTransitionState>>,
mut next_transition: ResMut<NextState<PhaseTransitionState>>,
curr_state: Res<State<BasePhaseState>>,
time: Res<Time>,
mut timer: ResMut<TransitionTimer>,
) {
let ctx = contexts.ctx_mut().unwrap();
timer.tick(time.delta());
if timer.finished() {
next_transition.set(PhaseTransitionState::None);
timer.reset();
return;
}
egui::Window::new("TRANSITION")
.resizable(false)
.collapsible(false)
.title_bar(false)
.anchor(Align2::CENTER_CENTER, [0., 0.])
.show(ctx, |ui| {
ui.label(
RichText::new(format!(
"{} {}",
match curr_transition.get() {
PhaseTransitionState::None => "WHAT",
PhaseTransitionState::ToExecute => "EXECUTE",
PhaseTransitionState::ToPlan => "PLAN",
},
curr_state.get().phase_count()
))
.size(64.),
);
});
}
fn side_panel(
mut contexts: EguiContexts,
mut commands: Commands,
resources: Res<Resources>,
@ -39,14 +189,13 @@ fn resource_ui(
mut select_writer: EventWriter<SelectEntity>,
) {
let ctx = contexts.ctx_mut().unwrap();
let large_font_size = 24.;
let font_size = 16.;
egui::Window::new("Resources")
.resizable(false)
.collapsible(false)
.title_bar(false)
.anchor(Align2::RIGHT_TOP, [0., 0.])
.show(ctx, |ui| {
// --- view when object selected ---
if let Ok((selected, selected_name)) = selected.single() {
if ui.button("x").clicked() {
select_writer.write(SelectEntity(None));
@ -56,7 +205,7 @@ fn resource_ui(
// resource deposit management
if let Ok((deposit, mut opt_worked)) = selected_resource_deposit.get_mut(selected) {
ui.label(
RichText::new(format!("Level {} deposit", deposit.level)).size(font_size),
RichText::new(format!("Level {} deposit", deposit.level)).size(FONT_SIZE),
);
ui.heading(
RichText::new(if let Some(ref worked) = opt_worked {
@ -64,7 +213,7 @@ fn resource_ui(
} else {
"Not worked".to_string()
})
.size(large_font_size)
.size(LARGE_FONT_SIZE)
.underline(),
);
// worker management buttons
@ -96,10 +245,10 @@ fn resource_ui(
}
ui.heading(RichText::new("Bounty").underline());
ui.label(
RichText::new(format!("Souls: {}", deposit.bounty.souls)).size(font_size),
RichText::new(format!("Souls: {}", deposit.bounty.souls)).size(FONT_SIZE),
);
ui.label(
RichText::new(format!("Bones: {}", deposit.bounty.bones)).size(font_size),
RichText::new(format!("Bones: {}", deposit.bounty.bones)).size(FONT_SIZE),
);
}
@ -118,7 +267,7 @@ fn resource_ui(
.filter_map(|u| upgrades.get(*u).map(|(up, n)| (*u, up, n)).ok())
{
if ui
.button(RichText::new(upgrade_name.as_str()).size(large_font_size))
.button(RichText::new(upgrade_name.as_str()).size(LARGE_FONT_SIZE))
.clicked()
{
commands.trigger_targets(
@ -126,8 +275,8 @@ fn resource_ui(
upgrade_entity,
);
}
ui.label(RichText::new(upgrade.description.as_str()).size(font_size));
ui.label(RichText::new(upgrade.cost.to_string()).size(font_size));
ui.label(RichText::new(upgrade.description.as_str()).size(FONT_SIZE));
ui.label(RichText::new(upgrade.cost.to_string()).size(FONT_SIZE));
}
if !active_upgrades.is_empty() {
ui.collapsing("Active Upgrades", |ui| {
@ -135,20 +284,21 @@ fn resource_ui(
active_upgrades.iter().filter_map(|u| upgrades.get(*u).ok())
{
ui.label(
RichText::new(upgrade_name.as_str()).size(large_font_size),
RichText::new(upgrade_name.as_str()).size(LARGE_FONT_SIZE),
);
ui.label(
RichText::new(upgrade.description.as_str()).size(font_size),
RichText::new(upgrade.description.as_str()).size(FONT_SIZE),
);
ui.label(RichText::new(upgrade.cost.to_string()).size(font_size));
ui.label(RichText::new(upgrade.cost.to_string()).size(FONT_SIZE));
}
});
}
}
return;
}
ui.label(RichText::new(format!("Souls: {}", resources.souls)).size(large_font_size));
ui.label(RichText::new(format!("Bones: {}", resources.bones)).size(large_font_size));
// --- view when nothing's selected ---
ui.label(RichText::new(format!("Souls: {}", resources.souls)).size(LARGE_FONT_SIZE));
ui.label(RichText::new(format!("Bones: {}", resources.bones)).size(LARGE_FONT_SIZE));
ui.heading(RichText::new("Buildings").underline());
for (info, cost, name) in buildings.iter() {
if !info.unlocked {
@ -157,7 +307,7 @@ fn resource_ui(
if ui
.button(
RichText::new(format!("{} - {}", name, cost.0.to_string())).size(font_size),
RichText::new(format!("{} - {}", name, cost.0.to_string())).size(FONT_SIZE),
)
.clicked()
{