Add key rebinding

This commit is contained in:
Juhani Krekelä 2019-06-29 15:46:17 +03:00
parent b497deffd9
commit 8c453d6069
2 changed files with 71 additions and 11 deletions

1
README
View File

@ -10,3 +10,4 @@ Movement:
r - restart
q - quit
c - configure keybindings

View File

@ -18,9 +18,12 @@ local last_key_pressed = nil
local last_direction_moved = nil
local move_repeat_counter = nil
local gamemodes = {normal = 0, won = 1}
local gamemodes = {normal = 0, won = 1, config = 2}
local game_mode = gamemodes.normal
local configuration_steps = {quit = -3, restart = -2, configure = -1}
local configuration_step = nil
local direction_keys = {}
direction_keys['i'] = directions.up
direction_keys['u'] = directions.upleft
@ -30,7 +33,7 @@ direction_keys['k'] = directions.down
direction_keys['.'] = directions.downright
direction_keys['l'] = directions.right
direction_keys['o'] = directions.upright
local control_keys = {quit = 'q', restart = 'r'}
local control_keys = {quit = 'q', restart = 'r', configure = 'c'}
local win_image = nil
@ -679,6 +682,38 @@ function drawWin()
love.graphics.draw(win_image, left_border, up_border, 0, scale, scale)
end
function drawConfig()
local text = nil
if configuration_step == configuration_steps.quit then
text = "Quit"
elseif configuration_step == configuration_steps.restart then
text = "Restart"
elseif configuration_step == configuration_steps.configure then
text = "Button configuration"
elseif configuration_step == directions.up then
text = "Up"
elseif configuration_step == directions.upleft then
text = "Up-Left"
elseif configuration_step == directions.left then
text = "Left"
elseif configuration_step == directions.downleft then
text = "Down-Left"
elseif configuration_step == directions.down then
text = "Down"
elseif configuration_step == directions.downright then
text = "Down-Right"
elseif configuration_step == directions.right then
text = "Right"
elseif configuration_step == directions.upright then
text = "Up-Right"
else
error("Impossible configuration step")
end
love.graphics.setColor(1, 1, 1)
love.graphics.print(text)
end
-- ------------------------------------------------------------------
-- Callbacks
-- ------------------------------------------------------------------
@ -701,16 +736,38 @@ function love.update(dt)
end
function love.keypressed(key)
last_key_pressed = key
if game_mode == gamemodes.config then
-- This is a hack. Each "enum value" corresponds to an integer, and we are iterating
-- through them. -3 to -1 are control keys, and 0 to 7 are direction keys
if configuration_step >= directions.up and configuration_step <= directions.upright then
direction_keys[key] = configuration_step
elseif configuration_step == configuration_steps.quit then
control_keys.quit = key
elseif configuration_step == configuration_steps.restart then
control_keys.restart = key
elseif configuration_step == configuration_steps.configure then
control_keys.configure = key
end
if key == control_keys.quit then
love.event.quit()
elseif key == control_keys.restart then
newGame()
elseif direction_keys[key] ~= nil then
step(direction_keys[key])
elseif key == 'printscreen' then
debug = true
configuration_step = configuration_step + 1
if configuration_step > directions.upright then
game_mode = gamemodes.normal
end
else
last_key_pressed = key
if key == control_keys.configure and game_mode == gamemodes.normal then
game_mode = gamemodes.config
configuration_step = configuration_steps.quit
elseif key == control_keys.quit then
love.event.quit()
elseif key == control_keys.restart then
newGame()
elseif direction_keys[key] ~= nil then
step(direction_keys[key])
elseif key == 'printscreen' then
debug = true
end
end
end
@ -727,6 +784,8 @@ function love.draw()
drawPlayer()
elseif game_mode == gamemodes.won then
drawWin()
elseif game_mode == gamemodes.config then
drawConfig()
else
error("Impossible game mode")
end