2025-08-30 20:41:42 -04:00
|
|
|
//bevy system signatures often violate these rules
|
|
|
|
|
#![allow(clippy::type_complexity)]
|
|
|
|
|
#![allow(clippy::too_many_arguments)]
|
|
|
|
|
|
|
|
|
|
use bevy::prelude::*;
|
2025-08-31 12:08:44 -04:00
|
|
|
use building::{BuildingInfo, BuildingPreview, PlaceBuilding, PlacedBuildings};
|
|
|
|
|
use input::{Cancellable, Inputs, actions::InputAction};
|
2025-08-30 20:41:42 -04:00
|
|
|
use physics::Ground;
|
|
|
|
|
|
|
|
|
|
pub struct BuildingPlacementPlugin;
|
|
|
|
|
|
|
|
|
|
impl Plugin for BuildingPlacementPlugin {
|
|
|
|
|
fn build(&self, app: &mut App) {
|
2025-08-31 12:08:44 -04:00
|
|
|
app.add_systems(Update, (init, cancel_preview, select_preview).chain())
|
|
|
|
|
.add_event::<SelectBuildingPreview>();
|
2025-08-30 20:41:42 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-31 12:08:44 -04:00
|
|
|
#[derive(Component, Default)]
|
|
|
|
|
pub struct ActiveBuildingPreview(pub IVec3);
|
|
|
|
|
|
|
|
|
|
#[derive(Event)]
|
|
|
|
|
pub struct SelectBuildingPreview(pub Option<Entity>);
|
|
|
|
|
|
2025-08-30 20:41:42 -04:00
|
|
|
fn init(mut commands: Commands, query: Single<Entity, With<Ground>>, mut setup: Local<bool>) {
|
|
|
|
|
if *setup {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
*setup = true;
|
|
|
|
|
commands
|
|
|
|
|
.entity(*query)
|
|
|
|
|
.observe(move_preview)
|
|
|
|
|
.observe(place_preview);
|
|
|
|
|
info!("added building placement observers!");
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-31 12:08:44 -04:00
|
|
|
fn cancel_preview(
|
|
|
|
|
mut writer: EventWriter<SelectBuildingPreview>,
|
|
|
|
|
query: Query<(), (With<ActiveBuildingPreview>, With<Cancellable>)>,
|
|
|
|
|
inputs: Res<Inputs<Update>>,
|
|
|
|
|
) {
|
|
|
|
|
if !query.is_empty() && inputs.just_pressed(InputAction::Cancel) {
|
|
|
|
|
writer.write(SelectBuildingPreview(None));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-30 20:41:42 -04:00
|
|
|
fn move_preview(
|
|
|
|
|
trigger: Trigger<Pointer<Move>>,
|
2025-08-30 23:13:23 -04:00
|
|
|
preview: Single<(&mut Transform, &mut ActiveBuildingPreview, &BuildingPreview)>,
|
2025-08-30 20:41:42 -04:00
|
|
|
info_query: Query<&BuildingInfo>,
|
|
|
|
|
placed_buildings: Res<PlacedBuildings>,
|
|
|
|
|
) {
|
|
|
|
|
let Some(dest) = trigger.event().hit.position else {
|
|
|
|
|
return;
|
|
|
|
|
};
|
2025-08-30 23:13:23 -04:00
|
|
|
let (mut tf, mut active, preview) = preview.into_inner();
|
2025-08-30 20:41:42 -04:00
|
|
|
let Ok(info) = info_query.get(preview.info) else {
|
|
|
|
|
error!("building preview has no associated info!");
|
|
|
|
|
return;
|
|
|
|
|
};
|
2025-08-30 23:13:23 -04:00
|
|
|
// center mouse on building
|
2025-08-30 20:41:42 -04:00
|
|
|
let loc = dest.round();
|
|
|
|
|
if !placed_buildings.placement_valid(loc.as_ivec3(), info) {
|
|
|
|
|
info!("invalid placement!!!!!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-08-30 23:13:23 -04:00
|
|
|
active.0 = loc.as_ivec3();
|
|
|
|
|
tf.translation = loc + Vec3::Y * info.size.y as f32 / 2.;
|
2025-08-30 20:41:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn place_preview(
|
2025-08-30 23:13:23 -04:00
|
|
|
_trigger: Trigger<Pointer<Click>>,
|
|
|
|
|
inputs: Res<Inputs<Update>>,
|
|
|
|
|
query: Single<(&BuildingPreview, &ActiveBuildingPreview)>,
|
2025-08-30 20:41:42 -04:00
|
|
|
info_query: Query<&BuildingInfo>,
|
|
|
|
|
placed_buildings: Res<PlacedBuildings>,
|
|
|
|
|
mut place_writer: EventWriter<PlaceBuilding>,
|
2025-08-31 12:08:44 -04:00
|
|
|
mut select_writer: EventWriter<SelectBuildingPreview>,
|
2025-08-30 20:41:42 -04:00
|
|
|
) {
|
2025-08-31 12:08:44 -04:00
|
|
|
if !inputs.pressed(InputAction::PlaceTower) {
|
2025-08-30 20:41:42 -04:00
|
|
|
return;
|
2025-08-30 23:13:23 -04:00
|
|
|
}
|
|
|
|
|
let (preview, active) = query.into_inner();
|
2025-08-30 20:41:42 -04:00
|
|
|
let Ok(info) = info_query.get(preview.info) else {
|
|
|
|
|
error!("building preview has no associated info!");
|
|
|
|
|
return;
|
|
|
|
|
};
|
2025-08-30 23:13:23 -04:00
|
|
|
if !placed_buildings.placement_valid(active.0, info) {
|
2025-08-30 20:41:42 -04:00
|
|
|
info!("invalid placement!!!!!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-08-31 12:08:44 -04:00
|
|
|
if !inputs.pressed(InputAction::MultiPlaceTower) {
|
|
|
|
|
select_writer.write(SelectBuildingPreview(None));
|
|
|
|
|
}
|
2025-08-30 20:41:42 -04:00
|
|
|
place_writer.write(PlaceBuilding {
|
|
|
|
|
info: preview.info,
|
2025-08-30 23:13:23 -04:00
|
|
|
location: active.0,
|
2025-08-30 20:41:42 -04:00
|
|
|
});
|
|
|
|
|
}
|
2025-08-31 12:08:44 -04:00
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
));
|
|
|
|
|
}
|