show range indicator when placing tower, cleanup tower info stats
This commit is contained in:
parent
e6b04bbdd1
commit
3a25a1bd87
9 changed files with 106 additions and 49 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -5335,6 +5335,7 @@ name = "ui_range_indicator"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bevy",
|
"bevy",
|
||||||
|
"building",
|
||||||
"combat",
|
"combat",
|
||||||
"input",
|
"input",
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ use bevy::{
|
||||||
platform::collections::HashMap,
|
platform::collections::HashMap,
|
||||||
prelude::*,
|
prelude::*,
|
||||||
};
|
};
|
||||||
use combat::{Health, Range, Team};
|
use combat::{Defense, Health, Range, Team};
|
||||||
use input::Selectable;
|
use input::Selectable;
|
||||||
use resources::{Cost, Resources};
|
use resources::{Cost, Resources};
|
||||||
use state::GameSystemSet;
|
use state::GameSystemSet;
|
||||||
|
|
@ -61,6 +61,9 @@ pub struct BuildingPreview {
|
||||||
pub info: Entity,
|
pub info: Entity,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Component, Default)]
|
||||||
|
pub struct ActiveBuildingPreview(pub IVec3);
|
||||||
|
|
||||||
pub const PREVIEW_ALPHA: f32 = 0.5;
|
pub const PREVIEW_ALPHA: f32 = 0.5;
|
||||||
|
|
||||||
fn register_building_info(mut world: DeferredWorld, context: HookContext) {
|
fn register_building_info(mut world: DeferredWorld, context: HookContext) {
|
||||||
|
|
@ -141,7 +144,17 @@ fn place_building(
|
||||||
mut reader: EventReader<PlaceBuilding>,
|
mut reader: EventReader<PlaceBuilding>,
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
mut resources: ResMut<Resources>,
|
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>,
|
building_query: Query<&Building>,
|
||||||
core_query: Query<(&GlobalTransform, &Range), With<Core>>,
|
core_query: Query<(&GlobalTransform, &Range), With<Core>>,
|
||||||
spatial_query: SpatialQuery,
|
spatial_query: SpatialQuery,
|
||||||
|
|
@ -151,7 +164,8 @@ fn place_building(
|
||||||
location,
|
location,
|
||||||
} in reader.read()
|
} 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!");
|
error!("tried to place building without info or required components!");
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
|
@ -175,27 +189,31 @@ fn place_building(
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let instance = commands
|
let mut ec = commands.spawn((
|
||||||
.spawn((
|
// adjust model up slightly so base of all towers are aligned
|
||||||
// adjust model up slightly so base of all towers are aligned
|
Transform::from_translation(position),
|
||||||
Transform::from_translation(position),
|
Building {
|
||||||
Building {
|
level: 0,
|
||||||
level: 0,
|
info: *info_entity,
|
||||||
info: *info_entity,
|
},
|
||||||
},
|
collider,
|
||||||
collider,
|
Mesh3d::default(),
|
||||||
Mesh3d::default(),
|
name.clone(),
|
||||||
name.clone(),
|
RigidBody::Static,
|
||||||
RigidBody::Static,
|
Team::Core.collision_layers(),
|
||||||
Team::Core.collision_layers(),
|
Upgradable::new(*info_entity),
|
||||||
Upgradable::new(*info_entity),
|
*health,
|
||||||
BarSettings {
|
*defense,
|
||||||
offset: size_v3.y / 2. + 0.5,
|
BarSettings {
|
||||||
width: (size_v3.x.max(size_v3.z) * 8.).ceil() as u32,
|
offset: size_v3.y / 2. + 0.5,
|
||||||
..default()
|
width: (size_v3.x.max(size_v3.z) * 8.).ceil() as u32,
|
||||||
},
|
..default()
|
||||||
))
|
},
|
||||||
.id();
|
));
|
||||||
|
if let Some(range) = opt_range {
|
||||||
|
ec.insert(*range);
|
||||||
|
}
|
||||||
|
let instance = ec.id();
|
||||||
|
|
||||||
commands.trigger_targets(PopulatePlacedBuilding { info: *info_entity }, instance);
|
commands.trigger_targets(PopulatePlacedBuilding { info: *info_entity }, instance);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -147,6 +147,9 @@ fn register(
|
||||||
size: SIZE,
|
size: SIZE,
|
||||||
unlocked: true,
|
unlocked: true,
|
||||||
},
|
},
|
||||||
|
Range(10.0),
|
||||||
|
Health::new(20.0),
|
||||||
|
Defense::new(7.0),
|
||||||
UpgradeTree { upgrades },
|
UpgradeTree { upgrades },
|
||||||
Cost(Quantity {
|
Cost(Quantity {
|
||||||
souls: 10,
|
souls: 10,
|
||||||
|
|
@ -178,10 +181,7 @@ fn populate(
|
||||||
Barrager {
|
Barrager {
|
||||||
projectile_count: 5,
|
projectile_count: 5,
|
||||||
},
|
},
|
||||||
Health::new(20.0),
|
|
||||||
Defense::new(7.0),
|
|
||||||
ContactDamage::new(2.0),
|
ContactDamage::new(2.0),
|
||||||
Range(10.0),
|
|
||||||
ProjectileLauncher::new(
|
ProjectileLauncher::new(
|
||||||
Duration::from_secs(1),
|
Duration::from_secs(1),
|
||||||
5.0,
|
5.0,
|
||||||
|
|
|
||||||
|
|
@ -59,6 +59,9 @@ fn register(
|
||||||
unlocked: false,
|
unlocked: false,
|
||||||
},
|
},
|
||||||
Name::new("Core"),
|
Name::new("Core"),
|
||||||
|
Health::new(100.0),
|
||||||
|
Defense::new(5.0),
|
||||||
|
Range(25.0),
|
||||||
));
|
));
|
||||||
commands.insert_resource(CoreResources {
|
commands.insert_resource(CoreResources {
|
||||||
mesh: mesh,
|
mesh: mesh,
|
||||||
|
|
@ -78,9 +81,6 @@ fn populate(
|
||||||
}
|
}
|
||||||
commands.entity(trigger.target()).insert((
|
commands.entity(trigger.target()).insert((
|
||||||
Core,
|
Core,
|
||||||
Health::new(100.0),
|
|
||||||
Defense::new(5.0),
|
|
||||||
Range(25.0),
|
|
||||||
Mesh3d(res.mesh.clone()),
|
Mesh3d(res.mesh.clone()),
|
||||||
MeshMaterial3d(res.material.clone()),
|
MeshMaterial3d(res.material.clone()),
|
||||||
CustomRangeIndicator::new(materials.add(StandardMaterial {
|
CustomRangeIndicator::new(materials.add(StandardMaterial {
|
||||||
|
|
|
||||||
|
|
@ -63,6 +63,8 @@ fn register(
|
||||||
unlocked: true,
|
unlocked: true,
|
||||||
},
|
},
|
||||||
Cost(Quantity { souls: 5, bones: 0 }),
|
Cost(Quantity { souls: 5, bones: 0 }),
|
||||||
|
Health::new(20.0),
|
||||||
|
Defense::new(5.0),
|
||||||
Name::new("Impaler"),
|
Name::new("Impaler"),
|
||||||
));
|
));
|
||||||
commands.insert_resource(ImpalerResources {
|
commands.insert_resource(ImpalerResources {
|
||||||
|
|
@ -82,8 +84,6 @@ fn populate(
|
||||||
}
|
}
|
||||||
commands.entity(trigger.target()).insert((
|
commands.entity(trigger.target()).insert((
|
||||||
Impaler,
|
Impaler,
|
||||||
Health::new(20.0),
|
|
||||||
Defense::new(5.0),
|
|
||||||
ContactDamage::new(5.0),
|
ContactDamage::new(5.0),
|
||||||
Mesh3d(res.mesh.clone()),
|
Mesh3d(res.mesh.clone()),
|
||||||
MeshMaterial3d(res.material.clone()),
|
MeshMaterial3d(res.material.clone()),
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,10 @@
|
||||||
|
|
||||||
use avian3d::prelude::SpatialQuery;
|
use avian3d::prelude::SpatialQuery;
|
||||||
use bevy::prelude::*;
|
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 combat::Range;
|
||||||
use input::{Cancellable, Inputs, Selected, actions::InputAction};
|
use input::{Cancellable, Inputs, Selected, actions::InputAction};
|
||||||
use physics::Ground;
|
use physics::Ground;
|
||||||
|
|
@ -19,9 +22,6 @@ impl Plugin for BuildingPlacementPlugin {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component, Default)]
|
|
||||||
pub struct ActiveBuildingPreview(pub IVec3);
|
|
||||||
|
|
||||||
#[derive(Event)]
|
#[derive(Event)]
|
||||||
pub struct SelectBuildingPreview(pub Option<Entity>);
|
pub struct SelectBuildingPreview(pub Option<Entity>);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -69,8 +69,8 @@ pub struct Died {
|
||||||
pub slayer: Option<Entity>,
|
pub slayer: Option<Entity>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component, Clone, Debug, Reflect)]
|
#[derive(Component, Clone, Copy, Debug, Reflect)]
|
||||||
#[require(InvulnerabilityTime)]
|
#[require(InvulnerabilityTime, Defense)]
|
||||||
pub struct Health {
|
pub struct Health {
|
||||||
pub max: f32,
|
pub max: f32,
|
||||||
pub current: f32,
|
pub current: f32,
|
||||||
|
|
@ -127,8 +127,7 @@ impl From<&InvulnerabilityTime> for Invulnerable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component, Clone, Debug, Default)]
|
#[derive(Component, Clone, Copy, Debug, Default)]
|
||||||
#[require(Health)]
|
|
||||||
pub struct Defense {
|
pub struct Defense {
|
||||||
pub amount: f32,
|
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);
|
pub struct Range(pub f32);
|
||||||
|
|
||||||
impl Range {
|
impl Range {
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ bevy = { workspace = true }
|
||||||
|
|
||||||
combat = { path = "../combat" }
|
combat = { path = "../combat" }
|
||||||
input = { path = "../input" }
|
input = { path = "../input" }
|
||||||
|
building = { path = "../building" }
|
||||||
|
|
||||||
[lints]
|
[lints]
|
||||||
workspace = true
|
workspace = true
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ use bevy::{
|
||||||
pbr::{NotShadowCaster, NotShadowReceiver},
|
pbr::{NotShadowCaster, NotShadowReceiver},
|
||||||
prelude::*,
|
prelude::*,
|
||||||
};
|
};
|
||||||
|
use building::{ActiveBuildingPreview, BuildingPreview};
|
||||||
use combat::Range;
|
use combat::Range;
|
||||||
use input::Selected;
|
use input::Selected;
|
||||||
|
|
||||||
|
|
@ -40,6 +41,7 @@ pub struct DisplayCustomRangeIndicator;
|
||||||
#[derive(Resource)]
|
#[derive(Resource)]
|
||||||
struct RangeResources {
|
struct RangeResources {
|
||||||
base_mesh: Handle<Mesh>,
|
base_mesh: Handle<Mesh>,
|
||||||
|
base_material: Handle<StandardMaterial>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CustomRangeIndicator {
|
impl CustomRangeIndicator {
|
||||||
|
|
@ -114,6 +116,7 @@ fn init(
|
||||||
let mesh = meshes.add(Extrusion::new(Annulus::new(1000. - 0.1, 1000.), 1.0));
|
let mesh = meshes.add(Extrusion::new(Annulus::new(1000. - 0.1, 1000.), 1.0));
|
||||||
commands.insert_resource(RangeResources {
|
commands.insert_resource(RangeResources {
|
||||||
base_mesh: mesh.clone(),
|
base_mesh: mesh.clone(),
|
||||||
|
base_material: material.clone(),
|
||||||
});
|
});
|
||||||
let selected_indicator = create_indicator(&mut commands, material, mesh);
|
let selected_indicator = create_indicator(&mut commands, material, mesh);
|
||||||
commands
|
commands
|
||||||
|
|
@ -127,8 +130,24 @@ fn display(
|
||||||
(Entity, &Range, &GlobalTransform),
|
(Entity, &Range, &GlobalTransform),
|
||||||
(With<Selected>, Without<CustomRangeIndicator>),
|
(With<Selected>, Without<CustomRangeIndicator>),
|
||||||
>,
|
>,
|
||||||
|
preview_query: Query<
|
||||||
|
(
|
||||||
|
Entity,
|
||||||
|
&BuildingPreview,
|
||||||
|
&GlobalTransform,
|
||||||
|
Option<&CustomRangeIndicator>,
|
||||||
|
),
|
||||||
|
With<ActiveBuildingPreview>,
|
||||||
|
>,
|
||||||
|
range_query: Query<&Range>,
|
||||||
selected_indicator_query: Single<
|
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>,
|
With<SelectedIndicator>,
|
||||||
>,
|
>,
|
||||||
custom_entity_query: Query<(
|
custom_entity_query: Query<(
|
||||||
|
|
@ -143,7 +162,7 @@ fn display(
|
||||||
(&mut Transform, &mut Visibility, &mut Indicator, &mut Mesh3d),
|
(&mut Transform, &mut Visibility, &mut Indicator, &mut Mesh3d),
|
||||||
Without<SelectedIndicator>,
|
Without<SelectedIndicator>,
|
||||||
>,
|
>,
|
||||||
|
resources: Res<RangeResources>,
|
||||||
mut meshes: ResMut<Assets<Mesh>>,
|
mut meshes: ResMut<Assets<Mesh>>,
|
||||||
) {
|
) {
|
||||||
for (
|
for (
|
||||||
|
|
@ -178,18 +197,37 @@ fn display(
|
||||||
tf.translation = gtf.translation().with_y(1.0);
|
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 (mut tf, mut vis, mut indicator, mut mesh, mut mesh_material) =
|
||||||
let Ok((entity, range, gtf)) = selected_query.single() else {
|
selected_indicator_query.into_inner();
|
||||||
*vis = Visibility::Hidden;
|
|
||||||
return;
|
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 {
|
if entity != indicator.0 || indicator.1 != range.0 {
|
||||||
indicator.0 = entity;
|
indicator.0 = entity;
|
||||||
indicator.1 = range.0;
|
indicator.1 = range.0;
|
||||||
// range or entity changed, generate new mesh
|
// range or entity changed, generate new mesh
|
||||||
mesh.0 = meshes.add(Extrusion::new(Annulus::new(range.0 - 0.1, range.0), 1.0));
|
mesh.0 = meshes.add(Extrusion::new(Annulus::new(range.0 - 0.1, range.0), 1.0));
|
||||||
|
mesh_material.0 = material;
|
||||||
}
|
}
|
||||||
*vis = Visibility::Inherited;
|
*vis = Visibility::Inherited;
|
||||||
tf.translation = gtf.translation().with_y(1.0);
|
tf.translation = gtf.translation().with_y(1.0);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue