range indicators, limit tower placement to within range of core
This commit is contained in:
parent
9c4787f5e2
commit
6a4978041c
16 changed files with 350 additions and 60 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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<SelectBuildingPreview>,
|
||||
mut old_query: Query<(Entity, &mut Visibility), With<ActiveBuildingPreview>>,
|
||||
core_query: Query<Entity, With<Core>>,
|
||||
new_query: Query<(), With<BuildingPreview>>,
|
||||
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::<DisplayCustomRangeIndicator>();
|
||||
}
|
||||
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::<DisplayCustomRangeIndicator>();
|
||||
}
|
||||
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<Pointer<Move>>,
|
||||
preview: Single<(&mut Transform, &mut ActiveBuildingPreview, &BuildingPreview)>,
|
||||
info_query: Query<&BuildingInfo>,
|
||||
building_query: Query<&Building>,
|
||||
core_query: Query<(&GlobalTransform, &Range), With<Core>>,
|
||||
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<SelectBuildingPreview>,
|
||||
mut old_query: Query<(Entity, &mut Visibility), With<ActiveBuildingPreview>>,
|
||||
new_query: Query<(), With<BuildingPreview>>,
|
||||
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,
|
||||
));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue