add impaler scaffolding and teams

This commit is contained in:
Jim 2025-08-25 08:31:33 -04:00
parent af45237701
commit 6659fe8e5a
Signed by: jim
GPG key ID: 3236D2F059A7C0AC
12 changed files with 151 additions and 21 deletions

13
Cargo.lock generated
View file

@ -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",

View file

@ -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

View file

@ -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,
],
},
)

View file

@ -13,4 +13,6 @@ impl Plugin for BuildingPlugin {
#[derive(Component, Default)]
#[require(Health)]
pub struct Building;
pub struct Building {
pub level: u32,
}

View file

@ -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),

View file

@ -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

View file

@ -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::<SpawnImpaler>()
.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<SpawnImpaler>,
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
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"),
));
}
}

View file

@ -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<Entity>,
@ -70,13 +76,21 @@ impl Defense {
fn on_attack(
trigger: Trigger<Attacked>,
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);

View file

@ -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

View file

@ -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<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut core_writer: EventWriter<SpawnCore>,
mut impaler_writer: EventWriter<SpawnImpaler>,
) {
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((

View file

@ -13,6 +13,7 @@ impl Plugin for StatePlugin {
.enable_state_scoped_entities::<AppState>()
.add_sub_state::<GameState>()
.enable_state_scoped_entities::<GameState>()
.add_sub_state::<Phase>()
.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<Time<Physics>>) {

View file

@ -24,6 +24,7 @@ fn main() {
combat::CombatPlugin,
building_core::BuildingCorePlugin,
building::BuildingPlugin,
building_impaler::BuildingImpalerPlugin,
// new internal crates go here
));