Add key rebinding
This commit is contained in:
parent
b497deffd9
commit
8c453d6069
1
README
1
README
|
@ -10,3 +10,4 @@ Movement:
|
||||||
|
|
||||||
r - restart
|
r - restart
|
||||||
q - quit
|
q - quit
|
||||||
|
c - configure keybindings
|
||||||
|
|
|
@ -18,9 +18,12 @@ local last_key_pressed = nil
|
||||||
local last_direction_moved = nil
|
local last_direction_moved = nil
|
||||||
local move_repeat_counter = 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 game_mode = gamemodes.normal
|
||||||
|
|
||||||
|
local configuration_steps = {quit = -3, restart = -2, configure = -1}
|
||||||
|
local configuration_step = nil
|
||||||
|
|
||||||
local direction_keys = {}
|
local direction_keys = {}
|
||||||
direction_keys['i'] = directions.up
|
direction_keys['i'] = directions.up
|
||||||
direction_keys['u'] = directions.upleft
|
direction_keys['u'] = directions.upleft
|
||||||
|
@ -30,7 +33,7 @@ direction_keys['k'] = directions.down
|
||||||
direction_keys['.'] = directions.downright
|
direction_keys['.'] = directions.downright
|
||||||
direction_keys['l'] = directions.right
|
direction_keys['l'] = directions.right
|
||||||
direction_keys['o'] = directions.upright
|
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
|
local win_image = nil
|
||||||
|
|
||||||
|
@ -679,6 +682,38 @@ function drawWin()
|
||||||
love.graphics.draw(win_image, left_border, up_border, 0, scale, scale)
|
love.graphics.draw(win_image, left_border, up_border, 0, scale, scale)
|
||||||
end
|
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
|
-- Callbacks
|
||||||
-- ------------------------------------------------------------------
|
-- ------------------------------------------------------------------
|
||||||
|
@ -701,16 +736,38 @@ function love.update(dt)
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.keypressed(key)
|
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
|
configuration_step = configuration_step + 1
|
||||||
love.event.quit()
|
if configuration_step > directions.upright then
|
||||||
elseif key == control_keys.restart then
|
game_mode = gamemodes.normal
|
||||||
newGame()
|
end
|
||||||
elseif direction_keys[key] ~= nil then
|
else
|
||||||
step(direction_keys[key])
|
last_key_pressed = key
|
||||||
elseif key == 'printscreen' then
|
|
||||||
debug = true
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -727,6 +784,8 @@ function love.draw()
|
||||||
drawPlayer()
|
drawPlayer()
|
||||||
elseif game_mode == gamemodes.won then
|
elseif game_mode == gamemodes.won then
|
||||||
drawWin()
|
drawWin()
|
||||||
|
elseif game_mode == gamemodes.config then
|
||||||
|
drawConfig()
|
||||||
else
|
else
|
||||||
error("Impossible game mode")
|
error("Impossible game mode")
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue