place buildings

This commit is contained in:
Jim 2025-08-30 20:41:42 -04:00
parent b8c34a37b6
commit 2808f6dd2c
Signed by: jim
GPG key ID: 3D7D94BA53088BF4
14 changed files with 338 additions and 36 deletions

View file

@ -4,17 +4,18 @@
use avian3d::prelude::*;
use bevy::prelude::*;
use building::Building;
use building::{
ActiveBuildingPreview, Building, BuildingInfo, BuildingPreview, PREVIEW_ALPHA,
PopulatePlacedBuilding,
};
use combat::{Defense, Health};
use combat_damagers::ContactDamage;
use state::GameSystemSet;
pub struct BuildingImpalerPlugin;
impl Plugin for BuildingImpalerPlugin {
fn build(&self, app: &mut App) {
app.add_event::<SpawnImpaler>()
.add_systems(FixedUpdate, spawn.in_set(GameSystemSet::PostTick));
app.add_systems(Startup, register).add_observer(populate);
}
}
@ -22,34 +23,87 @@ impl Plugin for BuildingImpalerPlugin {
#[require(Building)]
pub struct Impaler;
#[derive(Event)]
pub struct SpawnImpaler {
pub transform: Transform,
#[derive(Resource)]
struct ImpalerResources {
mesh: Handle<Mesh>,
material: Handle<StandardMaterial>,
info_entity: Entity,
}
fn spawn(
mut reader: EventReader<SpawnImpaler>,
fn register(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
for SpawnImpaler { transform } in reader.read() {
commands.spawn((
let mesh = meshes.add(Cuboid::from_length(1.0));
let base_material = StandardMaterial {
base_color: Color::srgb_u8(0xd6, 0x50, 0x61),
metallic: 0.2,
perceptual_roughness: 0.6,
..default()
};
let material = materials.add(base_material.clone());
let preview_material = materials.add(StandardMaterial {
alpha_mode: AlphaMode::Blend,
base_color: base_material.base_color.with_alpha(PREVIEW_ALPHA),
..base_material
});
let info = commands.spawn_empty().id();
let preview = commands
.spawn((
BuildingPreview { info },
ActiveBuildingPreview, // Visibility::Hidden,
Mesh3d::default(),
))
.with_child((
Transform::from_xyz(0., 0.5, 0.),
Mesh3d(mesh.clone()),
MeshMaterial3d(preview_material),
))
.id();
commands.entity(info).insert((
BuildingInfo {
preview,
size: UVec2::new(1, 1),
},
Name::new("Impaler"),
));
commands.insert_resource(ImpalerResources {
mesh: mesh,
material: material,
info_entity: info,
});
}
fn populate(
trigger: Trigger<PopulatePlacedBuilding>,
mut commands: Commands,
res: Res<ImpalerResources>,
) {
if trigger.event().info != res.info_entity {
return;
}
commands
.entity(trigger.target())
.insert((
RigidBody::Static,
Collider::cuboid(1.0, 1.0, 1.0),
Mesh3d(meshes.add(Cuboid::from_length(1.0))),
MeshMaterial3d(materials.add(StandardMaterial {
base_color: Color::srgb_u8(0xd6, 0x50, 0x61),
metallic: 0.2,
perceptual_roughness: 0.6,
..default()
})),
Impaler,
transform.clone(),
Health::new(20.0),
Defense::new(5.0),
ContactDamage { amount: 5.0 },
Name::from("Impaler"),
Mesh3d::default(),
Collider::compound(vec![(
Vec3::Y * 0.5,
Quat::IDENTITY,
Collider::cuboid(1.0, 1.0, 1.0),
)]),
))
.with_child((
// don't think I'll need this with actual models
Transform::from_xyz(0., 0.5, 0.),
Mesh3d(res.mesh.clone()),
MeshMaterial3d(res.material.clone()),
));
}
}