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",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "combat"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"bevy",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "combine"
|
||||
version = "4.6.7"
|
||||
|
|
@ -1909,6 +1916,7 @@ dependencies = [
|
|||
"bevy",
|
||||
"bevy-inspector-egui",
|
||||
"camera",
|
||||
"combat",
|
||||
"input",
|
||||
"level",
|
||||
"physics",
|
||||
|
|
@ -2943,6 +2951,7 @@ dependencies = [
|
|||
"avian3d",
|
||||
"bevy",
|
||||
"camera",
|
||||
"combat",
|
||||
"state",
|
||||
]
|
||||
|
||||
|
|
|
|||
|
|
@ -42,4 +42,5 @@ setup = { path = "crates/setup" }
|
|||
ui_pause = { path = "crates/ui_pause" }
|
||||
level = { path = "crates/level" }
|
||||
camera = { path = "crates/camera" }
|
||||
combat = { path = "crates/combat" }
|
||||
#new internal crates go here
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
(
|
||||
map: {
|
||||
KeyCode(F1): [
|
||||
ToggleDebugUI,
|
||||
MouseButton(Left): [
|
||||
CameraPanEnable,
|
||||
],
|
||||
MouseScroll: [
|
||||
CameraZoom,
|
||||
|
|
@ -10,17 +10,17 @@
|
|||
CameraYaw,
|
||||
CameraPanX,
|
||||
],
|
||||
KeyCode(Escape): [
|
||||
TogglePause,
|
||||
],
|
||||
MouseMovementY: [
|
||||
CameraPanY,
|
||||
],
|
||||
MouseButton(Middle): [
|
||||
CameraOrbitEnable,
|
||||
],
|
||||
MouseButton(Left): [
|
||||
CameraPanEnable,
|
||||
],
|
||||
KeyCode(Escape): [
|
||||
TogglePause,
|
||||
KeyCode(F1): [
|
||||
ToggleDebugUI,
|
||||
],
|
||||
},
|
||||
)
|
||||
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]
|
||||
bevy = { workspace = true }
|
||||
avian3d = { workspace = true }
|
||||
|
||||
state = { path = "../state" }
|
||||
camera = { path = "../camera" }
|
||||
avian3d = { workspace = true }
|
||||
combat = { path = "../combat" }
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ use bevy::{
|
|||
render::camera::Exposure,
|
||||
};
|
||||
use camera::ControlledCamera;
|
||||
use combat::{Attacked, Defense, Health};
|
||||
use state::AppState;
|
||||
|
||||
pub struct LevelPlugin;
|
||||
|
|
@ -53,12 +54,20 @@ fn setup(
|
|||
..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<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()) {
|
||||
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,
|
||||
level::LevelPlugin,
|
||||
camera::CameraPlugin,
|
||||
combat::CombatPlugin,
|
||||
// new internal crates go here
|
||||
));
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue