Implement health bar system: add HealthBar class for managing health, integrate health updates in HUDLayer, and modify enemy attack handling to reflect damage taken.

This commit is contained in:
2026-03-30 20:43:06 +00:00
parent 0369d24338
commit 32de437f86
9 changed files with 110 additions and 24 deletions
+7 -4
View File
@@ -26,9 +26,9 @@ class Enemy
@w = 25
@h = 30
@health = health
@lorentz = false
@lorentz = $bus.get(:lorentz_field?) || false
$bus.on(:lorentz_field?) do |lorentz|
$bus.on(:lorentz_field!) do |lorentz|
@lorentz = lorentz
end
@@ -41,9 +41,7 @@ class Enemy
def take_damage(amount)
@health -= amount
pp "Enemy takes #{amount} damage, health now #{@health}"
if @health <= 0
pp "Enemy dies"
$bus.emit(:enemy_died, self)
end
end
@@ -76,6 +74,11 @@ class Enemy
self.class.frame_size[2]
)
# Health bar
health_width = (@w * @health / 100.0)
Gosu.draw_rect(screen_x - @w / 2, screen_y - @h / 2 - 10, health_width, 5, Gosu::Color.new(0xFF00FF00), Float::INFINITY)
if $bus.get(:settings, :debug)
Gosu.draw_rect(screen_x - @w / 2, screen_y - @h / 2, @w, @h, Gosu::Color.new(0x40FF0000), Float::INFINITY)
end
+3 -1
View File
@@ -6,11 +6,13 @@ class Force < Enemy
def initialize(x, y)
super(x, y, 100)
$bus.on(:occams_razor_attack) do |attack_x, attack_y, range, damage|
$bus.on(:attack) do |attack_x, attack_y, range, damage|
# Whoa, did'nt know ruby had hypot functions built in!
if Math.hypot(@x - attack_x, @y - attack_y) <= range
take_damage(damage)
next true
end
false
end
end
end