range indicators, limit tower placement to within range of core

This commit is contained in:
Jim 2025-09-01 19:10:16 -04:00
parent 9c4787f5e2
commit 6a4978041c
Signed by: jim
GPG key ID: 3D7D94BA53088BF4
16 changed files with 350 additions and 60 deletions

View file

@ -0,0 +1,13 @@
[package]
name = "ui_range_indicator"
version = "0.1.0"
edition = "2024"
[dependencies]
bevy = { workspace = true }
combat = { path = "../combat" }
input = { path = "../input" }
[lints]
workspace = true

View file

@ -0,0 +1,197 @@
//bevy system signatures often violate these rules
#![allow(clippy::type_complexity)]
#![allow(clippy::too_many_arguments)]
use std::f32::consts::PI;
use bevy::{
ecs::{component::HookContext, world::DeferredWorld},
pbr::{NotShadowCaster, NotShadowReceiver},
prelude::*,
};
use combat::Range;
use input::Selected;
pub struct UiRangeIndicatorPlugin;
impl Plugin for UiRangeIndicatorPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, init).add_systems(Update, display);
}
}
#[derive(Component)]
struct Indicator(Entity, f32);
#[derive(Component)]
struct SelectedIndicator;
#[derive(Component, Default)]
#[component(on_add=create_custom_indicator)]
#[component(on_remove=remove_custom_indicator)]
pub struct CustomRangeIndicator {
material: Handle<StandardMaterial>,
indicator: Option<Entity>,
}
#[derive(Component)]
pub struct DisplayCustomRangeIndicator;
#[derive(Resource)]
struct RangeResources {
base_mesh: Handle<Mesh>,
}
impl CustomRangeIndicator {
pub fn new(material: Handle<StandardMaterial>) -> Self {
Self {
material,
indicator: None,
}
}
}
fn create_custom_indicator(mut world: DeferredWorld, ctx: HookContext) {
let attaching_to = ctx.entity;
let material = world
.entity(attaching_to)
.get::<CustomRangeIndicator>()
.unwrap()
.material
.clone();
let base_mesh = world.resource::<RangeResources>().base_mesh.clone();
let mut commands = world.commands();
let indicator = create_indicator(&mut commands, material, base_mesh);
world
.entity_mut(attaching_to)
.get_mut::<CustomRangeIndicator>()
.unwrap()
.indicator = Some(indicator);
}
fn remove_custom_indicator(mut world: DeferredWorld, ctx: HookContext) {
let Some(indicator) = world
.entity(ctx.entity)
.get::<CustomRangeIndicator>()
.unwrap()
.indicator
else {
return;
};
world.commands().entity(indicator).despawn();
}
fn create_indicator(
commands: &mut Commands,
material: Handle<StandardMaterial>,
base_mesh: Handle<Mesh>,
) -> Entity {
commands
.spawn((
Mesh3d(base_mesh),
Transform::from_rotation(Quat::from_rotation_x(-PI / 2.)),
MeshMaterial3d(material),
Indicator(Entity::PLACEHOLDER, 1.0),
Visibility::Hidden,
NotShadowCaster,
NotShadowReceiver,
))
.id()
}
fn init(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let material = materials.add(StandardMaterial {
alpha_mode: AlphaMode::Blend,
base_color: Color::srgba(0.7, 0.7, 0.7, 0.5),
unlit: true,
double_sided: true,
..default()
});
let mesh = meshes.add(Extrusion::new(Annulus::new(1000. - 0.1, 1000.), 1.0));
commands.insert_resource(RangeResources {
base_mesh: mesh.clone(),
});
let selected_indicator = create_indicator(&mut commands, material, mesh);
commands
.entity(selected_indicator)
.insert(SelectedIndicator);
}
// todo - perf and visuals kind of suck here. honeslty this whole thing should be split up and sucks.
fn display(
selected_query: Query<
(Entity, &Range, &GlobalTransform),
(With<Selected>, Without<CustomRangeIndicator>),
>,
selected_indicator_query: Single<
(&mut Transform, &mut Visibility, &mut Indicator, &mut Mesh3d),
With<SelectedIndicator>,
>,
custom_entity_query: Query<(
Entity,
&Range,
&GlobalTransform,
&CustomRangeIndicator,
Option<&Selected>,
Option<&DisplayCustomRangeIndicator>,
)>,
mut indicator_query: Query<
(&mut Transform, &mut Visibility, &mut Indicator, &mut Mesh3d),
Without<SelectedIndicator>,
>,
mut meshes: ResMut<Assets<Mesh>>,
) {
for (
target_entity,
range,
gtf,
CustomRangeIndicator { indicator, .. },
opt_selected,
opt_display,
) in custom_entity_query.iter()
{
let Some(indicator_entity) = indicator else {
continue;
};
let Ok((mut tf, mut vis, mut indicator, mut mesh)) =
indicator_query.get_mut(*indicator_entity)
else {
error!("invalid setup for custom range indicator");
continue;
};
if target_entity != indicator.0 || indicator.1 != range.0 {
indicator.0 = target_entity; //shouldn't ever be different after init, but just to be sure...
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));
}
*vis = if opt_selected.is_some() || opt_display.is_some() {
Visibility::Inherited
} else {
Visibility::Hidden
};
tf.translation = gtf.translation().with_y(1.0);
}
//update selected entity
{
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;
};
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));
}
*vis = Visibility::Inherited;
tf.translation = gtf.translation().with_y(1.0);
}
}