Health documentation done

This commit is contained in:
Rendo 2026-04-30 22:10:25 +05:00
commit fa2a98720e

View file

@ -2,6 +2,7 @@ use bevy::prelude::*;
use std::ops::{Add,Sub,AddAssign,SubAssign}; use std::ops::{Add,Sub,AddAssign,SubAssign};
/// Plugin that hooks killed entity cleanup and damage event consumer.
pub struct HealthPlugin; pub struct HealthPlugin;
impl Plugin for HealthPlugin { impl Plugin for HealthPlugin {
@ -11,7 +12,7 @@ impl Plugin for HealthPlugin {
} }
} }
/// Component that holds health information of damageable entity /// Component that holds health information of damageable entity.
#[derive(Component)] #[derive(Component)]
pub struct Health{ pub struct Health{
health: u32, health: u32,
@ -19,7 +20,7 @@ pub struct Health{
} }
impl Default for Health { impl Default for Health {
/// Create entity with minimum hp to not despawn immediatly /// Create entity with minimum hp to not despawn immediatly.
fn default() -> Self { fn default() -> Self {
Self { health: 1, max_health: 1 } Self { health: 1, max_health: 1 }
} }
@ -28,6 +29,7 @@ impl Default for Health {
impl Add<u32> for Health { impl Add<u32> for Health {
type Output = u32; type Output = u32;
/// Add a number in a way to prevent overflow.
fn add(self, rhs: u32) -> Self::Output { fn add(self, rhs: u32) -> Self::Output {
if self.max_health - self.health < rhs { if self.max_health - self.health < rhs {
self.max_health self.max_health
@ -40,7 +42,8 @@ impl Add<u32> for Health {
impl Sub<u32> for Health { impl Sub<u32> for Health {
type Output = u32; type Output = u32;
/// Substract a number in a way to prevent overflow.
fn sub(self, rhs: u32) -> Self::Output { fn sub(self, rhs: u32) -> Self::Output {
if self.health < rhs { if self.health < rhs {
0 0
@ -52,6 +55,7 @@ impl Sub<u32> for Health {
} }
impl AddAssign<u32> for Health { impl AddAssign<u32> for Health {
/// Add and assign a number in a way to prevent overflow.
fn add_assign(&mut self, rhs: u32) { fn add_assign(&mut self, rhs: u32) {
if u32::MAX - self.health < rhs { if u32::MAX - self.health < rhs {
self.health = u32::MAX; self.health = u32::MAX;
@ -63,6 +67,7 @@ impl AddAssign<u32> for Health {
} }
impl SubAssign<u32> for Health { impl SubAssign<u32> for Health {
/// Substract and assign a number in a way to prevent overflow.
fn sub_assign(&mut self, rhs: u32) { fn sub_assign(&mut self, rhs: u32) {
if self.health < rhs { if self.health < rhs {
self.health = 0; self.health = 0;
@ -74,14 +79,14 @@ impl SubAssign<u32> for Health {
} }
impl Health { impl Health {
/// Create new health component with set health and max health /// Create new health component with set health and max health.
pub fn new(health: u32, max_health: u32) -> Self { pub fn new(health: u32, max_health: u32) -> Self {
Self { Self {
health, health,
max_health max_health
} }
} }
/// Create new health from max health /// Create new health from max health.
pub fn new_full(max_health: u32) -> Self { pub fn new_full(max_health: u32) -> Self {
Self { Self {
health: max_health, health: max_health,
@ -107,7 +112,7 @@ pub struct Vulnerable;
#[require(Health)] #[require(Health)]
pub struct Unkillable; pub struct Unkillable;
/// Despawn all killable entites with health equal to zero /// Despawn all killable entites with health equal to zero.
pub fn despawn_killed(mut commands: Commands, query: Query<(Entity, &Health), Without<Unkillable>>) { pub fn despawn_killed(mut commands: Commands, query: Query<(Entity, &Health), Without<Unkillable>>) {
for (entity, health) in query { for (entity, health) in query {
if health.health == 0 { if health.health == 0 {
@ -116,7 +121,7 @@ pub fn despawn_killed(mut commands: Commands, query: Query<(Entity, &Health), Wi
} }
} }
/// Process damage event for vulnerable entities /// Process damage event for vulnerable entities.
pub fn recieve_damage(event: On<Damage>, mut query: Query<&mut Health, With<Vulnerable>>) { pub fn recieve_damage(event: On<Damage>, mut query: Query<&mut Health, With<Vulnerable>>) {
let Ok(mut health) = query.get_mut(event.entity) else { let Ok(mut health) = query.get_mut(event.entity) else {
return; return;