2025-08-24 20:13:22 -04:00
|
|
|
//bevy system signatures often violate these rules
|
|
|
|
|
#![allow(clippy::type_complexity)]
|
|
|
|
|
#![allow(clippy::too_many_arguments)]
|
|
|
|
|
|
2025-08-30 23:13:23 -04:00
|
|
|
use avian3d::prelude::*;
|
2025-08-30 20:41:42 -04:00
|
|
|
use bevy::{
|
|
|
|
|
ecs::{component::HookContext, world::DeferredWorld},
|
|
|
|
|
platform::collections::HashMap,
|
|
|
|
|
prelude::*,
|
|
|
|
|
};
|
2025-08-28 08:34:40 -04:00
|
|
|
use combat::{Health, Team};
|
2025-08-31 22:26:07 -04:00
|
|
|
use input::Selectable;
|
2025-08-30 23:39:18 -04:00
|
|
|
use resources::{Cost, Resources};
|
2025-08-30 20:41:42 -04:00
|
|
|
use state::GameSystemSet;
|
2025-08-31 23:59:50 -04:00
|
|
|
use upgrade::{Upgradable, UpgradeTree};
|
2025-08-24 20:13:22 -04:00
|
|
|
|
|
|
|
|
pub struct BuildingPlugin;
|
|
|
|
|
|
|
|
|
|
impl Plugin for BuildingPlugin {
|
2025-08-30 20:41:42 -04:00
|
|
|
fn build(&self, app: &mut App) {
|
|
|
|
|
app.init_resource::<BuildingTypes>()
|
|
|
|
|
.init_resource::<PlacedBuildings>()
|
|
|
|
|
.add_event::<PlaceBuilding>()
|
|
|
|
|
.add_systems(FixedUpdate, place_building.in_set(GameSystemSet::PostTick));
|
|
|
|
|
}
|
2025-08-24 20:13:22 -04:00
|
|
|
}
|
|
|
|
|
|
2025-08-30 23:13:23 -04:00
|
|
|
#[derive(Component)]
|
2025-08-31 22:26:07 -04:00
|
|
|
#[require(Health, Team::Core, PhysicsPickable, Selectable)]
|
2025-08-25 08:31:33 -04:00
|
|
|
pub struct Building {
|
|
|
|
|
pub level: u32,
|
2025-08-30 23:13:23 -04:00
|
|
|
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);
|
2025-08-25 08:31:33 -04:00
|
|
|
}
|
2025-08-30 20:41:42 -04:00
|
|
|
|
|
|
|
|
#[derive(Component, Clone)]
|
2025-08-31 23:59:50 -04:00
|
|
|
#[require(Name, Cost, UpgradeTree)]
|
2025-08-30 20:41:42 -04:00
|
|
|
#[component(on_add = register_building_info)]
|
|
|
|
|
#[component(on_remove = unregister_building_info)]
|
|
|
|
|
#[component(immutable)]
|
|
|
|
|
pub struct BuildingInfo {
|
|
|
|
|
pub preview: Entity,
|
2025-08-30 23:13:23 -04:00
|
|
|
pub size: UVec3,
|
2025-08-31 12:08:44 -04:00
|
|
|
pub unlocked: bool,
|
2025-08-30 20:41:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Component)]
|
|
|
|
|
pub struct BuildingPreview {
|
|
|
|
|
pub info: Entity,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub const PREVIEW_ALPHA: f32 = 0.5;
|
|
|
|
|
|
|
|
|
|
fn register_building_info(mut world: DeferredWorld, context: HookContext) {
|
|
|
|
|
let (info, name) = world
|
|
|
|
|
.get_entity(context.entity)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.components::<(&BuildingInfo, &Name)>();
|
|
|
|
|
if name.is_empty() {
|
|
|
|
|
panic!("name cannot be empty when registering building");
|
|
|
|
|
}
|
|
|
|
|
let (info, name) = (info.clone(), name.to_string());
|
|
|
|
|
let mut types = world.get_resource_mut::<BuildingTypes>().unwrap();
|
|
|
|
|
info!("registering building {}", name);
|
|
|
|
|
types.info.insert(name, (context.entity, info));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn unregister_building_info(mut world: DeferredWorld, context: HookContext) {
|
|
|
|
|
let name = world
|
|
|
|
|
.get_entity(context.entity)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.components::<&Name>()
|
|
|
|
|
.to_string();
|
|
|
|
|
if name.is_empty() {
|
|
|
|
|
panic!("name cannot be empty when ungregistering building");
|
|
|
|
|
}
|
|
|
|
|
let mut types = world.get_resource_mut::<BuildingTypes>().unwrap();
|
|
|
|
|
info!("unregistering building {}", name);
|
|
|
|
|
types.info.remove(&name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Resource, Default)]
|
|
|
|
|
pub struct BuildingTypes {
|
|
|
|
|
pub info: HashMap<String, (Entity, BuildingInfo)>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct PlacedBuilding {
|
|
|
|
|
pub info: Entity,
|
|
|
|
|
pub instance: Entity,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Resource, Default)]
|
|
|
|
|
pub struct PlacedBuildings {
|
|
|
|
|
pub map: HashMap<IVec3, PlacedBuilding>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl PlacedBuildings {
|
2025-08-30 23:13:23 -04:00
|
|
|
// todo - update these to account for building being in the center
|
2025-08-30 20:41:42 -04:00
|
|
|
pub fn placement_valid(&self, location: IVec3, info: &BuildingInfo) -> bool {
|
2025-08-30 23:13:23 -04:00
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-30 20:41:42 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Event)]
|
|
|
|
|
pub struct PlaceBuilding {
|
|
|
|
|
pub info: Entity,
|
|
|
|
|
pub location: IVec3,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Event)]
|
|
|
|
|
pub struct PopulatePlacedBuilding {
|
|
|
|
|
pub info: Entity,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn place_building(
|
|
|
|
|
mut reader: EventReader<PlaceBuilding>,
|
|
|
|
|
mut building_map: ResMut<PlacedBuildings>,
|
|
|
|
|
mut commands: Commands,
|
2025-08-30 23:39:18 -04:00
|
|
|
mut resources: ResMut<Resources>,
|
2025-08-31 23:59:50 -04:00
|
|
|
info_query: Query<(&BuildingInfo, &Cost, &Name), With<UpgradeTree>>,
|
2025-08-30 20:41:42 -04:00
|
|
|
) {
|
|
|
|
|
for PlaceBuilding {
|
|
|
|
|
info: info_entity,
|
|
|
|
|
location,
|
|
|
|
|
} in reader.read()
|
|
|
|
|
{
|
2025-08-31 19:00:19 -04:00
|
|
|
let Ok((info, cost, name)) = info_query.get(*info_entity) else {
|
2025-08-31 23:59:50 -04:00
|
|
|
error!("tried to place building without info or required components!");
|
2025-08-30 20:41:42 -04:00
|
|
|
continue;
|
|
|
|
|
};
|
|
|
|
|
if !building_map.placement_valid(*location, info) {
|
|
|
|
|
error!("invalid building placement at {:?}, rejecting", location);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2025-08-30 23:39:18 -04:00
|
|
|
if !resources.buy(&cost) {
|
|
|
|
|
info!("not enough money to buy tower!");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2025-08-30 23:13:23 -04:00
|
|
|
let size_v3 = info.size.as_vec3();
|
2025-08-30 20:41:42 -04:00
|
|
|
let instance = commands
|
|
|
|
|
.spawn((
|
2025-08-30 23:13:23 -04:00
|
|
|
// 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(),
|
2025-08-31 19:00:19 -04:00
|
|
|
name.clone(),
|
|
|
|
|
RigidBody::Static,
|
|
|
|
|
Team::Core.collision_layers(),
|
2025-08-31 23:59:50 -04:00
|
|
|
Upgradable::new(*info_entity),
|
2025-08-30 20:41:42 -04:00
|
|
|
))
|
|
|
|
|
.id();
|
2025-08-30 23:13:23 -04:00
|
|
|
building_map.place_building(*location, info, *info_entity, instance);
|
|
|
|
|
|
2025-08-30 20:41:42 -04:00
|
|
|
commands.trigger_targets(PopulatePlacedBuilding { info: *info_entity }, instance);
|
|
|
|
|
}
|
|
|
|
|
}
|