2025-08-30 23:39:18 -04:00
|
|
|
//bevy system signatures often violate these rules
|
|
|
|
|
#![allow(clippy::type_complexity)]
|
|
|
|
|
#![allow(clippy::too_many_arguments)]
|
|
|
|
|
|
2025-09-01 12:08:39 -04:00
|
|
|
use std::time::Duration;
|
|
|
|
|
|
2025-08-30 23:39:18 -04:00
|
|
|
use bevy::prelude::*;
|
2025-08-31 12:08:44 -04:00
|
|
|
use bevy_egui::{
|
|
|
|
|
EguiContexts, EguiPrimaryContextPass,
|
|
|
|
|
egui::{self, Align2, RichText},
|
|
|
|
|
};
|
2025-09-01 21:09:37 -04:00
|
|
|
use building::{BuildingInfo, Core};
|
2025-08-31 12:08:44 -04:00
|
|
|
use building_placement::SelectBuildingPreview;
|
2025-09-01 21:09:37 -04:00
|
|
|
use combat::Range;
|
2025-08-31 22:26:07 -04:00
|
|
|
use input::{SelectEntity, Selected};
|
|
|
|
|
use resources::{AvailableWorkers, Cost, ResourceDeposit, Resources, WorkedResourceDeposit};
|
2025-09-01 12:08:39 -04:00
|
|
|
use state::{BasePhaseState, Phase};
|
2025-08-31 12:08:44 -04:00
|
|
|
use ui_state::GameUIState;
|
2025-08-31 23:59:50 -04:00
|
|
|
use upgrade::{UnlockUpgrade, Upgradable, Upgrade};
|
2025-08-30 23:39:18 -04:00
|
|
|
|
|
|
|
|
pub struct UiIngamePlugin;
|
|
|
|
|
|
|
|
|
|
impl Plugin for UiIngamePlugin {
|
2025-08-31 12:08:44 -04:00
|
|
|
fn build(&self, app: &mut App) {
|
2025-09-01 12:08:39 -04:00
|
|
|
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,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
2025-08-31 12:08:44 -04:00
|
|
|
}
|
2025-09-01 12:08:39 -04:00
|
|
|
egui::Window::new("TRANSITION")
|
|
|
|
|
.collapsible(false)
|
|
|
|
|
.title_bar(false)
|
|
|
|
|
.anchor(Align2::CENTER_CENTER, [0., 0.])
|
2025-09-01 19:10:16 -04:00
|
|
|
.fixed_size([600., 200.])
|
2025-09-01 12:08:39 -04:00
|
|
|
.show(ctx, |ui| {
|
|
|
|
|
ui.label(
|
|
|
|
|
RichText::new(format!(
|
2025-09-01 19:10:16 -04:00
|
|
|
"{} PHASE {}",
|
2025-09-01 12:08:39 -04:00
|
|
|
match curr_transition.get() {
|
|
|
|
|
PhaseTransitionState::None => "WHAT",
|
|
|
|
|
PhaseTransitionState::ToExecute => "EXECUTE",
|
|
|
|
|
PhaseTransitionState::ToPlan => "PLAN",
|
|
|
|
|
},
|
|
|
|
|
curr_state.get().phase_count()
|
|
|
|
|
))
|
|
|
|
|
.size(64.),
|
|
|
|
|
);
|
|
|
|
|
});
|
2025-08-31 12:08:44 -04:00
|
|
|
}
|
|
|
|
|
|
2025-09-01 12:08:39 -04:00
|
|
|
fn side_panel(
|
2025-08-31 12:08:44 -04:00
|
|
|
mut contexts: EguiContexts,
|
2025-08-31 22:26:07 -04:00
|
|
|
mut commands: Commands,
|
2025-08-31 12:08:44 -04:00
|
|
|
resources: Res<Resources>,
|
2025-08-31 22:26:07 -04:00
|
|
|
mut available_workers: ResMut<AvailableWorkers>,
|
2025-08-31 12:08:44 -04:00
|
|
|
buildings: Query<(&BuildingInfo, &Cost, &Name)>,
|
2025-08-31 22:26:07 -04:00
|
|
|
selected: Query<(Entity, &Name), With<Selected>>,
|
2025-08-31 23:59:50 -04:00
|
|
|
upgradable: Query<&Upgradable>,
|
|
|
|
|
upgrades: Query<(&Upgrade, &Name)>,
|
2025-09-01 21:09:37 -04:00
|
|
|
core_query: Query<(&GlobalTransform, &Range), With<Core>>,
|
|
|
|
|
mut selected_resource_deposit: Query<(
|
|
|
|
|
&ResourceDeposit,
|
|
|
|
|
&GlobalTransform,
|
|
|
|
|
Option<&mut WorkedResourceDeposit>,
|
|
|
|
|
)>,
|
2025-08-31 22:26:07 -04:00
|
|
|
mut selected_preview_writer: EventWriter<SelectBuildingPreview>,
|
|
|
|
|
mut select_writer: EventWriter<SelectEntity>,
|
2025-08-31 12:08:44 -04:00
|
|
|
) {
|
|
|
|
|
let ctx = contexts.ctx_mut().unwrap();
|
|
|
|
|
egui::Window::new("Resources")
|
|
|
|
|
.resizable(false)
|
|
|
|
|
.collapsible(false)
|
|
|
|
|
.title_bar(false)
|
|
|
|
|
.anchor(Align2::RIGHT_TOP, [0., 0.])
|
|
|
|
|
.show(ctx, |ui| {
|
2025-09-01 12:08:39 -04:00
|
|
|
// --- view when object selected ---
|
2025-08-31 22:26:07 -04:00
|
|
|
if let Ok((selected, selected_name)) = selected.single() {
|
|
|
|
|
if ui.button("x").clicked() {
|
|
|
|
|
select_writer.write(SelectEntity(None));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ui.heading(selected_name.as_str());
|
2025-08-31 23:59:50 -04:00
|
|
|
// resource deposit management
|
2025-09-01 21:09:37 -04:00
|
|
|
if let Ok((deposit, deposit_gtf, mut opt_worked)) =
|
|
|
|
|
selected_resource_deposit.get_mut(selected)
|
|
|
|
|
{
|
2025-08-31 22:26:07 -04:00
|
|
|
ui.label(
|
2025-09-01 12:08:39 -04:00
|
|
|
RichText::new(format!("Level {} deposit", deposit.level)).size(FONT_SIZE),
|
2025-08-31 22:26:07 -04:00
|
|
|
);
|
|
|
|
|
ui.heading(
|
|
|
|
|
RichText::new(if let Some(ref worked) = opt_worked {
|
|
|
|
|
format!("{} workers", worked.workers)
|
|
|
|
|
} else {
|
|
|
|
|
"Not worked".to_string()
|
|
|
|
|
})
|
2025-09-01 12:08:39 -04:00
|
|
|
.size(LARGE_FONT_SIZE)
|
2025-08-31 22:26:07 -04:00
|
|
|
.underline(),
|
|
|
|
|
);
|
|
|
|
|
// worker management buttons
|
|
|
|
|
// add
|
2025-09-01 21:09:37 -04:00
|
|
|
if let Ok((core_gtf, range)) = core_query.single() {
|
|
|
|
|
if !range.within(core_gtf.translation(), deposit_gtf.translation()) {
|
|
|
|
|
ui.add_enabled(false, egui::Button::new("Out of Core's range"));
|
|
|
|
|
} else if available_workers.0 == 0 {
|
|
|
|
|
ui.add_enabled(false, egui::Button::new("No workers available"));
|
|
|
|
|
} else {
|
|
|
|
|
if ui.button("Add worker").clicked() {
|
|
|
|
|
available_workers.0 = available_workers.0.saturating_sub(1);
|
|
|
|
|
if let Some(ref mut worked) = opt_worked {
|
|
|
|
|
worked.workers = worked.workers.saturating_add(1);
|
|
|
|
|
} else {
|
|
|
|
|
commands.entity(selected).insert(WorkedResourceDeposit {
|
|
|
|
|
workers: 1,
|
|
|
|
|
..default()
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-08-31 22:26:07 -04:00
|
|
|
}
|
|
|
|
|
}
|
2025-09-01 21:09:37 -04:00
|
|
|
} else {
|
|
|
|
|
ui.add_enabled(false, egui::Button::new("Core required"));
|
2025-08-31 22:26:07 -04:00
|
|
|
}
|
2025-09-01 21:09:37 -04:00
|
|
|
|
2025-08-31 22:26:07 -04:00
|
|
|
// remove
|
|
|
|
|
if let Some(ref mut worked) = opt_worked {
|
|
|
|
|
if ui.button("Remove worker").clicked() {
|
|
|
|
|
worked.workers = worked.workers.saturating_sub(1);
|
|
|
|
|
available_workers.0 = available_workers.0.saturating_add(1);
|
|
|
|
|
if worked.workers == 0 {
|
|
|
|
|
commands.entity(selected).remove::<WorkedResourceDeposit>();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ui.heading(RichText::new("Bounty").underline());
|
|
|
|
|
ui.label(
|
2025-09-01 12:08:39 -04:00
|
|
|
RichText::new(format!("Souls: {}", deposit.bounty.souls)).size(FONT_SIZE),
|
2025-08-31 22:26:07 -04:00
|
|
|
);
|
|
|
|
|
ui.label(
|
2025-09-01 12:08:39 -04:00
|
|
|
RichText::new(format!("Bones: {}", deposit.bounty.bones)).size(FONT_SIZE),
|
2025-08-31 22:26:07 -04:00
|
|
|
);
|
|
|
|
|
}
|
2025-08-31 23:59:50 -04:00
|
|
|
|
|
|
|
|
//upgrades
|
|
|
|
|
if let Ok(Upgradable {
|
|
|
|
|
active_upgrades,
|
|
|
|
|
available_upgrades,
|
|
|
|
|
..
|
|
|
|
|
}) = upgradable.get(selected)
|
|
|
|
|
{
|
|
|
|
|
if !active_upgrades.is_empty() && !available_upgrades.is_empty() {
|
|
|
|
|
ui.heading(RichText::new("Upgrades").underline());
|
|
|
|
|
}
|
|
|
|
|
for (upgrade_entity, upgrade, upgrade_name) in available_upgrades
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(|u| upgrades.get(*u).map(|(up, n)| (*u, up, n)).ok())
|
|
|
|
|
{
|
2025-09-01 21:09:37 -04:00
|
|
|
let button_text =
|
|
|
|
|
RichText::new(upgrade_name.as_str()).size(LARGE_FONT_SIZE);
|
|
|
|
|
if resources.0.can_buy(upgrade.cost) {
|
|
|
|
|
if ui.button(button_text).clicked() {
|
|
|
|
|
commands.trigger_targets(
|
|
|
|
|
UnlockUpgrade { building: selected },
|
|
|
|
|
upgrade_entity,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
ui.add_enabled(false, egui::Button::new(button_text));
|
2025-08-31 23:59:50 -04:00
|
|
|
}
|
2025-09-01 12:08:39 -04:00
|
|
|
ui.label(RichText::new(upgrade.description.as_str()).size(FONT_SIZE));
|
|
|
|
|
ui.label(RichText::new(upgrade.cost.to_string()).size(FONT_SIZE));
|
2025-08-31 23:59:50 -04:00
|
|
|
}
|
|
|
|
|
if !active_upgrades.is_empty() {
|
|
|
|
|
ui.collapsing("Active Upgrades", |ui| {
|
|
|
|
|
for (upgrade, upgrade_name) in
|
|
|
|
|
active_upgrades.iter().filter_map(|u| upgrades.get(*u).ok())
|
|
|
|
|
{
|
|
|
|
|
ui.label(
|
2025-09-01 12:08:39 -04:00
|
|
|
RichText::new(upgrade_name.as_str()).size(LARGE_FONT_SIZE),
|
2025-08-31 23:59:50 -04:00
|
|
|
);
|
|
|
|
|
ui.label(
|
2025-09-01 12:08:39 -04:00
|
|
|
RichText::new(upgrade.description.as_str()).size(FONT_SIZE),
|
2025-08-31 23:59:50 -04:00
|
|
|
);
|
2025-09-01 12:08:39 -04:00
|
|
|
ui.label(RichText::new(upgrade.cost.to_string()).size(FONT_SIZE));
|
2025-08-31 23:59:50 -04:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-31 22:26:07 -04:00
|
|
|
return;
|
|
|
|
|
}
|
2025-09-01 12:08:39 -04:00
|
|
|
// --- 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));
|
2025-08-31 12:08:44 -04:00
|
|
|
ui.heading(RichText::new("Buildings").underline());
|
|
|
|
|
for (info, cost, name) in buildings.iter() {
|
|
|
|
|
if !info.unlocked {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2025-09-01 21:09:37 -04:00
|
|
|
let button_text =
|
|
|
|
|
RichText::new(format!("{} - {}", name, cost.0.to_string())).size(FONT_SIZE);
|
|
|
|
|
if resources.can_buy(cost.0) {
|
|
|
|
|
if ui.button(button_text).clicked() {
|
|
|
|
|
selected_preview_writer.write(SelectBuildingPreview(Some(info.preview)));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
ui.add_enabled(false, egui::Button::new(button_text));
|
2025-08-31 12:08:44 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2025-08-30 23:39:18 -04:00
|
|
|
}
|