combat system
This commit is contained in:
parent
928faf9570
commit
f5d022d1d0
8 changed files with 150 additions and 10 deletions
9
Cargo.lock
generated
9
Cargo.lock
generated
|
|
@ -1766,6 +1766,13 @@ dependencies = [
|
||||||
"unicode-width",
|
"unicode-width",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "combat"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"bevy",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "combine"
|
name = "combine"
|
||||||
version = "4.6.7"
|
version = "4.6.7"
|
||||||
|
|
@ -1909,6 +1916,7 @@ dependencies = [
|
||||||
"bevy",
|
"bevy",
|
||||||
"bevy-inspector-egui",
|
"bevy-inspector-egui",
|
||||||
"camera",
|
"camera",
|
||||||
|
"combat",
|
||||||
"input",
|
"input",
|
||||||
"level",
|
"level",
|
||||||
"physics",
|
"physics",
|
||||||
|
|
@ -2943,6 +2951,7 @@ dependencies = [
|
||||||
"avian3d",
|
"avian3d",
|
||||||
"bevy",
|
"bevy",
|
||||||
"camera",
|
"camera",
|
||||||
|
"combat",
|
||||||
"state",
|
"state",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -42,4 +42,5 @@ setup = { path = "crates/setup" }
|
||||||
ui_pause = { path = "crates/ui_pause" }
|
ui_pause = { path = "crates/ui_pause" }
|
||||||
level = { path = "crates/level" }
|
level = { path = "crates/level" }
|
||||||
camera = { path = "crates/camera" }
|
camera = { path = "crates/camera" }
|
||||||
|
combat = { path = "crates/combat" }
|
||||||
#new internal crates go here
|
#new internal crates go here
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
(
|
(
|
||||||
map: {
|
map: {
|
||||||
KeyCode(F1): [
|
MouseButton(Left): [
|
||||||
ToggleDebugUI,
|
CameraPanEnable,
|
||||||
],
|
],
|
||||||
MouseScroll: [
|
MouseScroll: [
|
||||||
CameraZoom,
|
CameraZoom,
|
||||||
|
|
@ -10,17 +10,17 @@
|
||||||
CameraYaw,
|
CameraYaw,
|
||||||
CameraPanX,
|
CameraPanX,
|
||||||
],
|
],
|
||||||
|
KeyCode(Escape): [
|
||||||
|
TogglePause,
|
||||||
|
],
|
||||||
MouseMovementY: [
|
MouseMovementY: [
|
||||||
CameraPanY,
|
CameraPanY,
|
||||||
],
|
],
|
||||||
MouseButton(Middle): [
|
MouseButton(Middle): [
|
||||||
CameraOrbitEnable,
|
CameraOrbitEnable,
|
||||||
],
|
],
|
||||||
MouseButton(Left): [
|
KeyCode(F1): [
|
||||||
CameraPanEnable,
|
ToggleDebugUI,
|
||||||
],
|
|
||||||
KeyCode(Escape): [
|
|
||||||
TogglePause,
|
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
10
crates/combat/Cargo.toml
Normal file
10
crates/combat/Cargo.toml
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
[package]
|
||||||
|
name = "combat"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
bevy = { workspace = true }
|
||||||
|
|
||||||
|
[lints]
|
||||||
|
workspace = true
|
||||||
108
crates/combat/src/lib.rs
Normal file
108
crates/combat/src/lib.rs
Normal file
|
|
@ -0,0 +1,108 @@
|
||||||
|
//bevy system signatures often violate these rules
|
||||||
|
#![allow(clippy::type_complexity)]
|
||||||
|
#![allow(clippy::too_many_arguments)]
|
||||||
|
|
||||||
|
use bevy::{audio::Sample, prelude::*};
|
||||||
|
|
||||||
|
pub struct CombatPlugin;
|
||||||
|
|
||||||
|
impl Plugin for CombatPlugin {
|
||||||
|
fn build(&self, app: &mut App) {
|
||||||
|
app.add_observer(on_attack).add_observer(on_die);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Event, Debug)]
|
||||||
|
pub struct Attacked {
|
||||||
|
pub attacker: Option<Entity>,
|
||||||
|
pub amount: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Event, Debug)]
|
||||||
|
pub struct Died {
|
||||||
|
pub slayer: Option<Entity>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Component, Clone, Debug)]
|
||||||
|
pub struct Health {
|
||||||
|
pub max: f32,
|
||||||
|
pub current: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Health {
|
||||||
|
pub fn new(health: f32) -> Self {
|
||||||
|
Self {
|
||||||
|
max: health,
|
||||||
|
current: health,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Health {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
max: 1.0,
|
||||||
|
current: 1.0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Component, Clone, Debug, Default)]
|
||||||
|
#[require(Health)]
|
||||||
|
pub struct Defense {
|
||||||
|
pub amount: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Defense {
|
||||||
|
pub fn calc_damage(&self, incoming_damage: f32) -> f32 {
|
||||||
|
(incoming_damage - self.amount * 0.5).max(0.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new(amount: f32) -> Self {
|
||||||
|
Self { amount }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn on_attack(
|
||||||
|
trigger: Trigger<Attacked>,
|
||||||
|
mut commands: Commands,
|
||||||
|
mut query: Query<(&mut Health, Option<&Defense>)>,
|
||||||
|
names: Query<&Name>,
|
||||||
|
) {
|
||||||
|
let Ok((mut health, opt_defense)) = query.get_mut(trigger.target()) else {
|
||||||
|
warn!("tried to attack entity without health");
|
||||||
|
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);
|
||||||
|
info!(
|
||||||
|
"{:?} attacked {:?} for {} damage. New health = {}",
|
||||||
|
trigger
|
||||||
|
.event()
|
||||||
|
.attacker
|
||||||
|
.map(|e| names.get(e).ok())
|
||||||
|
.flatten(),
|
||||||
|
names.get(trigger.target()),
|
||||||
|
damage_taken,
|
||||||
|
health.current
|
||||||
|
);
|
||||||
|
if health.current <= 0.
|
||||||
|
&& let Ok(mut ec) = commands.get_entity(trigger.target())
|
||||||
|
{
|
||||||
|
ec.trigger(Died {
|
||||||
|
slayer: trigger.event().attacker,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn on_die(trigger: Trigger<Died>, names: Query<&Name>, mut commands: Commands) {
|
||||||
|
if let Ok(mut ec) = commands.get_entity(trigger.target()) {
|
||||||
|
info!(
|
||||||
|
"{:?} killed {:?}!",
|
||||||
|
trigger.event().slayer.map(|e| names.get(e).ok()).flatten(),
|
||||||
|
names.get(trigger.target())
|
||||||
|
);
|
||||||
|
ec.despawn();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -5,9 +5,11 @@ edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bevy = { workspace = true }
|
bevy = { workspace = true }
|
||||||
|
avian3d = { workspace = true }
|
||||||
|
|
||||||
state = { path = "../state" }
|
state = { path = "../state" }
|
||||||
camera = { path = "../camera" }
|
camera = { path = "../camera" }
|
||||||
avian3d = { workspace = true }
|
combat = { path = "../combat" }
|
||||||
|
|
||||||
[lints]
|
[lints]
|
||||||
workspace = true
|
workspace = true
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ use bevy::{
|
||||||
render::camera::Exposure,
|
render::camera::Exposure,
|
||||||
};
|
};
|
||||||
use camera::ControlledCamera;
|
use camera::ControlledCamera;
|
||||||
|
use combat::{Attacked, Defense, Health};
|
||||||
use state::AppState;
|
use state::AppState;
|
||||||
|
|
||||||
pub struct LevelPlugin;
|
pub struct LevelPlugin;
|
||||||
|
|
@ -53,12 +54,20 @@ fn setup(
|
||||||
..default()
|
..default()
|
||||||
})),
|
})),
|
||||||
Transform::from_xyz(0.0, y as f32 * 1.1, 0.0),
|
Transform::from_xyz(0.0, y as f32 * 1.1, 0.0),
|
||||||
|
Health::new(5.0),
|
||||||
|
Defense::new(y as f32 - 5.0),
|
||||||
))
|
))
|
||||||
.observe(
|
.observe(
|
||||||
|trigger: Trigger<Pointer<Click>>, mut query: Query<&mut LinearVelocity>| {
|
|trigger: Trigger<Pointer<Click>>,
|
||||||
|
mut query: Query<&mut LinearVelocity>,
|
||||||
|
mut commands: Commands| {
|
||||||
if let Ok(mut v) = query.get_mut(trigger.target()) {
|
if let Ok(mut v) = query.get_mut(trigger.target()) {
|
||||||
v.y += 3.0;
|
v.y += 5.0;
|
||||||
}
|
}
|
||||||
|
commands.entity(trigger.target()).trigger(Attacked {
|
||||||
|
attacker: None,
|
||||||
|
amount: 8.,
|
||||||
|
});
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@ fn main() {
|
||||||
ui_pause::UiPausePlugin,
|
ui_pause::UiPausePlugin,
|
||||||
level::LevelPlugin,
|
level::LevelPlugin,
|
||||||
camera::CameraPlugin,
|
camera::CameraPlugin,
|
||||||
|
combat::CombatPlugin,
|
||||||
// new internal crates go here
|
// new internal crates go here
|
||||||
));
|
));
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue