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

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)]