//bevy system signatures often violate these rules #![allow(clippy::type_complexity)] #![allow(clippy::too_many_arguments)] use avian3d::prelude::*; use bevy::{ ecs::{component::HookContext, world::DeferredWorld}, platform::collections::HashMap, prelude::*, }; use combat::{Health, Team}; use input::Selectable; use resources::{Cost, Resources}; use state::GameSystemSet; pub struct BuildingPlugin; impl Plugin for BuildingPlugin { fn build(&self, app: &mut App) { app.init_resource::() .init_resource::() .add_event::() .add_systems(FixedUpdate, place_building.in_set(GameSystemSet::PostTick)); } } #[derive(Component)] #[require(Health, Team::Core, PhysicsPickable, Selectable)] 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::() .remove_building(location, size); } #[derive(Component, Clone)] #[require(Name, Cost)] #[component(on_add = register_building_info)] #[component(on_remove = unregister_building_info)] #[component(immutable)] pub struct BuildingInfo { pub preview: Entity, pub size: UVec3, pub unlocked: bool, } #[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::().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::().unwrap(); info!("unregistering building {}", name); types.info.remove(&name); } #[derive(Resource, Default)] pub struct BuildingTypes { pub info: HashMap, } #[derive(Clone)] pub struct PlacedBuilding { pub info: Entity, pub instance: Entity, } #[derive(Resource, Default)] pub struct PlacedBuildings { pub map: HashMap, } impl PlacedBuildings { // todo - update these to account for building being in the center pub fn placement_valid(&self, location: IVec3, info: &BuildingInfo) -> bool { 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"); } } } } } } #[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, mut building_map: ResMut, mut commands: Commands, mut resources: ResMut, info_query: Query<(&BuildingInfo, &Cost, &Name)>, ) { for PlaceBuilding { info: info_entity, location, } in reader.read() { let Ok((info, cost, name)) = info_query.get(*info_entity) else { error!("tried to place building without info!"); continue; }; if !building_map.placement_valid(*location, info) { error!("invalid building placement at {:?}, rejecting", location); continue; } if !resources.buy(&cost) { info!("not enough money to buy tower!"); continue; } let size_v3 = info.size.as_vec3(); let instance = commands .spawn(( // 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(), name.clone(), RigidBody::Static, Team::Core.collision_layers(), )) .id(); building_map.place_building(*location, info, *info_entity, instance); commands.trigger_targets(PopulatePlacedBuilding { info: *info_entity }, instance); } }