2025-08-28 08:34:40 -04:00
|
|
|
//bevy system signatures often violate these rules
|
|
|
|
|
#![allow(clippy::type_complexity)]
|
|
|
|
|
#![allow(clippy::too_many_arguments)]
|
|
|
|
|
|
|
|
|
|
use avian3d::prelude::*;
|
|
|
|
|
use bevy::prelude::*;
|
|
|
|
|
use building::Building;
|
2025-08-31 19:00:19 -04:00
|
|
|
use combat::{GaveDamage, Health, Team};
|
2025-08-28 08:34:40 -04:00
|
|
|
use combat_damagers::ContactDamage;
|
|
|
|
|
use inhabitant::Inhabitant;
|
|
|
|
|
use state::GameSystemSet;
|
|
|
|
|
|
|
|
|
|
pub struct InhabitantCoranthPlugin;
|
|
|
|
|
|
|
|
|
|
impl Plugin for InhabitantCoranthPlugin {
|
|
|
|
|
fn build(&self, app: &mut App) {
|
|
|
|
|
app.add_event::<SpawnCoranth>()
|
|
|
|
|
.add_systems(FixedUpdate, tick.in_set(GameSystemSet::Execute))
|
|
|
|
|
.add_systems(FixedUpdate, spawn.in_set(GameSystemSet::PostTick));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Component)]
|
|
|
|
|
#[require(Inhabitant)]
|
|
|
|
|
pub struct Coranth;
|
|
|
|
|
|
|
|
|
|
#[derive(Event)]
|
|
|
|
|
pub struct SpawnCoranth {
|
|
|
|
|
pub transform: Transform,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn spawn(
|
|
|
|
|
mut reader: EventReader<SpawnCoranth>,
|
|
|
|
|
mut commands: Commands,
|
|
|
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
|
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
|
|
|
|
) {
|
|
|
|
|
for SpawnCoranth { transform } in reader.read() {
|
|
|
|
|
commands
|
|
|
|
|
.spawn((
|
|
|
|
|
RigidBody::Dynamic,
|
|
|
|
|
Collider::cuboid(0.5, 0.5, 0.5),
|
|
|
|
|
Mesh3d(meshes.add(Cuboid::from_length(0.5))),
|
|
|
|
|
MeshMaterial3d(materials.add(StandardMaterial {
|
|
|
|
|
base_color: Color::srgb_u8(220, 156, 86),
|
|
|
|
|
metallic: 1.0,
|
|
|
|
|
perceptual_roughness: 0.3,
|
|
|
|
|
..default()
|
|
|
|
|
})),
|
|
|
|
|
MaxLinearSpeed(25.),
|
|
|
|
|
Coranth,
|
|
|
|
|
transform.clone(),
|
|
|
|
|
Health::new(5.0),
|
2025-08-31 19:00:19 -04:00
|
|
|
ContactDamage::new(5.0),
|
2025-08-28 08:34:40 -04:00
|
|
|
Name::from("Coranth"),
|
2025-08-31 19:00:19 -04:00
|
|
|
Team::Inhabitant.collision_layers(),
|
2025-08-28 08:34:40 -04:00
|
|
|
))
|
|
|
|
|
.observe(explode);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn explode(
|
|
|
|
|
trigger: Trigger<GaveDamage>,
|
|
|
|
|
spatial_query: SpatialQuery,
|
|
|
|
|
trigger_query: Query<&GlobalTransform>,
|
|
|
|
|
mut other_query: Query<(&GlobalTransform, &mut LinearVelocity)>,
|
|
|
|
|
) {
|
|
|
|
|
let Ok(gtf) = trigger_query.get(trigger.target()) else {
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
let origin = gtf.translation();
|
|
|
|
|
for e in spatial_query.shape_intersections(
|
|
|
|
|
&Collider::sphere(5.0),
|
|
|
|
|
origin,
|
|
|
|
|
Quat::IDENTITY,
|
|
|
|
|
// &SpatialQueryFilter::from_mask(Team::Inhabitant),
|
|
|
|
|
&SpatialQueryFilter::default(),
|
|
|
|
|
) {
|
|
|
|
|
if let Ok((gtf, mut v)) = other_query.get_mut(e) {
|
|
|
|
|
let delta = gtf.translation() - origin;
|
2025-08-30 23:39:18 -04:00
|
|
|
v.0 += delta.normalize_or_zero() * 2.0;
|
2025-08-28 08:34:40 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn tick(
|
|
|
|
|
mut coranths: Query<(&GlobalTransform, &mut LinearVelocity), With<Coranth>>,
|
|
|
|
|
buildings: Query<&GlobalTransform, With<Building>>,
|
|
|
|
|
) {
|
|
|
|
|
for (coranth_tf, mut coranth_v) in coranths.iter_mut() {
|
|
|
|
|
let coranth_pos = coranth_tf.translation();
|
|
|
|
|
if let Some(closest_building) =
|
|
|
|
|
buildings
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|gtf| gtf.translation())
|
|
|
|
|
.min_by(|a, b| {
|
|
|
|
|
coranth_pos
|
|
|
|
|
.distance_squared(*a)
|
|
|
|
|
.total_cmp(&coranth_pos.distance_squared(*b))
|
|
|
|
|
})
|
|
|
|
|
{
|
|
|
|
|
let delta = closest_building - coranth_pos;
|
|
|
|
|
coranth_v.0 += delta.normalize_or_zero() * 0.1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|