diff --git a/Cargo.lock b/Cargo.lock index ac6d7a5..8a75a2e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1801,6 +1801,7 @@ dependencies = [ name = "combat" version = "0.1.0" dependencies = [ + "avian3d", "bevy", "state", ] @@ -1963,6 +1964,8 @@ dependencies = [ "camera", "combat", "combat_damagers", + "inhabitant", + "inhabitant_coranth", "input", "level", "physics", @@ -2846,6 +2849,27 @@ version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a257582fdcde896fd96463bf2d40eefea0580021c0712a0e2b028b60b47a837a" +[[package]] +name = "inhabitant" +version = "0.1.0" +dependencies = [ + "bevy", + "combat", +] + +[[package]] +name = "inhabitant_coranth" +version = "0.1.0" +dependencies = [ + "avian3d", + "bevy", + "building", + "combat", + "combat_damagers", + "inhabitant", + "state", +] + [[package]] name = "inotify" version = "0.11.0" @@ -3000,6 +3024,7 @@ dependencies = [ "building_impaler", "camera", "combat", + "inhabitant_coranth", "state", ] diff --git a/Cargo.toml b/Cargo.toml index 0d4f2fb..79d35c9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,4 +47,6 @@ building_core = { path = "crates/building_core" } building = { path = "crates/building" } building_impaler = { path = "crates/building_impaler" } combat_damagers = { path = "crates/combat_damagers" } +inhabitant = { path = "crates/inhabitant" } +inhabitant_coranth = { path = "crates/inhabitant_coranth" } #new internal crates go here diff --git a/assets/settings/input_map.ron b/assets/settings/input_map.ron index 1c55182..c39cc77 100644 --- a/assets/settings/input_map.ron +++ b/assets/settings/input_map.ron @@ -1,7 +1,7 @@ ( map: { - MouseButton(Left): [ - CameraPanEnable, + MouseMovementY: [ + CameraPanY, ], MouseScroll: [ CameraZoom, @@ -10,14 +10,14 @@ CameraYaw, CameraPanX, ], - MouseMovementY: [ - CameraPanY, + KeyCode(Escape): [ + TogglePause, ], MouseButton(Middle): [ CameraOrbitEnable, ], - KeyCode(Escape): [ - TogglePause, + MouseButton(Left): [ + CameraPanEnable, ], KeyCode(F1): [ ToggleDebugUI, diff --git a/crates/building/src/lib.rs b/crates/building/src/lib.rs index 4af2a94..f4ee401 100644 --- a/crates/building/src/lib.rs +++ b/crates/building/src/lib.rs @@ -3,7 +3,7 @@ #![allow(clippy::too_many_arguments)] use bevy::prelude::*; -use combat::Health; +use combat::{Health, Team}; pub struct BuildingPlugin; @@ -12,7 +12,7 @@ impl Plugin for BuildingPlugin { } #[derive(Component, Default)] -#[require(Health)] +#[require(Health, Team::Core)] pub struct Building { pub level: u32, } diff --git a/crates/combat/Cargo.toml b/crates/combat/Cargo.toml index 4fd9151..e351c43 100644 --- a/crates/combat/Cargo.toml +++ b/crates/combat/Cargo.toml @@ -5,6 +5,7 @@ edition = "2024" [dependencies] bevy = { workspace = true } +avian3d = { workspace = true } state = { path = "../state" } diff --git a/crates/combat/src/lib.rs b/crates/combat/src/lib.rs index 2157e22..56c6909 100644 --- a/crates/combat/src/lib.rs +++ b/crates/combat/src/lib.rs @@ -4,6 +4,7 @@ use std::time::Duration; +use avian3d::prelude::*; use bevy::prelude::*; use state::GameSystemSet; @@ -16,14 +17,25 @@ impl Plugin for CombatPlugin { .add_systems( FixedUpdate, tick_invulnerability.in_set(GameSystemSet::StatusEffects), - ); + ) + .register_type::(); } } -#[derive(Component, Clone, Debug, PartialEq, Eq)] +#[derive(PhysicsLayer, Default, Component, Clone, Debug, PartialEq, Eq, Reflect)] pub enum Team { + #[default] Core, - Immune, + Inhabitant, +} + +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), + } + } } #[derive(Event, Debug, Default)] @@ -33,6 +45,13 @@ pub struct Attacked { pub skip_invulnerability: bool, } +#[derive(Event, Debug)] +pub struct GaveDamage { + pub target: Entity, + pub amount: f32, + pub skip_invulnerability: bool, +} + #[derive(Event, Debug)] pub struct Died { pub slayer: Option, @@ -133,12 +152,12 @@ fn on_attack( && let Some(team) = opt_team && team == attacker_team { - info!("avoiding friendly fire!"); return; } let defense = opt_defense.cloned().unwrap_or_default(); let damage_taken = defense.calc_damage(trigger.event().amount); - health.current = (health.current - damage_taken).max(0.0); + let old_health = health.current; + health.current = (old_health - damage_taken).max(0.0); info!( "{:?} attacked {:?} for {} damage. New health = {}", trigger @@ -150,6 +169,16 @@ fn on_attack( damage_taken, health.current ); + if let Some(attacker) = trigger.event().attacker { + commands.trigger_targets( + GaveDamage { + target: trigger.target(), + amount: old_health - health.current, + skip_invulnerability: trigger.event().skip_invulnerability, + }, + attacker, + ); + } if let Ok(mut ec) = commands.get_entity(trigger.target()) { if health.current <= 0. { ec.trigger(Died { diff --git a/crates/combat_damagers/src/lib.rs b/crates/combat_damagers/src/lib.rs index b833bb2..9093c86 100644 --- a/crates/combat_damagers/src/lib.rs +++ b/crates/combat_damagers/src/lib.rs @@ -30,7 +30,6 @@ fn deal_contact_damage( mut commands: Commands, ) { for event in reader.read() { - info!("event recv!"); if let Ok(contact) = query.get(event.0) { commands.trigger_targets( Attacked { diff --git a/crates/inhabitant/Cargo.toml b/crates/inhabitant/Cargo.toml new file mode 100644 index 0000000..0cb466f --- /dev/null +++ b/crates/inhabitant/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "inhabitant" +version = "0.1.0" +edition = "2024" + +[dependencies] +bevy = { workspace = true } +combat = { path = "../combat" } + +[lints] +workspace = true diff --git a/crates/inhabitant/src/lib.rs b/crates/inhabitant/src/lib.rs new file mode 100644 index 0000000..eda86cc --- /dev/null +++ b/crates/inhabitant/src/lib.rs @@ -0,0 +1,16 @@ +//bevy system signatures often violate these rules +#![allow(clippy::type_complexity)] +#![allow(clippy::too_many_arguments)] + +use bevy::prelude::*; +use combat::{Health, Team}; + +pub struct InhabitantPlugin; + +impl Plugin for InhabitantPlugin { + fn build(&self, _app: &mut App) {} +} + +#[derive(Component, Default)] +#[require(Health, Team::Inhabitant)] +pub struct Inhabitant; diff --git a/crates/inhabitant_coranth/Cargo.toml b/crates/inhabitant_coranth/Cargo.toml new file mode 100644 index 0000000..0c3a72d --- /dev/null +++ b/crates/inhabitant_coranth/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "inhabitant_coranth" +version = "0.1.0" +edition = "2024" + +[dependencies] +bevy = { workspace = true } +avian3d = { workspace = true } + +state = { path = "../state" } +combat = { path = "../combat" } +building = { path = "../building" } +inhabitant = { path = "../inhabitant" } +combat_damagers = { path = "../combat_damagers" } + +[lints] +workspace = true diff --git a/crates/inhabitant_coranth/src/lib.rs b/crates/inhabitant_coranth/src/lib.rs new file mode 100644 index 0000000..a0e20e8 --- /dev/null +++ b/crates/inhabitant_coranth/src/lib.rs @@ -0,0 +1,107 @@ +//bevy system signatures often violate these rules +#![allow(clippy::type_complexity)] +#![allow(clippy::too_many_arguments)] + +use avian3d::prelude::*; +use bevy::prelude::*; +use building::Building; +use combat::{GaveDamage, Health, Team}; +use combat_damagers::ContactDamage; +use inhabitant::Inhabitant; +use state::GameSystemSet; + +pub struct InhabitantCoranthPlugin; + +impl Plugin for InhabitantCoranthPlugin { + fn build(&self, app: &mut App) { + app.add_event::() + .add_systems(FixedUpdate, tick.in_set(GameSystemSet::Execute)) + .add_systems(FixedUpdate, spawn.in_set(GameSystemSet::PostTick)); + } +} + +#[derive(Component)] +#[require(Inhabitant)] +pub struct Coranth; + +#[derive(Event)] +pub struct SpawnCoranth { + pub transform: Transform, +} + +fn spawn( + mut reader: EventReader, + mut commands: Commands, + mut meshes: ResMut>, + mut materials: ResMut>, +) { + for SpawnCoranth { transform } in reader.read() { + commands + .spawn(( + RigidBody::Dynamic, + Collider::cuboid(0.5, 0.5, 0.5), + Mesh3d(meshes.add(Cuboid::from_length(0.5))), + MeshMaterial3d(materials.add(StandardMaterial { + base_color: Color::srgb_u8(220, 156, 86), + metallic: 1.0, + perceptual_roughness: 0.3, + ..default() + })), + MaxLinearSpeed(25.), + Coranth, + transform.clone(), + Health::new(5.0), + ContactDamage { amount: 5.0 }, + Name::from("Coranth"), + )) + .observe(explode); + } +} + +fn explode( + trigger: Trigger, + spatial_query: SpatialQuery, + trigger_query: Query<&GlobalTransform>, + mut other_query: Query<(&GlobalTransform, &mut LinearVelocity)>, +) { + info!("blasthing!"); + let Ok(gtf) = trigger_query.get(trigger.target()) else { + return; + }; + let origin = gtf.translation(); + for e in spatial_query.shape_intersections( + &Collider::sphere(5.0), + origin, + Quat::IDENTITY, + // &SpatialQueryFilter::from_mask(Team::Inhabitant), + &SpatialQueryFilter::default(), + ) { + info!("found hit"); + if let Ok((gtf, mut v)) = other_query.get_mut(e) { + let delta = gtf.translation() - origin; + v.0 += delta.normalize_or_zero() * 10.0; + } + } +} + +fn tick( + mut coranths: Query<(&GlobalTransform, &mut LinearVelocity), With>, + buildings: Query<&GlobalTransform, With>, +) { + for (coranth_tf, mut coranth_v) in coranths.iter_mut() { + let coranth_pos = coranth_tf.translation(); + if let Some(closest_building) = + buildings + .iter() + .map(|gtf| gtf.translation()) + .min_by(|a, b| { + coranth_pos + .distance_squared(*a) + .total_cmp(&coranth_pos.distance_squared(*b)) + }) + { + let delta = closest_building - coranth_pos; + coranth_v.0 += delta.normalize_or_zero() * 0.1; + } + } +} diff --git a/crates/level/Cargo.toml b/crates/level/Cargo.toml index 0496b0c..44fdbc9 100644 --- a/crates/level/Cargo.toml +++ b/crates/level/Cargo.toml @@ -12,6 +12,7 @@ camera = { path = "../camera" } combat = { path = "../combat" } building_core = { path = "../building_core" } building_impaler = { path = "../building_impaler" } +inhabitant_coranth = { path = "../inhabitant_coranth" } [lints] workspace = true diff --git a/crates/level/src/lib.rs b/crates/level/src/lib.rs index 6dba5a0..8ddf9e8 100644 --- a/crates/level/src/lib.rs +++ b/crates/level/src/lib.rs @@ -10,9 +10,9 @@ use bevy::{ render::camera::Exposure, }; use building_core::SpawnCore; -use building_impaler::{Impaler, SpawnImpaler}; +use building_impaler::SpawnImpaler; use camera::ControlledCamera; -use combat::{Defense, Health}; +use inhabitant_coranth::SpawnCoranth; use state::AppState; pub struct LevelPlugin; @@ -29,6 +29,7 @@ fn setup( mut materials: ResMut>, mut core_writer: EventWriter, mut impaler_writer: EventWriter, + mut coranth_writer: EventWriter, ) { let level_size = 50.0; // Static physics object with a collision shape @@ -44,35 +45,10 @@ fn setup( })), )); - // Dynamic physics object with a collision shape and initial angular velocity for y in 5..15 { - commands - .spawn(( - RigidBody::Dynamic, - Collider::cuboid(0.5, 0.5, 0.5), - AngularVelocity(Vec3::new(2.5, 3.5, 1.5)), - Mesh3d(meshes.add(Cuboid::from_length(0.5))), - MeshMaterial3d(materials.add(StandardMaterial { - base_color: Color::srgb_u8(220, 156, 86), - metallic: 1.0, - perceptual_roughness: 0.3, - ..default() - })), - Transform::from_xyz(0.0, y as f32 * 1.1, 0.0), - Health::new(5.0), - Defense::new(y as f32 - 5.0), - )) - .observe( - |trigger: Trigger>, - target: Single<&GlobalTransform, With>, - mut query: Query<(&GlobalTransform, &mut LinearVelocity)>| { - if let Ok((gtf, mut v)) = query.get_mut(trigger.target()) { - let delta = target.translation() - gtf.translation(); - v.y += 5.0; - v.0 += delta; - } - }, - ); + coranth_writer.write(SpawnCoranth { + transform: Transform::from_xyz(y as f32 * 0.25, y as f32 * 1.1, 0.0), + }); } core_writer.write(SpawnCore { diff --git a/crates/physics/src/lib.rs b/crates/physics/src/lib.rs index 6f0020d..132466a 100644 --- a/crates/physics/src/lib.rs +++ b/crates/physics/src/lib.rs @@ -9,6 +9,9 @@ pub struct PhysicsPlugin; impl Plugin for PhysicsPlugin { fn build(&self, app: &mut App) { - app.add_plugins((PhysicsPlugins::default(), PhysicsPickingPlugin)); + app.add_plugins(( + PhysicsPlugins::default().set(PhysicsInterpolationPlugin::interpolate_all()), + PhysicsPickingPlugin, + )); } } diff --git a/src/main.rs b/src/main.rs index c1c6a1a..70993a8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,19 +13,23 @@ fn main() { state::StatePlugin, physics::PhysicsPlugin, // ui must come before the sub crates - ui::UiPlugin, - ui_state::UiStatePlugin, - ui_debug::UiDebugPlugin, - input::InputPlugin, - setup::SetupPlugin, - ui_pause::UiPausePlugin, - level::LevelPlugin, - camera::CameraPlugin, - combat::CombatPlugin, - building_core::BuildingCorePlugin, - building::BuildingPlugin, - building_impaler::BuildingImpalerPlugin, - combat_damagers::CombatDamagersPlugin, + ( + ui::UiPlugin, + ui_state::UiStatePlugin, + ui_debug::UiDebugPlugin, + input::InputPlugin, + setup::SetupPlugin, + ui_pause::UiPausePlugin, + level::LevelPlugin, + camera::CameraPlugin, + combat::CombatPlugin, + building_core::BuildingCorePlugin, + building::BuildingPlugin, + building_impaler::BuildingImpalerPlugin, + combat_damagers::CombatDamagersPlugin, + ), + inhabitant::InhabitantPlugin, + inhabitant_coranth::InhabitantCoranthPlugin, // new internal crates go here ));