From 6659fe8e5a069864a971f344755fd12f0d280bf9 Mon Sep 17 00:00:00 2001 From: Jim Date: Mon, 25 Aug 2025 08:31:33 -0400 Subject: [PATCH] add impaler scaffolding and teams --- Cargo.lock | 13 ++++++++ Cargo.toml | 1 + assets/settings/input_map.ron | 14 ++++---- crates/building/src/lib.rs | 4 ++- crates/building_core/src/lib.rs | 6 ++-- crates/building_impaler/Cargo.toml | 15 +++++++++ crates/building_impaler/src/lib.rs | 53 ++++++++++++++++++++++++++++++ crates/combat/src/lib.rs | 18 ++++++++-- crates/level/Cargo.toml | 1 + crates/level/src/lib.rs | 9 +++-- crates/state/src/lib.rs | 37 ++++++++++++++++++--- src/main.rs | 1 + 12 files changed, 151 insertions(+), 21 deletions(-) create mode 100644 crates/building_impaler/Cargo.toml create mode 100644 crates/building_impaler/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index b70cf6d..9ca0c53 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1636,6 +1636,17 @@ dependencies = [ "state", ] +[[package]] +name = "building_impaler" +version = "0.1.0" +dependencies = [ + "avian3d", + "bevy", + "building", + "combat", + "state", +] + [[package]] name = "bumpalo" version = "3.19.0" @@ -1936,6 +1947,7 @@ dependencies = [ "bevy-inspector-egui", "building", "building_core", + "building_impaler", "camera", "combat", "input", @@ -2972,6 +2984,7 @@ dependencies = [ "avian3d", "bevy", "building_core", + "building_impaler", "camera", "combat", "state", diff --git a/Cargo.toml b/Cargo.toml index 9270aa1..9d86378 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,4 +45,5 @@ camera = { path = "crates/camera" } combat = { path = "crates/combat" } building_core = { path = "crates/building_core" } building = { path = "crates/building" } +building_impaler = { path = "crates/building_impaler" } #new internal crates go here diff --git a/assets/settings/input_map.ron b/assets/settings/input_map.ron index e6b702a..ceea066 100644 --- a/assets/settings/input_map.ron +++ b/assets/settings/input_map.ron @@ -1,7 +1,7 @@ ( map: { - KeyCode(F1): [ - ToggleDebugUI, + MouseButton(Left): [ + CameraPanEnable, ], MouseScroll: [ CameraZoom, @@ -10,17 +10,17 @@ CameraYaw, CameraPanX, ], + MouseMovementY: [ + CameraPanY, + ], KeyCode(Escape): [ TogglePause, ], MouseButton(Middle): [ CameraOrbitEnable, ], - MouseButton(Left): [ - CameraPanEnable, - ], - MouseMovementY: [ - CameraPanY, + KeyCode(F1): [ + ToggleDebugUI, ], }, ) \ No newline at end of file diff --git a/crates/building/src/lib.rs b/crates/building/src/lib.rs index 7e28e42..4af2a94 100644 --- a/crates/building/src/lib.rs +++ b/crates/building/src/lib.rs @@ -13,4 +13,6 @@ impl Plugin for BuildingPlugin { #[derive(Component, Default)] #[require(Health)] -pub struct Building; +pub struct Building { + pub level: u32, +} diff --git a/crates/building_core/src/lib.rs b/crates/building_core/src/lib.rs index 505b767..7de02d5 100644 --- a/crates/building_core/src/lib.rs +++ b/crates/building_core/src/lib.rs @@ -19,9 +19,7 @@ impl Plugin for BuildingCorePlugin { #[derive(Component)] #[require(Building)] -pub struct Core { - pub level: u32, -} +pub struct Core; #[derive(Event)] pub struct SpawnCore { @@ -45,7 +43,7 @@ fn spawn_core( perceptual_roughness: 0.8, ..default() })), - Core { level: 0 }, + Core, transform.clone(), Health::new(100.0), Defense::new(10.0), diff --git a/crates/building_impaler/Cargo.toml b/crates/building_impaler/Cargo.toml new file mode 100644 index 0000000..83e6781 --- /dev/null +++ b/crates/building_impaler/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "building_impaler" +version = "0.1.0" +edition = "2024" + +[dependencies] +bevy = { workspace = true } +avian3d = { workspace = true } + +state = { path = "../state" } +combat = { path = "../combat" } +building = { path = "../building" } + +[lints] +workspace = true diff --git a/crates/building_impaler/src/lib.rs b/crates/building_impaler/src/lib.rs new file mode 100644 index 0000000..a6afbe8 --- /dev/null +++ b/crates/building_impaler/src/lib.rs @@ -0,0 +1,53 @@ +//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::{Defense, Health}; +use state::GameSystemSet; + +pub struct BuildingImpalerPlugin; + +impl Plugin for BuildingImpalerPlugin { + fn build(&self, app: &mut App) { + app.add_event::() + .add_systems(FixedUpdate, spawn.in_set(GameSystemSet::PostTick)); + } +} + +#[derive(Component)] +#[require(Building)] +pub struct Impaler; + +#[derive(Event)] +pub struct SpawnImpaler { + pub transform: Transform, +} + +fn spawn( + mut reader: EventReader, + mut commands: Commands, + mut meshes: ResMut>, + mut materials: ResMut>, +) { + for SpawnImpaler { transform } in reader.read() { + commands.spawn(( + RigidBody::Static, + Collider::cuboid(1.0, 1.0, 1.0), + Mesh3d(meshes.add(Cuboid::from_length(1.0))), + MeshMaterial3d(materials.add(StandardMaterial { + base_color: Color::srgb_u8(0xd6, 0x50, 0x61), + metallic: 0.2, + perceptual_roughness: 0.6, + ..default() + })), + Impaler, + transform.clone(), + Health::new(20.0), + Defense::new(5.0), + Name::from("Impaler"), + )); + } +} diff --git a/crates/combat/src/lib.rs b/crates/combat/src/lib.rs index b337ebf..2fca551 100644 --- a/crates/combat/src/lib.rs +++ b/crates/combat/src/lib.rs @@ -12,6 +12,12 @@ impl Plugin for CombatPlugin { } } +#[derive(Component, Clone, Debug, PartialEq, Eq)] +pub enum Team { + Core, + Immune, +} + #[derive(Event, Debug)] pub struct Attacked { pub attacker: Option, @@ -70,13 +76,21 @@ impl Defense { fn on_attack( trigger: Trigger, mut commands: Commands, - mut query: Query<(&mut Health, Option<&Defense>)>, + attacker_query: Query<&Team>, + mut attacked_query: Query<(&mut Health, Option<&Defense>, Option<&Team>)>, names: Query<&Name>, ) { - let Ok((mut health, opt_defense)) = query.get_mut(trigger.target()) else { + let Ok((mut health, opt_defense, opt_team)) = attacked_query.get_mut(trigger.target()) else { warn!("tried to attack entity without health"); return; }; + if let Some(Ok(attacker_team)) = trigger.event().attacker.map(|a| attacker_query.get(a)) + && 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); diff --git a/crates/level/Cargo.toml b/crates/level/Cargo.toml index 8100f0d..0496b0c 100644 --- a/crates/level/Cargo.toml +++ b/crates/level/Cargo.toml @@ -11,6 +11,7 @@ state = { path = "../state" } camera = { path = "../camera" } combat = { path = "../combat" } building_core = { path = "../building_core" } +building_impaler = { path = "../building_impaler" } [lints] workspace = true diff --git a/crates/level/src/lib.rs b/crates/level/src/lib.rs index e5ccdbc..afbe876 100644 --- a/crates/level/src/lib.rs +++ b/crates/level/src/lib.rs @@ -10,6 +10,7 @@ use bevy::{ render::camera::Exposure, }; use building_core::SpawnCore; +use building_impaler::SpawnImpaler; use camera::ControlledCamera; use combat::{Attacked, Defense, Health}; use state::AppState; @@ -27,6 +28,7 @@ fn setup( mut meshes: ResMut>, mut materials: ResMut>, mut core_writer: EventWriter, + mut impaler_writer: EventWriter, ) { let level_size = 50.0; // Static physics object with a collision shape @@ -47,9 +49,9 @@ fn setup( commands .spawn(( RigidBody::Dynamic, - Collider::cuboid(1.0, 1.0, 1.0), + Collider::cuboid(0.5, 0.5, 0.5), AngularVelocity(Vec3::new(2.5, 3.5, 1.5)), - Mesh3d(meshes.add(Cuboid::from_length(1.0))), + Mesh3d(meshes.add(Cuboid::from_length(0.5))), MeshMaterial3d(materials.add(StandardMaterial { base_color: Color::srgb_u8(220, 156, 86), metallic: 1.0, @@ -78,6 +80,9 @@ fn setup( core_writer.write(SpawnCore { transform: Transform::from_xyz(5.0, 1.0, 5.0), }); + impaler_writer.write(SpawnImpaler { + transform: Transform::from_xyz(-5.0, 0.5, 5.0), + }); // Light commands.spawn(( diff --git a/crates/state/src/lib.rs b/crates/state/src/lib.rs index f5f5b6b..6aa852c 100644 --- a/crates/state/src/lib.rs +++ b/crates/state/src/lib.rs @@ -13,6 +13,7 @@ impl Plugin for StatePlugin { .enable_state_scoped_entities::() .add_sub_state::() .enable_state_scoped_entities::() + .add_sub_state::() .configure_sets( FixedUpdate, GameSystemSet::LoadingTick.run_if(in_state(GameState::Loading)), @@ -22,8 +23,20 @@ impl Plugin for StatePlugin { ( // these all run before physics, which is in FixedPostUpdate by default GameSystemSet::PreTick, + ( + GameSystemSet::PrePlan.run_if(in_state(Phase::Plan)), + GameSystemSet::PreExecute.run_if(in_state(Phase::Execute)), + ), GameSystemSet::Tick, + ( + GameSystemSet::Plan.run_if(in_state(Phase::Plan)), + GameSystemSet::Execute.run_if(in_state(Phase::Execute)), + ), GameSystemSet::PostTick, + ( + GameSystemSet::PostPlan.run_if(in_state(Phase::Plan)), + GameSystemSet::PostExecute.run_if(in_state(Phase::Execute)), + ), ) .run_if(in_state(GameState::Running)) .chain(), @@ -65,8 +78,27 @@ pub enum GameState { Paused, } +#[derive(Debug, Clone, Eq, PartialEq, Hash, SubStates, Default)] +#[source(GameState = GameState::Running)] +pub enum Phase { + #[default] + Execute, + Plan, +} + #[derive(SystemSet, Debug, Hash, PartialEq, Eq, Clone)] pub enum GameSystemSet { + // fixed update - contains all gameplay logic. does not run when game is paused + LoadingTick, + PreTick, + PreExecute, + PrePlan, + Tick, + Execute, + Plan, + PostTick, + PostExecute, + PostPlan, // runs even if game is not loaded/active UpdateInputs, UI, @@ -74,11 +106,6 @@ pub enum GameSystemSet { PreUpdate, Update, PostUpdate, - // fixed update - contains all gameplay logic. does not run when game is paused - LoadingTick, - PreTick, - Tick, - PostTick, } fn start_physics(mut time: ResMut>) { diff --git a/src/main.rs b/src/main.rs index 7b172a2..b9edd60 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,6 +24,7 @@ fn main() { combat::CombatPlugin, building_core::BuildingCorePlugin, building::BuildingPlugin, + building_impaler::BuildingImpalerPlugin, // new internal crates go here ));