From 4c5a650a1ac12bc3d2e6abe2d5b7e30cf20fe008 Mon Sep 17 00:00:00 2001 From: Jim Date: Tue, 2 Sep 2025 15:54:29 -0400 Subject: [PATCH] add suncannon --- Cargo.lock | 15 ++ Cargo.toml | 1 + assets/settings/input_map.ron | 16 +- crates/building_ramtower/src/lib.rs | 1 - crates/building_suncannon/Cargo.toml | 18 ++ crates/building_suncannon/src/lib.rs | 312 +++++++++++++++++++++++++++ src/main.rs | 1 + 7 files changed, 355 insertions(+), 9 deletions(-) create mode 100644 crates/building_suncannon/Cargo.toml create mode 100644 crates/building_suncannon/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index fc50f8a..4e16425 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -1702,6 +1702,20 @@ dependencies = [ "upgrade", ] +[[package]] +name = "building_suncannon" +version = "0.1.0" +dependencies = [ + "avian3d", + "bevy", + "building", + "combat", + "combat_damagers", + "resources", + "state", + "upgrade", +] + [[package]] name = "bumpalo" version = "3.19.0" @@ -2021,6 +2035,7 @@ dependencies = [ "building_impaler", "building_placement", "building_ramtower", + "building_suncannon", "camera", "combat", "combat_damagers", diff --git a/Cargo.toml b/Cargo.toml index 657ce5a..f3664d2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -62,4 +62,5 @@ inhabitant_simbert = { path = "crates/inhabitant_simbert" } inhabitant_endrake = { path = "crates/inhabitant_endrake" } resource_soul_spot = { path = "crates/resource_soul_spot" } building_ramtower = { path = "crates/building_ramtower" } +building_suncannon = { path = "crates/building_suncannon" } #new internal crates go here diff --git a/assets/settings/input_map.ron b/assets/settings/input_map.ron index 954bb8b..ed21f50 100755 --- a/assets/settings/input_map.ron +++ b/assets/settings/input_map.ron @@ -1,22 +1,22 @@ ( map: { - KeyCode(ShiftLeft): [ - MultiPlaceTower, - ], MouseButton(Middle): [ CameraOrbitEnable, ], - KeyCode(F1): [ - ToggleDebugUI, - ], MouseButton(Right): [ PlaceTower, ], + MouseButton(Left): [ + CameraPanEnable, + ], + KeyCode(ShiftLeft): [ + MultiPlaceTower, + ], MouseScroll: [ CameraZoom, ], - MouseButton(Left): [ - CameraPanEnable, + KeyCode(F1): [ + ToggleDebugUI, ], MouseMovementX: [ CameraYaw, diff --git a/crates/building_ramtower/src/lib.rs b/crates/building_ramtower/src/lib.rs index d081f9b..75767eb 100644 --- a/crates/building_ramtower/src/lib.rs +++ b/crates/building_ramtower/src/lib.rs @@ -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, diff --git a/crates/building_suncannon/Cargo.toml b/crates/building_suncannon/Cargo.toml new file mode 100644 index 0000000..c828985 --- /dev/null +++ b/crates/building_suncannon/Cargo.toml @@ -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 diff --git a/crates/building_suncannon/src/lib.rs b/crates/building_suncannon/src/lib.rs new file mode 100644 index 0000000..690732e --- /dev/null +++ b/crates/building_suncannon/src/lib.rs @@ -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, + material: Handle, + projectile_mesh: Handle, + projectile_material: Handle, + 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>, + mut materials: ResMut>, +) { + 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, + 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, + 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, 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, + mut commands: Commands, + res: Res, +) { + 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, + query: Query<&Suncannon>, + mut commands: Commands, + res: Res, +) { + 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, + proj_query: Query<(&GlobalTransform, &Team, &SuncannonProjectile)>, + spatial_query: SpatialQuery, + res: Res, + 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()), + )); + } +} diff --git a/src/main.rs b/src/main.rs index 5ca1172..25a6baf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -45,6 +45,7 @@ fn main() { resource_soul_spot::ResourceSoulSpotPlugin, ), building_ramtower::BuildingRamtowerPlugin, + building_suncannon::BuildingSuncannonPlugin, // new internal crates go here ));