Damage check improvement
This commit is contained in:
parent
385f1ba20f
commit
cab8792207
4 changed files with 25 additions and 3 deletions
|
|
@ -1,5 +1,13 @@
|
|||
use bevy::prelude::*;
|
||||
|
||||
pub struct DamagablePlugin;
|
||||
|
||||
impl Plugin for DamagablePlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(FixedPostUpdate, broken_ships_cleanup);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Damagable {
|
||||
hp: u32,
|
||||
|
|
@ -12,6 +20,18 @@ impl Damagable {
|
|||
}
|
||||
|
||||
pub fn damage(&mut self, amount: u32) {
|
||||
self.hp = u32::clamp(self.hp - amount, 0, self.max_hp)
|
||||
if amount > self.hp {
|
||||
self.hp = 0;
|
||||
return;
|
||||
}
|
||||
self.hp -= amount;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn broken_ships_cleanup(mut commands: Commands, query: Query<(Entity, &Damagable)>) {
|
||||
for (entity, damagable) in query {
|
||||
if damagable.hp == 0 {
|
||||
commands.get_entity(entity).unwrap().despawn();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
use bevy::prelude::*;
|
||||
|
||||
use crate::collision::CollisionPlugin;
|
||||
use crate::damagable::DamagablePlugin;
|
||||
use crate::movable::MovablePlugin;
|
||||
use crate::ships::ShipsPlugin;
|
||||
|
||||
|
|
@ -16,5 +17,6 @@ fn main() {
|
|||
.add_plugins(MovablePlugin)
|
||||
.add_plugins(CollisionPlugin)
|
||||
.add_plugins(ShipsPlugin)
|
||||
.add_plugins(DamagablePlugin)
|
||||
.run();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ pub fn observe_collision(
|
|||
let Ok((mut collided, faction)) = collision_query.get_mut(collision.with) else {
|
||||
return;
|
||||
};
|
||||
if projectile.faction.can_damage(faction) {
|
||||
if projectile.faction.can_damage(faction) == false {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -53,5 +53,5 @@ fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|||
|
||||
player::spawn_player(&mut commands, player_sprite, Vec2::new(100., 100.));
|
||||
enemy::spawn_enemy(&mut commands, enemy_sprite.clone(), Vec2::new(0., 0.));
|
||||
spawn_projectile(&mut commands, projectile, 1, Factions::PlayerFaction);
|
||||
spawn_projectile(&mut commands, projectile, 100, Factions::PlayerFaction);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue