From fa2a98720e3e86b4ab27c3df98582d522c318277 Mon Sep 17 00:00:00 2001 From: Rendo Date: Thu, 30 Apr 2026 22:10:25 +0500 Subject: [PATCH] Health documentation done --- src/health.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/health.rs b/src/health.rs index b2f64f6..06658f7 100644 --- a/src/health.rs +++ b/src/health.rs @@ -2,6 +2,7 @@ use bevy::prelude::*; use std::ops::{Add,Sub,AddAssign,SubAssign}; +/// Plugin that hooks killed entity cleanup and damage event consumer. pub struct 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)] pub struct Health{ health: u32, @@ -19,7 +20,7 @@ pub struct 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 { Self { health: 1, max_health: 1 } } @@ -28,6 +29,7 @@ impl Default for Health { impl Add for Health { type Output = u32; + /// Add a number in a way to prevent overflow. fn add(self, rhs: u32) -> Self::Output { if self.max_health - self.health < rhs { self.max_health @@ -40,7 +42,8 @@ impl Add for Health { impl Sub for Health { type Output = u32; - + + /// Substract a number in a way to prevent overflow. fn sub(self, rhs: u32) -> Self::Output { if self.health < rhs { 0 @@ -52,6 +55,7 @@ impl Sub for Health { } impl AddAssign for Health { + /// Add and assign a number in a way to prevent overflow. fn add_assign(&mut self, rhs: u32) { if u32::MAX - self.health < rhs { self.health = u32::MAX; @@ -63,6 +67,7 @@ impl AddAssign for Health { } impl SubAssign for Health { + /// Substract and assign a number in a way to prevent overflow. fn sub_assign(&mut self, rhs: u32) { if self.health < rhs { self.health = 0; @@ -74,14 +79,14 @@ impl SubAssign for 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 { Self { health, max_health } } - /// Create new health from max health + /// Create new health from max health. pub fn new_full(max_health: u32) -> Self { Self { health: max_health, @@ -107,7 +112,7 @@ pub struct Vulnerable; #[require(Health)] 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>) { for (entity, health) in query { 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, mut query: Query<&mut Health, With>) { let Ok(mut health) = query.get_mut(event.entity) else { return;