-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeyhandler.lua
More file actions
93 lines (79 loc) · 1.88 KB
/
keyhandler.lua
File metadata and controls
93 lines (79 loc) · 1.88 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
91
92
93
module(..., package.seeall)
assert(type(love) ~= nil, 'Love2D is required for this package')
require 'xml'
require 'utils'
function KeyHandler()
self = {}
self.pressed = {}
self.downtime = {}
self.pressHandle = {}
self.fromLast = {}
self.keys = {}
self.actions = {}
self.load = function(self)
local chunk = love.filesystem.load('keys.lua')
local keys = chunk()
for _,v in ipairs(keys) do
self.keys[v.key] = v.action
self.pressed[v.key] = false
self.pressHandle[v.key] = false
self.fromLast[v.key] = 0
self.downtime[v.key] = 0
if type(self.actions[v.action]) == 'table' then
table.insert(self.actions[v.action], v.key)
else
self.actions[v.action] = {v.key}
end
end
end
self:load()
self.update = function(self, key, state)
if self.pressed[key] ~= nil then
self.pressed[key] = state
self.pressHandle[key] = state
self.downtime[key] = 0
end
end
self.updateTimes = function(self, dt)
for k,v in pairs(self.pressed) do
if v then
self.downtime[k] = self.downtime[k] + dt
end
end
end
self.check = function(self, action)
local key = self:pressedKey(action)
if key then
return self.downtime[key]
end
return nil
end
self.handle = function(self, action)
local key = self:pressedKey(action)
if key then
if self.pressHandle[key] then
self.pressHandle[key] = false
return true
end
end
return false
end
self.reset = function(self, action)
local key = self:pressedKey(action)
if key then
self.downtime[key] = 0
end
end
self.pressedKey = function(self, action)
action = self.actions[action]
if action then
for _,v in ipairs(action) do
if self.pressed[v] then
return v
end
end
end
return nil
end
return self
end