place buildings
This commit is contained in:
parent
b8c34a37b6
commit
2808f6dd2c
14 changed files with 338 additions and 36 deletions
14
crates/building_placement/Cargo.toml
Normal file
14
crates/building_placement/Cargo.toml
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
[package]
|
||||
name = "building_placement"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
bevy = { workspace = true }
|
||||
avian3d = { workspace = true }
|
||||
|
||||
building = { path = "../building" }
|
||||
physics = { path = "../physics" }
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
76
crates/building_placement/src/lib.rs
Normal file
76
crates/building_placement/src/lib.rs
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
//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(),
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue