77 lines
2.1 KiB
Rust
77 lines
2.1 KiB
Rust
|
|
//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,
|
||
|
|
};
|
||
|
|
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>>,
|
||
|
|
preview: Single<(&mut Transform, &BuildingPreview), With<ActiveBuildingPreview>>,
|
||
|
|
info_query: Query<&BuildingInfo>,
|
||
|
|
placed_buildings: Res<PlacedBuildings>,
|
||
|
|
) {
|
||
|
|
let Some(dest) = trigger.event().hit.position else {
|
||
|
|
return;
|
||
|
|
};
|
||
|
|
let (mut tf, preview) = preview.into_inner();
|
||
|
|
let Ok(info) = info_query.get(preview.info) else {
|
||
|
|
error!("building preview has no associated info!");
|
||
|
|
return;
|
||
|
|
};
|
||
|
|
let loc = dest.round();
|
||
|
|
if !placed_buildings.placement_valid(loc.as_ivec3(), info) {
|
||
|
|
info!("invalid placement!!!!!");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
tf.translation = dest.round();
|
||
|
|
}
|
||
|
|
|
||
|
|
fn place_preview(
|
||
|
|
trigger: Trigger<Pointer<Click>>,
|
||
|
|
preview: Single<&BuildingPreview, With<ActiveBuildingPreview>>,
|
||
|
|
info_query: Query<&BuildingInfo>,
|
||
|
|
placed_buildings: Res<PlacedBuildings>,
|
||
|
|
mut place_writer: EventWriter<PlaceBuilding>,
|
||
|
|
) {
|
||
|
|
let Some(dest) = trigger.event().hit.position else {
|
||
|
|
return;
|
||
|
|
};
|
||
|
|
let Ok(info) = info_query.get(preview.info) else {
|
||
|
|
error!("building preview has no associated info!");
|
||
|
|
return;
|
||
|
|
};
|
||
|
|
let loc = dest.round();
|
||
|
|
if !placed_buildings.placement_valid(loc.as_ivec3(), info) {
|
||
|
|
info!("invalid placement!!!!!");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
place_writer.write(PlaceBuilding {
|
||
|
|
info: preview.info,
|
||
|
|
location: loc.as_ivec3(),
|
||
|
|
});
|
||
|
|
}
|