-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
94 lines (79 loc) · 2.18 KB
/
main.lua
File metadata and controls
94 lines (79 loc) · 2.18 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
94
-- Import
local Gamepad = require "Gamepad"
local Keyboard = require "Keyboard"
local Constants = require "Constants"
local StageContext = require "StageContext"
local gamepads = {}
setmetatable (gamepads, {
__index = {
update = function (self, dt)
for _, gamepad in pairs(self) do
gamepad:update(dt)
end
end
}
})
local currentContext = nil
local keyboard = nil
function love.load()
-- Screen size
love.window.setMode(Constants.screenWidth, Constants.screenHeight)
-- Keyboard initialization
love.keyboard.setKeyRepeat(false)
keyboard = Keyboard(20)
for i, joystick in ipairs(love.joystick.getJoysticks()) do
if joystick:isGamepad() then
table.insert(gamepads, Gamepad(20, joystick:getID()))
end
end
currentContext = StageContext(gamepads, keyboard, "map")
end
function love.update(dt)
currentContext:update(dt)
gamepads:update(dt)
keyboard:update(dt)
end
function love.draw()
currentContext:draw()
end
function love.keypressed(key)
keyboard:updateKey(key, true)
end
function love.keyreleased(key)
keyboard:updateKey(key, false)
end
function love.joystickadded(joystick)
if joystick:isGamepad() then
table.insert(gamepads, Gamepad(20, joystick:getID()))
end
end
function love.joystickremoved(joystick)
if joystick:isGamepad() then
for i, gamepad in ipairs(gamepads) do
if joystick:getID() == gamepad.id then
table.remove(gamepads, i)
end
end
end
end
function love.gamepadpressed(joystick, button)
for _, gamepad in pairs(gamepads) do
if joystick:getID() == gamepad.id then
gamepad:updateButton(button, true)
end
end
end
function love.gamepadreleased(joystick, button)
for _, gamepad in pairs(gamepads) do
if joystick:getID() == gamepad.id then
gamepad:updateButton(button, false)
end
end
end
function love.gamepadaxis(joystick, axis)
for _, gamepad in pairs(gamepads) do
if joystick:getID() == gamepad.id then
gamepad:updateAxis(axis, joystick:getGamepadAxis(axis))
end
end
end