//bevy system signatures often violate these rules #![allow(clippy::type_complexity)] #![allow(clippy::too_many_arguments)] use avian3d::prelude::*; use bevy::prelude::*; use building::Core; use combat::{GaveDamage, Health, Team}; use combat_damagers::ContactDamage; use inhabitant::Inhabitant; use state::{BasePhaseState, GameSystemSet}; pub struct InhabitantCoranthPlugin; impl Plugin for InhabitantCoranthPlugin { fn build(&self, app: &mut App) { app.add_event::() .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 Coranth; #[derive(Event)] pub struct SpawnCoranth { pub transform: Transform, } #[derive(Resource)] struct CoranthResources { material: Handle, mesh: Handle, charge_timer: Timer, } fn init( mut commands: Commands, mut meshes: ResMut>, mut materials: ResMut>, ) { commands.insert_resource(CoranthResources { material: materials.add(StandardMaterial { base_color: Color::srgb_u8(220, 156, 86), metallic: 1.0, perceptual_roughness: 0.3, ..default() }), mesh: meshes.add(Sphere::new(0.25)), charge_timer: Timer::from_seconds(1.5, TimerMode::Repeating), }); } fn spawn( mut reader: EventReader, mut commands: Commands, phase: Res>, res: Res, ) { for SpawnCoranth { transform } in reader.read() { commands .spawn(( RigidBody::Dynamic, Collider::sphere(0.25), Mesh3d(res.mesh.clone()), MeshMaterial3d(res.material.clone()), MaxLinearSpeed(25.), Coranth, transform.clone(), Health::new(1.0 + phase.get().phase_count() as f32), ContactDamage::new(5.0), Name::from("Coranth"), Team::Inhabitant.collision_layers(), )) .observe(explode); } } fn explode( trigger: Trigger, 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 coranths: Query<(&GlobalTransform, &mut LinearVelocity), With>, core_gtf: Single<&GlobalTransform, With>, mut res: ResMut, time: Res