gdritter repos animaltransiro / e703309
Factor out event-handling into event lookup Getty Ritter 6 years ago
2 changed file(s) with 26 addition(s) and 20 deletion(s). Collapse all Expand all
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 }
22 local consts = require 'constants'
33 local entity = require 'entity'
44 local tile = require 'tile'
5 local event = require 'event'
56 local state = {}
67
78 local sprites = {}
2526 end
2627
2728 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)
3931 end
4032
4133 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)
5136 end
5237
5338 function love.draw()