150 lines
4.4 KiB
Rust
150 lines
4.4 KiB
Rust
|
|
//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;
|
||
|
|
use combat::{GaveDamage, Health, Team};
|
||
|
|
use combat_damagers::ContactDamage;
|
||
|
|
use inhabitant::Inhabitant;
|
||
|
|
use state::{BasePhaseState, GameSystemSet};
|
||
|
|
|
||
|
|
pub struct InhabitantSimbertPlugin;
|
||
|
|
|
||
|
|
impl Plugin for InhabitantSimbertPlugin {
|
||
|
|
fn build(&self, app: &mut App) {
|
||
|
|
app.add_event::<SpawnSimbert>()
|
||
|
|
.add_systems(Startup, init)
|
||
|
|
.add_systems(FixedUpdate, tick.in_set(GameSystemSet::Execute))
|
||
|
|
.add_systems(FixedUpdate, spawn.in_set(GameSystemSet::PostTick));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Component)]
|
||
|
|
#[require(Inhabitant)]
|
||
|
|
pub struct Simbert;
|
||
|
|
|
||
|
|
#[derive(Event)]
|
||
|
|
pub struct SpawnSimbert {
|
||
|
|
pub transform: Transform,
|
||
|
|
pub mega: bool,
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Resource)]
|
||
|
|
struct SimbertResources {
|
||
|
|
material: Handle<StandardMaterial>,
|
||
|
|
mesh: Handle<Mesh>,
|
||
|
|
mega_material: Handle<StandardMaterial>,
|
||
|
|
mega_scale: f32,
|
||
|
|
}
|
||
|
|
|
||
|
|
fn init(
|
||
|
|
mut commands: Commands,
|
||
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
||
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
||
|
|
) {
|
||
|
|
commands.insert_resource(SimbertResources {
|
||
|
|
material: materials.add(StandardMaterial {
|
||
|
|
base_color: Color::srgb_u8(167, 174, 56),
|
||
|
|
metallic: 0.1,
|
||
|
|
perceptual_roughness: 0.8,
|
||
|
|
..default()
|
||
|
|
}),
|
||
|
|
mesh: meshes.add(Tetrahedron::default()),
|
||
|
|
mega_material: materials.add(StandardMaterial {
|
||
|
|
base_color: Color::srgb_u8(239, 244, 164),
|
||
|
|
metallic: 0.4,
|
||
|
|
perceptual_roughness: 0.5,
|
||
|
|
..default()
|
||
|
|
}),
|
||
|
|
mega_scale: 1.3,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
fn spawn(
|
||
|
|
mut reader: EventReader<SpawnSimbert>,
|
||
|
|
mut commands: Commands,
|
||
|
|
phase: Res<State<BasePhaseState>>,
|
||
|
|
res: Res<SimbertResources>,
|
||
|
|
) {
|
||
|
|
for SpawnSimbert { transform, mega } in reader.read() {
|
||
|
|
let (health, damage, transform, material) = if *mega {
|
||
|
|
(
|
||
|
|
Health::new(5.0 + 1.5 * phase.get().phase_count() as f32),
|
||
|
|
ContactDamage::new(6.0),
|
||
|
|
transform.with_scale(transform.scale * res.mega_scale),
|
||
|
|
res.mega_material.clone(),
|
||
|
|
)
|
||
|
|
} else {
|
||
|
|
(
|
||
|
|
Health::new(1.0 + 1. * phase.get().phase_count() as f32),
|
||
|
|
ContactDamage::new(3.0),
|
||
|
|
transform.clone(),
|
||
|
|
res.material.clone(),
|
||
|
|
)
|
||
|
|
};
|
||
|
|
commands
|
||
|
|
.spawn((
|
||
|
|
RigidBody::Dynamic,
|
||
|
|
Collider::cone(0.5, 0.5),
|
||
|
|
Mesh3d(res.mesh.clone()),
|
||
|
|
MeshMaterial3d(material),
|
||
|
|
MaxLinearSpeed(25.),
|
||
|
|
Simbert,
|
||
|
|
transform,
|
||
|
|
health,
|
||
|
|
damage,
|
||
|
|
Name::from("Simbert"),
|
||
|
|
Team::Inhabitant.collision_layers(),
|
||
|
|
))
|
||
|
|
.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(2.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;
|
||
|
|
v.0 += delta.normalize_or_zero();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
fn tick(
|
||
|
|
mut simberts: Query<(&GlobalTransform, &mut LinearVelocity), With<Simbert>>,
|
||
|
|
buildings: Query<&GlobalTransform, With<Building>>,
|
||
|
|
) {
|
||
|
|
for (simbert_tf, mut simbert_v) in simberts.iter_mut() {
|
||
|
|
let simbert_pos = simbert_tf.translation();
|
||
|
|
|
||
|
|
if let Some(closest_building) =
|
||
|
|
buildings
|
||
|
|
.iter()
|
||
|
|
.map(|gtf| gtf.translation())
|
||
|
|
.min_by(|a, b| {
|
||
|
|
simbert_pos
|
||
|
|
.distance_squared(*a)
|
||
|
|
.total_cmp(&simbert_pos.distance_squared(*b))
|
||
|
|
})
|
||
|
|
{
|
||
|
|
let delta = (closest_building - simbert_pos).with_y(0.);
|
||
|
|
simbert_v.0 += delta.normalize_or_zero() * 0.15;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|