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 20:41:42 -04:00
|
|
|
use avian3d::picking::PhysicsPickable;
|
|
|
|
|
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-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, Default)]
|
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,
|
|
|
|
|
}
|
2025-08-30 20:41:42 -04:00
|
|
|
|
|
|
|
|
#[derive(Component, Clone)]
|
|
|
|
|
#[require(Name)]
|
|
|
|
|
#[component(on_add = register_building_info)]
|
|
|
|
|
#[component(on_remove = unregister_building_info)]
|
|
|
|
|
#[component(immutable)]
|
|
|
|
|
pub struct BuildingInfo {
|
|
|
|
|
pub preview: Entity,
|
|
|
|
|
pub size: UVec2,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Component)]
|
|
|
|
|
pub struct BuildingPreview {
|
|
|
|
|
pub info: Entity,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Component)]
|
|
|
|
|
pub struct ActiveBuildingPreview;
|
|
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
pub fn placement_valid(&self, location: IVec3, info: &BuildingInfo) -> bool {
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[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,
|
|
|
|
|
info_query: Query<&BuildingInfo>,
|
|
|
|
|
) {
|
|
|
|
|
for PlaceBuilding {
|
|
|
|
|
info: info_entity,
|
|
|
|
|
location,
|
|
|
|
|
} in reader.read()
|
|
|
|
|
{
|
|
|
|
|
let Ok(info) = 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;
|
|
|
|
|
}
|
|
|
|
|
let instance = commands
|
|
|
|
|
.spawn((
|
|
|
|
|
Transform::from_translation(location.as_vec3()),
|
|
|
|
|
Building::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();
|
|
|
|
|
}
|
|
|
|
|
commands.trigger_targets(PopulatePlacedBuilding { info: *info_entity }, instance);
|
|
|
|
|
}
|
|
|
|
|
}
|