Implement logging system: add GameLogger class for managing log messages, integrate logging in HUDLayer and ShrodingersMine for enhanced gameplay feedback.

This commit is contained in:
2026-04-03 00:38:27 +01:00
parent 94be1f104f
commit b325b35c88
5 changed files with 61 additions and 2 deletions
+7
View File
@@ -1,5 +1,6 @@
require_relative "inventory"
require_relative "health"
require_relative "logs"
class HUDLayer
def initialize
@@ -7,17 +8,23 @@ class HUDLayer
@hud_fg = Gosu::Image.new("assets/images/hud_fg.png", retro: true)
@inventory = Inventory.new
@health = HealthBar.new(100)
@logs = GameLogger.new
$bus.on(:enemy_attack) do |damage|
@health.take_damage(damage)
end
end
def update(dt)
@logs.update(dt)
end
def draw
@hud_bg.draw(0, 0, Float::INFINITY)
@inventory.draw
@health.draw
@logs.draw
@hud_fg.draw(0, 0, Float::INFINITY)
end
+47
View File
@@ -0,0 +1,47 @@
class GameLogger
MAX_ENTRIES = 5 # max messages to show
DURATION = 3.0 # seconds each message stays
Entry = Struct.new(:text, :time)
def initialize
@entries = []
@font = Gosu::Font.new(10, name: 'assets/fonts/tn.ttf')
$bus.on(:log) do |msg|
log(msg)
end
end
# add a message
def log(msg)
@entries << Entry.new(msg, DURATION)
@entries.shift if @entries.size > MAX_ENTRIES
end
def update(dt)
@entries.each { |e| e.time -= dt }
@entries.reject! { |e| e.time <= 0 }
end
def draw
padding_x = 10
padding_y = 10
line_height = 24
# bottom-left start
base_x = padding_x
base_y = SCREEN_SIZE[1] - padding_y - line_height * @entries.size
@entries.each_with_index do |e, i|
@font.draw_text(
e.text,
base_x,
base_y + i * line_height,
Float::INFINITY,
1, 1,
Gosu::Color::YELLOW
)
end
end
end