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",
|
"avian3d",
|
||||||
"bevy",
|
"bevy",
|
||||||
"physics",
|
"physics",
|
||||||
|
"rand",
|
||||||
"resources",
|
"resources",
|
||||||
"state",
|
"state",
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
(
|
(
|
||||||
map: {
|
map: {
|
||||||
KeyCode(ShiftLeft): [
|
MouseButton(Middle): [
|
||||||
MultiPlaceTower,
|
CameraOrbitEnable,
|
||||||
],
|
],
|
||||||
MouseButton(Right): [
|
MouseButton(Right): [
|
||||||
PlaceTower,
|
PlaceTower,
|
||||||
|
|
@ -9,8 +9,8 @@
|
||||||
MouseButton(Left): [
|
MouseButton(Left): [
|
||||||
CameraPanEnable,
|
CameraPanEnable,
|
||||||
],
|
],
|
||||||
MouseButton(Middle): [
|
KeyCode(ShiftLeft): [
|
||||||
CameraOrbitEnable,
|
MultiPlaceTower,
|
||||||
],
|
],
|
||||||
MouseScroll: [
|
MouseScroll: [
|
||||||
CameraZoom,
|
CameraZoom,
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ use bevy::prelude::*;
|
||||||
use building::{BuildingInfo, BuildingPreview, PREVIEW_ALPHA, PopulatePlacedBuilding};
|
use building::{BuildingInfo, BuildingPreview, PREVIEW_ALPHA, PopulatePlacedBuilding};
|
||||||
use combat::{
|
use combat::{
|
||||||
Defense, Health,
|
Defense, Health,
|
||||||
projectiles::{Fire, Projectile, ProjectileLauncher, get_launch_vector},
|
projectiles::{Fire, Projectile, ProjectileLauncher, get_shotgun_launch_vectors},
|
||||||
};
|
};
|
||||||
use combat_damagers::ContactDamage;
|
use combat_damagers::ContactDamage;
|
||||||
use resources::Cost;
|
use resources::Cost;
|
||||||
|
|
@ -106,6 +106,7 @@ fn populate(
|
||||||
5.0,
|
5.0,
|
||||||
Vec3::Y * (SIZE.y as f32 + 0.5),
|
Vec3::Y * (SIZE.y as f32 + 0.5),
|
||||||
Duration::from_secs(10),
|
Duration::from_secs(10),
|
||||||
|
10.0_f32.to_radians(),
|
||||||
),
|
),
|
||||||
Mesh3d(res.mesh.clone()),
|
Mesh3d(res.mesh.clone()),
|
||||||
MeshMaterial3d(res.material.clone()),
|
MeshMaterial3d(res.material.clone()),
|
||||||
|
|
@ -114,6 +115,13 @@ fn populate(
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_fire(trigger: Trigger<Fire>, mut commands: Commands, res: Res<BuildingResources>) {
|
fn on_fire(trigger: Trigger<Fire>, mut commands: Commands, res: Res<BuildingResources>) {
|
||||||
|
for v in get_shotgun_launch_vectors(
|
||||||
|
trigger.event().spawn_pos,
|
||||||
|
trigger.event().target_pos,
|
||||||
|
5.0,
|
||||||
|
trigger.event().launcher.spread,
|
||||||
|
5,
|
||||||
|
) {
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
RigidBody::Dynamic,
|
RigidBody::Dynamic,
|
||||||
trigger.event().team,
|
trigger.event().team,
|
||||||
|
|
@ -121,13 +129,11 @@ fn on_fire(trigger: Trigger<Fire>, mut commands: Commands, res: Res<BuildingReso
|
||||||
ContactDamage::projectile(5.0, f32::INFINITY),
|
ContactDamage::projectile(5.0, f32::INFINITY),
|
||||||
Transform::from_translation(trigger.event().spawn_pos),
|
Transform::from_translation(trigger.event().spawn_pos),
|
||||||
Projectile::from(&trigger.event().launcher),
|
Projectile::from(&trigger.event().launcher),
|
||||||
LinearVelocity(get_launch_vector(
|
Projectile::collision_layers(trigger.event().team),
|
||||||
trigger.event().spawn_pos,
|
LinearVelocity(v),
|
||||||
trigger.event().target_pos,
|
|
||||||
5.0,
|
|
||||||
)),
|
|
||||||
res.projectile_collider.clone(),
|
res.projectile_collider.clone(),
|
||||||
MeshMaterial3d(res.projectile_material.clone()),
|
MeshMaterial3d(res.projectile_material.clone()),
|
||||||
Mesh3d(res.projectile_mesh.clone()),
|
Mesh3d(res.projectile_mesh.clone()),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ edition = "2024"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bevy = { workspace = true }
|
bevy = { workspace = true }
|
||||||
avian3d = { workspace = true }
|
avian3d = { workspace = true }
|
||||||
|
rand = { workspace = true }
|
||||||
|
|
||||||
state = { path = "../state" }
|
state = { path = "../state" }
|
||||||
physics = { path = "../physics"}
|
physics = { path = "../physics"}
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ use std::time::Duration;
|
||||||
|
|
||||||
use avian3d::prelude::*;
|
use avian3d::prelude::*;
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
use physics::PhysicsLayers;
|
||||||
use resources::{Drops, Resources};
|
use resources::{Drops, Resources};
|
||||||
use state::GameSystemSet;
|
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 {
|
pub enum Team {
|
||||||
#[default]
|
#[default]
|
||||||
Core,
|
Core = 1,
|
||||||
Inhabitant,
|
Inhabitant = 2,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Team {
|
impl Team {
|
||||||
pub fn collision_layers(&self) -> CollisionLayers {
|
pub fn collision_layers(&self) -> CollisionLayers {
|
||||||
match self {
|
match self {
|
||||||
Team::Core => CollisionLayers::new(Team::Core, LayerMask::ALL),
|
Team::Core => CollisionLayers::new(PhysicsLayers::Core, LayerMask::ALL),
|
||||||
Team::Inhabitant => CollisionLayers::new(Team::Inhabitant, LayerMask::ALL),
|
Team::Inhabitant => CollisionLayers::new(PhysicsLayers::Inhabitant, LayerMask::ALL),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn enemy_layers(&self) -> LayerMask {
|
pub fn enemy_layers(&self) -> LayerMask {
|
||||||
match self {
|
match self {
|
||||||
Team::Core => Team::Inhabitant.into(),
|
Team::Core => PhysicsLayers::Inhabitant.into(),
|
||||||
Team::Inhabitant => Team::Core.into(),
|
Team::Inhabitant => PhysicsLayers::Core.into(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,8 @@ use std::time::Duration;
|
||||||
|
|
||||||
use avian3d::prelude::*;
|
use avian3d::prelude::*;
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
use physics::PhysicsLayers;
|
||||||
|
use rand::{Rng, thread_rng};
|
||||||
use state::GameSystemSet;
|
use state::GameSystemSet;
|
||||||
|
|
||||||
use crate::Team;
|
use crate::Team;
|
||||||
|
|
@ -24,6 +26,17 @@ pub struct Projectile {
|
||||||
pub lifetime: Duration,
|
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 {
|
impl From<&ProjectileLauncher> for Projectile {
|
||||||
fn from(value: &ProjectileLauncher) -> Self {
|
fn from(value: &ProjectileLauncher) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
|
@ -37,6 +50,7 @@ impl From<&ProjectileLauncher> for Projectile {
|
||||||
pub struct ProjectileLauncher {
|
pub struct ProjectileLauncher {
|
||||||
pub fire_timer: Timer,
|
pub fire_timer: Timer,
|
||||||
pub fire_offset: Vec3,
|
pub fire_offset: Vec3,
|
||||||
|
pub spread: f32,
|
||||||
pub lifetime: Duration,
|
pub lifetime: Duration,
|
||||||
pub range: f32,
|
pub range: f32,
|
||||||
pub damage: f32,
|
pub damage: f32,
|
||||||
|
|
@ -49,10 +63,12 @@ impl ProjectileLauncher {
|
||||||
damage: f32,
|
damage: f32,
|
||||||
fire_offset: Vec3,
|
fire_offset: Vec3,
|
||||||
lifetime: Duration,
|
lifetime: Duration,
|
||||||
|
spread: f32,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
fire_timer: Timer::new(fire_interval, TimerMode::Once),
|
fire_timer: Timer::new(fire_interval, TimerMode::Once),
|
||||||
range,
|
range,
|
||||||
|
spread,
|
||||||
damage,
|
damage,
|
||||||
fire_offset,
|
fire_offset,
|
||||||
lifetime,
|
lifetime,
|
||||||
|
|
@ -132,7 +148,7 @@ fn fire_launcher(
|
||||||
}
|
}
|
||||||
|
|
||||||
/// may shoot higher than height if delta y is too big
|
/// 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
|
// actually +/-, but go with + for now
|
||||||
let delta_y = target.y - start.y;
|
let delta_y = target.y - start.y;
|
||||||
let height = height.max(delta_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 delta_x = delta_x_vector.length();
|
||||||
let v_x = delta_x / t;
|
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 building::{BuildingTypes, PlaceBuilding};
|
||||||
use camera::ControlledCamera;
|
use camera::ControlledCamera;
|
||||||
use inhabitant_coranth::SpawnCoranth;
|
use inhabitant_coranth::SpawnCoranth;
|
||||||
use physics::Ground;
|
use physics::{Ground, PhysicsLayers};
|
||||||
use state::{AppState, GameSystemSet};
|
use state::{AppState, GameSystemSet};
|
||||||
|
|
||||||
pub struct LevelPlugin;
|
pub struct LevelPlugin;
|
||||||
|
|
@ -47,6 +47,7 @@ fn setup(
|
||||||
perceptual_roughness: 0.8,
|
perceptual_roughness: 0.8,
|
||||||
..default()
|
..default()
|
||||||
})),
|
})),
|
||||||
|
CollisionLayers::new(PhysicsLayers::Ground, LayerMask::ALL),
|
||||||
PhysicsPickable,
|
PhysicsPickable,
|
||||||
Ground,
|
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)]
|
#[derive(Component)]
|
||||||
pub struct Ground;
|
pub struct Ground;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue