disable resource collection outside of core range, disable purchase buttons if too much money

This commit is contained in:
Jim 2025-09-01 21:09:37 -04:00
parent 6a4978041c
commit e6b04bbdd1
Signed by: jim
GPG key ID: 3D7D94BA53088BF4
8 changed files with 97 additions and 56 deletions

1
Cargo.lock generated
View file

@ -5311,6 +5311,7 @@ dependencies = [
"bevy_egui",
"building",
"building_placement",
"combat",
"input",
"resources",
"state",

View file

@ -1,11 +1,11 @@
(
map: {
KeyCode(ShiftLeft): [
MultiPlaceTower,
],
MouseButton(Middle): [
CameraOrbitEnable,
],
KeyCode(ShiftLeft): [
MultiPlaceTower,
],
KeyCode(F1): [
ToggleDebugUI,
],

View file

@ -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;
}

View file

@ -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 {

View file

@ -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.)

View file

@ -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<u32> for Quantity {
@ -92,6 +85,28 @@ impl std::ops::Mul<u32> for Quantity {
}
}
impl std::ops::Add<Quantity> 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<Quantity> 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)]

View file

@ -14,6 +14,7 @@ building = { path = "../building" }
input = { path = "../input" }
building_placement = { path = "../building_placement" }
upgrade = { path = "../upgrade" }
combat = { path = "../combat" }
[lints]
workspace = true

View file

@ -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<Selected>>,
upgradable: Query<&Upgradable>,
upgrades: Query<(&Upgrade, &Name)>,
mut selected_resource_deposit: Query<(&ResourceDeposit, Option<&mut WorkedResourceDeposit>)>,
core_query: Query<(&GlobalTransform, &Range), With<Core>>,
mut selected_resource_deposit: Query<(
&ResourceDeposit,
&GlobalTransform,
Option<&mut WorkedResourceDeposit>,
)>,
mut selected_preview_writer: EventWriter<SelectBuildingPreview>,
mut select_writer: EventWriter<SelectEntity>,
) {
@ -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));
}
}
});