add shotgun and fix up collision layers

This commit is contained in:
Jim 2025-08-31 20:30:31 -04:00
parent ad4a2cf393
commit 87fcc35a49
Signed by: jim
GPG key ID: 3D7D94BA53088BF4
8 changed files with 92 additions and 31 deletions

View file

@ -6,6 +6,7 @@ edition = "2024"
[dependencies]
bevy = { workspace = true }
avian3d = { workspace = true }
rand = { workspace = true }
state = { path = "../state" }
physics = { path = "../physics"}

View file

@ -8,6 +8,7 @@ use std::time::Duration;
use avian3d::prelude::*;
use bevy::prelude::*;
use physics::PhysicsLayers;
use resources::{Drops, Resources};
use state::GameSystemSet;
@ -26,24 +27,24 @@ impl Plugin for CombatPlugin {
}
}
#[derive(PhysicsLayer, Default, Component, Clone, Copy, Debug, PartialEq, Eq, Reflect)]
#[derive(Default, Component, Clone, Copy, Debug, PartialEq, Eq, Reflect)]
pub enum Team {
#[default]
Core,
Inhabitant,
Core = 1,
Inhabitant = 2,
}
impl Team {
pub fn collision_layers(&self) -> CollisionLayers {
match self {
Team::Core => CollisionLayers::new(Team::Core, LayerMask::ALL),
Team::Inhabitant => CollisionLayers::new(Team::Inhabitant, LayerMask::ALL),
Team::Core => CollisionLayers::new(PhysicsLayers::Core, LayerMask::ALL),
Team::Inhabitant => CollisionLayers::new(PhysicsLayers::Inhabitant, LayerMask::ALL),
}
}
pub fn enemy_layers(&self) -> LayerMask {
match self {
Team::Core => Team::Inhabitant.into(),
Team::Inhabitant => Team::Core.into(),
Team::Core => PhysicsLayers::Inhabitant.into(),
Team::Inhabitant => PhysicsLayers::Core.into(),
}
}
}

View file

@ -6,6 +6,8 @@ use std::time::Duration;
use avian3d::prelude::*;
use bevy::prelude::*;
use physics::PhysicsLayers;
use rand::{Rng, thread_rng};
use state::GameSystemSet;
use crate::Team;
@ -24,6 +26,17 @@ pub struct Projectile {
pub lifetime: Duration,
}
impl Projectile {
pub fn collision_layers(team: Team) -> CollisionLayers {
// projectiles shouldn't intersect with each other, otherwise it can be hard to do things like shotguns
// (and perf would be rough)
CollisionLayers::new(
PhysicsLayers::Projectiles,
LayerMask::ALL ^ PhysicsLayers::Projectiles ^ team.collision_layers().memberships,
)
}
}
impl From<&ProjectileLauncher> for Projectile {
fn from(value: &ProjectileLauncher) -> Self {
Self {
@ -37,6 +50,7 @@ impl From<&ProjectileLauncher> for Projectile {
pub struct ProjectileLauncher {
pub fire_timer: Timer,
pub fire_offset: Vec3,
pub spread: f32,
pub lifetime: Duration,
pub range: f32,
pub damage: f32,
@ -49,10 +63,12 @@ impl ProjectileLauncher {
damage: f32,
fire_offset: Vec3,
lifetime: Duration,
spread: f32,
) -> Self {
Self {
fire_timer: Timer::new(fire_interval, TimerMode::Once),
range,
spread,
damage,
fire_offset,
lifetime,
@ -132,7 +148,7 @@ fn fire_launcher(
}
/// may shoot higher than height if delta y is too big
pub fn get_launch_vector(start: Vec3, target: Vec3, height: f32) -> Vec3 {
pub fn get_launch_vector(start: Vec3, target: Vec3, height: f32, spread: f32) -> Vec3 {
// actually +/-, but go with + for now
let delta_y = target.y - start.y;
let height = height.max(delta_y);
@ -144,5 +160,31 @@ pub fn get_launch_vector(start: Vec3, target: Vec3, height: f32) -> Vec3 {
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;
let launch = v_x * delta_x_vector / delta_x + Vec3::Y * v_y;
if spread == 0.0 {
return launch;
} else {
return twiddle_angle(launch, spread, &mut thread_rng());
}
}
pub fn get_shotgun_launch_vectors(
start: Vec3,
target: Vec3,
height: f32,
spread: f32,
shots: usize,
) -> impl Iterator<Item = Vec3> {
let base_v = get_launch_vector(start, target, height, 0.0);
std::iter::repeat_n(base_v, shots).map(move |v| twiddle_angle(v, spread, &mut thread_rng()))
}
pub fn twiddle_angle(v: Vec3, max_angle: f32, rng: &mut impl Rng) -> Vec3 {
Quat::from_euler(
EulerRot::XYX,
rng.gen_range(-max_angle..max_angle),
rng.gen_range(-max_angle..max_angle),
0.,
)
.mul_vec3(v)
}