show range indicator when placing tower, cleanup tower info stats

This commit is contained in:
Jim 2025-09-01 21:37:18 -04:00
parent e6b04bbdd1
commit 3a25a1bd87
Signed by: jim
GPG key ID: 3D7D94BA53088BF4
9 changed files with 106 additions and 49 deletions

1
Cargo.lock generated
View file

@ -5335,6 +5335,7 @@ name = "ui_range_indicator"
version = "0.1.0"
dependencies = [
"bevy",
"building",
"combat",
"input",
]

View file

@ -8,7 +8,7 @@ use bevy::{
platform::collections::HashMap,
prelude::*,
};
use combat::{Health, Range, Team};
use combat::{Defense, Health, Range, Team};
use input::Selectable;
use resources::{Cost, Resources};
use state::GameSystemSet;
@ -61,6 +61,9 @@ pub struct BuildingPreview {
pub info: Entity,
}
#[derive(Component, Default)]
pub struct ActiveBuildingPreview(pub IVec3);
pub const PREVIEW_ALPHA: f32 = 0.5;
fn register_building_info(mut world: DeferredWorld, context: HookContext) {
@ -141,7 +144,17 @@ fn place_building(
mut reader: EventReader<PlaceBuilding>,
mut commands: Commands,
mut resources: ResMut<Resources>,
info_query: Query<(&BuildingInfo, &Cost, &Name), With<UpgradeTree>>,
info_query: Query<
(
&BuildingInfo,
&Cost,
&Name,
&Health,
&Defense,
Option<&Range>,
),
With<UpgradeTree>,
>,
building_query: Query<&Building>,
core_query: Query<(&GlobalTransform, &Range), With<Core>>,
spatial_query: SpatialQuery,
@ -151,7 +164,8 @@ fn place_building(
location,
} in reader.read()
{
let Ok((info, cost, name)) = info_query.get(*info_entity) else {
let Ok((info, cost, name, health, defense, opt_range)) = info_query.get(*info_entity)
else {
error!("tried to place building without info or required components!");
continue;
};
@ -175,27 +189,31 @@ fn place_building(
continue;
}
let instance = commands
.spawn((
// adjust model up slightly so base of all towers are aligned
Transform::from_translation(position),
Building {
level: 0,
info: *info_entity,
},
collider,
Mesh3d::default(),
name.clone(),
RigidBody::Static,
Team::Core.collision_layers(),
Upgradable::new(*info_entity),
BarSettings {
offset: size_v3.y / 2. + 0.5,
width: (size_v3.x.max(size_v3.z) * 8.).ceil() as u32,
..default()
},
))
.id();
let mut ec = commands.spawn((
// adjust model up slightly so base of all towers are aligned
Transform::from_translation(position),
Building {
level: 0,
info: *info_entity,
},
collider,
Mesh3d::default(),
name.clone(),
RigidBody::Static,
Team::Core.collision_layers(),
Upgradable::new(*info_entity),
*health,
*defense,
BarSettings {
offset: size_v3.y / 2. + 0.5,
width: (size_v3.x.max(size_v3.z) * 8.).ceil() as u32,
..default()
},
));
if let Some(range) = opt_range {
ec.insert(*range);
}
let instance = ec.id();
commands.trigger_targets(PopulatePlacedBuilding { info: *info_entity }, instance);
}

View file

@ -147,6 +147,9 @@ fn register(
size: SIZE,
unlocked: true,
},
Range(10.0),
Health::new(20.0),
Defense::new(7.0),
UpgradeTree { upgrades },
Cost(Quantity {
souls: 10,
@ -178,10 +181,7 @@ fn populate(
Barrager {
projectile_count: 5,
},
Health::new(20.0),
Defense::new(7.0),
ContactDamage::new(2.0),
Range(10.0),
ProjectileLauncher::new(
Duration::from_secs(1),
5.0,

View file

@ -59,6 +59,9 @@ fn register(
unlocked: false,
},
Name::new("Core"),
Health::new(100.0),
Defense::new(5.0),
Range(25.0),
));
commands.insert_resource(CoreResources {
mesh: mesh,
@ -78,9 +81,6 @@ fn populate(
}
commands.entity(trigger.target()).insert((
Core,
Health::new(100.0),
Defense::new(5.0),
Range(25.0),
Mesh3d(res.mesh.clone()),
MeshMaterial3d(res.material.clone()),
CustomRangeIndicator::new(materials.add(StandardMaterial {

View file

@ -63,6 +63,8 @@ fn register(
unlocked: true,
},
Cost(Quantity { souls: 5, bones: 0 }),
Health::new(20.0),
Defense::new(5.0),
Name::new("Impaler"),
));
commands.insert_resource(ImpalerResources {
@ -82,8 +84,6 @@ fn populate(
}
commands.entity(trigger.target()).insert((
Impaler,
Health::new(20.0),
Defense::new(5.0),
ContactDamage::new(5.0),
Mesh3d(res.mesh.clone()),
MeshMaterial3d(res.material.clone()),

View file

@ -4,7 +4,10 @@
use avian3d::prelude::SpatialQuery;
use bevy::prelude::*;
use building::{Building, BuildingInfo, BuildingPreview, Core, PlaceBuilding, valid_placement};
use building::{
ActiveBuildingPreview, Building, BuildingInfo, BuildingPreview, Core, PlaceBuilding,
valid_placement,
};
use combat::Range;
use input::{Cancellable, Inputs, Selected, actions::InputAction};
use physics::Ground;
@ -19,9 +22,6 @@ impl Plugin for BuildingPlacementPlugin {
}
}
#[derive(Component, Default)]
pub struct ActiveBuildingPreview(pub IVec3);
#[derive(Event)]
pub struct SelectBuildingPreview(pub Option<Entity>);

View file

@ -69,8 +69,8 @@ pub struct Died {
pub slayer: Option<Entity>,
}
#[derive(Component, Clone, Debug, Reflect)]
#[require(InvulnerabilityTime)]
#[derive(Component, Clone, Copy, Debug, Reflect)]
#[require(InvulnerabilityTime, Defense)]
pub struct Health {
pub max: f32,
pub current: f32,
@ -127,8 +127,7 @@ impl From<&InvulnerabilityTime> for Invulnerable {
}
}
#[derive(Component, Clone, Debug, Default)]
#[require(Health)]
#[derive(Component, Clone, Copy, Debug, Default)]
pub struct Defense {
pub amount: f32,
}
@ -254,7 +253,7 @@ fn tick_invulnerability(
}
}
#[derive(Component, Deref, DerefMut)]
#[derive(Component, Deref, DerefMut, Clone, Copy)]
pub struct Range(pub f32);
impl Range {

View file

@ -8,6 +8,7 @@ bevy = { workspace = true }
combat = { path = "../combat" }
input = { path = "../input" }
building = { path = "../building" }
[lints]
workspace = true

View file

@ -9,6 +9,7 @@ use bevy::{
pbr::{NotShadowCaster, NotShadowReceiver},
prelude::*,
};
use building::{ActiveBuildingPreview, BuildingPreview};
use combat::Range;
use input::Selected;
@ -40,6 +41,7 @@ pub struct DisplayCustomRangeIndicator;
#[derive(Resource)]
struct RangeResources {
base_mesh: Handle<Mesh>,
base_material: Handle<StandardMaterial>,
}
impl CustomRangeIndicator {
@ -114,6 +116,7 @@ fn init(
let mesh = meshes.add(Extrusion::new(Annulus::new(1000. - 0.1, 1000.), 1.0));
commands.insert_resource(RangeResources {
base_mesh: mesh.clone(),
base_material: material.clone(),
});
let selected_indicator = create_indicator(&mut commands, material, mesh);
commands
@ -127,8 +130,24 @@ fn display(
(Entity, &Range, &GlobalTransform),
(With<Selected>, Without<CustomRangeIndicator>),
>,
preview_query: Query<
(
Entity,
&BuildingPreview,
&GlobalTransform,
Option<&CustomRangeIndicator>,
),
With<ActiveBuildingPreview>,
>,
range_query: Query<&Range>,
selected_indicator_query: Single<
(&mut Transform, &mut Visibility, &mut Indicator, &mut Mesh3d),
(
&mut Transform,
&mut Visibility,
&mut Indicator,
&mut Mesh3d,
&mut MeshMaterial3d<StandardMaterial>,
),
With<SelectedIndicator>,
>,
custom_entity_query: Query<(
@ -143,7 +162,7 @@ fn display(
(&mut Transform, &mut Visibility, &mut Indicator, &mut Mesh3d),
Without<SelectedIndicator>,
>,
resources: Res<RangeResources>,
mut meshes: ResMut<Assets<Mesh>>,
) {
for (
@ -178,18 +197,37 @@ fn display(
tf.translation = gtf.translation().with_y(1.0);
}
//update selected entity
//update selected entity (or preview indicator)
{
let (mut tf, mut vis, mut indicator, mut mesh) = selected_indicator_query.into_inner();
let Ok((entity, range, gtf)) = selected_query.single() else {
*vis = Visibility::Hidden;
return;
};
let (mut tf, mut vis, mut indicator, mut mesh, mut mesh_material) =
selected_indicator_query.into_inner();
let (entity, range, gtf, material) =
if let Ok((entity, preview, gtf, opt_custom_indicator)) = preview_query.single() {
let Ok(range) = range_query.get(preview.info) else {
*vis = Visibility::Hidden;
return;
};
(
entity,
range,
gtf,
opt_custom_indicator
.map(|CustomRangeIndicator { material, .. }| material.clone())
.unwrap_or(resources.base_material.clone()),
)
} else if let Ok((entity, range, gtf)) = selected_query.single() {
(entity, range, gtf, resources.base_material.clone())
} else {
*vis = Visibility::Hidden;
return;
};
if entity != indicator.0 || indicator.1 != range.0 {
indicator.0 = entity;
indicator.1 = range.0;
// range or entity changed, generate new mesh
mesh.0 = meshes.add(Extrusion::new(Annulus::new(range.0 - 0.1, range.0), 1.0));
mesh_material.0 = material;
}
*vis = Visibility::Inherited;
tf.translation = gtf.translation().with_y(1.0);