202 lines
5.8 KiB
Rust
Executable file
202 lines
5.8 KiB
Rust
Executable file
//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, Range, Team};
|
|
use input::Selectable;
|
|
use resources::{Cost, Resources};
|
|
use state::GameSystemSet;
|
|
use ui_health_bar::BarSettings;
|
|
use upgrade::{Upgradable, UpgradeTree};
|
|
|
|
pub struct BuildingPlugin;
|
|
|
|
impl Plugin for BuildingPlugin {
|
|
fn build(&self, app: &mut App) {
|
|
app.init_resource::<BuildingTypes>()
|
|
.add_event::<PlaceBuilding>()
|
|
.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)]
|
|
#[require(Range)]
|
|
pub struct Core;
|
|
|
|
#[derive(Component, Clone)]
|
|
#[require(Name, Cost, UpgradeTree)]
|
|
#[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,
|
|
}
|
|
|
|
impl BuildingInfo {
|
|
pub fn get_placed_collider_and_position(&self, pos: Vec3) -> (Collider, Vec3) {
|
|
let size_v3 = self.size.as_vec3();
|
|
let collider = Collider::cuboid(size_v3.x, size_v3.y, size_v3.z);
|
|
let position = pos + Vec3::new(0., size_v3.y / 2., 0.);
|
|
(collider, position)
|
|
}
|
|
}
|
|
|
|
#[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(Event)]
|
|
pub struct PlaceBuilding {
|
|
pub info: Entity,
|
|
pub location: IVec3,
|
|
}
|
|
|
|
#[derive(Event)]
|
|
pub struct PopulatePlacedBuilding {
|
|
pub info: Entity,
|
|
}
|
|
|
|
pub fn valid_placement(
|
|
spatial_query: &SpatialQuery,
|
|
collider_query: &Query<&Building>,
|
|
size: Vec3,
|
|
position: Vec3,
|
|
core_position_range: Option<(Vec3, f32)>,
|
|
) -> bool {
|
|
if let Some((core_position, core_range)) = core_position_range
|
|
&& position.distance_squared(core_position) > core_range * core_range
|
|
{
|
|
return false;
|
|
}
|
|
!spatial_query
|
|
.shape_intersections(
|
|
// shrink the collider a bit to allow for buildings right next to each other
|
|
&Collider::cuboid(size.x - 0.1, size.y - 0.1, size.z - 0.1),
|
|
position,
|
|
Quat::IDENTITY,
|
|
&SpatialQueryFilter::from_mask(Team::Core.collision_layers().memberships),
|
|
)
|
|
.iter()
|
|
.any(|b| collider_query.contains(*b))
|
|
}
|
|
|
|
fn place_building(
|
|
mut reader: EventReader<PlaceBuilding>,
|
|
mut commands: Commands,
|
|
mut resources: ResMut<Resources>,
|
|
info_query: Query<(&BuildingInfo, &Cost, &Name), With<UpgradeTree>>,
|
|
building_query: Query<&Building>,
|
|
core_query: Query<(&GlobalTransform, &Range), With<Core>>,
|
|
spatial_query: SpatialQuery,
|
|
) {
|
|
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 or required components!");
|
|
continue;
|
|
};
|
|
if !resources.buy(&cost) {
|
|
info!("not enough money to buy tower!");
|
|
continue;
|
|
}
|
|
let size_v3 = info.size.as_vec3();
|
|
let (collider, position) = info.get_placed_collider_and_position(location.as_vec3());
|
|
if !valid_placement(
|
|
&spatial_query,
|
|
&building_query,
|
|
size_v3,
|
|
position,
|
|
core_query
|
|
.single()
|
|
.ok()
|
|
.map(|(gtf, r)| (gtf.translation(), r.0)),
|
|
) {
|
|
error!("tried to place building colliding with other building");
|
|
continue;
|
|
}
|
|
|
|
let instance = commands
|
|
.spawn((
|
|
// adjust model up slightly so base of all towers are aligned
|
|
Transform::from_translation(position),
|
|
Building {
|
|
level: 0,
|
|
info: *info_entity,
|
|
},
|
|
collider,
|
|
Mesh3d::default(),
|
|
name.clone(),
|
|
RigidBody::Static,
|
|
Team::Core.collision_layers(),
|
|
Upgradable::new(*info_entity),
|
|
BarSettings {
|
|
offset: size_v3.y / 2. + 0.5,
|
|
width: (size_v3.x.max(size_v3.z) * 8.).ceil() as u32,
|
|
..default()
|
|
},
|
|
))
|
|
.id();
|
|
|
|
commands.trigger_targets(PopulatePlacedBuilding { info: *info_entity }, instance);
|
|
}
|
|
}
|