//bevy system signatures often violate these rules #![allow(clippy::type_complexity)] #![allow(clippy::too_many_arguments)] use bevy::prelude::*; use building::{BuildingInfo, BuildingPreview, Core, PREVIEW_ALPHA, PopulatePlacedBuilding}; use combat::{Defense, Health, Range}; use ui_range_indicator::CustomRangeIndicator; pub struct BuildingCorePlugin; impl Plugin for BuildingCorePlugin { fn build(&self, app: &mut App) { app.add_systems(Startup, register).add_observer(populate); } } #[derive(Resource)] struct CoreResources { mesh: Handle, material: Handle, info_entity: Entity, } const SIZE: UVec3 = UVec3::new(2, 2, 2); fn register( mut commands: Commands, mut meshes: ResMut>, mut materials: ResMut>, ) { 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, unlocked: false, }, Name::new("Core"), )); commands.insert_resource(CoreResources { mesh: mesh, material: material, info_entity: info, }); } fn populate( trigger: Trigger, mut commands: Commands, mut materials: ResMut>, res: Res, ) { if trigger.event().info != res.info_entity { return; } commands.entity(trigger.target()).insert(( Core, Health::new(100.0), Defense::new(5.0), Range(25.0), Mesh3d(res.mesh.clone()), MeshMaterial3d(res.material.clone()), CustomRangeIndicator::new(materials.add(StandardMaterial { alpha_mode: AlphaMode::Blend, base_color: Color::srgba(0.6, 0.2, 0.2, 0.4), unlit: true, double_sided: true, ..default() })), )); }