diff --git a/Cargo.lock b/Cargo.lock index 2970fc8..1b6432f 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -1632,10 +1632,22 @@ dependencies = [ ] [[package]] -name = "building_core" +name = "building_barrager" version = "0.1.0" dependencies = [ "avian3d", + "bevy", + "building", + "combat", + "combat_damagers", + "resources", + "state", +] + +[[package]] +name = "building_core" +version = "0.1.0" +dependencies = [ "bevy", "building", "combat", @@ -1646,7 +1658,6 @@ dependencies = [ name = "building_impaler" version = "0.1.0" dependencies = [ - "avian3d", "bevy", "building", "combat", @@ -1979,6 +1990,7 @@ dependencies = [ "bevy", "bevy-inspector-egui", "building", + "building_barrager", "building_core", "building_impaler", "building_placement", @@ -3003,6 +3015,7 @@ checksum = "a257582fdcde896fd96463bf2d40eefea0580021c0712a0e2b028b60b47a837a" name = "inhabitant" version = "0.1.0" dependencies = [ + "avian3d", "bevy", "combat", "resources", diff --git a/Cargo.toml b/Cargo.toml index 2fdc1d5..9bdfa13 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -53,4 +53,5 @@ inhabitant_coranth = { path = "crates/inhabitant_coranth" } building_placement = { path = "crates/building_placement" } ui_ingame = { path = "crates/ui_ingame" } resources = { path = "crates/resources" } +building_barrager = { path = "crates/building_barrager" } #new internal crates go here diff --git a/crates/building/src/lib.rs b/crates/building/src/lib.rs index 66bdb3c..84287e6 100755 --- a/crates/building/src/lib.rs +++ b/crates/building/src/lib.rs @@ -197,14 +197,14 @@ fn place_building( mut building_map: ResMut, mut commands: Commands, mut resources: ResMut, - info_query: Query<(&BuildingInfo, &Cost)>, + info_query: Query<(&BuildingInfo, &Cost, &Name)>, ) { for PlaceBuilding { info: info_entity, location, } in reader.read() { - let Ok((info, cost)) = info_query.get(*info_entity) else { + let Ok((info, cost, name)) = info_query.get(*info_entity) else { error!("tried to place building without info!"); continue; }; @@ -228,6 +228,9 @@ fn place_building( BuildingPosition(*location), Collider::cuboid(size_v3.x, size_v3.y, size_v3.z), Mesh3d::default(), + name.clone(), + RigidBody::Static, + Team::Core.collision_layers(), )) .id(); building_map.place_building(*location, info, *info_entity, instance); diff --git a/crates/building_barrager/Cargo.toml b/crates/building_barrager/Cargo.toml new file mode 100644 index 0000000..db8b97a --- /dev/null +++ b/crates/building_barrager/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "building_barrager" +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" } + +[lints] +workspace = true diff --git a/crates/building_barrager/src/lib.rs b/crates/building_barrager/src/lib.rs new file mode 100644 index 0000000..1d1d125 --- /dev/null +++ b/crates/building_barrager/src/lib.rs @@ -0,0 +1,133 @@ +//bevy system signatures often violate these rules +#![allow(clippy::type_complexity)] +#![allow(clippy::too_many_arguments)] + +use std::{f32, time::Duration}; + +use avian3d::prelude::{Collider, LinearVelocity, RigidBody}; +use bevy::prelude::*; +use building::{BuildingInfo, BuildingPreview, PREVIEW_ALPHA, PopulatePlacedBuilding}; +use combat::{ + Defense, Health, + projectiles::{Fire, Projectile, ProjectileLauncher, get_launch_vector}, +}; +use combat_damagers::ContactDamage; +use resources::Cost; + +pub struct BuildingBarragerPlugin; + +impl Plugin for BuildingBarragerPlugin { + fn build(&self, app: &mut App) { + app.add_systems(Startup, register).add_observer(populate); + } +} + +#[derive(Component)] +pub struct Barrager; + +#[derive(Resource)] +struct BuildingResources { + mesh: Handle, + material: Handle, + projectile_mesh: Handle, + projectile_material: Handle, + projectile_collider: Collider, + info_entity: Entity, +} + +const SIZE: UVec3 = UVec3::splat(2); +const PROJ_SIZE: Vec3 = Vec3::splat(0.25); + +fn register( + mut commands: Commands, + mut meshes: ResMut>, + mut materials: ResMut>, +) { + let mesh = meshes.add(Cuboid::from_length(2.0)); + let base_material = StandardMaterial { + base_color: Color::srgb_u8(0x3d, 0x95, 0xa5), + metallic: 0.2, + perceptual_roughness: 0.6, + ..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 info = commands.spawn_empty().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, + }, + Cost { souls: 0, bones: 0 }, + Name::new("Barrager"), + )); + commands.insert_resource(BuildingResources { + mesh: mesh, + material: material.clone(), + projectile_material: 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(( + Barrager, + Health::new(20.0), + Defense::new(7.0), + ContactDamage::new(2.0), + ProjectileLauncher::new( + Duration::from_secs(1), + 50.0, + 5.0, + Vec3::Y * (SIZE.y as f32 + 0.5), + Duration::from_secs(10), + ), + Mesh3d(res.mesh.clone()), + MeshMaterial3d(res.material.clone()), + )) + .observe(on_fire); +} + +fn on_fire(trigger: Trigger, mut commands: Commands, res: Res) { + 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()), + )); +} diff --git a/crates/building_core/Cargo.toml b/crates/building_core/Cargo.toml index b9e7632..ff3173d 100755 --- a/crates/building_core/Cargo.toml +++ b/crates/building_core/Cargo.toml @@ -5,7 +5,6 @@ edition = "2024" [dependencies] bevy = { workspace = true } -avian3d = { workspace = true } combat = { path = "../combat" } building = { path = "../building" } diff --git a/crates/building_core/src/lib.rs b/crates/building_core/src/lib.rs index eddf542..3d114c1 100755 --- a/crates/building_core/src/lib.rs +++ b/crates/building_core/src/lib.rs @@ -2,7 +2,6 @@ #![allow(clippy::type_complexity)] #![allow(clippy::too_many_arguments)] -use avian3d::prelude::*; use bevy::prelude::*; use building::{BuildingInfo, BuildingPreview, PREVIEW_ALPHA, PopulatePlacedBuilding}; use combat::{Defense, Health}; @@ -79,11 +78,9 @@ fn populate( return; } commands.entity(trigger.target()).insert(( - RigidBody::Static, Core, Health::new(100.0), Defense::new(5.0), - Name::from("Core"), Mesh3d(res.mesh.clone()), MeshMaterial3d(res.material.clone()), )); diff --git a/crates/building_impaler/Cargo.toml b/crates/building_impaler/Cargo.toml index 32d83b7..41c8a71 100755 --- a/crates/building_impaler/Cargo.toml +++ b/crates/building_impaler/Cargo.toml @@ -5,7 +5,6 @@ edition = "2024" [dependencies] bevy = { workspace = true } -avian3d = { workspace = true } state = { path = "../state" } combat = { path = "../combat" } diff --git a/crates/building_impaler/src/lib.rs b/crates/building_impaler/src/lib.rs index 7b11de3..8673b68 100755 --- a/crates/building_impaler/src/lib.rs +++ b/crates/building_impaler/src/lib.rs @@ -2,7 +2,6 @@ #![allow(clippy::type_complexity)] #![allow(clippy::too_many_arguments)] -use avian3d::prelude::*; use bevy::prelude::*; use building::{BuildingInfo, BuildingPreview, PREVIEW_ALPHA, PopulatePlacedBuilding}; use combat::{Defense, Health}; @@ -82,12 +81,10 @@ fn populate( return; } commands.entity(trigger.target()).insert(( - RigidBody::Static, Impaler, Health::new(20.0), Defense::new(5.0), - ContactDamage { amount: 5.0 }, - Name::from("Impaler"), + ContactDamage::new(5.0), Mesh3d(res.mesh.clone()), MeshMaterial3d(res.material.clone()), )); diff --git a/crates/combat/src/lib.rs b/crates/combat/src/lib.rs index b3b9099..16d0ee2 100755 --- a/crates/combat/src/lib.rs +++ b/crates/combat/src/lib.rs @@ -2,6 +2,8 @@ #![allow(clippy::type_complexity)] #![allow(clippy::too_many_arguments)] +pub mod projectiles; + use std::time::Duration; use avian3d::prelude::*; @@ -13,7 +15,8 @@ pub struct CombatPlugin; impl Plugin for CombatPlugin { fn build(&self, app: &mut App) { - app.add_observer(on_attack) + app.add_plugins(projectiles::ProjectilePlugin) + .add_observer(on_attack) .add_observer(on_die) .add_systems( FixedUpdate, @@ -23,7 +26,7 @@ impl Plugin for CombatPlugin { } } -#[derive(PhysicsLayer, Default, Component, Clone, Debug, PartialEq, Eq, Reflect)] +#[derive(PhysicsLayer, Default, Component, Clone, Copy, Debug, PartialEq, Eq, Reflect)] pub enum Team { #[default] Core, @@ -37,6 +40,12 @@ impl Team { Team::Inhabitant => CollisionLayers::new(Team::Inhabitant, LayerMask::ALL), } } + pub fn enemy_layers(&self) -> LayerMask { + match self { + Team::Core => Team::Inhabitant.into(), + Team::Inhabitant => Team::Core.into(), + } + } } #[derive(Event, Debug, Default)] diff --git a/crates/combat/src/projectiles.rs b/crates/combat/src/projectiles.rs new file mode 100644 index 0000000..b47fb55 --- /dev/null +++ b/crates/combat/src/projectiles.rs @@ -0,0 +1,148 @@ +//bevy system signatures often violate these rules +#![allow(clippy::type_complexity)] +#![allow(clippy::too_many_arguments)] + +use std::time::Duration; + +use avian3d::prelude::*; +use bevy::prelude::*; +use state::GameSystemSet; + +use crate::Team; + +pub(crate) struct ProjectilePlugin; + +impl Plugin for ProjectilePlugin { + fn build(&self, app: &mut App) { + app.add_systems(FixedUpdate, fire_launcher.in_set(GameSystemSet::Execute)); + } +} + +#[derive(Component)] +#[require(Team)] +pub struct Projectile { + pub lifetime: Duration, +} + +impl From<&ProjectileLauncher> for Projectile { + fn from(value: &ProjectileLauncher) -> Self { + Self { + lifetime: value.lifetime, + } + } +} + +#[derive(Component, Clone)] +#[require(Team)] +pub struct ProjectileLauncher { + pub fire_timer: Timer, + pub fire_offset: Vec3, + pub lifetime: Duration, + pub range: f32, + pub damage: f32, +} + +impl ProjectileLauncher { + pub fn new( + fire_interval: Duration, + range: f32, + damage: f32, + fire_offset: Vec3, + lifetime: Duration, + ) -> Self { + Self { + fire_timer: Timer::new(fire_interval, TimerMode::Once), + range, + damage, + fire_offset, + lifetime, + } + } +} + +#[derive(Event)] +pub struct Fire { + pub target: Entity, + pub target_pos: Vec3, + pub spawn_pos: Vec3, + pub launcher: ProjectileLauncher, + pub team: Team, +} + +fn fire_launcher( + mut commands: Commands, + mut launcher_query: Query<(Entity, &mut ProjectileLauncher, &Team, &GlobalTransform)>, + spatial_query: SpatialQuery, + enemy_query: Query<(&Team, &GlobalTransform)>, + time: Res