place buildings
This commit is contained in:
parent
b8c34a37b6
commit
2808f6dd2c
14 changed files with 338 additions and 36 deletions
|
|
@ -5,7 +5,9 @@ edition = "2024"
|
|||
|
||||
[dependencies]
|
||||
bevy = { workspace = true }
|
||||
avian3d = { workspace = true }
|
||||
combat = { path = "../combat" }
|
||||
state = { path = "../state" }
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
|
|
|||
|
|
@ -2,17 +2,149 @@
|
|||
#![allow(clippy::type_complexity)]
|
||||
#![allow(clippy::too_many_arguments)]
|
||||
|
||||
use bevy::prelude::*;
|
||||
use avian3d::picking::PhysicsPickable;
|
||||
use bevy::{
|
||||
ecs::{component::HookContext, world::DeferredWorld},
|
||||
platform::collections::HashMap,
|
||||
prelude::*,
|
||||
};
|
||||
use combat::{Health, Team};
|
||||
use state::GameSystemSet;
|
||||
|
||||
pub struct BuildingPlugin;
|
||||
|
||||
impl Plugin for BuildingPlugin {
|
||||
fn build(&self, _app: &mut App) {}
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component, Default)]
|
||||
#[require(Health, Team::Core)]
|
||||
#[require(Health, Team::Core, PhysicsPickable)]
|
||||
pub struct Building {
|
||||
pub level: u32,
|
||||
}
|
||||
|
||||
#[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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue