From 3dbeadf9766c740ef68dfb11e0d71052855e2ec8 Mon Sep 17 00:00:00 2001 From: Daanish Date: Sat, 28 Mar 2026 23:24:09 +0000 Subject: [PATCH] Refactor debug checks to use event bus settings for consistency across game components. --- game/character.rb | 4 ++-- game/enemy/ai.rb | 2 +- game/hud/inventory.rb | 2 +- game/maze.rb | 2 +- game/props/base.rb | 2 +- game/scene.rb | 4 ++-- main.rb | 6 +++--- settings/configuration.rb | 19 ++++++++++++++++++- utils/maze.rb | 2 +- 9 files changed, 30 insertions(+), 13 deletions(-) diff --git a/game/character.rb b/game/character.rb index bb9034b..0a242e0 100644 --- a/game/character.rb +++ b/game/character.rb @@ -114,7 +114,7 @@ class Character end # teleport to boss room for debug purposes - if DEBUG && Gosu.button_down?(Gosu::KB_T) + if $bus.get(:settings, :debug) && Gosu.button_down?(Gosu::KB_T) room_coords = $bus.get(:boss_room_coords) if room_coords @world_x, @world_y = [room_coords[0] * 60 + 5, room_coords[1] * 60 + 5] @@ -138,7 +138,7 @@ class Character frame_index = ((elapsed * 7).floor) % 6 @animations[@current_animation][frame_index].draw(sprite_x, sprite_y, sprite_y + 30, 1.8, 1.8) - return unless DEBUG + return unless $bus.get(:settings, :debug) Gosu.draw_rect(cx, cy, SIZE[0], SIZE[1], Gosu::Color.new(0x8000FF00), cy) end diff --git a/game/enemy/ai.rb b/game/enemy/ai.rb index 9f0cc52..74e8fd7 100644 --- a/game/enemy/ai.rb +++ b/game/enemy/ai.rb @@ -76,7 +76,7 @@ class EnemyAI end def draw - return unless DEBUG + return unless $bus.get(:settings, :debug) cam_x, cam_y = $bus.get(:camera_pos) || [0, 0] @waypoints.each do |wx, wy| Gosu.draw_rect(wx - cam_x - 5, wy - cam_y - 5, 10, 10, Gosu::Color.new(0x88ffff00), Float::INFINITY) diff --git a/game/hud/inventory.rb b/game/hud/inventory.rb index aedcc7f..fc1ae1d 100644 --- a/game/hud/inventory.rb +++ b/game/hud/inventory.rb @@ -130,7 +130,7 @@ class Inventory if id == Gosu::MS_LEFT @inventory_selected = type elsif id == Gosu::MS_RIGHT - if DEBUG && [:wood, :metal, :science].include?(type) + if $bus.get(:settings, :debug) && [:wood, :metal, :science].include?(type) @grid[row_idx][col_idx][1][0] = max else craft(type, ctrl: Gosu.button_down?(Gosu::KB_RIGHT_CONTROL) || Gosu.button_down?(Gosu::KB_LEFT_CONTROL)) diff --git a/game/maze.rb b/game/maze.rb index 661f643..fea6ecf 100644 --- a/game/maze.rb +++ b/game/maze.rb @@ -103,7 +103,7 @@ class Maze @spritesheet[index - 1].draw(screen_x, screen_y, screen_y, scale, scale) - next unless DEBUG + next unless $bus.get(:settings, :debug) shapes = shapes_for(index, gx * tile_size, gy * tile_size) shapes.each do |shape| diff --git a/game/props/base.rb b/game/props/base.rb index 72d6303..e5719de 100644 --- a/game/props/base.rb +++ b/game/props/base.rb @@ -100,7 +100,7 @@ class Prop Gosu.draw_rect(bar_x, bar_y, bar_width, bar_height, Gosu::Color::RED, screen_y - 19) end - return unless DEBUG + return unless $bus.get(:settings, :debug) # collision box centered on same point Gosu.draw_rect(screen_x - SIZE[0] / 2, screen_y - SIZE[1], *SIZE, Gosu::Color.new(0x88ff0000), Float::INFINITY) diff --git a/game/scene.rb b/game/scene.rb index bc373d3..cf94f09 100644 --- a/game/scene.rb +++ b/game/scene.rb @@ -38,8 +38,8 @@ class Game < Scene @enemies.draw @props.draw @hud.draw - draw_fog! - draw_debug! if DEBUG + draw_fog! if $bus.get(:settings, :fog) + draw_debug! if $bus.get(:settings, :debug) end def draw_debug! diff --git a/main.rb b/main.rb index fcdb018..d6b5a7f 100644 --- a/main.rb +++ b/main.rb @@ -1,15 +1,15 @@ require 'gosu' -require_relative 'settings/configuration' require_relative 'utils/bus' +$bus = EventBus.new + +require_relative 'settings/configuration' require_relative 'utils/utils' require_relative 'utils/scene' require_relative 'settings/scene' require_relative 'menu/scene' require_relative 'game/scene' -$bus = EventBus.new - # Main window class Window < Gosu::Window def initialize diff --git a/settings/configuration.rb b/settings/configuration.rb index 116017c..e5fbfd3 100644 --- a/settings/configuration.rb +++ b/settings/configuration.rb @@ -3,4 +3,21 @@ SCREEN_SIZE = [720, 480].freeze N, S, W, E = 1, 2, 4, 8 -DEBUG = ARGV.include?("--debug") +# Config module to hold global settings that can be toggled in the settings scene, and accessed anywhere else in the code via the event bus +module Config + @config = { + debug: ARGV.include?("--debug"), + fog: true, + torches_lightup: false + } + + $bus.on(:settings) do |k| + next @config[k] + end + + module_function + + def toggle(key) + @config[key] = !@config[key] + end +end diff --git a/utils/maze.rb b/utils/maze.rb index c1182e9..78447af 100644 --- a/utils/maze.rb +++ b/utils/maze.rb @@ -26,7 +26,7 @@ class MazeData end end - return unless DEBUG + return unless $bus.get(:settings, :debug) print_debug $bus.on(:boss_room_coords) do