rework building placement to use physics system
This commit is contained in:
parent
56a4f8b597
commit
9c4787f5e2
3 changed files with 53 additions and 122 deletions
|
|
@ -2,8 +2,9 @@
|
|||
#![allow(clippy::type_complexity)]
|
||||
#![allow(clippy::too_many_arguments)]
|
||||
|
||||
use avian3d::prelude::SpatialQuery;
|
||||
use bevy::prelude::*;
|
||||
use building::{BuildingInfo, BuildingPreview, PlaceBuilding, PlacedBuildings};
|
||||
use building::{Building, BuildingInfo, BuildingPreview, PlaceBuilding, valid_placement};
|
||||
use input::{Cancellable, Inputs, Selected, actions::InputAction};
|
||||
use physics::Ground;
|
||||
|
||||
|
|
@ -49,7 +50,8 @@ fn move_preview(
|
|||
trigger: Trigger<Pointer<Move>>,
|
||||
preview: Single<(&mut Transform, &mut ActiveBuildingPreview, &BuildingPreview)>,
|
||||
info_query: Query<&BuildingInfo>,
|
||||
placed_buildings: Res<PlacedBuildings>,
|
||||
building_query: Query<&Building>,
|
||||
spatial_query: SpatialQuery,
|
||||
) {
|
||||
let Some(dest) = trigger.event().hit.position else {
|
||||
return;
|
||||
|
|
@ -61,20 +63,24 @@ fn move_preview(
|
|||
};
|
||||
// center mouse on building
|
||||
let loc = dest.round();
|
||||
if !placed_buildings.placement_valid(loc.as_ivec3(), info) {
|
||||
let (_, position) = info.get_placed_collider_and_position(loc);
|
||||
if !valid_placement(
|
||||
&spatial_query,
|
||||
&building_query,
|
||||
info.size.as_vec3(),
|
||||
position,
|
||||
) {
|
||||
info!("invalid placement!!!!!");
|
||||
return;
|
||||
}
|
||||
active.0 = loc.as_ivec3();
|
||||
tf.translation = loc + Vec3::Y * info.size.y as f32 / 2.;
|
||||
tf.translation = position;
|
||||
}
|
||||
|
||||
fn place_preview(
|
||||
_trigger: Trigger<Pointer<Click>>,
|
||||
inputs: Res<Inputs<Update>>,
|
||||
query: Single<(&BuildingPreview, &ActiveBuildingPreview)>,
|
||||
info_query: Query<&BuildingInfo>,
|
||||
placed_buildings: Res<PlacedBuildings>,
|
||||
mut place_writer: EventWriter<PlaceBuilding>,
|
||||
mut select_writer: EventWriter<SelectBuildingPreview>,
|
||||
) {
|
||||
|
|
@ -82,14 +88,6 @@ fn place_preview(
|
|||
return;
|
||||
}
|
||||
let (preview, active) = query.into_inner();
|
||||
let Ok(info) = info_query.get(preview.info) else {
|
||||
error!("building preview has no associated info!");
|
||||
return;
|
||||
};
|
||||
if !placed_buildings.placement_valid(active.0, info) {
|
||||
info!("invalid placement!!!!!");
|
||||
return;
|
||||
}
|
||||
if !inputs.pressed(InputAction::MultiPlaceTower) {
|
||||
select_writer.write(SelectBuildingPreview(None));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue