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
+20 -11
View File
@@ -35,7 +35,7 @@ class Character
end
@current_animation = :idle_front
@facing = :front
@facing = 0.0
$bus.on(:player_position) do
next [@world_x, @world_y]
@@ -44,6 +44,10 @@ class Character
$bus.on(:collides?) do |rect|
next collides?(rect) ? :character : nil
end
$bus.on(:player_direction) do
@facing
end
end
def rect
@@ -56,6 +60,18 @@ class Character
false
end
def angle_to_direction(angle)
if angle >= -Math::PI / 4 && angle < Math::PI / 4
:right
elsif angle >= Math::PI / 4 && angle < 3 * Math::PI / 4
:front # down
elsif angle >= -3 * Math::PI / 4 && angle < -Math::PI / 4
:back # up
else
:left
end
end
def update(dt)
dx = 0.0
dy = 0.0
@@ -68,18 +84,11 @@ class Character
moving = dx != 0 || dy != 0
if moving
if dy < 0
@facing = :back
elsif dy > 0
@facing = :front
elsif dx < 0
@facing = :left
elsif dx > 0
@facing = :right
end
@facing = Math.atan2(dy, dx)
end
@current_animation = moving ? :"walk_#{@facing}" : :"idle_#{@facing}"
direction = angle_to_direction(@facing)
@current_animation = moving ? :"walk_#{direction}" : :"idle_#{direction}"
# Normalize vector if moving diagonally
if dx != 0 && dy != 0