add suncannon
This commit is contained in:
parent
3e42fb6cf1
commit
4c5a650a1a
7 changed files with 355 additions and 9 deletions
|
|
@ -62,7 +62,6 @@ fn register(
|
|||
..base_material
|
||||
});
|
||||
let projectile_material = materials.add(StandardMaterial {
|
||||
alpha_mode: AlphaMode::Blend,
|
||||
base_color: Color::srgb_u8(86, 106, 106),
|
||||
metallic: 1.0,
|
||||
perceptual_roughness: 0.0,
|
||||
|
|
|
|||
18
crates/building_suncannon/Cargo.toml
Normal file
18
crates/building_suncannon/Cargo.toml
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
[package]
|
||||
name = "building_suncannon"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
bevy = { workspace = true }
|
||||
avian3d = { workspace = true }
|
||||
|
||||
state = { path = "../state" }
|
||||
combat = { path = "../combat" }
|
||||
building = { path = "../building" }
|
||||
resources = { path = "../resources" }
|
||||
combat_damagers = { path = "../combat_damagers" }
|
||||
upgrade = { path = "../upgrade" }
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
312
crates/building_suncannon/src/lib.rs
Normal file
312
crates/building_suncannon/src/lib.rs
Normal file
|
|
@ -0,0 +1,312 @@
|
|||
//bevy system signatures often violate these rules
|
||||
#![allow(clippy::type_complexity)]
|
||||
#![allow(clippy::too_many_arguments)]
|
||||
|
||||
use std::{
|
||||
f32::{self, consts::PI},
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
use avian3d::prelude::*;
|
||||
use bevy::prelude::*;
|
||||
use building::{BuildingInfo, BuildingPreview, PREVIEW_ALPHA, PopulatePlacedBuilding};
|
||||
use combat::{
|
||||
Attacked, Defense, Died, Health, Range, Team,
|
||||
projectiles::{Fire, Projectile, ProjectileLauncher, get_launch_vector},
|
||||
};
|
||||
use combat_damagers::ContactDamage;
|
||||
use resources::{Cost, Quantity};
|
||||
use upgrade::{Upgrade, UpgradeApplied, UpgradeTree};
|
||||
|
||||
pub struct BuildingSuncannonPlugin;
|
||||
|
||||
impl Plugin for BuildingSuncannonPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(Startup, register).add_observer(populate);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component, Clone)]
|
||||
pub struct Suncannon {
|
||||
pub explosion_radius: f32,
|
||||
pub explosion_damage: f32,
|
||||
pub explosion_knockback: f32,
|
||||
pub pellet_count: u32,
|
||||
pub pellet_damage: f32,
|
||||
pub pellet_speed: f32,
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
struct SuncannonProjectile(Suncannon);
|
||||
|
||||
#[derive(Resource)]
|
||||
struct BuildingResources {
|
||||
mesh: Handle<Mesh>,
|
||||
material: Handle<StandardMaterial>,
|
||||
projectile_mesh: Handle<Mesh>,
|
||||
projectile_material: Handle<StandardMaterial>,
|
||||
projectile_collider: Collider,
|
||||
info_entity: Entity,
|
||||
}
|
||||
|
||||
const SIZE: UVec3 = UVec3::new(3, 1, 3);
|
||||
const PROJ_SIZE: Vec3 = Vec3::new(1.0, 1.0, 1.0);
|
||||
|
||||
fn register(
|
||||
mut commands: Commands,
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||
) {
|
||||
let mesh = meshes.add(Cuboid::from_size(SIZE.as_vec3()));
|
||||
let base_material = StandardMaterial {
|
||||
base_color: Color::srgb_u8(173, 114, 57),
|
||||
metallic: 0.8,
|
||||
perceptual_roughness: 0.0,
|
||||
emissive: Color::srgb_u8(173, 114, 57).to_linear(),
|
||||
..default()
|
||||
};
|
||||
let material = materials.add(base_material.clone());
|
||||
let preview_material = materials.add(StandardMaterial {
|
||||
alpha_mode: AlphaMode::Blend,
|
||||
base_color: base_material.base_color.with_alpha(PREVIEW_ALPHA),
|
||||
..base_material
|
||||
});
|
||||
let projectile_material = materials.add(StandardMaterial {
|
||||
base_color: Color::srgb_u8(242, 151, 70),
|
||||
metallic: 0.8,
|
||||
perceptual_roughness: 0.0,
|
||||
emissive: Color::srgb_u8(250, 211, 150).to_linear() * 5.,
|
||||
..default()
|
||||
});
|
||||
|
||||
let info = commands.spawn_empty().id();
|
||||
let tier3 = vec![
|
||||
commands
|
||||
.spawn((
|
||||
Name::new("Sun Geyser"),
|
||||
Upgrade {
|
||||
description: "I AM THE SUN!".to_string(),
|
||||
cost: Quantity {
|
||||
souls: 1000,
|
||||
bones: 0,
|
||||
},
|
||||
unlocks: vec![],
|
||||
},
|
||||
))
|
||||
.observe(
|
||||
|trigger: Trigger<UpgradeApplied>,
|
||||
mut query: Query<(&mut Range, &mut ProjectileLauncher, &mut Suncannon)>| {
|
||||
if let Ok((mut range, mut launcher, mut suncannon)) = query.get_mut(trigger.event().building) {
|
||||
range.0 += 10.;
|
||||
let new_duration = launcher.fire_timer.duration() / 4;
|
||||
launcher.set_interval(new_duration);
|
||||
suncannon.pellet_damage *= 2.;
|
||||
}
|
||||
},
|
||||
)
|
||||
.id(),
|
||||
];
|
||||
let tier2 = vec![
|
||||
commands
|
||||
.spawn((
|
||||
Name::new("Solar Power"),
|
||||
Upgrade {
|
||||
description: "I WILL PUSH THEM ALL BACK. (WITH FORCE)".to_string(),
|
||||
cost: Quantity {
|
||||
souls: 500,
|
||||
bones: 0,
|
||||
},
|
||||
unlocks: tier3,
|
||||
},
|
||||
))
|
||||
.observe(
|
||||
|trigger: Trigger<UpgradeApplied>,
|
||||
mut query: Query<(&mut ProjectileLauncher, &mut Suncannon)>| {
|
||||
if let Ok((mut launcher, mut suncannon)) =
|
||||
query.get_mut(trigger.event().building)
|
||||
{
|
||||
launcher.damage *= 1.5;
|
||||
suncannon.explosion_damage *= 2.;
|
||||
suncannon.explosion_knockback *= 2.;
|
||||
suncannon.pellet_damage *= 2.;
|
||||
}
|
||||
},
|
||||
)
|
||||
.id(),
|
||||
];
|
||||
let upgrades = vec![
|
||||
commands
|
||||
.spawn((
|
||||
Name::new("Sun Pellets"),
|
||||
Upgrade {
|
||||
description: "THEY WILL EAT THIS! AND NOT LIKE IT!".to_string(),
|
||||
cost: Quantity {
|
||||
souls: 200,
|
||||
bones: 0,
|
||||
},
|
||||
unlocks: tier2,
|
||||
},
|
||||
))
|
||||
.observe(
|
||||
|trigger: Trigger<UpgradeApplied>, mut query: Query<&mut Suncannon>| {
|
||||
if let Ok(mut suncannon) = query.get_mut(trigger.event().building) {
|
||||
suncannon.pellet_count += 5;
|
||||
}
|
||||
},
|
||||
)
|
||||
.id(),
|
||||
];
|
||||
let preview = commands
|
||||
.spawn((
|
||||
BuildingPreview { info },
|
||||
Visibility::Hidden,
|
||||
Mesh3d(mesh.clone()),
|
||||
MeshMaterial3d(preview_material),
|
||||
))
|
||||
.id();
|
||||
commands.entity(info).insert((
|
||||
BuildingInfo {
|
||||
preview,
|
||||
size: SIZE,
|
||||
unlocked: true,
|
||||
},
|
||||
Range(20.0),
|
||||
Health::new(50.0),
|
||||
Defense::new(6.0),
|
||||
UpgradeTree { upgrades },
|
||||
Cost(Quantity {
|
||||
souls: 100,
|
||||
bones: 15,
|
||||
}),
|
||||
Name::new("Suncannon"),
|
||||
));
|
||||
commands.insert_resource(BuildingResources {
|
||||
mesh: mesh,
|
||||
material: material.clone(),
|
||||
projectile_material: projectile_material,
|
||||
projectile_mesh: meshes.add(Cuboid::from_size(PROJ_SIZE)),
|
||||
projectile_collider: Collider::cuboid(PROJ_SIZE.x, PROJ_SIZE.y, PROJ_SIZE.z),
|
||||
info_entity: info,
|
||||
});
|
||||
}
|
||||
|
||||
fn populate(
|
||||
trigger: Trigger<PopulatePlacedBuilding>,
|
||||
mut commands: Commands,
|
||||
res: Res<BuildingResources>,
|
||||
) {
|
||||
if trigger.event().info != res.info_entity {
|
||||
return;
|
||||
}
|
||||
commands
|
||||
.entity(trigger.target())
|
||||
.insert((
|
||||
Suncannon {
|
||||
explosion_radius: 10.0,
|
||||
explosion_damage: 20.0,
|
||||
explosion_knockback: 5.0,
|
||||
pellet_count: 0,
|
||||
pellet_damage: 10.,
|
||||
pellet_speed: 50.,
|
||||
},
|
||||
ContactDamage::new(2.0),
|
||||
ProjectileLauncher::new(
|
||||
Duration::from_secs(3),
|
||||
50.0,
|
||||
Vec3::Y * (SIZE.y as f32 + 0.5),
|
||||
Duration::from_secs(10),
|
||||
0.,
|
||||
),
|
||||
Mesh3d(res.mesh.clone()),
|
||||
MeshMaterial3d(res.material.clone()),
|
||||
))
|
||||
.observe(on_fire);
|
||||
}
|
||||
|
||||
fn on_fire(
|
||||
trigger: Trigger<Fire>,
|
||||
query: Query<&Suncannon>,
|
||||
mut commands: Commands,
|
||||
res: Res<BuildingResources>,
|
||||
) {
|
||||
let Ok(suncannon) = query.get(trigger.target()) else {
|
||||
return;
|
||||
};
|
||||
let v = get_launch_vector(
|
||||
trigger.event().spawn_pos,
|
||||
trigger.event().target_pos,
|
||||
100.0,
|
||||
0.0,
|
||||
);
|
||||
commands
|
||||
.spawn((
|
||||
RigidBody::Dynamic,
|
||||
trigger.event().team,
|
||||
Health::new(1.0),
|
||||
ContactDamage::projectile(trigger.event().damage, f32::INFINITY),
|
||||
Transform::from_translation(trigger.event().spawn_pos),
|
||||
Projectile::from(&trigger.event().launcher),
|
||||
Projectile::collision_layers(trigger.event().team),
|
||||
SuncannonProjectile(suncannon.clone()),
|
||||
LinearVelocity(v),
|
||||
res.projectile_collider.clone(),
|
||||
MeshMaterial3d(res.projectile_material.clone()),
|
||||
Mesh3d(res.projectile_mesh.clone()),
|
||||
))
|
||||
.observe(on_projectile_explode);
|
||||
}
|
||||
|
||||
fn on_projectile_explode(
|
||||
trigger: Trigger<Died>,
|
||||
proj_query: Query<(&GlobalTransform, &Team, &SuncannonProjectile)>,
|
||||
spatial_query: SpatialQuery,
|
||||
res: Res<BuildingResources>,
|
||||
mut enemy_query: Query<(&Team, &GlobalTransform, &mut ExternalImpulse)>,
|
||||
mut commands: Commands,
|
||||
) {
|
||||
let Ok((gtf, team, projectile)) = proj_query.get(trigger.target()) else {
|
||||
return;
|
||||
};
|
||||
let enemies = spatial_query.shape_intersections(
|
||||
&Collider::sphere(projectile.0.explosion_radius),
|
||||
gtf.translation(),
|
||||
Quat::default(),
|
||||
&SpatialQueryFilter::from_mask(team.enemy_layers()),
|
||||
);
|
||||
for enemy in enemies {
|
||||
let Ok((enemy_team, enemy_gtf, mut enemy_v)) = enemy_query.get_mut(enemy) else {
|
||||
continue;
|
||||
};
|
||||
if enemy_team == team {
|
||||
continue;
|
||||
}
|
||||
let knockback = (enemy_gtf.translation() - gtf.translation()).normalize_or_zero()
|
||||
* projectile.0.explosion_knockback;
|
||||
enemy_v.apply_impulse(knockback);
|
||||
commands.entity(enemy).trigger(Attacked {
|
||||
attacker: Some(trigger.target()),
|
||||
amount: projectile.0.explosion_damage,
|
||||
skip_invulnerability: true,
|
||||
});
|
||||
}
|
||||
for pellet in 0..projectile.0.pellet_count {
|
||||
let angle = 2. * PI / (projectile.0.pellet_count as f32) * pellet as f32;
|
||||
let v = Vec3::new(angle.sin(), 0., angle.cos()) * projectile.0.pellet_speed;
|
||||
commands.spawn((
|
||||
RigidBody::Dynamic,
|
||||
team.clone(),
|
||||
Health::new(1.0),
|
||||
ContactDamage::projectile(projectile.0.pellet_damage, f32::INFINITY),
|
||||
Transform::from_translation(gtf.translation() + Vec3::Y * 0.1)
|
||||
.with_scale(Vec3::splat(0.25)),
|
||||
Projectile {
|
||||
lifetime: Duration::from_secs(1),
|
||||
},
|
||||
Projectile::collision_layers(*team),
|
||||
LinearVelocity(v),
|
||||
res.projectile_collider.clone(),
|
||||
MeshMaterial3d(res.projectile_material.clone()),
|
||||
Mesh3d(res.projectile_mesh.clone()),
|
||||
));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue