projectile system
This commit is contained in:
parent
918b4884b2
commit
ad4a2cf393
17 changed files with 400 additions and 23 deletions
|
|
@ -2,6 +2,8 @@
|
|||
#![allow(clippy::type_complexity)]
|
||||
#![allow(clippy::too_many_arguments)]
|
||||
|
||||
pub mod projectiles;
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use avian3d::prelude::*;
|
||||
|
|
@ -13,7 +15,8 @@ pub struct CombatPlugin;
|
|||
|
||||
impl Plugin for CombatPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_observer(on_attack)
|
||||
app.add_plugins(projectiles::ProjectilePlugin)
|
||||
.add_observer(on_attack)
|
||||
.add_observer(on_die)
|
||||
.add_systems(
|
||||
FixedUpdate,
|
||||
|
|
@ -23,7 +26,7 @@ impl Plugin for CombatPlugin {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(PhysicsLayer, Default, Component, Clone, Debug, PartialEq, Eq, Reflect)]
|
||||
#[derive(PhysicsLayer, Default, Component, Clone, Copy, Debug, PartialEq, Eq, Reflect)]
|
||||
pub enum Team {
|
||||
#[default]
|
||||
Core,
|
||||
|
|
@ -37,6 +40,12 @@ impl Team {
|
|||
Team::Inhabitant => CollisionLayers::new(Team::Inhabitant, LayerMask::ALL),
|
||||
}
|
||||
}
|
||||
pub fn enemy_layers(&self) -> LayerMask {
|
||||
match self {
|
||||
Team::Core => Team::Inhabitant.into(),
|
||||
Team::Inhabitant => Team::Core.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Event, Debug, Default)]
|
||||
|
|
|
|||
148
crates/combat/src/projectiles.rs
Normal file
148
crates/combat/src/projectiles.rs
Normal file
|
|
@ -0,0 +1,148 @@
|
|||
//bevy system signatures often violate these rules
|
||||
#![allow(clippy::type_complexity)]
|
||||
#![allow(clippy::too_many_arguments)]
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use avian3d::prelude::*;
|
||||
use bevy::prelude::*;
|
||||
use state::GameSystemSet;
|
||||
|
||||
use crate::Team;
|
||||
|
||||
pub(crate) struct ProjectilePlugin;
|
||||
|
||||
impl Plugin for ProjectilePlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(FixedUpdate, fire_launcher.in_set(GameSystemSet::Execute));
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
#[require(Team)]
|
||||
pub struct Projectile {
|
||||
pub lifetime: Duration,
|
||||
}
|
||||
|
||||
impl From<&ProjectileLauncher> for Projectile {
|
||||
fn from(value: &ProjectileLauncher) -> Self {
|
||||
Self {
|
||||
lifetime: value.lifetime,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component, Clone)]
|
||||
#[require(Team)]
|
||||
pub struct ProjectileLauncher {
|
||||
pub fire_timer: Timer,
|
||||
pub fire_offset: Vec3,
|
||||
pub lifetime: Duration,
|
||||
pub range: f32,
|
||||
pub damage: f32,
|
||||
}
|
||||
|
||||
impl ProjectileLauncher {
|
||||
pub fn new(
|
||||
fire_interval: Duration,
|
||||
range: f32,
|
||||
damage: f32,
|
||||
fire_offset: Vec3,
|
||||
lifetime: Duration,
|
||||
) -> Self {
|
||||
Self {
|
||||
fire_timer: Timer::new(fire_interval, TimerMode::Once),
|
||||
range,
|
||||
damage,
|
||||
fire_offset,
|
||||
lifetime,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Event)]
|
||||
pub struct Fire {
|
||||
pub target: Entity,
|
||||
pub target_pos: Vec3,
|
||||
pub spawn_pos: Vec3,
|
||||
pub launcher: ProjectileLauncher,
|
||||
pub team: Team,
|
||||
}
|
||||
|
||||
fn fire_launcher(
|
||||
mut commands: Commands,
|
||||
mut launcher_query: Query<(Entity, &mut ProjectileLauncher, &Team, &GlobalTransform)>,
|
||||
spatial_query: SpatialQuery,
|
||||
enemy_query: Query<(&Team, &GlobalTransform)>,
|
||||
time: Res<Time>,
|
||||
) {
|
||||
let delta = time.delta();
|
||||
for (launcher_entity, mut launcher, team, gtf) in launcher_query.iter_mut() {
|
||||
// see if enough time has passed to shoot
|
||||
launcher.fire_timer.tick(delta);
|
||||
if !launcher.fire_timer.finished() {
|
||||
continue;
|
||||
}
|
||||
|
||||
// see if enemy in range
|
||||
let enemies = spatial_query.shape_intersections(
|
||||
&Collider::sphere(launcher.range),
|
||||
gtf.translation(),
|
||||
Quat::default(),
|
||||
&SpatialQueryFilter::from_mask(team.enemy_layers()),
|
||||
);
|
||||
if enemies.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let Some((closest_enemy, enemy_pos)) = enemies
|
||||
.iter()
|
||||
.filter_map(|e| {
|
||||
enemy_query
|
||||
.get(*e)
|
||||
.ok()
|
||||
.and_then(|(enemy_team, enemy_gtf)| {
|
||||
if enemy_team != team {
|
||||
Some((*e, enemy_gtf.translation()))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
})
|
||||
.min_by(|(_, a), (_, b)| {
|
||||
gtf.translation()
|
||||
.distance_squared(*a)
|
||||
.total_cmp(>f.translation().distance_squared(*b))
|
||||
})
|
||||
else {
|
||||
info!("no closest enemy, is someone on the wrong physics layer for their team?");
|
||||
return;
|
||||
};
|
||||
launcher.fire_timer.reset();
|
||||
commands.trigger_targets(
|
||||
Fire {
|
||||
target: closest_enemy,
|
||||
target_pos: enemy_pos,
|
||||
spawn_pos: gtf.translation() + launcher.fire_offset,
|
||||
launcher: launcher.clone(),
|
||||
team: team.clone(),
|
||||
},
|
||||
[launcher_entity],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// may shoot higher than height if delta y is too big
|
||||
pub fn get_launch_vector(start: Vec3, target: Vec3, height: f32) -> Vec3 {
|
||||
// actually +/-, but go with + for now
|
||||
let delta_y = target.y - start.y;
|
||||
let height = height.max(delta_y);
|
||||
let v_y = -1. + (1. + 2. * height).sqrt();
|
||||
|
||||
let t = (2. * (delta_y - v_y) / (-9.81)).sqrt();
|
||||
|
||||
let delta_x_vector = target.with_y(0.) - start.with_y(0.);
|
||||
let delta_x = delta_x_vector.length();
|
||||
let v_x = delta_x / t;
|
||||
|
||||
return v_x * delta_x_vector / delta_x + Vec3::Y * v_y;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue