/
main.lua
190 lines (156 loc) · 4.16 KB
/
main.lua
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
-- Compatibility
unpack = unpack or table.unpack
pack = table.pack or function(...) return {...} end
require "util/class"
require "util/trim"
require "util/split"
require "util/clone"
require "util/count"
require "util/wordwrap"
FontCache = {
ConsolasLarge = love.graphics.newFont("art/fonts/consola.ttf", 36),
Consolas = love.graphics.newFont("art/fonts/consola.ttf", 24),
ConsolasSmall = love.graphics.newFont("art/fonts/consola.ttf", 14),
Outline = love.graphics.newFont("art/fonts/outline.ttf", 36),
Stonebangs = love.graphics.newFont("art/fonts/stonebangs.ttf", 42),
TechnoSmall = love.graphics.newFont("art/fonts/techno.ttf", 24),
TechnoMed = love.graphics.newFont("art/fonts/techno.ttf", 42),
Techno = love.graphics.newFont("art/fonts/techno.ttf", 72),
}
CursorSprite = love.graphics.newImage("art/sprites/cursor.png")
CursorSprite:setFilter("nearest", "nearest")
ScreenShader = love.graphics.newShader([[
extern vec4 multColor;
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords)
{
vec4 texturecolor = Texel(texture, texture_coords);
return texturecolor * color * multColor;
}
]])
GameState = (require "object/GameState")()
local sceneMgr = (require "scene/SceneManager")()
local fullScreen = false
function love.load()
love.profiler = require "lib/profile"
love.profiler.hookall("Lua")
collectgarbage("stop")
love.graphics.setShader(ScreenShader)
math.randomseed(os.time())
sceneMgr:pushScene {class = "SageSplashScene"}
end
function love.update(dt)
if love.keyboard.isDown("f") then
dt = dt * 10
elseif love.keyboard.isDown("s") then
dt = dt / 4
end
sceneMgr:update(dt)
end
function love.draw()
sceneMgr:draw()
end
local isDown = love.keyboard.isDown
love.keyboard.isDown = function(key)
local dpadMapping = {
up = function()
return isDown("up") or (sceneMgr.gamepad and sceneMgr.gamepad:getAxis(2) < 0)
end,
down = function()
return isDown("down") or (sceneMgr.gamepad and sceneMgr.gamepad:getAxis(2) > 0)
end,
left = function()
return isDown("left") or (sceneMgr.gamepad and sceneMgr.gamepad:getAxis(1) < 0)
end,
right = function()
return isDown("right") or (sceneMgr.gamepad and sceneMgr.gamepad:getAxis(1) > 0)
end,
lshift = function()
return isDown("lshift") or (sceneMgr.gamepad and sceneMgr.gamepad:isDown(3))
end
}
local fun = dpadMapping[key]
if fun then
return fun()
else
return isDown(key)
end
end
function love.joystickadded(joystick)
sceneMgr:joystickadded(joystick)
end
function love.joystickaxis(joystick, axis, value)
-- Gamepad mapping
-- x, y = self.sceneMgr.gamepad:getAxes()
-- -1 = up
-- 1 = down
-- -1 = left
-- 1 = right
local dpadMapping = {
["1"] = {["1"] = "right", ["-1"] = "left"},
["2"] = {["1"] = "down", ["-1"] = "up"},
}
if dpadMapping[tostring(axis)] then
local val = dpadMapping[tostring(axis)][tostring(value)]
sceneMgr:keypressed(val, val)
end
-- Hack: Map axis of l/r triggers to flat buttons (c)
if (axis == 3 or axis == 6) then
if value == 1 then
love.joystickpressed(joystick, 5)
else
love.joystickreleased(joystick, 5)
end
end
end
function love.joystickpressed(joystick, button)
-- 1 = "x"
-- 2 = "a"
-- 3 = "b"
-- 4 = "y"
-- 5 = "L"
-- 6 = "R"
-- 9 = "select"
-- 10 = "start"
local buttonMap = {
[1] = "z",
[2] = "x",
--[3] = "lshift",
[4] = "z",
[5] = "c",
[6] = "c"
}
local val = buttonMap[button]
sceneMgr:keypressed(val, val)
end
function love.joystickreleased(joystick, button)
local buttonMap = {
[1] = "z",
[2] = "x",
--[3] = "lshift",
[4] = "z",
[5] = "c",
[6] = "c"
}
local val = buttonMap[button]
sceneMgr:keyreleased(val, val)
end
function love.keypressed(key, uni)
if key == "tab" then
if not fullScreen then
love.window.setFullscreen(true, "exclusive")
else
love.window.setFullscreen(false, "desktop")
end
fullScreen = not fullScreen
end
sceneMgr:keypressed(key, uni)
end
function love.keyreleased(key, uni)
sceneMgr:keyreleased(key, uni)
end
function love.mousepressed(x, y, button)
sceneMgr:mousepressed(x, y, button)
end
function love.mousereleased(x, y, button)
sceneMgr:mousereleased(x, y, button)
end