convert core to new format, improve tower placement

This commit is contained in:
Jim 2025-08-30 23:13:23 -04:00
parent 2808f6dd2c
commit bc02db7b26
Signed by: jim
GPG key ID: 3D7D94BA53088BF4
12 changed files with 250 additions and 107 deletions

View file

@ -2,7 +2,7 @@
#![allow(clippy::type_complexity)]
#![allow(clippy::too_many_arguments)]
use avian3d::picking::PhysicsPickable;
use avian3d::prelude::*;
use bevy::{
ecs::{component::HookContext, world::DeferredWorld},
platform::collections::HashMap,
@ -22,10 +22,35 @@ impl Plugin for BuildingPlugin {
}
}
#[derive(Component, Default)]
#[derive(Component)]
#[require(Health, Team::Core, PhysicsPickable)]
pub struct Building {
pub level: u32,
pub info: Entity,
}
#[derive(Component)]
#[component(on_remove = on_building_removed)]
struct BuildingPosition(IVec3);
fn on_building_removed(mut world: DeferredWorld, context: HookContext) {
let (info, location) = world
.get_entity(context.entity)
.map(|e| {
(
e.components::<&Building>().info,
e.components::<&BuildingPosition>().0,
)
})
.unwrap();
let size = world
.get_entity(info)
.unwrap()
.components::<&BuildingInfo>()
.size;
world
.resource_mut::<PlacedBuildings>()
.remove_building(location, size);
}
#[derive(Component, Clone)]
@ -35,7 +60,7 @@ pub struct Building {
#[component(immutable)]
pub struct BuildingInfo {
pub preview: Entity,
pub size: UVec2,
pub size: UVec3,
}
#[derive(Component)]
@ -43,8 +68,8 @@ pub struct BuildingPreview {
pub info: Entity,
}
#[derive(Component)]
pub struct ActiveBuildingPreview;
#[derive(Component, Default)]
pub struct ActiveBuildingPreview(pub IVec3);
pub const PREVIEW_ALPHA: f32 = 0.5;
@ -93,8 +118,67 @@ pub struct PlacedBuildings {
}
impl PlacedBuildings {
// todo - update these to account for building being in the center
pub fn placement_valid(&self, location: IVec3, info: &BuildingInfo) -> bool {
true
for x in 0..info.size.x {
for y in 0..info.size.y {
for z in 0..info.size.z {
if self
.map
.contains_key(&(location + IVec3::new(x as i32, y as i32, z as i32)))
{
//shouldn't happen
return false;
}
}
}
}
return true;
}
fn place_building(
&mut self,
location: IVec3,
info: &BuildingInfo,
info_entity: Entity,
instance_entity: Entity,
) {
for x in 0..info.size.x {
for y in 0..info.size.y {
for z in 0..info.size.z {
if self
.map
.insert(
location + IVec3::new(x as i32, y as i32, z as i32),
PlacedBuilding {
info: info_entity,
instance: instance_entity,
},
)
.is_some()
{
//shouldn't happen
error!("placed building on old building!!! RIP");
}
}
}
}
}
fn remove_building(&mut self, location: IVec3, size: UVec3) {
for x in 0..size.x {
for y in 0..size.y {
for z in 0..size.z {
if self
.map
.remove(&(location + IVec3::new(x as i32, y as i32, z as i32)))
.is_none()
{
//shouldn't happen
error!("removed building where there wasn't one!!!! RIP");
}
}
}
}
}
}
@ -128,23 +212,22 @@ fn place_building(
error!("invalid building placement at {:?}, rejecting", location);
continue;
}
let size_v3 = info.size.as_vec3();
let instance = commands
.spawn((
Transform::from_translation(location.as_vec3()),
Building::default(),
// adjust model up slightly so base of all towers are aligned
Transform::from_translation(location.as_vec3() + Vec3::new(0., size_v3.y / 2., 0.)),
Building {
level: 0,
info: *info_entity,
},
BuildingPosition(*location),
Collider::cuboid(size_v3.x, size_v3.y, size_v3.z),
Mesh3d::default(),
))
.id();
if let Some(old_placement) = building_map.map.insert(
*location,
PlacedBuilding {
info: *info_entity,
instance,
},
) {
//shouldn't happen
error!("placed building on old building, removing old building");
commands.entity(old_placement.instance).despawn();
}
building_map.place_building(*location, info, *info_entity, instance);
commands.trigger_targets(PopulatePlacedBuilding { info: *info_entity }, instance);
}
}