add shotgun and fix up collision layers
This commit is contained in:
parent
ad4a2cf393
commit
87fcc35a49
8 changed files with 92 additions and 31 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -1833,6 +1833,7 @@ dependencies = [
|
|||
"avian3d",
|
||||
"bevy",
|
||||
"physics",
|
||||
"rand",
|
||||
"resources",
|
||||
"state",
|
||||
]
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
(
|
||||
map: {
|
||||
KeyCode(ShiftLeft): [
|
||||
MultiPlaceTower,
|
||||
MouseButton(Middle): [
|
||||
CameraOrbitEnable,
|
||||
],
|
||||
MouseButton(Right): [
|
||||
PlaceTower,
|
||||
|
|
@ -9,8 +9,8 @@
|
|||
MouseButton(Left): [
|
||||
CameraPanEnable,
|
||||
],
|
||||
MouseButton(Middle): [
|
||||
CameraOrbitEnable,
|
||||
KeyCode(ShiftLeft): [
|
||||
MultiPlaceTower,
|
||||
],
|
||||
MouseScroll: [
|
||||
CameraZoom,
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ use bevy::prelude::*;
|
|||
use building::{BuildingInfo, BuildingPreview, PREVIEW_ALPHA, PopulatePlacedBuilding};
|
||||
use combat::{
|
||||
Defense, Health,
|
||||
projectiles::{Fire, Projectile, ProjectileLauncher, get_launch_vector},
|
||||
projectiles::{Fire, Projectile, ProjectileLauncher, get_shotgun_launch_vectors},
|
||||
};
|
||||
use combat_damagers::ContactDamage;
|
||||
use resources::Cost;
|
||||
|
|
@ -106,6 +106,7 @@ fn populate(
|
|||
5.0,
|
||||
Vec3::Y * (SIZE.y as f32 + 0.5),
|
||||
Duration::from_secs(10),
|
||||
10.0_f32.to_radians(),
|
||||
),
|
||||
Mesh3d(res.mesh.clone()),
|
||||
MeshMaterial3d(res.material.clone()),
|
||||
|
|
@ -114,20 +115,25 @@ fn populate(
|
|||
}
|
||||
|
||||
fn on_fire(trigger: Trigger<Fire>, mut commands: Commands, res: Res<BuildingResources>) {
|
||||
commands.spawn((
|
||||
RigidBody::Dynamic,
|
||||
trigger.event().team,
|
||||
Health::new(1.0),
|
||||
ContactDamage::projectile(5.0, f32::INFINITY),
|
||||
Transform::from_translation(trigger.event().spawn_pos),
|
||||
Projectile::from(&trigger.event().launcher),
|
||||
LinearVelocity(get_launch_vector(
|
||||
trigger.event().spawn_pos,
|
||||
trigger.event().target_pos,
|
||||
5.0,
|
||||
)),
|
||||
res.projectile_collider.clone(),
|
||||
MeshMaterial3d(res.projectile_material.clone()),
|
||||
Mesh3d(res.projectile_mesh.clone()),
|
||||
));
|
||||
for v in get_shotgun_launch_vectors(
|
||||
trigger.event().spawn_pos,
|
||||
trigger.event().target_pos,
|
||||
5.0,
|
||||
trigger.event().launcher.spread,
|
||||
5,
|
||||
) {
|
||||
commands.spawn((
|
||||
RigidBody::Dynamic,
|
||||
trigger.event().team,
|
||||
Health::new(1.0),
|
||||
ContactDamage::projectile(5.0, f32::INFINITY),
|
||||
Transform::from_translation(trigger.event().spawn_pos),
|
||||
Projectile::from(&trigger.event().launcher),
|
||||
Projectile::collision_layers(trigger.event().team),
|
||||
LinearVelocity(v),
|
||||
res.projectile_collider.clone(),
|
||||
MeshMaterial3d(res.projectile_material.clone()),
|
||||
Mesh3d(res.projectile_mesh.clone()),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ edition = "2024"
|
|||
[dependencies]
|
||||
bevy = { workspace = true }
|
||||
avian3d = { workspace = true }
|
||||
rand = { workspace = true }
|
||||
|
||||
state = { path = "../state" }
|
||||
physics = { path = "../physics"}
|
||||
|
|
|
|||
|
|
@ -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(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ use bevy_egui::PrimaryEguiContext;
|
|||
use building::{BuildingTypes, PlaceBuilding};
|
||||
use camera::ControlledCamera;
|
||||
use inhabitant_coranth::SpawnCoranth;
|
||||
use physics::Ground;
|
||||
use physics::{Ground, PhysicsLayers};
|
||||
use state::{AppState, GameSystemSet};
|
||||
|
||||
pub struct LevelPlugin;
|
||||
|
|
@ -47,6 +47,7 @@ fn setup(
|
|||
perceptual_roughness: 0.8,
|
||||
..default()
|
||||
})),
|
||||
CollisionLayers::new(PhysicsLayers::Ground, LayerMask::ALL),
|
||||
PhysicsPickable,
|
||||
Ground,
|
||||
));
|
||||
|
|
|
|||
|
|
@ -19,5 +19,14 @@ impl Plugin for PhysicsPlugin {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(PhysicsLayer, Default, Clone, Copy, Debug, PartialEq, Eq, Reflect)]
|
||||
pub enum PhysicsLayers {
|
||||
#[default]
|
||||
Ground,
|
||||
Core,
|
||||
Inhabitant,
|
||||
Projectiles,
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Ground;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue