coreheart/crates/building_core/src/lib.rs
2025-09-01 22:05:18 -04:00

113 lines
3.1 KiB
Rust
Executable file

//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, Died, Health, Range};
use state::{CoreState, GameState};
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<Mesh>,
material: Handle<StandardMaterial>,
info_entity: Entity,
}
#[derive(Component)]
pub struct CorePreview;
const SIZE: UVec3 = UVec3::new(2, 2, 2);
fn register(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
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),
CorePreview,
))
.id();
commands.entity(info).insert((
BuildingInfo {
preview,
size: SIZE,
unlocked: false,
},
Name::new("Core"),
Health::new(100.0),
Defense::new(5.0),
Range(25.0),
));
commands.insert_resource(CoreResources {
mesh: mesh,
material: material,
info_entity: info,
});
}
fn populate(
trigger: Trigger<PopulatePlacedBuilding>,
mut commands: Commands,
mut materials: ResMut<Assets<StandardMaterial>>,
mut next_core_state: ResMut<NextState<CoreState>>,
res: Res<CoreResources>,
) {
if trigger.event().info != res.info_entity {
return;
}
commands
.entity(trigger.target())
.insert((
Core,
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()
})),
))
.observe(core_dead);
next_core_state.set(CoreState::Placed);
}
fn core_dead(
_trigger: Trigger<Died>,
mut next_core_state: ResMut<NextState<CoreState>>,
mut next_game_state: ResMut<NextState<GameState>>,
) {
next_core_state.set(CoreState::Dead);
next_game_state.set(GameState::GameOver);
}