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:
2026-04-03 14:46:35 +01:00
parent 5814a1fdee
commit bf1171a775
13 changed files with 175 additions and 41 deletions
+4 -4
View File
@@ -76,10 +76,10 @@ class Character
dx = 0.0
dy = 0.0
dx -= 1 if Gosu.button_down?(Gosu::KB_A)
dx += 1 if Gosu.button_down?(Gosu::KB_D)
dy -= 1 if Gosu.button_down?(Gosu::KB_W)
dy += 1 if Gosu.button_down?(Gosu::KB_S)
dx -= 1 if Gosu.button_down?($bus.get(:settings, :leftward))
dx += 1 if Gosu.button_down?($bus.get(:settings, :rightward))
dy -= 1 if Gosu.button_down?($bus.get(:settings, :forward))
dy += 1 if Gosu.button_down?($bus.get(:settings, :backward))
moving = dx != 0 || dy != 0
+3 -3
View File
@@ -35,12 +35,12 @@ class HealthBar
def draw
# Draw the background of the health bar (red)
Gosu.draw_rect(SCREEN_SIZE[0] - 100 - 10, 180, 100, 10, Gosu::Color::RED)
Gosu.draw_rect(SCREEN_SIZE[0] - 100 - 10, 180, 100, 10, Gosu::Color::RED, Float::INFINITY)
# Draw the foreground of the health bar (green) based on current health
health_width = (health.to_f / max_health) * 100
Gosu.draw_rect(SCREEN_SIZE[0] - 100 - 10, 180, health_width, 10, Gosu::Color::GREEN)
Gosu.draw_rect(SCREEN_SIZE[0] - 100 - 10, 180, health_width, 10, Gosu::Color::GREEN, Float::INFINITY)
@heart_image.draw(SCREEN_SIZE[0] - 100 - 20, 173, 1, 1.5, 1.5)
@heart_image.draw(SCREEN_SIZE[0] - 100 - 20, 173, Float::INFINITY, 1.5, 1.5)
end
end
+5 -5
View File
@@ -94,19 +94,19 @@ class Inventory
when Gosu::KB_1 then @inventory_selected = :occams_razor
when Gosu::KB_2 then @inventory_selected = :quantum_bow
when Gosu::KB_3 then @inventory_selected = :blade_of_recursion
when Gosu::KB_DOWN
when $bus.get(:settings, :"inventory down")
row, col = find_slot(@inventory_selected)
@inventory_selected = @grid[(row + 1) % @grid.size][col][0]
when Gosu::KB_UP
when $bus.get(:settings, :"inventory up")
row, col = find_slot(@inventory_selected)
@inventory_selected = @grid[(row - 1) % @grid.size][col][0]
when Gosu::KB_LEFT
when $bus.get(:settings, :"inventory left")
row, col = find_slot(@inventory_selected)
@inventory_selected = @grid[row][(col - 1) % @grid[row].size][0]
when Gosu::KB_RIGHT
when $bus.get(:settings, :"inventory right")
row, col = find_slot(@inventory_selected)
@inventory_selected = @grid[row][(col + 1) % @grid[row].size][0]
when Gosu::KB_RETURN
when $bus.get(:settings, :craft)
craft(@inventory_selected, ctrl: Gosu.button_down?(Gosu::KB_RIGHT_CONTROL) || Gosu.button_down?(Gosu::KB_LEFT_CONTROL))
else
return false
+1 -1
View File
@@ -49,7 +49,7 @@ class Prop
end
def update(dt)
return unless Gosu.button_down?(Gosu::MS_LEFT) || Gosu.button_down?(Gosu::KB_X)
return unless Gosu.button_down?(Gosu::MS_LEFT) || Gosu.button_down?($bus.get(:settings, :break))
player_x, player_y = $bus.get(:player_position) || [0, 0]
+1 -1
View File
@@ -122,7 +122,7 @@ class PropsHandler
@grid.delete([prop.x, prop.y])
end
end
if Gosu.button_down?(Gosu::KB_P) # place selected if it is a placeable item
if Gosu.button_down?($bus.get(:settings, :place))
selected_item = $bus.get(:selected_item)
if selected_item == :torch
player_x, player_y = $bus.get(:player_position) || [0, 0]
+3 -3
View File
@@ -27,7 +27,7 @@ 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.emit(:log, "Welcome to the Mist!")
$bus.on(:player_move) do |pos|
@camera = pos
@@ -55,7 +55,7 @@ class Game < Scene
@enemies.draw
@props.draw
@traps.draw
$bus.emit(:shade) if $bus.get(:settings, :fog)
$bus.emit(:shade)
@hud.draw
@weapons.draw
draw_debug! if $bus.get(:settings, :debug)
@@ -98,7 +98,7 @@ class Game < Scene
def button_down(id, pos)
# Pressing ESC returns to the menu, this is bad but I don't have time to make a proper pause menu,
# as the game has no persistent state outside of the current scene, just returning to the menu resets everything
return $bus.emit(:change_scene, Menu.new) if id == Gosu::KB_ESCAPE
return $bus.emit(:change_scene, Menu) if id == Gosu::KB_ESCAPE
return if @hud.button_down(id, pos)
return if @props.button_down(id, pos)
+1 -1
View File
@@ -27,7 +27,7 @@ class WeaponHandler
weapon = @weapons[selected_item]
if $bus.get(:count, selected_item) > 0
if id == Gosu::KB_SPACE
if id == $bus.get(:settings, :attack)
player_x, player_y = $bus.get(:player_position)
cam_x, cam_y = $bus.get(:camera_pos) || [0, 0]
mouse_x, mouse_y = $bus.get(:mouse_pos) || [0, 0]