Death detection

This commit is contained in:
Rendo 2025-11-17 17:25:46 +05:00
commit 5ea5d9e2de
2 changed files with 21 additions and 5 deletions

View file

@ -1,7 +1,7 @@
use bevy::prelude::*;
use std::default::Default;
use crate::GameState;
use crate::{GameState, ships::Factions};
pub struct DamagablePlugin;
@ -20,6 +20,9 @@ pub struct Damagable {
max_hp: u32,
}
#[derive(Event)]
pub struct DamageableKilled(Option<Factions>);
impl Default for Damagable {
fn default() -> Self {
Self { max_hp: 15, hp: 15 }
@ -38,12 +41,24 @@ impl Damagable {
}
self.hp -= amount;
}
pub fn health(&self) -> u32 {
return self.hp;
}
pub fn broken_ships_cleanup(mut commands: Commands, query: Query<(Entity, &Damagable)>) {
for (entity, damagable) in query {
pub fn max_health(&self) -> u32 {
return self.max_hp;
}
}
pub fn broken_ships_cleanup(
mut commands: Commands,
query: Query<(Entity, &Damagable, Option<&Factions>)>,
) {
for (entity, damagable, faction) in query {
if damagable.hp == 0 {
commands.get_entity(entity).unwrap().despawn();
commands.trigger(DamageableKilled(faction.cloned()));
}
}
}

View file

@ -30,7 +30,7 @@ pub fn spawn_enemy(commands: &mut Commands, sprite: Handle<Image>, at: Vec2) {
},
Gun::new(2, f32::to_radians(15.), 0.2),
Collider::new(8.),
Velocity::moving(256., f32::to_radians(360.)),
Velocity::moving(256., f32::to_radians(180.)),
Sprite::from(sprite),
Transform::from_xyz(at.x, at.y, 0.),
Damagable::new(100),
@ -77,17 +77,18 @@ pub fn enemy_ai_system(
if distance_to_player <= enemy.return_radius * enemy.return_radius {
enemy_velocity.linear_speed = enemy_velocity.max_linear_speed;
enemy.state = EnemyState::Distancing;
gun.shoot = false;
continue;
}
}
EnemyState::Distancing => {
if distance_to_player > enemy.return_radius * enemy.return_radius {
enemy.state = EnemyState::Approaching;
gun.shoot = false;
continue;
}
if direction.angle_between(dir_to_player) > f32::to_radians(90.) {
enemy_velocity.rotation_speed = 0.;
gun.shoot = false;
continue;
}