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
+3
View File
@@ -25,6 +25,8 @@ class Game < Scene
@camera = [0, 0]
$bus.emit(:log, "Welcome to the Mist! Use WASD to move, SPACE or left-click to attack with your equipped weapon, and ESC to return to the menu.")
$bus.on(:player_move) do |pos|
@camera = pos
end
@@ -88,6 +90,7 @@ class Game < Scene
@props.update(dt)
@traps.update(dt)
@weapons.update(dt)
@hud.update(dt)
end
def button_down(id, pos)
+2
View File
@@ -10,6 +10,8 @@ class ShrodingersMine < Trap
return false if entity == :character # shrodingers mine is safe for the player, so don't destroy it or cause a blast if the player steps on it
if rand < 0.7 # 70% chance to be a landmine, 30% chance to be a dud
$bus.emit(:blast, @x, @y, 50, 50, :safe) # blast radius of 50 and damage of 50, and always safe for the player
else
$bus.emit(:log, "You hear a click, but nothing happens. It was a dud!")
end
true # to destroy the trap
end