From 138a524abd6ee7bd89bb8246b14b107e1b71acea Mon Sep 17 00:00:00 2001 From: Jim Date: Sat, 30 Aug 2025 23:39:18 -0400 Subject: [PATCH] add souls and bones + tower cost and enemy drops --- Cargo.lock | 21 ++++++++++ Cargo.toml | 2 + assets/settings/input_map.ron | 6 +-- crates/building/Cargo.toml | 1 + crates/building/src/lib.rs | 12 ++++-- crates/building_impaler/Cargo.toml | 1 + crates/building_impaler/src/lib.rs | 2 + crates/combat/Cargo.toml | 1 + crates/combat/src/lib.rs | 13 ++++++- crates/inhabitant/Cargo.toml | 1 + crates/inhabitant/src/lib.rs | 3 +- crates/inhabitant_coranth/src/lib.rs | 3 +- crates/resources/Cargo.toml | 12 ++++++ crates/resources/src/lib.rs | 58 ++++++++++++++++++++++++++++ crates/ui_ingame/Cargo.toml | 10 +++++ crates/ui_ingame/src/lib.rs | 11 ++++++ crates/ui_pause/src/lib.rs | 6 +-- src/main.rs | 2 + 18 files changed, 151 insertions(+), 14 deletions(-) create mode 100644 crates/resources/Cargo.toml create mode 100644 crates/resources/src/lib.rs create mode 100644 crates/ui_ingame/Cargo.toml create mode 100644 crates/ui_ingame/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 7afc9c5..e7f2428 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -1624,6 +1624,7 @@ dependencies = [ "avian3d", "bevy", "combat", + "resources", "state", ] @@ -1647,6 +1648,7 @@ dependencies = [ "building", "combat", "combat_damagers", + "resources", "state", ] @@ -1817,6 +1819,7 @@ dependencies = [ "avian3d", "bevy", "physics", + "resources", "state", ] @@ -1984,10 +1987,12 @@ dependencies = [ "input", "level", "physics", + "resources", "setup", "state", "ui", "ui_debug", + "ui_ingame", "ui_pause", "ui_state", ] @@ -2870,6 +2875,7 @@ version = "0.1.0" dependencies = [ "bevy", "combat", + "resources", ] [[package]] @@ -4324,6 +4330,14 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "19b30a45b0cd0bcca8037f3d0dc3421eaf95327a17cad11964fb8179b4fc4832" +[[package]] +name = "resources" +version = "0.1.0" +dependencies = [ + "bevy", + "state", +] + [[package]] name = "robust" version = "1.2.0" @@ -5075,6 +5089,13 @@ dependencies = [ "state", ] +[[package]] +name = "ui_ingame" +version = "0.1.0" +dependencies = [ + "bevy", +] + [[package]] name = "ui_pause" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 3e9f279..63eb6a6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -50,4 +50,6 @@ combat_damagers = { path = "crates/combat_damagers" } inhabitant = { path = "crates/inhabitant" } inhabitant_coranth = { path = "crates/inhabitant_coranth" } building_placement = { path = "crates/building_placement" } +ui_ingame = { path = "crates/ui_ingame" } +resources = { path = "crates/resources" } #new internal crates go here diff --git a/assets/settings/input_map.ron b/assets/settings/input_map.ron index 5b01783..4310980 100755 --- a/assets/settings/input_map.ron +++ b/assets/settings/input_map.ron @@ -3,15 +3,15 @@ MouseButton(Middle): [ CameraOrbitEnable, ], + MouseButton(Right): [ + PlaceTower, + ], MouseButton(Left): [ CameraPanEnable, ], KeyCode(F1): [ ToggleDebugUI, ], - MouseButton(Right): [ - PlaceTower, - ], MouseScroll: [ CameraZoom, ], diff --git a/crates/building/Cargo.toml b/crates/building/Cargo.toml index 6383d03..e8f3b7c 100755 --- a/crates/building/Cargo.toml +++ b/crates/building/Cargo.toml @@ -8,6 +8,7 @@ bevy = { workspace = true } avian3d = { workspace = true } combat = { path = "../combat" } state = { path = "../state" } +resources = { path = "../resources" } [lints] workspace = true diff --git a/crates/building/src/lib.rs b/crates/building/src/lib.rs index 88a92d8..e4ea04c 100755 --- a/crates/building/src/lib.rs +++ b/crates/building/src/lib.rs @@ -9,6 +9,7 @@ use bevy::{ prelude::*, }; use combat::{Health, Team}; +use resources::{Cost, Resources}; use state::GameSystemSet; pub struct BuildingPlugin; @@ -54,7 +55,7 @@ fn on_building_removed(mut world: DeferredWorld, context: HookContext) { } #[derive(Component, Clone)] -#[require(Name)] +#[require(Name, Cost)] #[component(on_add = register_building_info)] #[component(on_remove = unregister_building_info)] #[component(immutable)] @@ -197,14 +198,15 @@ fn place_building( mut reader: EventReader, mut building_map: ResMut, mut commands: Commands, - info_query: Query<&BuildingInfo>, + mut resources: ResMut, + info_query: Query<(&BuildingInfo, &Cost)>, ) { for PlaceBuilding { info: info_entity, location, } in reader.read() { - let Ok(info) = info_query.get(*info_entity) else { + let Ok((info, cost)) = info_query.get(*info_entity) else { error!("tried to place building without info!"); continue; }; @@ -212,6 +214,10 @@ fn place_building( error!("invalid building placement at {:?}, rejecting", location); continue; } + if !resources.buy(&cost) { + info!("not enough money to buy tower!"); + continue; + } let size_v3 = info.size.as_vec3(); let instance = commands .spawn(( diff --git a/crates/building_impaler/Cargo.toml b/crates/building_impaler/Cargo.toml index 342a3d2..32d83b7 100755 --- a/crates/building_impaler/Cargo.toml +++ b/crates/building_impaler/Cargo.toml @@ -10,6 +10,7 @@ avian3d = { workspace = true } state = { path = "../state" } combat = { path = "../combat" } building = { path = "../building" } +resources = { path = "../resources" } combat_damagers = { path = "../combat_damagers" } [lints] diff --git a/crates/building_impaler/src/lib.rs b/crates/building_impaler/src/lib.rs index 33918f7..d6d369d 100755 --- a/crates/building_impaler/src/lib.rs +++ b/crates/building_impaler/src/lib.rs @@ -9,6 +9,7 @@ use building::{ }; use combat::{Defense, Health}; use combat_damagers::ContactDamage; +use resources::Cost; pub struct BuildingImpalerPlugin; @@ -63,6 +64,7 @@ fn register( preview, size: SIZE, }, + Cost { souls: 5, bones: 0 }, Name::new("Impaler"), )); commands.insert_resource(ImpalerResources { diff --git a/crates/combat/Cargo.toml b/crates/combat/Cargo.toml index 386601e..552614d 100755 --- a/crates/combat/Cargo.toml +++ b/crates/combat/Cargo.toml @@ -9,6 +9,7 @@ avian3d = { workspace = true } state = { path = "../state" } physics = { path = "../physics"} +resources = { path = "../resources" } [lints] workspace = true diff --git a/crates/combat/src/lib.rs b/crates/combat/src/lib.rs index 56c6909..b3b9099 100755 --- a/crates/combat/src/lib.rs +++ b/crates/combat/src/lib.rs @@ -6,6 +6,7 @@ use std::time::Duration; use avian3d::prelude::*; use bevy::prelude::*; +use resources::{Drops, Resources}; use state::GameSystemSet; pub struct CombatPlugin; @@ -190,7 +191,17 @@ fn on_attack( } } -fn on_die(trigger: Trigger, names: Query<&Name>, mut commands: Commands) { +fn on_die( + trigger: Trigger, + names: Query<&Name>, + drops: Query<&Drops>, + mut resources: ResMut, + mut commands: Commands, +) { + if let Ok(drop) = drops.get(trigger.target()) { + resources.souls += drop.souls; + info!("you now have {} souls", resources.souls); + } if let Ok(mut ec) = commands.get_entity(trigger.target()) { info!( "{:?} killed {:?}!", diff --git a/crates/inhabitant/Cargo.toml b/crates/inhabitant/Cargo.toml index 0cb466f..144d2fe 100755 --- a/crates/inhabitant/Cargo.toml +++ b/crates/inhabitant/Cargo.toml @@ -6,6 +6,7 @@ edition = "2024" [dependencies] bevy = { workspace = true } combat = { path = "../combat" } +resources = { path = "../resources" } [lints] workspace = true diff --git a/crates/inhabitant/src/lib.rs b/crates/inhabitant/src/lib.rs index eda86cc..9559b24 100755 --- a/crates/inhabitant/src/lib.rs +++ b/crates/inhabitant/src/lib.rs @@ -4,6 +4,7 @@ use bevy::prelude::*; use combat::{Health, Team}; +use resources::Drops; pub struct InhabitantPlugin; @@ -12,5 +13,5 @@ impl Plugin for InhabitantPlugin { } #[derive(Component, Default)] -#[require(Health, Team::Inhabitant)] +#[require(Health, Team::Inhabitant, Drops)] pub struct Inhabitant; diff --git a/crates/inhabitant_coranth/src/lib.rs b/crates/inhabitant_coranth/src/lib.rs index ba7d383..66fa904 100755 --- a/crates/inhabitant_coranth/src/lib.rs +++ b/crates/inhabitant_coranth/src/lib.rs @@ -64,7 +64,6 @@ fn explode( trigger_query: Query<&GlobalTransform>, mut other_query: Query<(&GlobalTransform, &mut LinearVelocity)>, ) { - info!("blasthing!"); let Ok(gtf) = trigger_query.get(trigger.target()) else { return; }; @@ -78,7 +77,7 @@ fn explode( ) { if let Ok((gtf, mut v)) = other_query.get_mut(e) { let delta = gtf.translation() - origin; - v.0 += delta.normalize_or_zero() * 10.0; + v.0 += delta.normalize_or_zero() * 2.0; } } } diff --git a/crates/resources/Cargo.toml b/crates/resources/Cargo.toml new file mode 100644 index 0000000..bd4c8ac --- /dev/null +++ b/crates/resources/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "resources" +version = "0.1.0" +edition = "2024" + +[dependencies] +bevy = { workspace = true } + +state = { path = "../state" } + +[lints] +workspace = true diff --git a/crates/resources/src/lib.rs b/crates/resources/src/lib.rs new file mode 100644 index 0000000..ed3fc25 --- /dev/null +++ b/crates/resources/src/lib.rs @@ -0,0 +1,58 @@ +//bevy system signatures often violate these rules +#![allow(clippy::type_complexity)] +#![allow(clippy::too_many_arguments)] + +use bevy::prelude::*; +use state::AppState; + +pub struct ResourcesPlugin; + +impl Plugin for ResourcesPlugin { + fn build(&self, app: &mut App) { + app.add_systems(OnEnter(AppState::Game), setup); + } +} + +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 + } +} + +#[derive(Component, Default)] +pub struct Cost { + pub souls: u32, + pub bones: u32, +} + +#[derive(Component)] +pub struct Drops { + pub souls: u32, +} + +impl Default for Drops { + fn default() -> Self { + Self { souls: 1 } + } +} diff --git a/crates/ui_ingame/Cargo.toml b/crates/ui_ingame/Cargo.toml new file mode 100644 index 0000000..be861c0 --- /dev/null +++ b/crates/ui_ingame/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "ui_ingame" +version = "0.1.0" +edition = "2024" + +[dependencies] +bevy = { workspace = true } + +[lints] +workspace = true diff --git a/crates/ui_ingame/src/lib.rs b/crates/ui_ingame/src/lib.rs new file mode 100644 index 0000000..6520116 --- /dev/null +++ b/crates/ui_ingame/src/lib.rs @@ -0,0 +1,11 @@ +//bevy system signatures often violate these rules +#![allow(clippy::type_complexity)] +#![allow(clippy::too_many_arguments)] + +use bevy::prelude::*; + +pub struct UiIngamePlugin; + +impl Plugin for UiIngamePlugin { + fn build(&self, app: &mut App) {} +} diff --git a/crates/ui_pause/src/lib.rs b/crates/ui_pause/src/lib.rs index 0fa52cd..5bf1cbb 100755 --- a/crates/ui_pause/src/lib.rs +++ b/crates/ui_pause/src/lib.rs @@ -4,7 +4,7 @@ use bevy::prelude::*; use input::{actions::InputAction, conditions::action_just_pressed}; -use state::{GameState, GameSystemSet}; +use state::GameState; pub struct UiPausePlugin; @@ -12,9 +12,7 @@ impl Plugin for UiPausePlugin { fn build(&self, app: &mut App) { app.add_systems( Update, - toggle_pause - .in_set(GameSystemSet::PreUpdate) - .run_if(action_just_pressed::(InputAction::TogglePause)), + toggle_pause.run_if(action_just_pressed::(InputAction::TogglePause)), ); } } diff --git a/src/main.rs b/src/main.rs index 9bd3ffd..18a2b20 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,6 +32,8 @@ fn main() { inhabitant::InhabitantPlugin, inhabitant_coranth::InhabitantCoranthPlugin, building_placement::BuildingPlacementPlugin, + ui_ingame::UiIngamePlugin, + resources::ResourcesPlugin, // new internal crates go here ));