-
Notifications
You must be signed in to change notification settings - Fork 0
/
persistence.lua
291 lines (240 loc) · 9.39 KB
/
persistence.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
module(..., package.seeall)
local GBCDataCabinet = require("plugin.GBCDataCabinet")
-- -----------------------------------------------------------------------------------
-- Declaração das variáveis
-- -----------------------------------------------------------------------------------
local M = { }
GBCDataCabinet.createCabinet( "currentFileName" )
-- -----------------------------------------------------------------------------------
-- Funções referentes à persistência dos arquivos
-- -----------------------------------------------------------------------------------
function M.setGlobal( mod, name, value )
GBCDataCabinet.createCabinet( mod )
GBCDataCabinet.set( mod, name, value )
end
function M.getGlobal( mod, name )
return GBCDataCabinet.get( mod, name )
end
-- Retorna uma lista com os nomes dos arquivos de jogo salvos
function M.filesNames()
if ( GBCDataCabinet.load( "files" ) == true ) then
return GBCDataCabinet.get( "files", "names" )
end
end
-- Retorna um arquivo de jogo com os valores "default" de um jogo novo
function defaultFile( characterName )
local character = { stepping = { x, y, point }, flipped, name }
local house = { isComplete, isGameComplete, wonSurprise, controlsTutorial, collectedPieces, bikeTutorial, stars, onRepeat, mapRepeat }
local school = { isComplete, stars, onRepeat, mapRepeat }
local restaurant = { isComplete, stars, onRepeat, mapRepeat }
local default = { character = character, house = house, restaurant = restaurant, school = school }
default.character.flipped = false
default.character.stepping.x, default.character.stepping.y = M.startingPoint( "house" )
default.character.stepping.point = "exit"
default.character.name = characterName
default.currentMiniGame = "house"
default.house.isComplete = false
default.house.isGameComplete = false
default.house.shownCompletion = false
default.house.wonSurprise = false
default.house.controlsTutorial = "incomplete"
default.house.bikeTutorial = "incomplete"
default.house.stars = 0
default.house.previousStars = 0
default.house.onRepeat = false
default.house.mapRepeat = false
default.school.isComplete = false
default.school.stars = 0
default.school.previousStars = 0
default.school.onRepeat = false
default.school.mapRepeat = false
default.restaurant.isComplete = false
default.restaurant.stars = 0
default.restaurant.previousStars = 0
default.restaurant.onRepeat = false
default.restaurant.mapRepeat = false
return default
end
function M.resetGame()
local gameFile = M.loadGameFile()
local fileName = M.getCurrentFileName()
if ( gameFile ) then
newGameFile = defaultFile( gameFile.character.name )
GBCDataCabinet.set( fileName, "gameState", defaultFile( gameFile.character.name ) )
GBCDataCabinet.save( fileName )
end
end
function M.fileExists( fileName )
local filesNames = M.filesNames()
if ( filesNames ) then
for k, v in pairs( filesNames ) do
if ( v == fileName ) then return true end
end
end
return false
end
-- Cria um jogo novo
function M.newGameFile( newFileName, characterName )
local files = { }
-- Verifica se a lista de jogos salvos existe. Caso não exista, ela é criada
if ( GBCDataCabinet.load( "files" ) == false ) then
GBCDataCabinet.createCabinet( "files" )
GBCDataCabinet.set( "files", "names", files )
end
files = GBCDataCabinet.get( "files", "names" )
-- Insere um novo nome da lista de jogos salvos
table.insert( files, newFileName )
-- Cria um cabinet para o novo jogo e já adiciona um estado de jogo default
GBCDataCabinet.createCabinet( newFileName )
GBCDataCabinet.set( newFileName, "gameState", defaultFile( characterName ) )
-- Salva a lista com os nomes dos arquivos e o novo cabinet do jogo
GBCDataCabinet.save( "files" )
GBCDataCabinet.save( newFileName )
-- Define o nome do jogo atual para ser acessado pelas cenas
M.setCurrentFileName( newFileName )
--M.deleteFiles()
end
-- Salva o estado atual do jogo
function M.saveGameFile( gameState )
local fileName = M.getCurrentFileName()
GBCDataCabinet.set( fileName, "gameState", gameState )
GBCDataCabinet.save( fileName )
end
-- Retorna um arquivo salvo
function M.loadGameFile()
local fileName = M.getCurrentFileName()
GBCDataCabinet.load( fileName )
return GBCDataCabinet.get( fileName, "gameState" )
end
-- Define nome do arquivo atual que será acessado pelas cenas
function M.setCurrentFileName( fileName )
GBCDataCabinet.set( "currentFileName", "name", fileName )
GBCDataCabinet.save( "currentFileName" )
end
-- Retorna arquivo atual
function M.getCurrentFileName()
return GBCDataCabinet.get( "currentFileName", "name" )
end
-- Deleta os arquivos de jogo
function M.deleteFiles()
local files
GBCDataCabinet.load( "files" )
files = GBCDataCabinet.get( "files", "names" )
for i = #files, 1, -1 do
GBCDataCabinet.load( files[i] )
GBCDataCabinet.deleteCabinet(files[i], true)
table.remove( files )
end
GBCDataCabinet.save( "files" )
end
-- Deleta arquivo único
function M.deleteFile( fileName )
local files
GBCDataCabinet.load( "files" )
files = GBCDataCabinet.get( "files", "names" )
GBCDataCabinet.load( fileName )
GBCDataCabinet.deleteCabinet( fileName, true )
for i = 1, #files do
if ( files[i] == fileName ) then
table.remove( files, i )
break
end
end
GBCDataCabinet.save( "files" )
end
-- -----------------------------------------------------------------------------------
-- Funções referentes à persistência dos estados do jogo
-- -----------------------------------------------------------------------------------
-- Retorna o ponto inicial do minijogo atual
function M.startingPoint( currentMiniGame )
if ( currentMiniGame == "map" ) then
return 144, 96
elseif ( currentMiniGame == "house" ) then
return 80, 304
elseif ( currentMiniGame == "school" ) then
return 80, 272
elseif ( currentMiniGame == "restaurant" ) then
return 336-32, 304+32
end
end
function M.mapProgress( position, miniGame )
if ( position == "entrance" ) then
if ( miniGame == "house" ) then return 80, 160
elseif ( miniGame == "school" ) then return 240, 160
elseif ( miniGame == "restaurant" ) then return 208, 288 end
elseif ( position == "exit" ) then
if ( miniGame == "house" ) then return 144, 96
elseif ( miniGame == "school" ) then return 336, 160
elseif ( miniGame == "restaurant" ) then return 304, 288 end
end
end
-- Informa onde o character irá se posicionar dependendo de onde ele entrou/saiu ou pausou
-- o jogo anteriormente
function M.goBackPoint( currentMiniGame, previousMiniGameFile, onRepeat )
local houseExitX, houseExitY = 336, 208
local houseEntranceX, houseEntranceY = 80, 304
local houseMapExitX, houseMapExitY = M.mapProgress( "exit", "house" )
local houseMapEntranceX, houseMapEntranceY = M.mapProgress( "entrance", "house" )
local schoolExitX, schoolExitY = 304, 336
local schoolEntranceX, schoolEntranceY = 48, 272
local schoolMapExitX, schoolMapExitY = M.mapProgress( "exit", "school" )
local schoolMapEntranceX, schoolMapEntranceY = M.mapProgress( "entrance", "school" )
local restaurantMapExitX, restaurantMapExitY = M.mapProgress( "exit", "restaurant" )
local restaurantMapEntranceX, restaurantMapEntranceY = M.mapProgress( "entrance", "restaurant" )
local restaurantExitX, restaurantExitY = 48, 304
local restaurantEntranceX, restaurantEntranceY = 304, 336
local startingPointX, startingPointY = M.startingPoint( currentMiniGame )
local entrance, exit = "entrance", "exit"
local flipped = false
if ( onRepeat == true ) then
return startingPointX, startingPointY, entrance, flipped
elseif ( currentMiniGame == previousMiniGameFile.currentMiniGame ) then
return startingPointX, startingPointY, entrance, flipped
elseif ( currentMiniGame == "map" ) then
if ( previousMiniGameFile.currentMiniGame == "house" ) then
if ( previousMiniGameFile.character.stepping.point == exit ) then
return houseMapExitX, houseMapExitY, exit, flipped
else
return houseMapEntranceX, houseMapEntranceY, entrance, flipped
end
elseif ( previousMiniGameFile.currentMiniGame == "school" ) then
if ( previousMiniGameFile.character.stepping.point == exit ) then
return schoolMapExitX, schoolMapExitY, exit, flipped
else
flipped = true
return schoolMapEntranceX, schoolMapEntranceY, entrance, flipped
end
elseif ( previousMiniGameFile.currentMiniGame == "restaurant" ) then
if ( previousMiniGameFile.character.stepping.point == exit ) then
return restaurantMapExitX, restaurantMapExitY, exit, flipped
else
flipped = true
return restaurantMapEntranceX, restaurantMapEntranceY, entrance, flipped
end
end
return houseMapExitX, houseMapExitY, flipped
elseif ( currentMiniGame == "house" ) then
if ( previousMiniGameFile.character.stepping.point == exit ) then
flipped = true
return houseExitX, houseExitY, exit, flipped
else
return houseEntranceX, houseEntranceY, entrance, flipped
end
elseif ( currentMiniGame == "school" ) then
if ( previousMiniGameFile.character.stepping.point == exit ) then
flipped = true
return schoolExitX, schoolExitY, exit, flipped
else
return schoolEntranceX, schoolEntranceY, entrance, flipped
end
elseif ( currentMiniGame == "restaurant" ) then
if ( previousMiniGameFile.character.stepping.point == exit ) then
return restaurantExitX, restaurantExitY, exit, flipped
else
return restaurantEntranceX, restaurantEntranceY, entrance, flipped
end
end
end
function M.addInstructionsTable( direction, steps )
end
return M