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::*;
|
|
|
|
|
use building::{
|
|
|
|
|
ActiveBuildingPreview, BuildingInfo, BuildingPreview, PlaceBuilding, PlacedBuildings,
|
|
|
|
|
};
|
2025-08-30 23:13:23 -04:00
|
|
|
use input::Inputs;
|
2025-08-30 20:41:42 -04:00
|
|
|
use physics::Ground;
|
|
|
|
|
|
|
|
|
|
pub struct BuildingPlacementPlugin;
|
|
|
|
|
|
|
|
|
|
impl Plugin for BuildingPlacementPlugin {
|
|
|
|
|
fn build(&self, app: &mut App) {
|
|
|
|
|
app.add_systems(Update, init);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-30 23:13:23 -04:00
|
|
|
if !inputs.pressed(input::actions::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;
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
});
|
|
|
|
|
}
|