convert core to new format, improve tower placement

This commit is contained in:
Jim 2025-08-30 23:13:23 -04:00
parent 2808f6dd2c
commit bc02db7b26
Signed by: jim
GPG key ID: 3D7D94BA53088BF4
12 changed files with 250 additions and 107 deletions

View file

@ -4,50 +4,86 @@
use avian3d::prelude::*;
use bevy::prelude::*;
use building::Building;
use building::{BuildingInfo, BuildingPreview, PREVIEW_ALPHA, PopulatePlacedBuilding};
use combat::{Defense, Health};
use state::GameSystemSet;
pub struct BuildingCorePlugin;
impl Plugin for BuildingCorePlugin {
fn build(&self, app: &mut App) {
app.add_event::<SpawnCore>()
.add_systems(FixedUpdate, spawn_core.in_set(GameSystemSet::PostTick));
app.add_systems(Startup, register).add_observer(populate);
}
}
#[derive(Component)]
#[require(Building)]
pub struct Core;
#[derive(Event)]
pub struct SpawnCore {
pub transform: Transform,
#[derive(Resource)]
struct CoreResources {
mesh: Handle<Mesh>,
material: Handle<StandardMaterial>,
info_entity: Entity,
}
fn spawn_core(
mut reader: EventReader<SpawnCore>,
const SIZE: UVec3 = UVec3::new(2, 2, 2);
fn register(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
for SpawnCore { transform } in reader.read() {
commands.spawn((
RigidBody::Static,
Collider::cuboid(2.0, 2.0, 2.0),
Mesh3d(meshes.add(Cuboid::from_length(2.0))),
MeshMaterial3d(materials.add(StandardMaterial {
base_color: Color::srgb_u8(166, 69, 16),
metallic: 0.0,
perceptual_roughness: 0.8,
..default()
})),
Core,
transform.clone(),
Health::new(100.0),
Defense::new(10.0),
Name::from("Core"),
));
}
let mesh = meshes.add(Cuboid::from_length(2.0));
let base_material = StandardMaterial {
base_color: Color::srgb_u8(166, 69, 16),
metallic: 0.0,
perceptual_roughness: 0.8,
..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 },
Visibility::Hidden,
Mesh3d(mesh.clone()),
MeshMaterial3d(preview_material),
))
.id();
commands.entity(info).insert((
BuildingInfo {
preview,
size: SIZE,
},
Name::new("Core"),
));
commands.insert_resource(CoreResources {
mesh: mesh,
material: material,
info_entity: info,
});
}
fn populate(
trigger: Trigger<PopulatePlacedBuilding>,
mut commands: Commands,
res: Res<CoreResources>,
) {
if trigger.event().info != res.info_entity {
return;
}
commands.entity(trigger.target()).insert((
RigidBody::Static,
Core,
Health::new(100.0),
Defense::new(5.0),
Name::from("Core"),
Mesh3d(res.mesh.clone()),
MeshMaterial3d(res.material.clone()),
));
}