-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworld.lua
More file actions
288 lines (239 loc) · 7.65 KB
/
world.lua
File metadata and controls
288 lines (239 loc) · 7.65 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
SoundHandler = require("soundHandler")
MusicHandler = require("musicHandler")
EffectsHandler = require("effectsHandler")
IndexNameHandler = require("indexNameHandler")
EntityHandler = require("entityHandler")
ShopHandler = require("shopHandler")
LevelHandler = require("levelHandler")
Camera = require("utilities/cameraUtilities")
InterfaceUtil = require("utilities/interfaceUtilities")
Delay = require("utilities/delay")
local PriorityQueue = require("include/PriorityQueue")
local self = {}
local api = {}
function api.SetMenuState(newState)
self.menuState = newState
end
function api.ToggleMusic()
self.musicEnabled = not self.musicEnabled
if not self.musicEnabled then
MusicHandler.StopCurrentTrack()
end
end
function api.GetPaused()
return self.paused or self.menuState
end
function api.MusicEnabled()
return self.musicEnabled
end
function api.GetGameOver()
return self.gameWon or self.gameLost, self.gameWon, self.gameLost, self.overType
end
function api.Restart()
--PhysicsHandler.Destroy()
api.Initialize(self.levelIndex, self.levelTableOverride, self.musicEnabled)
end
function api.LoadLevelByTable(levelTable)
api.Initialize(self.levelIndex, levelTable, self.musicEnabled)
end
function api.GetLifetime()
return self.lifetime
end
function api.TakeScreenshot()
love.filesystem.createDirectory("screenshots")
print("working", love.filesystem.getWorkingDirectory())
print("save", love.filesystem.getSaveDirectory())
love.graphics.captureScreenshot("screenshots/screenshot_" .. math.floor(math.random()*100000) .. "_.png")
end
function api.SetGameOver(hasWon, overType)
if self.gameWon or self.gameLost then
return
end
if hasWon then
self.gameWon = true
else
self.gameLost = true
self.overType = overType
end
end
function api.SetPaused(newPause, force)
self.paused = newPause
self.forcePaused = force
end
function api.KeyPressed(key, scancode, isRepeat)
if ShopHandler.KeyPressed(key, scancode, isRepeat) then
return
end
if LevelHandler.KeyPressed(key, scancode, isRepeat) then
return
end
--if key == "escape" or key == "return" or key == "kpenter" then
-- self.paused = not self.paused
--end
end
function api.MousePressed(x, y, button)
self.pressButton = button
if api.GetPaused() then
return
end
local uiX, uiY = self.interfaceTransform:inverse():transformPoint(x, y)
if ShopHandler.MousePressed(x, y, button) then
return
end
if LevelHandler.MousePressed(x, y, button) then
return
end
if api.GetGameOver() then
return -- No doing actions
end
x, y = self.cameraTransform:inverse():transformPoint(x, y)
-- Send event to game components
if Global.DEBUG_PRINT_CLICK_POS and button == 2 then
print("{")
print([[ name = "BLA",]])
print(" pos = {" .. (math.floor(x/10)*10) .. ", " .. (math.floor(y/10)*10) .. "},")
print("},")
return true
end
end
function api.MouseReleased(x, y, button)
x, y = self.cameraTransform:inverse():transformPoint(x, y)
self.pressButton = false
-- Send event to game components
end
function api.MouseMoved(x, y, dx, dy)
if ShopHandler.MouseMoved(x, y, self.pressButton, dx, dy) then
return
end
end
function api.WorldToScreen(pos)
local x, y = self.cameraTransform:transformPoint(pos[1], pos[2])
return {x, y}
end
function api.ScreenToWorld(pos)
local x, y = self.cameraTransform:inverse():transformPoint(pos[1], pos[2])
return {x, y}
end
function api.ScreenToInterface(pos)
local x, y = self.interfaceTransform:inverse():transformPoint(pos[1], pos[2])
return {x, y}
end
function api.GetMousePositionInterface()
local x, y = love.mouse.getPosition()
return api.ScreenToInterface({x, y})
end
function api.GetMousePosition()
local x, y = love.mouse.getPosition()
return api.ScreenToWorld({x, y})
end
function api.WorldScaleToScreenScale()
local m11 = self.cameraTransform:getMatrix()
return m11
end
function api.GetOrderMult()
return self.orderMult
end
function api.GetCameraExtents(buffer)
local screenWidth, screenHeight = love.window.getMode()
local topLeftPos = api.ScreenToWorld({0, 0})
local botRightPos = api.ScreenToWorld({screenWidth, screenHeight})
buffer = buffer or 0
return topLeftPos[1] - buffer, topLeftPos[2] - buffer, botRightPos[1] + buffer, botRightPos[2] + buffer
end
function api.GetPhysicsWorld()
return PhysicsHandler.GetPhysicsWorld()
end
local function UpdateCamera()
local cameraX, cameraY, cameraScale = Camera.UpdateCameraToViewPoints(dt,
{
{pos = {0, 0}, radius = Global.MAIN_PADDING},
{pos = {Global.VIEW_WIDTH + Global.SHOP_WIDTH, Global.VIEW_HEIGHT}, radius = Global.MAIN_PADDING}
}, 0, 0)
Camera.UpdateTransform(self.cameraTransform, cameraX, cameraY, cameraScale)
end
function api.Update(dt, realDt)
MusicHandler.Update(realDt)
SoundHandler.Update(realDt)
if api.GetPaused() then
UpdateCamera()
return
end
self.lifetime = self.lifetime + dt
Delay.Update(dt)
InterfaceUtil.Update(dt)
EffectsHandler.Update(dt)
IndexNameHandler.Update(dt)
EntityHandler.Update(dt)
ShopHandler.Update(dt)
UpdateCamera()
end
function api.Draw()
local preShadowQueue = PriorityQueue.new(function(l, r) return l.y < r.y end)
local drawQueue = PriorityQueue.new(function(l, r) return l.y < r.y end)
-- Draw world
love.graphics.replaceTransform(self.cameraTransform)
--ModuleTest.Draw(drawQueue)
love.graphics.replaceTransform(self.cameraTransform)
while true do
local d = preShadowQueue:pop()
if not d then break end
d.f()
end
EffectsHandler.Draw(drawQueue)
IndexNameHandler.Draw(drawQueue)
EntityHandler.Draw(drawQueue)
ShopHandler.Draw(drawQueue)
LevelHandler.Draw(drawQueue)
if not Global.DEBUG_NO_SHADOW and not (Global.DEBUG_SPACE_ZOOM_OUT and love.keyboard.isDown("space")) then
--ShadowHandler.DrawGroundShadow(self.cameraTransform)
end
love.graphics.replaceTransform(self.cameraTransform)
while true do
local d = drawQueue:pop()
if not d then break end
d.f()
end
if not Global.DEBUG_NO_SHADOW and not (Global.DEBUG_SPACE_ZOOM_OUT and love.keyboard.isDown("space")) then
--ShadowHandler.DrawVisionShadow(self.cameraTransform)
end
--local windowX, windowY = love.window.getMode()
--if windowX/windowY > 16/9 then
-- self.interfaceTransform:setTransformation(0, 0, 0, windowY/1080, windowY/1080, 0, 0)
--else
-- self.interfaceTransform:setTransformation(0, 0, 0, windowX/1920, windowX/1920, 0, 0)
--end
love.graphics.replaceTransform(self.emptyTransform)
-- Draw interface
LevelHandler.DrawInterface()
EffectsHandler.DrawInterface()
love.graphics.replaceTransform(self.emptyTransform)
end
function api.ViewResize(width, height)
--ShadowHandler.ViewResize(width, height)
end
function api.Initialize(levelIndex, levelTableOverride, musicEnabled)
self = {}
self.cameraTransform = love.math.newTransform()
self.interfaceTransform = love.math.newTransform()
self.emptyTransform = love.math.newTransform()
self.paused = false
self.musicEnabled = false
self.lifetime = Global.DEBUG_START_LIFETIME or 0
self.levelIndex = levelIndex or Global.INIT_LEVEL
self.levelTableOverride = levelTableOverride
Delay.Initialise()
InterfaceUtil.Initialize()
EffectsHandler.Initialize(api)
SoundHandler.Initialize()
MusicHandler.Initialize(api)
IndexNameHandler.Initialize(api)
EntityHandler.Initialize(api)
ShopHandler.Initialize(api)
LevelHandler.Initialize(api, self.levelIndex, self.levelTableOverride)
-- Note that the camera pins only function for these particular second entries.
Camera.Initialize({
minScale = 1000,
initPos = {0, 0},
})
end
return api