-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgamestate.lua
410 lines (364 loc) · 11.3 KB
/
gamestate.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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
-- Game state system
-- Part of Live Simulator: 2
-- See copyright notice in main.lua
local love = require("love")
local Luaoop = require("libs.Luaoop")
local lily = require("lily")
local Async = require("async")
local Cache = require("cache")
local log = require("logging")
local Setting = require("setting")
local Gamestate = {
list = {}, -- list of registered gamestate
internal = {}, -- internal functions
stack = {}, -- the active one is always gamestate.stack[#gamestate.stack]
preparedGamestate = nil, -- gamestate which is prepared
loadingState = nil, -- loading screen gamestate
loadingStateResumed = false,
}
local weak = {__mode = "kv"}
---------------------
-- Gamestate class --
---------------------
local gamestateConstructorObject = Luaoop.class("Gamestate.Constructor")
---@generic T
---@class Gamestate.Gamestate
---@field public persist table
---@field public assets {images:table<string,love.Image>,fonts:table<string,love.Font>}
local gamestateObject = Luaoop.class("Gamestate.Gamestate")
---@param info Gamestate.ConstructorInfo
function gamestateConstructorObject:__construct(info)
assert(info.fonts, "missing fonts table")
assert(info.images, "missing images table")
self.info = info
self.events = {}
end
function gamestateConstructorObject:new()
return gamestateObject(self)
end
function gamestateConstructorObject:registerEvent(name, func)
self.events[name] = func
end
function gamestateConstructorObject.load() end
function gamestateConstructorObject.start() end
function gamestateConstructorObject.exit() end
function gamestateConstructorObject.paused() end
function gamestateConstructorObject.resumed() end
function gamestateConstructorObject.update() end
function gamestateConstructorObject.draw() end
function gamestateObject:__construct(constructor)
local internal = Luaoop.class.data(self)
internal.constructor = constructor
internal.data = {}
internal.assets = {fonts = {}, images = {}}
internal.events = {}
internal.weak = false
self.data = setmetatable({}, {
__mode = "kv",
__newindex = function(d, var, val)
rawset(d, var, val)
if not(internal.weak) then
rawset(internal.data, var, val)
end
end,
__index = function(d, var)
local val = rawget(d, var)
if val == nil then
val = rawget(internal.data, var)
end
return val
end,
})
self.assets = {
fonts = setmetatable({}, weak),
images = setmetatable({}, weak)
}
self.persist = {}
end
do
local function makeShortcutMacro(name)
gamestateObject[name] = function(self, ...)
return Luaoop.class.data(self).constructor[name](self, ...)
end
end
makeShortcutMacro("load")
makeShortcutMacro("start")
makeShortcutMacro("exit")
makeShortcutMacro("paused")
makeShortcutMacro("resumed")
makeShortcutMacro("update")
makeShortcutMacro("draw")
end
if false then
---@generic T: Gamestate.Gamestate
---@param self T
---@param name string
---@param callback fun(self:T,...)
function gamestateObject.registerEvent(self, name, callback)
end
end
-------------------------------------
-- Internal function is procedural --
-------------------------------------
function Gamestate.internal.makeWeak(game)
local state = Luaoop.class.data(game)
-- Expensive. Use sparingly!
for k in pairs(state.data) do
state.data[k] = nil
end
for k in pairs(state.assets.fonts) do
state.assets.fonts[k] = nil
end
for k in pairs(state.assets.images) do
state.assets.images[k] = nil
end
state.weak = true
end
function Gamestate.internal.makeStrong(game)
local state = Luaoop.class.data(game)
-- Expensive. Use sparingly!
for k, v in pairs(game.data) do
state.data[k] = v
end
for k, v in pairs(game.assets.fonts) do
state.assets.fonts[k] = v
end
for k, v in pairs(game.assets.images) do
state.assets.images[k] = v
end
state.weak = false
end
local function getCacheByValue(v)
local n = tostring(v[1])
local s, e = n:find(":", 1, true)
local assetName
local cacheName
if s then
cacheName = n:sub(1, s-1)
assetName = n:sub(e+1)
else
cacheName = v[1].."_"..tostring(v[2])
assetName = v[1]
end
local object = Cache.get(cacheName)
if object then
return true, object
else
return false, cacheName, (tonumber(assetName) or assetName)
end
end
local function gamestateHandleMultiLily(udata, index, value)
local assetUdata = udata.asset
local game = udata.game
local assetType = assetUdata[index][1]
local internal = Luaoop.class.data(game)
log.debugf("gamestate", "asset loaded: %s", assetUdata[index][2])
internal.assets[assetType][assetUdata[index][2]] = value
game.assets[assetType][assetUdata[index][2]] = value
Cache.set(assetUdata[index][3], value)
end
-- Called in async.runFunction
function Gamestate.internal.initialize(game, arg)
Async.wait()
Gamestate.internal.loadAssets(game)
game:load(arg)
end
function Gamestate.internal.loadAssets(game)
local state = Luaoop.class.data(game)
local loadedAssetList = {}
local assetUdata = {}
-- Get fonts
for k, v in pairs(state.constructor.info.fonts or {}) do
if not(game.assets.fonts[k]) then
local s, cname, aname = getCacheByValue(v)
if s then
game.assets.fonts[k] = cname
state.assets.fonts[k] = cname
else
loadedAssetList[#loadedAssetList + 1] = {lily.newFont, aname, v[2]}
assetUdata[#assetUdata + 1] = {"fonts", k, cname}
end
end
end
-- Get images
for k, v in pairs(state.constructor.info.images or {}) do
if not(game.assets.images[k]) then
local s, cname, aname = getCacheByValue(v)
if s then
game.assets.images[k] = cname
state.assets.images[k] = cname
else
loadedAssetList[#loadedAssetList + 1] = {lily.newImage, aname, v[2]}
assetUdata[#assetUdata + 1] = {"images", k, cname}
end
end
end
if #loadedAssetList > 0 then
-- Multilily
local multi = lily.loadMulti(loadedAssetList)
:setUserData({asset = assetUdata, game = game})
:onLoaded(gamestateHandleMultiLily)
-- Wait
while multi:isComplete() == false do
Async.wait()
end
end
end
function Gamestate.internal.initPreparation(name, game, arg, mode)
local t = {
name = name,
game = game,
arg = arg,
coro = coroutine.create(Gamestate.internal.initialize),
mode = mode,
}
coroutine.resume(t.coro, game, arg)
return t
end
function Gamestate.internal.loop()
local prep = Gamestate.preparedGamestate
local current = nil
if Gamestate.loadingState and not(Gamestate.loadingStateResumed) then
Gamestate.loadingStateResumed = true
Gamestate.loadingState:resumed()
end
if Gamestate.preparedGamestate then
local coro = Gamestate.preparedGamestate.coro
if coroutine.status(coro) == "dead" then
-- Assume it's already done, but it may be dead because lua error
if prep.mode == "replace" then
current = Gamestate.stack[#Gamestate.stack]
Gamestate.stack[#Gamestate.stack] = {
name = Gamestate.preparedGamestate.name,
game = Gamestate.preparedGamestate.game
}
elseif prep.mode == "leave" then
current = table.remove(Gamestate.stack, #Gamestate.stack)
elseif prep.mode == "enter" then
current = Gamestate.stack[#Gamestate.stack]
Gamestate.stack[#Gamestate.stack + 1] = {
name = Gamestate.preparedGamestate.name,
game = Gamestate.preparedGamestate.game
}
end
Gamestate.preparedGamestate = nil
Gamestate.loadingState = nil
if current then
if prep.mode == "enter" then
current.game:paused()
elseif prep.mode == "leave" or prep.mode == "replace" then
current.game:exit()
end
if prep.mode == "enter" or prep.mode == "replace" then
prep.game:start(prep.arg)
elseif prep.mode == "leave" then
prep.game:resumed()
end
Gamestate.internal.makeWeak(current.game)
else
prep.game:start(prep.arg)
end
if Gamestate.loadingState and Gamestate.loadingStateResumed then
Gamestate.loadingStateResumed = false
Gamestate.loadingState:paused()
Gamestate.loadingState = nil
end
end
end
end
function Gamestate.internal.handleEvents(name, ...)
local current = Gamestate.stack[#Gamestate.stack]
if not(current) then return false end
local constructor = Luaoop.class.data(current.game).constructor
if constructor.events[name] then
constructor.events[name](current.game, select(1, ...))
return true
end
return false
end
function Gamestate.internal.getActive()
if Gamestate.preparedGamestate and Gamestate.loadingState then
return Gamestate.loadingState
end
local game = Gamestate.stack[#Gamestate.stack]
if game then
return game.game
end
return nil
end
function Gamestate.internal.quit()
for i = #Gamestate.stack, 1, -1 do
local game = Gamestate.stack[i]
Gamestate.stack[i] = nil
local s, msg = pcall(game.game.exit, game.game)
if s == false then
log.errorf("gamestate", "cleanup stack #%d failed: %s", i, msg)
end
end
end
----------------------
-- Public functions --
----------------------
---@param info Gamestate.ConstructorInfo
---@return Gamestate.Gamestate
function Gamestate.create(info)
return gamestateConstructorObject(info)
end
function Gamestate.newLoadingScreen(info)
local game = info:new()
Gamestate.internal.makeStrong(game)
game:start()
return game
end
function Gamestate.register(name, obj)
if Gamestate.list[name] then
error("gamestate with name '"..name.."' already exist", 2)
end
assert(Luaoop.class.is(obj, gamestateConstructorObject), "invalid gamestate object passed")
Gamestate.list[name] = obj
end
function Gamestate.enter(loading, name, arg)
if Gamestate.preparedGamestate then
log.warn("gamestate", "attempt to enter new gamestate but one is in progress")
return
end
local game = assert(Gamestate.list[name], "invalid gamestate name"):new()
log.infof("gamestate", "entering gamestate: %s", name)
Gamestate.internal.makeStrong(game)
Gamestate.preparedGamestate = Gamestate.internal.initPreparation(name, game, arg, "enter")
Gamestate.loadingState = loading
Gamestate.loadingStateResumed = false
Setting.update()
end
function Gamestate.leave(loading)
-- If it's the last game state, just send love.event.quit
if #Gamestate.stack == 1 then
love.event.quit()
return
end
if Gamestate.preparedGamestate then
log.warn("gamestate", "attempt to enter leave gamestate but one is in progress")
return
end
local game = Gamestate.stack[#Gamestate.stack - 1]
log.infof("gamestate", "leaving gamestate")
Gamestate.internal.makeStrong(game.game)
Gamestate.preparedGamestate = Gamestate.internal.initPreparation(game.name, game.game, nil, "leave")
Gamestate.loadingState = loading
Gamestate.loadingStateResumed = false
Setting.update()
end
function Gamestate.replace(loading, name, arg)
if Gamestate.preparedGamestate then
log.warn("gamestate", "attempt to enter new gamestate but one is in progress")
return
end
local game = assert(Gamestate.list[name], "invalid gamestate name"):new()
log.infof("gamestate", "replace current gamestate: %s", name)
Gamestate.internal.makeStrong(game)
Gamestate.preparedGamestate = Gamestate.internal.initPreparation(name, game, arg, "replace")
Gamestate.loadingState = loading
Gamestate.loadingStateResumed = false
Setting.update()
end
return Gamestate