From 921947c1314ad2f4349dde352fe9372bd39408d1 Mon Sep 17 00:00:00 2001 From: Jim Date: Sun, 31 Aug 2025 22:26:07 -0400 Subject: [PATCH] resource production via graveyard, workers, and selectable buildlings --- Cargo.lock | 16 ++++++ Cargo.toml | 1 + assets/settings/input_map.ron | 8 +-- crates/building/Cargo.toml | 1 + crates/building/src/lib.rs | 3 +- crates/building_barrager/src/lib.rs | 7 ++- crates/building_impaler/src/lib.rs | 4 +- crates/building_placement/src/lib.rs | 5 +- crates/combat/src/lib.rs | 1 - crates/input/Cargo.toml | 1 + crates/input/src/lib.rs | 79 +++++++++++++++++++++++++-- crates/level/Cargo.toml | 1 + crates/level/src/lib.rs | 11 ++++ crates/resource_graveyard/Cargo.toml | 15 ++++++ crates/resource_graveyard/src/lib.rs | 81 ++++++++++++++++++++++++++++ crates/resources/src/lib.rs | 80 +++++++++++++++++++++++++-- crates/ui_debug/src/lib.rs | 4 +- crates/ui_ingame/Cargo.toml | 1 + crates/ui_ingame/src/lib.rs | 68 +++++++++++++++++++++-- src/main.rs | 1 + 20 files changed, 364 insertions(+), 24 deletions(-) create mode 100644 crates/resource_graveyard/Cargo.toml create mode 100644 crates/resource_graveyard/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 64f9e7e..1ac0f5d 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -1627,6 +1627,7 @@ dependencies = [ "avian3d", "bevy", "combat", + "input", "resources", "state", ] @@ -2003,6 +2004,7 @@ dependencies = [ "input", "level", "physics", + "resource_graveyard", "resources", "setup", "state", @@ -3059,6 +3061,7 @@ dependencies = [ name = "input" version = "0.1.0" dependencies = [ + "avian3d", "bevy", "ron 0.10.1", "serde", @@ -3191,6 +3194,7 @@ dependencies = [ "combat", "inhabitant_coranth", "physics", + "resource_graveyard", "state", ] @@ -4490,6 +4494,17 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "19b30a45b0cd0bcca8037f3d0dc3421eaf95327a17cad11964fb8179b4fc4832" +[[package]] +name = "resource_graveyard" +version = "0.1.0" +dependencies = [ + "avian3d", + "bevy", + "input", + "resources", + "state", +] + [[package]] name = "resources" version = "0.1.0" @@ -5278,6 +5293,7 @@ dependencies = [ "bevy_egui", "building", "building_placement", + "input", "resources", "ui_state", ] diff --git a/Cargo.toml b/Cargo.toml index 9bdfa13..d6e01cd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -54,4 +54,5 @@ building_placement = { path = "crates/building_placement" } ui_ingame = { path = "crates/ui_ingame" } resources = { path = "crates/resources" } building_barrager = { path = "crates/building_barrager" } +resource_graveyard = { path = "crates/resource_graveyard" } #new internal crates go here diff --git a/assets/settings/input_map.ron b/assets/settings/input_map.ron index ed21f50..03d50a5 100755 --- a/assets/settings/input_map.ron +++ b/assets/settings/input_map.ron @@ -1,7 +1,7 @@ ( map: { - MouseButton(Middle): [ - CameraOrbitEnable, + KeyCode(ShiftLeft): [ + MultiPlaceTower, ], MouseButton(Right): [ PlaceTower, @@ -9,8 +9,8 @@ MouseButton(Left): [ CameraPanEnable, ], - KeyCode(ShiftLeft): [ - MultiPlaceTower, + MouseButton(Middle): [ + CameraOrbitEnable, ], MouseScroll: [ CameraZoom, diff --git a/crates/building/Cargo.toml b/crates/building/Cargo.toml index e8f3b7c..c38ce76 100755 --- a/crates/building/Cargo.toml +++ b/crates/building/Cargo.toml @@ -9,6 +9,7 @@ avian3d = { workspace = true } combat = { path = "../combat" } state = { path = "../state" } resources = { path = "../resources" } +input = { path = "../input" } [lints] workspace = true diff --git a/crates/building/src/lib.rs b/crates/building/src/lib.rs index 84287e6..9b4208f 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 input::Selectable; use resources::{Cost, Resources}; use state::GameSystemSet; @@ -24,7 +25,7 @@ impl Plugin for BuildingPlugin { } #[derive(Component)] -#[require(Health, Team::Core, PhysicsPickable)] +#[require(Health, Team::Core, PhysicsPickable, Selectable)] pub struct Building { pub level: u32, pub info: Entity, diff --git a/crates/building_barrager/src/lib.rs b/crates/building_barrager/src/lib.rs index 96b2dfc..4ea0a65 100644 --- a/crates/building_barrager/src/lib.rs +++ b/crates/building_barrager/src/lib.rs @@ -12,7 +12,7 @@ use combat::{ projectiles::{Fire, Projectile, ProjectileLauncher, get_shotgun_launch_vectors}, }; use combat_damagers::ContactDamage; -use resources::Cost; +use resources::{Cost, Quantity}; pub struct BuildingBarragerPlugin; @@ -72,7 +72,10 @@ fn register( size: SIZE, unlocked: true, }, - Cost { souls: 0, bones: 0 }, + Cost(Quantity { + souls: 20, + bones: 20, + }), Name::new("Barrager"), )); commands.insert_resource(BuildingResources { diff --git a/crates/building_impaler/src/lib.rs b/crates/building_impaler/src/lib.rs index 8673b68..c724130 100755 --- a/crates/building_impaler/src/lib.rs +++ b/crates/building_impaler/src/lib.rs @@ -6,7 +6,7 @@ use bevy::prelude::*; use building::{BuildingInfo, BuildingPreview, PREVIEW_ALPHA, PopulatePlacedBuilding}; use combat::{Defense, Health}; use combat_damagers::ContactDamage; -use resources::Cost; +use resources::{Cost, Quantity}; pub struct BuildingImpalerPlugin; @@ -62,7 +62,7 @@ fn register( size: SIZE, unlocked: true, }, - Cost { souls: 5, bones: 0 }, + Cost(Quantity { souls: 5, bones: 0 }), Name::new("Impaler"), )); commands.insert_resource(ImpalerResources { diff --git a/crates/building_placement/src/lib.rs b/crates/building_placement/src/lib.rs index df497dc..b66d898 100644 --- a/crates/building_placement/src/lib.rs +++ b/crates/building_placement/src/lib.rs @@ -4,7 +4,7 @@ use bevy::prelude::*; use building::{BuildingInfo, BuildingPreview, PlaceBuilding, PlacedBuildings}; -use input::{Cancellable, Inputs, actions::InputAction}; +use input::{Cancellable, Inputs, Selected, actions::InputAction}; use physics::Ground; pub struct BuildingPlacementPlugin; @@ -37,9 +37,10 @@ fn init(mut commands: Commands, query: Single>, mut setup: fn cancel_preview( mut writer: EventWriter, query: Query<(), (With, With)>, + selected: Query<(), With>, inputs: Res>, ) { - if !query.is_empty() && inputs.just_pressed(InputAction::Cancel) { + if !query.is_empty() && (inputs.just_pressed(InputAction::Cancel) || !selected.is_empty()) { writer.write(SelectBuildingPreview(None)); } } diff --git a/crates/combat/src/lib.rs b/crates/combat/src/lib.rs index 99f373a..41d0663 100755 --- a/crates/combat/src/lib.rs +++ b/crates/combat/src/lib.rs @@ -210,7 +210,6 @@ fn on_die( ) { 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!( diff --git a/crates/input/Cargo.toml b/crates/input/Cargo.toml index 3d93ef6..3523ab6 100755 --- a/crates/input/Cargo.toml +++ b/crates/input/Cargo.toml @@ -5,6 +5,7 @@ edition = "2024" [dependencies] bevy = { workspace = true } +avian3d = { workspace = true } serde = { workspace = true } ron = { workspace = true } state = { path = "../state" } diff --git a/crates/input/src/lib.rs b/crates/input/src/lib.rs index 2cb1364..e038b73 100755 --- a/crates/input/src/lib.rs +++ b/crates/input/src/lib.rs @@ -7,6 +7,7 @@ pub mod conditions; use std::{fs, marker::PhantomData}; +use avian3d::prelude::*; use bevy::{ input::mouse::{AccumulatedMouseMotion, AccumulatedMouseScroll}, platform::collections::HashMap, @@ -22,21 +23,93 @@ pub struct InputPlugin; impl Plugin for InputPlugin { fn build(&self, app: &mut App) { - app.init_resource::>() + app.add_event::() + .init_resource::>() .init_resource::>() .configure_sets(Update, GameSystemSet::UpdateInputs) .add_systems(Startup, setup_input_map) .add_systems( Update, - (update_inputs_frame, save_input_map).in_set(GameSystemSet::UpdateInputs), + ( + update_inputs_frame, + save_input_map, + (cancel_selection, update_selection).chain(), + ) + .in_set(GameSystemSet::UpdateInputs), ) - .add_systems(FixedPostUpdate, clear_changed_fixed); + .add_systems(FixedPostUpdate, clear_changed_fixed) + .add_observer(select); } } #[derive(Component)] pub struct Cancellable; +#[derive(Component, Default)] +pub struct Selectable; + +#[derive(Component)] +pub struct Selected; + +#[derive(Event)] +pub struct SelectEntity(pub Option); + +fn cancel_selection( + mut writer: EventWriter, + query: Query<(), (With, With)>, + inputs: Res>, +) { + if !query.is_empty() && inputs.just_pressed(InputAction::Cancel) { + writer.write(SelectEntity(None)); + } +} + +fn select( + trigger: Trigger>, + physics_query: Query<(), With>, + mut writer: EventWriter, +) { + // avoid deselecting when clicking on ui + if physics_query.contains(trigger.event().target) { + writer.write(SelectEntity(Some(trigger.event().target))); + } +} + +fn update_selection( + mut reader: EventReader, + mut old_query: Query>, + new_query: Query<(), With>, + name_query: Query<&Name>, + mut commands: Commands, +) { + let Some(SelectEntity(opt_entity)) = reader.read().last() else { + return; + }; + let Some(new_entity) = opt_entity else { + for entity in old_query.iter_mut() { + if *opt_entity != Some(entity) { + commands.entity(entity).remove::<(Selected, Cancellable)>(); + } + } + return; + }; + if !new_query.contains(*new_entity) { + info!( + "thing is not selectable {:?} {:?}", + new_entity, + name_query.get(*new_entity) + ); + return; + } + for entity in old_query.iter_mut() { + if *opt_entity != Some(entity) { + commands.entity(entity).remove::<(Selected, Cancellable)>(); + } + } + + commands.entity(*new_entity).insert((Selected, Cancellable)); +} + #[derive(Hash, Clone, Copy, Debug, Reflect, PartialEq, Eq, Serialize, Deserialize)] pub enum InputMethod { Unbound, diff --git a/crates/level/Cargo.toml b/crates/level/Cargo.toml index 925b618..9d7d651 100755 --- a/crates/level/Cargo.toml +++ b/crates/level/Cargo.toml @@ -14,6 +14,7 @@ combat = { path = "../combat" } physics = { path = "../physics" } building = { path = "../building" } inhabitant_coranth = { path = "../inhabitant_coranth" } +resource_graveyard = { path = "../resource_graveyard" } [lints] workspace = true diff --git a/crates/level/src/lib.rs b/crates/level/src/lib.rs index c2fde0c..7c1b0ee 100755 --- a/crates/level/src/lib.rs +++ b/crates/level/src/lib.rs @@ -16,6 +16,7 @@ use building::{BuildingTypes, PlaceBuilding}; use camera::ControlledCamera; use inhabitant_coranth::SpawnCoranth; use physics::{Ground, PhysicsLayers}; +use resource_graveyard::PlaceGraveyard; use state::{AppState, GameSystemSet}; pub struct LevelPlugin; @@ -34,6 +35,7 @@ fn setup( buildings: Res, mut place_writer: EventWriter, mut coranth_writer: EventWriter, + mut graveyard_writer: EventWriter, ) { let level_size = 50.0; // Static physics object with a collision shape @@ -50,6 +52,7 @@ fn setup( CollisionLayers::new(PhysicsLayers::Ground, LayerMask::ALL), PhysicsPickable, Ground, + Name::new("ground"), )); for y in 5..15 { @@ -57,6 +60,9 @@ fn setup( transform: Transform::from_xyz(y as f32 * 0.25, y as f32 * 1.1, 0.0), }); } + + graveyard_writer.write(PlaceGraveyard(Vec3::new(-10., 0.25, -10.))); + place_writer.write(PlaceBuilding { info: buildings.info.get("Core").unwrap().0, location: IVec3 { x: 5, y: 0, z: 5 }, @@ -72,6 +78,11 @@ fn setup( }, Transform::from_xyz(-2.5, 4.5, 2.5).looking_at(Vec3::ZERO, Dir3::Y), )); + commands.insert_resource(AmbientLight { + brightness: lux::AMBIENT_DAYLIGHT, + color: Color::srgb(0.98, 0.95, 0.82), + ..default() + }); // Camera commands.spawn(( diff --git a/crates/resource_graveyard/Cargo.toml b/crates/resource_graveyard/Cargo.toml new file mode 100644 index 0000000..9e219aa --- /dev/null +++ b/crates/resource_graveyard/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "resource_graveyard" +version = "0.1.0" +edition = "2024" + +[dependencies] +bevy = { workspace = true } +avian3d = {workspace = true } + +state = { path = "../state" } +resources = { path = "../resources" } +input = { path = "../input" } + +[lints] +workspace = true diff --git a/crates/resource_graveyard/src/lib.rs b/crates/resource_graveyard/src/lib.rs new file mode 100644 index 0000000..dbf3535 --- /dev/null +++ b/crates/resource_graveyard/src/lib.rs @@ -0,0 +1,81 @@ +//bevy system signatures often violate these rules +#![allow(clippy::type_complexity)] +#![allow(clippy::too_many_arguments)] + +use std::time::Duration; + +use avian3d::prelude::{Collider, PhysicsPickable, RigidBody, Sensor}; +use bevy::prelude::*; +use input::Selectable; +use resources::{Quantity, ResourceDeposit}; +use state::GameSystemSet; + +pub struct ResourceGraveyardPlugin; + +impl Plugin for ResourceGraveyardPlugin { + fn build(&self, app: &mut App) { + app.add_event::() + .add_systems(Startup, init) + .add_systems(FixedUpdate, spawn.in_set(GameSystemSet::PostTick)); + } +} + +#[derive(Component)] +pub struct Graveyard; + +#[derive(Event)] +pub struct PlaceGraveyard(pub Vec3); + +#[derive(Resource)] +struct GraveyardResources { + mesh: Handle, + material: Handle, +} + +const SIZE: Vec3 = Vec3::new(2.0, 0.5, 2.0); + +fn init( + mut commands: Commands, + mut meshes: ResMut>, + mut materials: ResMut>, +) { + let mesh = meshes.add(Cuboid::from_size(SIZE)); + let base_material = StandardMaterial { + base_color: Color::srgb_u8(0xff, 0xe2, 0xe2), + metallic: 0.2, + perceptual_roughness: 0.8, + ..default() + }; + let material = materials.add(base_material); + + commands.insert_resource(GraveyardResources { + mesh: mesh, + material: material, + }); +} + +fn spawn( + mut reader: EventReader, + mut commands: Commands, + res: Res, +) { + for PlaceGraveyard(dest) in reader.read() { + commands.spawn(( + Graveyard, + Transform::from_translation(*dest), + ResourceDeposit { + bounty: Quantity { souls: 0, bones: 5 }, + level: 0, + harvest_time: Duration::from_secs(5), + }, + Mesh3d(res.mesh.clone()), + MeshMaterial3d(res.material.clone()), + Sensor, + RigidBody::Static, + Collider::cuboid(SIZE.x, SIZE.y, SIZE.z), + PhysicsPickable, + Selectable, + Name::new("Graveyard"), + )); + } +} diff --git a/crates/resources/src/lib.rs b/crates/resources/src/lib.rs index ed3fc25..432033a 100644 --- a/crates/resources/src/lib.rs +++ b/crates/resources/src/lib.rs @@ -2,14 +2,19 @@ #![allow(clippy::type_complexity)] #![allow(clippy::too_many_arguments)] +use std::time::Duration; + use bevy::prelude::*; -use state::AppState; +use state::{AppState, GameSystemSet}; pub struct ResourcesPlugin; impl Plugin for ResourcesPlugin { fn build(&self, app: &mut App) { - app.add_systems(OnEnter(AppState::Game), setup); + app.add_systems(OnEnter(AppState::Game), setup) + .add_systems(FixedUpdate, tick_harvest.in_set(GameSystemSet::Execute)) + .insert_resource(AvailableWorkers(5)) + .add_observer(on_harvest); } } @@ -38,14 +43,32 @@ impl Resources { 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)] -pub struct Cost { +#[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, @@ -56,3 +79,52 @@ impl Default for Drops { 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