coreheart/crates/building/src/lib.rs

238 lines
6.8 KiB
Rust
Raw Normal View History

2025-08-24 20:13:22 -04:00
//bevy system signatures often violate these rules
#![allow(clippy::type_complexity)]
#![allow(clippy::too_many_arguments)]
use avian3d::prelude::*;
2025-08-30 20:41:42 -04:00
use bevy::{
ecs::{component::HookContext, world::DeferredWorld},
platform::collections::HashMap,
prelude::*,
};
use combat::{Health, Team};
use resources::{Cost, Resources};
2025-08-30 20:41:42 -04:00
use state::GameSystemSet;
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
}
#[derive(Component)]
2025-08-30 20:41:42 -04:00
#[require(Health, Team::Core, PhysicsPickable)]
2025-08-25 08:31:33 -04:00
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);
2025-08-25 08:31:33 -04:00
}
2025-08-30 20:41:42 -04:00
#[derive(Component, Clone)]
#[require(Name, Cost)]
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,
pub size: UVec3,
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 {
// 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 {
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,
mut resources: ResMut<Resources>,
info_query: Query<(&BuildingInfo, &Cost)>,
2025-08-30 20:41:42 -04:00
) {
for PlaceBuilding {
info: info_entity,
location,
} in reader.read()
{
let Ok((info, cost)) = info_query.get(*info_entity) else {
2025-08-30 20:41:42 -04:00
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();
2025-08-30 20:41:42 -04:00
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(),
2025-08-30 20:41:42 -04:00
))
.id();
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);
}
}