Refactor input handling: replace hardcoded key inputs with configurable key bindings for character movement, inventory navigation, and actions; enhance settings scene with key rebind functionality and add credits scene.
This commit is contained in:
+38
-11
@@ -5,28 +5,55 @@ N, S, W, E = 1, 2, 4, 8
|
||||
|
||||
# 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: true,
|
||||
@debug = false
|
||||
|
||||
@keybinds = {
|
||||
forward: Gosu::KB_W,
|
||||
backward: Gosu::KB_S,
|
||||
leftward: Gosu::KB_A,
|
||||
rightward: Gosu::KB_D,
|
||||
break: Gosu::KB_X,
|
||||
attack: Gosu::KB_SPACE,
|
||||
place: Gosu::KB_P,
|
||||
"inventory up": Gosu::KB_UP,
|
||||
"inventory down": Gosu::KB_DOWN,
|
||||
"inventory right": Gosu::KB_RIGHT,
|
||||
"inventory left": Gosu::KB_LEFT,
|
||||
craft: Gosu::KB_RETURN,
|
||||
}
|
||||
|
||||
|
||||
if File.exist?("config.json")
|
||||
data = JSON.parse(File.read("config.json"))
|
||||
@config.merge!(data.transform_keys(&:to_sym))
|
||||
@debug = data["debug"] || false
|
||||
@keybinds.merge!(data["keybinds"]&.transform_keys(&:to_sym) || {})
|
||||
end
|
||||
|
||||
$bus.on(:settings, :master) { |k| @config[k] }
|
||||
$bus.on(:settings, :master) { |k| k == :debug ? @debug : @keybinds[k] }
|
||||
|
||||
module_function
|
||||
|
||||
def toggle(key)
|
||||
@config[key] = !@config[key]
|
||||
def bindings
|
||||
@keybinds
|
||||
end
|
||||
|
||||
def debug
|
||||
@debug
|
||||
end
|
||||
|
||||
def debug=(value)
|
||||
@debug = value
|
||||
end
|
||||
|
||||
def set_key(action, key)
|
||||
@keybinds[action] = key
|
||||
end
|
||||
|
||||
def persist!
|
||||
File.open("config.json", "w") do |f|
|
||||
f.write(JSON.pretty_generate(@config))
|
||||
f.write(JSON.pretty_generate({
|
||||
keybinds: @keybinds,
|
||||
debug: @debug
|
||||
}))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
+65
-4
@@ -1,15 +1,76 @@
|
||||
class Settings < Scene
|
||||
def initialize
|
||||
super
|
||||
@font = Gosu::Font.new(24)
|
||||
@font_title = Gosu::Font.new(28, name: "assets/fonts/tn.ttf")
|
||||
@font = Gosu::Font.new(20, name: "assets/fonts/tn.ttf")
|
||||
@selected = 0
|
||||
@waiting_for_key = nil
|
||||
end
|
||||
|
||||
def draw
|
||||
Gosu.draw_rect(0, 0, SCREEN_SIZE[0], SCREEN_SIZE[1], Gosu::Color::BLACK)
|
||||
@font.draw_text("Settings Scene - Press ESC to return to Menu", 50, 50, 1, 1, 1, Gosu::Color::WHITE)
|
||||
Gosu.draw_rect(0, 0, *SCREEN_SIZE, Gosu::Color.new(0xFF131313))
|
||||
|
||||
title = "Settings"
|
||||
@font_title.draw_text(title, center_x(title), 40, 1, 1, 1, Gosu::Color::YELLOW)
|
||||
|
||||
debug_option = "Debug Mode".ljust(15) + " : " + "#{Config.debug ? 'ON' : 'OFF'}".rjust(12)
|
||||
debug_option = "> #{debug_option} <" if @selected == 0
|
||||
@font.draw_text(debug_option, center_x(debug_option), 90, 1, 1, 1, @selected == 0 ? Gosu::Color::YELLOW : Gosu::Color::WHITE)
|
||||
|
||||
Config.bindings.each_with_index do |opt, i|
|
||||
y = 120 + i * 25
|
||||
selected = (i == @selected - 1)
|
||||
|
||||
text = "#{opt[0].to_s.ljust(15)} : #{Gosu.button_name(opt[1]).rjust(12)}"
|
||||
text = "> #{text} <" if selected
|
||||
|
||||
color = selected ? Gosu::Color::YELLOW : Gosu::Color::WHITE
|
||||
@font.draw_text(text, center_x(text), y, 1, 1, 1, color)
|
||||
end
|
||||
|
||||
if @waiting_for_key
|
||||
msg = "Press a key... (ESC to cancel)"
|
||||
@font.draw_text(msg, center_x(msg), 430, 1, 1, 1, Gosu::Color::GRAY)
|
||||
else
|
||||
hint = "Enter: Select | ESC: Back"
|
||||
@font.draw_text(hint, center_x(hint), 430, 1, 1, 1, Gosu::Color::GRAY)
|
||||
end
|
||||
end
|
||||
|
||||
def button_down(id, _pos)
|
||||
$bus.emit(:change_scene, Menu) if id == Gosu::KB_ESCAPE
|
||||
# waiting for keybind input
|
||||
if @waiting_for_key
|
||||
if [Gosu::KB_ESCAPE].include?(id)
|
||||
@waiting_for_key = nil
|
||||
return
|
||||
end
|
||||
Config.set_key(@waiting_for_key, id)
|
||||
Config.persist!
|
||||
@waiting_for_key = nil
|
||||
return
|
||||
end
|
||||
|
||||
case id
|
||||
when Gosu::KB_UP
|
||||
@selected = (@selected - 1) % (Config.bindings.size + 1)
|
||||
when Gosu::KB_DOWN
|
||||
@selected = (@selected + 1) % (Config.bindings.size + 1)
|
||||
when Gosu::KB_RETURN
|
||||
if @selected == 0
|
||||
Config.debug = !Config.debug
|
||||
Config.persist!
|
||||
else
|
||||
@waiting_for_key = Config.bindings.keys[@selected]
|
||||
end
|
||||
when Gosu::KB_ESCAPE
|
||||
Config.persist!
|
||||
$bus.emit(:change_scene, Menu)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def center_x(text)
|
||||
(SCREEN_SIZE[0] - @font.text_width(text)) / 2
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user