From b325b35c8884fb08bc72a9da6e0148d18ab25a5f Mon Sep 17 00:00:00 2001 From: Daanish Date: Fri, 3 Apr 2026 00:38:27 +0100 Subject: [PATCH] Implement logging system: add GameLogger class for managing log messages, integrate logging in HUDLayer and ShrodingersMine for enhanced gameplay feedback. --- game/hud/layer.rb | 7 +++++ game/hud/logs.rb | 47 ++++++++++++++++++++++++++++++++++ game/scene.rb | 3 +++ game/traps/shrodingers_mine.rb | 2 ++ todo.md | 4 +-- 5 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 game/hud/logs.rb diff --git a/game/hud/layer.rb b/game/hud/layer.rb index a2f0984..e0b22d0 100644 --- a/game/hud/layer.rb +++ b/game/hud/layer.rb @@ -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 diff --git a/game/hud/logs.rb b/game/hud/logs.rb new file mode 100644 index 0000000..11abe5a --- /dev/null +++ b/game/hud/logs.rb @@ -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 \ No newline at end of file diff --git a/game/scene.rb b/game/scene.rb index 0e3f74e..d772d34 100644 --- a/game/scene.rb +++ b/game/scene.rb @@ -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) diff --git a/game/traps/shrodingers_mine.rb b/game/traps/shrodingers_mine.rb index cc9760e..7833e10 100644 --- a/game/traps/shrodingers_mine.rb +++ b/game/traps/shrodingers_mine.rb @@ -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 diff --git a/todo.md b/todo.md index 57111c8..2a29855 100644 --- a/todo.md +++ b/todo.md @@ -5,8 +5,8 @@ So, #make enemy spawn in the boss rooms with randomness + enemy count - done #make the lizard kind of enemy with very high health + dps but slow - done -make blade work as planned -make bow just normal +#make blade work as planned - done +#make bow quantum - done make minimap for the torches revealing teh maze click on minimap makes game pause to allow pausing for safety