Factor out event-handling into event lookup
Getty Ritter
8 years ago
| 1 |
local consts = require 'constants'
|
| 2 |
local keys = {
|
| 3 |
pressed = {},
|
| 4 |
released = {},
|
| 5 |
}
|
| 6 |
|
| 7 |
function keys.pressed.q(s) love.event.quit() end
|
| 8 |
|
| 9 |
function keys.pressed.w(s) s.char.dy = -consts.speed end
|
| 10 |
function keys.pressed.a(s) s.char.dx = -consts.speed end
|
| 11 |
function keys.pressed.s(s) s.char.dy = consts.speed end
|
| 12 |
function keys.pressed.d(s) s.char.dx = consts.speed end
|
| 13 |
|
| 14 |
function keys.released.w(s) s.char.dy = 0 end
|
| 15 |
function keys.released.a(s) s.char.dx = 0 end
|
| 16 |
function keys.released.s(s) s.char.dy = 0 end
|
| 17 |
function keys.released.d(s) s.char.dx = 0 end
|
| 18 |
|
| 19 |
return {
|
| 20 |
keys = keys
|
| 21 |
}
|
| 2 | 2 |
local consts = require 'constants'
|
| 3 | 3 |
local entity = require 'entity'
|
| 4 | 4 |
local tile = require 'tile'
|
| 5 |
local event = require 'event'
|
| 5 | 6 |
local state = {}
|
| 6 | 7 |
|
| 7 | 8 |
local sprites = {}
|
|
| 25 | 26 |
end
|
| 26 | 27 |
|
| 27 | 28 |
function love.keypressed(key)
|
| 28 | |
if key == 'q' then
|
| 29 | |
love.event.quit()
|
| 30 | |
elseif key == 'w' or key == 'up' then
|
| 31 | |
state.char.dy = -consts.speed
|
| 32 | |
elseif key == 'a' or key == 'left' then
|
| 33 | |
state.char.dx = -consts.speed
|
| 34 | |
elseif key == 's' or key == 'down' then
|
| 35 | |
state.char.dy = consts.speed
|
| 36 | |
elseif key == 'd' or key == 'right' then
|
| 37 | |
state.char.dx = consts.speed
|
| 38 | |
end
|
| 29 |
return event.keys.pressed[key] and
|
| 30 |
event.keys.pressed[key](state)
|
| 39 | 31 |
end
|
| 40 | 32 |
|
| 41 | 33 |
function love.keyreleased(key)
|
| 42 | |
if key == 'w' or key == 'up' then
|
| 43 | |
state.char.dy = 0
|
| 44 | |
elseif key == 'a' or key == 'left' then
|
| 45 | |
state.char.dx = 0
|
| 46 | |
elseif key == 's' or key == 'down' then
|
| 47 | |
state.char.dy = 0
|
| 48 | |
elseif key == 'd' or key == 'right' then
|
| 49 | |
state.char.dx = 0
|
| 50 | |
end
|
| 34 |
return event.keys.released[key] and
|
| 35 |
event.keys.released[key](state)
|
| 51 | 36 |
end
|
| 52 | 37 |
|
| 53 | 38 |
function love.draw()
|