//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 { souls: 10, bones: 10, }); } #[derive(Resource, Default)] pub struct Resources { pub souls: u32, pub bones: u32, } 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) { return false; } self.souls = self.souls.saturating_sub(cost.souls); self.bones = self.bones.saturating_sub(cost.bones); 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)] pub struct Cost(pub Quantity); #[derive(Default, Clone, Copy, PartialEq, Eq)] pub struct Quantity { pub souls: u32, pub bones: u32, } 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), } } } #[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, // 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