From 6a4978041c394cf8e96b78c3350f3705c0588f72 Mon Sep 17 00:00:00 2001 From: Jim Date: Mon, 1 Sep 2025 19:10:16 -0400 Subject: [PATCH] range indicators, limit tower placement to within range of core --- Cargo.lock | 13 ++ Cargo.toml | 1 + assets/settings/input_map.ron | 16 +-- crates/building/src/lib.rs | 24 +++- crates/building_barrager/src/lib.rs | 4 +- crates/building_core/Cargo.toml | 1 + crates/building_core/src/lib.rs | 17 ++- crates/building_placement/Cargo.toml | 2 + crates/building_placement/src/lib.rs | 87 +++++++----- crates/combat/src/lib.rs | 9 ++ crates/combat/src/projectiles.rs | 19 +-- crates/state/src/lib.rs | 2 +- crates/ui_ingame/src/lib.rs | 4 +- crates/ui_range_indicator/Cargo.toml | 13 ++ crates/ui_range_indicator/src/lib.rs | 197 +++++++++++++++++++++++++++ src/main.rs | 1 + 16 files changed, 350 insertions(+), 60 deletions(-) create mode 100644 crates/ui_range_indicator/Cargo.toml create mode 100644 crates/ui_range_indicator/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index d464352..570e4aa 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -1656,6 +1656,7 @@ dependencies = [ "building", "combat", "state", + "ui_range_indicator", ] [[package]] @@ -1677,8 +1678,10 @@ dependencies = [ "avian3d", "bevy", "building", + "combat", "input", "physics", + "ui_range_indicator", ] [[package]] @@ -2016,6 +2019,7 @@ dependencies = [ "ui_health_bar", "ui_ingame", "ui_pause", + "ui_range_indicator", "ui_state", "upgrade", ] @@ -5325,6 +5329,15 @@ dependencies = [ "ui_state", ] +[[package]] +name = "ui_range_indicator" +version = "0.1.0" +dependencies = [ + "bevy", + "combat", + "input", +] + [[package]] name = "ui_state" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 1456ab4..02dfe34 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -57,4 +57,5 @@ building_barrager = { path = "crates/building_barrager" } resource_graveyard = { path = "crates/resource_graveyard" } upgrade = { path = "crates/upgrade" } ui_health_bar = { path = "crates/ui_health_bar" } +ui_range_indicator = { path = "crates/ui_range_indicator" } #new internal crates go here diff --git a/assets/settings/input_map.ron b/assets/settings/input_map.ron index ed21f50..954bb8b 100755 --- a/assets/settings/input_map.ron +++ b/assets/settings/input_map.ron @@ -1,22 +1,22 @@ ( map: { + KeyCode(ShiftLeft): [ + MultiPlaceTower, + ], MouseButton(Middle): [ CameraOrbitEnable, ], + KeyCode(F1): [ + ToggleDebugUI, + ], MouseButton(Right): [ PlaceTower, ], - MouseButton(Left): [ - CameraPanEnable, - ], - KeyCode(ShiftLeft): [ - MultiPlaceTower, - ], MouseScroll: [ CameraZoom, ], - KeyCode(F1): [ - ToggleDebugUI, + MouseButton(Left): [ + CameraPanEnable, ], MouseMovementX: [ CameraYaw, diff --git a/crates/building/src/lib.rs b/crates/building/src/lib.rs index 53ad324..396f425 100755 --- a/crates/building/src/lib.rs +++ b/crates/building/src/lib.rs @@ -8,7 +8,7 @@ use bevy::{ platform::collections::HashMap, prelude::*, }; -use combat::{Health, Team}; +use combat::{Health, Range, Team}; use input::Selectable; use resources::{Cost, Resources}; use state::GameSystemSet; @@ -32,6 +32,10 @@ pub struct Building { pub info: Entity, } +#[derive(Component)] +#[require(Range)] +pub struct Core; + #[derive(Component, Clone)] #[require(Name, Cost, UpgradeTree)] #[component(on_add = register_building_info)] @@ -114,7 +118,13 @@ pub fn valid_placement( collider_query: &Query<&Building>, size: Vec3, position: Vec3, + core_position_range: Option<(Vec3, f32)>, ) -> bool { + if let Some((core_position, core_range)) = core_position_range + && position.distance_squared(core_position) > core_range * core_range + { + return false; + } !spatial_query .shape_intersections( // shrink the collider a bit to allow for buildings right next to each other @@ -133,6 +143,7 @@ fn place_building( mut resources: ResMut, info_query: Query<(&BuildingInfo, &Cost, &Name), With>, building_query: Query<&Building>, + core_query: Query<(&GlobalTransform, &Range), With>, spatial_query: SpatialQuery, ) { for PlaceBuilding { @@ -150,7 +161,16 @@ fn place_building( } let size_v3 = info.size.as_vec3(); let (collider, position) = info.get_placed_collider_and_position(location.as_vec3()); - if !valid_placement(&spatial_query, &building_query, size_v3, position) { + if !valid_placement( + &spatial_query, + &building_query, + size_v3, + position, + core_query + .single() + .ok() + .map(|(gtf, r)| (gtf.translation(), r.0)), + ) { error!("tried to place building colliding with other building"); continue; } diff --git a/crates/building_barrager/src/lib.rs b/crates/building_barrager/src/lib.rs index 2f6cc6a..33bd1e2 100644 --- a/crates/building_barrager/src/lib.rs +++ b/crates/building_barrager/src/lib.rs @@ -8,7 +8,7 @@ use avian3d::prelude::{Collider, LinearVelocity, RigidBody}; use bevy::prelude::*; use building::{BuildingInfo, BuildingPreview, PREVIEW_ALPHA, PopulatePlacedBuilding}; use combat::{ - Defense, Health, + Defense, Health, Range, projectiles::{Fire, Projectile, ProjectileLauncher, get_shotgun_launch_vectors}, }; use combat_damagers::ContactDamage; @@ -181,9 +181,9 @@ fn populate( Health::new(20.0), Defense::new(7.0), ContactDamage::new(2.0), + Range(10.0), ProjectileLauncher::new( Duration::from_secs(1), - 50.0, 5.0, Vec3::Y * (SIZE.y as f32 + 0.5), Duration::from_secs(10), diff --git a/crates/building_core/Cargo.toml b/crates/building_core/Cargo.toml index ff3173d..33bfc44 100755 --- a/crates/building_core/Cargo.toml +++ b/crates/building_core/Cargo.toml @@ -9,6 +9,7 @@ bevy = { workspace = true } combat = { path = "../combat" } building = { path = "../building" } state = { path = "../state" } +ui_range_indicator = { path = "../ui_range_indicator" } [lints] workspace = true diff --git a/crates/building_core/src/lib.rs b/crates/building_core/src/lib.rs index 3d114c1..bd8ce97 100755 --- a/crates/building_core/src/lib.rs +++ b/crates/building_core/src/lib.rs @@ -3,8 +3,9 @@ #![allow(clippy::too_many_arguments)] use bevy::prelude::*; -use building::{BuildingInfo, BuildingPreview, PREVIEW_ALPHA, PopulatePlacedBuilding}; -use combat::{Defense, Health}; +use building::{BuildingInfo, BuildingPreview, Core, PREVIEW_ALPHA, PopulatePlacedBuilding}; +use combat::{Defense, Health, Range}; +use ui_range_indicator::{CustomRangeIndicator, DisplayCustomRangeIndicator}; pub struct BuildingCorePlugin; @@ -14,9 +15,6 @@ impl Plugin for BuildingCorePlugin { } } -#[derive(Component)] -pub struct Core; - #[derive(Resource)] struct CoreResources { mesh: Handle, @@ -72,6 +70,7 @@ fn register( fn populate( trigger: Trigger, mut commands: Commands, + mut materials: ResMut>, res: Res, ) { if trigger.event().info != res.info_entity { @@ -81,7 +80,15 @@ fn populate( Core, Health::new(100.0), Defense::new(5.0), + Range(20.0), Mesh3d(res.mesh.clone()), MeshMaterial3d(res.material.clone()), + CustomRangeIndicator::new(materials.add(StandardMaterial { + alpha_mode: AlphaMode::Blend, + base_color: Color::srgba(0.6, 0.2, 0.2, 0.4), + unlit: true, + double_sided: true, + ..default() + })), )); } diff --git a/crates/building_placement/Cargo.toml b/crates/building_placement/Cargo.toml index abc807c..4f90c75 100644 --- a/crates/building_placement/Cargo.toml +++ b/crates/building_placement/Cargo.toml @@ -10,6 +10,8 @@ avian3d = { workspace = true } building = { path = "../building" } physics = { path = "../physics" } input = { path = "../input" } +combat = { path = "../combat" } +ui_range_indicator = { path = "../ui_range_indicator" } [lints] workspace = true diff --git a/crates/building_placement/src/lib.rs b/crates/building_placement/src/lib.rs index bee063b..04ed9b0 100644 --- a/crates/building_placement/src/lib.rs +++ b/crates/building_placement/src/lib.rs @@ -4,9 +4,11 @@ use avian3d::prelude::SpatialQuery; use bevy::prelude::*; -use building::{Building, BuildingInfo, BuildingPreview, PlaceBuilding, valid_placement}; +use building::{Building, BuildingInfo, BuildingPreview, Core, PlaceBuilding, valid_placement}; +use combat::Range; use input::{Cancellable, Inputs, Selected, actions::InputAction}; use physics::Ground; +use ui_range_indicator::DisplayCustomRangeIndicator; pub struct BuildingPlacementPlugin; @@ -46,11 +48,59 @@ fn cancel_preview( } } +fn select_preview( + mut reader: EventReader, + mut old_query: Query<(Entity, &mut Visibility), With>, + core_query: Query>, + new_query: Query<(), With>, + mut commands: Commands, +) { + let Some(SelectBuildingPreview(opt_entity)) = reader.read().last() else { + return; + }; + for (entity, mut vis) in old_query.iter_mut() { + if *opt_entity != Some(entity) { + commands + .entity(entity) + .remove::<(ActiveBuildingPreview, Cancellable)>(); + *vis = Visibility::Hidden; + } + } + let Some(new_entity) = opt_entity else { + if let Ok(core_entity) = core_query.single() { + commands + .entity(core_entity) + .remove::(); + } + return; + }; + if !new_query.contains(*new_entity) { + error!("tried to select a building without a preview!"); + if let Ok(core_entity) = core_query.single() { + commands + .entity(core_entity) + .remove::(); + } + return; + } + commands.entity(*new_entity).insert(( + Visibility::Visible, + ActiveBuildingPreview::default(), + Cancellable, + )); + if let Ok(core_entity) = core_query.single() { + commands + .entity(core_entity) + .insert(DisplayCustomRangeIndicator); + } +} + fn move_preview( trigger: Trigger>, preview: Single<(&mut Transform, &mut ActiveBuildingPreview, &BuildingPreview)>, info_query: Query<&BuildingInfo>, building_query: Query<&Building>, + core_query: Query<(&GlobalTransform, &Range), With>, spatial_query: SpatialQuery, ) { let Some(dest) = trigger.event().hit.position else { @@ -69,6 +119,10 @@ fn move_preview( &building_query, info.size.as_vec3(), position, + core_query + .single() + .ok() + .map(|(gtf, r)| (gtf.translation(), r.0)), ) { info!("invalid placement!!!!!"); return; @@ -96,34 +150,3 @@ fn place_preview( location: active.0, }); } - -fn select_preview( - mut reader: EventReader, - mut old_query: Query<(Entity, &mut Visibility), With>, - new_query: Query<(), With>, - mut commands: Commands, -) { - let Some(SelectBuildingPreview(opt_entity)) = reader.read().last() else { - return; - }; - for (entity, mut vis) in old_query.iter_mut() { - if *opt_entity != Some(entity) { - commands - .entity(entity) - .remove::<(ActiveBuildingPreview, Cancellable)>(); - *vis = Visibility::Hidden; - } - } - let Some(new_entity) = opt_entity else { - return; - }; - if !new_query.contains(*new_entity) { - error!("tried to select a building without a preview!"); - return; - } - commands.entity(*new_entity).insert(( - Visibility::Visible, - ActiveBuildingPreview::default(), - Cancellable, - )); -} diff --git a/crates/combat/src/lib.rs b/crates/combat/src/lib.rs index ca9bf79..0539e4e 100755 --- a/crates/combat/src/lib.rs +++ b/crates/combat/src/lib.rs @@ -253,3 +253,12 @@ fn tick_invulnerability( } } } + +#[derive(Component, Deref, DerefMut)] +pub struct Range(pub f32); + +impl Default for Range { + fn default() -> Self { + Self(5.) + } +} diff --git a/crates/combat/src/projectiles.rs b/crates/combat/src/projectiles.rs index 2257dc4..0c4c98c 100644 --- a/crates/combat/src/projectiles.rs +++ b/crates/combat/src/projectiles.rs @@ -10,7 +10,7 @@ use physics::PhysicsLayers; use rand::{Rng, thread_rng}; use state::GameSystemSet; -use crate::Team; +use crate::{Range, Team}; pub(crate) struct ProjectilePlugin; @@ -46,20 +46,18 @@ impl From<&ProjectileLauncher> for Projectile { } #[derive(Component, Clone)] -#[require(Team)] +#[require(Team, Range)] pub struct ProjectileLauncher { pub fire_timer: Timer, pub fire_offset: Vec3, pub spread: f32, pub lifetime: Duration, - pub range: f32, pub damage: f32, } impl ProjectileLauncher { pub fn new( fire_interval: Duration, - range: f32, damage: f32, fire_offset: Vec3, lifetime: Duration, @@ -67,7 +65,6 @@ impl ProjectileLauncher { ) -> Self { Self { fire_timer: Timer::new(fire_interval, TimerMode::Once), - range, spread, damage, fire_offset, @@ -91,13 +88,19 @@ pub struct Fire { fn fire_launcher( mut commands: Commands, - mut launcher_query: Query<(Entity, &mut ProjectileLauncher, &Team, &GlobalTransform)>, + mut launcher_query: Query<( + Entity, + &mut ProjectileLauncher, + &Range, + &Team, + &GlobalTransform, + )>, spatial_query: SpatialQuery, enemy_query: Query<(&Team, &GlobalTransform)>, time: Res