From e6b04bbdd15bddb4b68e211211aa33e96cc48faa Mon Sep 17 00:00:00 2001 From: Jim Date: Mon, 1 Sep 2025 21:09:37 -0400 Subject: [PATCH] disable resource collection outside of core range, disable purchase buttons if too much money --- Cargo.lock | 1 + assets/settings/input_map.ron | 6 +-- crates/building/src/lib.rs | 2 +- crates/building_core/src/lib.rs | 4 +- crates/combat/src/lib.rs | 6 +++ crates/resources/src/lib.rs | 53 ++++++++++++++-------- crates/ui_ingame/Cargo.toml | 1 + crates/ui_ingame/src/lib.rs | 80 ++++++++++++++++++++------------- 8 files changed, 97 insertions(+), 56 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 570e4aa..73791c6 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -5311,6 +5311,7 @@ dependencies = [ "bevy_egui", "building", "building_placement", + "combat", "input", "resources", "state", diff --git a/assets/settings/input_map.ron b/assets/settings/input_map.ron index 954bb8b..b82ce27 100755 --- a/assets/settings/input_map.ron +++ b/assets/settings/input_map.ron @@ -1,11 +1,11 @@ ( map: { - KeyCode(ShiftLeft): [ - MultiPlaceTower, - ], MouseButton(Middle): [ CameraOrbitEnable, ], + KeyCode(ShiftLeft): [ + MultiPlaceTower, + ], KeyCode(F1): [ ToggleDebugUI, ], diff --git a/crates/building/src/lib.rs b/crates/building/src/lib.rs index 396f425..7433f54 100755 --- a/crates/building/src/lib.rs +++ b/crates/building/src/lib.rs @@ -121,7 +121,7 @@ pub fn valid_placement( core_position_range: Option<(Vec3, f32)>, ) -> bool { if let Some((core_position, core_range)) = core_position_range - && position.distance_squared(core_position) > core_range * core_range + && !Range(core_range).within(position, core_position) { return false; } diff --git a/crates/building_core/src/lib.rs b/crates/building_core/src/lib.rs index bd8ce97..7861693 100755 --- a/crates/building_core/src/lib.rs +++ b/crates/building_core/src/lib.rs @@ -5,7 +5,7 @@ use bevy::prelude::*; use building::{BuildingInfo, BuildingPreview, Core, PREVIEW_ALPHA, PopulatePlacedBuilding}; use combat::{Defense, Health, Range}; -use ui_range_indicator::{CustomRangeIndicator, DisplayCustomRangeIndicator}; +use ui_range_indicator::CustomRangeIndicator; pub struct BuildingCorePlugin; @@ -80,7 +80,7 @@ fn populate( Core, Health::new(100.0), Defense::new(5.0), - Range(20.0), + Range(25.0), Mesh3d(res.mesh.clone()), MeshMaterial3d(res.material.clone()), CustomRangeIndicator::new(materials.add(StandardMaterial { diff --git a/crates/combat/src/lib.rs b/crates/combat/src/lib.rs index 0539e4e..985e63d 100755 --- a/crates/combat/src/lib.rs +++ b/crates/combat/src/lib.rs @@ -257,6 +257,12 @@ fn tick_invulnerability( #[derive(Component, Deref, DerefMut)] pub struct Range(pub f32); +impl Range { + pub fn within(&self, a: Vec3, b: Vec3) -> bool { + a.distance_squared(b) < self.0 * self.0 + } +} + impl Default for Range { fn default() -> Self { Self(5.) diff --git a/crates/resources/src/lib.rs b/crates/resources/src/lib.rs index d4affbf..8f058a7 100644 --- a/crates/resources/src/lib.rs +++ b/crates/resources/src/lib.rs @@ -19,34 +19,23 @@ impl Plugin for ResourcesPlugin { } fn setup(mut commands: Commands) { - commands.insert_resource(Resources { + commands.insert_resource(Resources(Quantity { souls: 10, bones: 10, - }); + })); } -#[derive(Resource, Default)] -pub struct Resources { - pub souls: u32, - pub bones: u32, -} +#[derive(Resource, Default, Deref, DerefMut)] +pub struct Resources(pub Quantity); impl Resources { - pub fn can_buy(&self, cost: &Cost) -> bool { - self.souls >= cost.souls && self.bones >= cost.bones - } pub fn buy(&mut self, cost: &Cost) -> bool { - if !self.can_buy(cost) { + if !self.can_buy(cost.0) { return false; } - self.souls = self.souls.saturating_sub(cost.souls); - self.bones = self.bones.saturating_sub(cost.bones); + self.0 = self.0 - cost.0; true } - pub fn add(&mut self, quantity: Quantity) { - self.souls = self.souls.saturating_add(quantity.souls); - self.bones = self.bones.saturating_add(quantity.bones); - } } #[derive(Component, Default, Deref, DerefMut)] @@ -59,7 +48,7 @@ pub struct Quantity { } impl Quantity { - pub fn to_string(&self) -> String { + pub fn to_string(self) -> String { let souls_text = if self.souls > 0 { format!("{} souls", self.souls) } else { @@ -79,6 +68,10 @@ impl Quantity { }; format!("{}{}{}", souls_text, divider_text, bones_text) } + + pub fn can_buy(self, product: Quantity) -> bool { + self.bones >= product.bones && self.souls >= product.souls + } } impl std::ops::Mul for Quantity { @@ -92,6 +85,28 @@ impl std::ops::Mul for Quantity { } } +impl std::ops::Add for Quantity { + type Output = Quantity; + + fn add(self, rhs: Quantity) -> Self::Output { + Quantity { + souls: self.souls.saturating_add(rhs.souls), + bones: self.bones.saturating_add(rhs.bones), + } + } +} + +impl std::ops::Sub for Quantity { + type Output = Quantity; + + fn sub(self, rhs: Quantity) -> Self::Output { + Quantity { + souls: self.souls.saturating_sub(rhs.souls), + bones: self.bones.saturating_sub(rhs.bones), + } + } +} + #[derive(Component)] pub struct Drops { pub souls: u32, @@ -146,7 +161,7 @@ fn on_harvest( error!("harvested something that was not a resource deposit or not worked OwO"); return; }; - resources.add(deposit.bounty * worked.workers); + resources.0 = resources.0 + deposit.bounty * worked.workers; } #[derive(Resource)] diff --git a/crates/ui_ingame/Cargo.toml b/crates/ui_ingame/Cargo.toml index 0cf3bbb..df537fe 100644 --- a/crates/ui_ingame/Cargo.toml +++ b/crates/ui_ingame/Cargo.toml @@ -14,6 +14,7 @@ building = { path = "../building" } input = { path = "../input" } building_placement = { path = "../building_placement" } upgrade = { path = "../upgrade" } +combat = { path = "../combat" } [lints] workspace = true diff --git a/crates/ui_ingame/src/lib.rs b/crates/ui_ingame/src/lib.rs index 55a2af4..2400869 100644 --- a/crates/ui_ingame/src/lib.rs +++ b/crates/ui_ingame/src/lib.rs @@ -9,8 +9,9 @@ use bevy_egui::{ EguiContexts, EguiPrimaryContextPass, egui::{self, Align2, RichText}, }; -use building::BuildingInfo; +use building::{BuildingInfo, Core}; use building_placement::SelectBuildingPreview; +use combat::Range; use input::{SelectEntity, Selected}; use resources::{AvailableWorkers, Cost, ResourceDeposit, Resources, WorkedResourceDeposit}; use state::{BasePhaseState, Phase}; @@ -184,7 +185,12 @@ fn side_panel( selected: Query<(Entity, &Name), With>, upgradable: Query<&Upgradable>, upgrades: Query<(&Upgrade, &Name)>, - mut selected_resource_deposit: Query<(&ResourceDeposit, Option<&mut WorkedResourceDeposit>)>, + core_query: Query<(&GlobalTransform, &Range), With>, + mut selected_resource_deposit: Query<( + &ResourceDeposit, + &GlobalTransform, + Option<&mut WorkedResourceDeposit>, + )>, mut selected_preview_writer: EventWriter, mut select_writer: EventWriter, ) { @@ -203,7 +209,9 @@ fn side_panel( ui.heading(selected_name.as_str()); // resource deposit management - if let Ok((deposit, mut opt_worked)) = selected_resource_deposit.get_mut(selected) { + if let Ok((deposit, deposit_gtf, mut opt_worked)) = + selected_resource_deposit.get_mut(selected) + { ui.label( RichText::new(format!("Level {} deposit", deposit.level)).size(FONT_SIZE), ); @@ -218,21 +226,28 @@ fn side_panel( ); // worker management buttons // add - 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() - }); + 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() + }); + } } } + } else { + ui.add_enabled(false, egui::Button::new("Core required")); } + // remove if let Some(ref mut worked) = opt_worked { if ui.button("Remove worker").clicked() { @@ -266,14 +281,17 @@ fn side_panel( .iter() .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)) - .clicked() - { - commands.trigger_targets( - UnlockUpgrade { building: selected }, - upgrade_entity, - ); + 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)); } ui.label(RichText::new(upgrade.description.as_str()).size(FONT_SIZE)); ui.label(RichText::new(upgrade.cost.to_string()).size(FONT_SIZE)); @@ -304,14 +322,14 @@ fn side_panel( if !info.unlocked { continue; } - - if ui - .button( - RichText::new(format!("{} - {}", name, cost.0.to_string())).size(FONT_SIZE), - ) - .clicked() - { - selected_preview_writer.write(SelectBuildingPreview(Some(info.preview))); + 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)); } } });