-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.lua
More file actions
90 lines (79 loc) · 2.32 KB
/
Copy pathcontroller.lua
File metadata and controls
90 lines (79 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
function _getEmptyKeyboardState()
r = {
up = false,
down = false,
right = false,
left = false,
a = false,
x = false,
z = false,
q = false,
w = false,
m1 = false,
m2 = false,
}
r["/"] = false
r["return"] = false
return r
end
-- Use this to know whether to respond to a keypress
keyboard = _getEmptyKeyboardState()
-- Used for keeping track of buttons that act only on down
_previousKeyboardState = _getEmptyKeyboardState()
function keyboardUpdate(dt)
-- This will be the previous state of the next frame
-- We currently need the current previous state, so this has to be
-- a seperate variable
local nextPreviousState = _getEmptyKeyboardState()
updateKeyDown("x", nextPreviousState)
updateKeyDown("z", nextPreviousState)
updateKeyDown("a", nextPreviousState)
updateKeyDown("q", nextPreviousState)
updateKeyDown("w", nextPreviousState)
updateKeyDown("up", nextPreviousState)
updateKeyDown("down", nextPreviousState)
updateKeyDown("left", nextPreviousState)
updateKeyDown("right", nextPreviousState)
updateKeyDown("return", nextPreviousState)
updateKeyDown("/", nextPreviousState)
updateMouseDown("m1", nextPreviousState)
updateMouseDown("m2", nextPreviousState)
-- TODO: When in menu, we want direction keys to behave like keyDown.
-- TODO: When in game, we want direction keys to fire every frame
_previousKeyboardState = nextPreviousState
end
function updateKeyDown(key, nextPreviousState)
if love.keyboard.isDown(key) then
nextPreviousState[key] = true
if _previousKeyboardState[key] == false then
keyboard[key] = true
else
keyboard[key] = false
end
else
keyboard[key] = false
nextPreviousState[key] = false
end
end
function updateMouseDown(key, nextPreviousState)
local code = 0
if key == "m1" then code = 1 else code = 2 end
if love.mouse.isDown(code) then
nextPreviousState[key] = true
if _previousKeyboardState[key] == false then
keyboard[key] = true
else
keyboard[key] = false
end
else
keyboard[key] = false
nextPreviousState[key] = false
end
end
function keyIsHeld(key)
if key == "m1" or key == 'm2' then
local code = ternary(key == "m1", 1, 2)
return love.mouse.isDown(code) and not keyboard[x]
end
return love.keyboard.isDown(key) and not keyboard[x]
end