//bevy system signatures often violate these rules #![allow(clippy::type_complexity)] #![allow(clippy::too_many_arguments)] use std::time::Duration; use bevy::prelude::*; use state::{AppState, GameSystemSet}; pub struct ResourcesPlugin; impl Plugin for ResourcesPlugin { fn build(&self, app: &mut App) { app.add_systems(OnEnter(AppState::Game), setup) .add_systems(FixedUpdate, tick_harvest.in_set(GameSystemSet::Execute)) .insert_resource(AvailableWorkers(5)) .add_observer(on_harvest); } } fn setup(mut commands: Commands) { commands.insert_resource(Resources(Quantity { souls: 10, bones: 10, })); } #[derive(Resource, Default, Deref, DerefMut)] pub struct Resources(pub Quantity); impl Resources { pub fn buy(&mut self, cost: &Cost) -> bool { if !self.can_buy(cost.0) { return false; } self.0 = self.0 - cost.0; true } } #[derive(Component, Default, Deref, DerefMut)] pub struct Cost(pub Quantity); #[derive(Default, Clone, Copy, PartialEq, Eq)] pub struct Quantity { pub souls: u32, pub bones: u32, } impl Quantity { pub fn to_string(self) -> String { let souls_text = if self.souls > 0 { format!("{} souls", self.souls) } else { "".to_string() }; let divider_text = if self.souls > 0 && self.bones > 0 { ", " } else if self.souls == 0 && self.bones == 0 { "FREE!!!" } else { "" }; let bones_text = if self.bones > 0 { format!("{} bones", self.bones) } else { "".to_string() }; 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 { type Output = Quantity; fn mul(self, rhs: u32) -> Self::Output { Self { souls: self.souls.saturating_mul(rhs), bones: self.bones.saturating_mul(rhs), } } } 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, } impl Default for Drops { fn default() -> Self { Self { souls: 1 } } } #[derive(Component, Default)] #[require(Transform, Name)] pub struct ResourceDeposit { pub bounty: Quantity, pub level: u32, pub harvest_time: Duration, pub max_workers: u32, // todo - add finite resources and refresh timer } #[derive(Component, Default)] #[require(ResourceDeposit)] pub struct WorkedResourceDeposit { pub workers: u32, pub worked_duration: Duration, } #[derive(Event)] pub struct Harvested; fn tick_harvest( mut commands: Commands, mut deposit_query: Query<(Entity, &ResourceDeposit, &mut WorkedResourceDeposit)>, time: Res