-
Notifications
You must be signed in to change notification settings - Fork 0
/
HighScores.lua
327 lines (261 loc) · 8.48 KB
/
HighScores.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
-- HighScores.lua
-- https://docs.coronalabs.com/guide/programming/06/index.html
local composer = require('composer')
local scene = composer.newScene()
-- local widget = require('widget')
local json = require('json')
local globalData = require 'globalData'
local Dim = require 'Dim'
local Ivory = require 'Ivory'
local Tappy = require 'Tappy'
local Util = require 'Util'
local toolbarGroup
local genericMore
local filePath = system.pathForFile(globalData.mode .. '_scores.json', system.DocumentsDirectory)
-- win32 c:\Users\oddst\AppData\Roaming\Wychwood Paddocks\Must\Documents
-- print(filePath)
local function loadScores()
local scoresTable
local file = io.open(filePath, 'r')
if file then
local contents = file:read('*a')
io.close(file)
scoresTable = json.decode(contents)
end
if scoresTable == nil or #scoresTable == 0 then
local interestingWords = {
-- maximum length without overflowing to right is 11
'TELEVISION',
'SERENDIPITY',
'SHENANIGANS',
'BAMBOOZLE',
'BODACIOUSLY',
'VIXENISH',
'ZIPPERS',
'BROUHAHA',
'SCRUMPTIOUS',
'CANOODLE',
'PETRICHOR',
'NINCOMPOOP',
'MALARKEY',
'ZOOMING',
'ZOOMORPHISM',
'NETWORKERS',
'DAUGHTERS',
'CONTAINERS',
'INCLUDING',
'RACCOONS',
'ZOMBIES',
'QUIVERS',
'ALPHABETS',
'ALPENSTOCKS',
'ALUMSTONES',
'MOSQUITO',
'SIAMEZING',
'SLEEZIEST',
'ANTICRUELTY',
'ODDSMAKERS',
'VIEWPOINT',
'YEARLINGS',
'WAVEFRONT',
'MAGAZINES',
'VAGABONDS',
'ROADHOUSES',
'SOCIALIZE',
'MAGAZINES',
'QUARTERED',
'SLEUTHING',
}
scoresTable = {}
for score = 1000, 50, -50 do
local word = table.remove(interestingWords, math.random(1,#interestingWords))
table.insert(scoresTable, {score=score, words={word}})
end
end
return scoresTable
end
local function saveScores(scoresTable)
for i = #scoresTable, 21, -1 do
table.remove(scoresTable, i)
end
local file = io.open(filePath, 'w')
if file then
-- trace('write', filePath, json.encode(scoresTable))
file:write(json.encode(scoresTable))
io.close(file )
end
end
local function backTouch(event)
local grp = event.target
if event.phase == 'began' then
-- trace('touch began', event.x, event.y)
elseif event.phase == 'moved' then
-- trace('touch moved, start', event.xStart, event.yStart, 'now', event.x, event.y)
grp.x = event.x - event.xStart
grp.y = event.y - event.yStart
elseif event.phase == 'ended' then
-- trace('touch ended', event.x, event.y)
transition.moveTo(grp, {x = 0, y = 0, transition = easing.outQuad })
if genericMore then
genericMore:removeSelf()
genericMore = nil
end
elseif event.phase == 'cancelled' then
-- trace('touch cancelled', event.x, event.yet)
transition.moveTo(grp, {x = 0, y = 0, transition = easing.outQuad })
end
return true
end
-- -----------------------------------------------------------------------------------
-- Code outside of the scene event functions below will only be executed ONCE unless
-- the scene is removed entirely (not recycled) via 'composer.removeScene()'
-- -----------------------------------------------------------------------------------
-- -----------------------------------------------------------------------------------
-- Scene event functions
-- -----------------------------------------------------------------------------------
-- create()
function scene:create(event)
local sceneGroup = self.view
-- Code here runs when the scene is first created but has not yet appeared on screen
trace('HighScores scene:create')
Util.setBackground(sceneGroup)
globalData.dim = Dim.new(7,7)
end
-- show()
function scene:show(event)
local sceneGroup = self.view
local phase = event.phase
local dim = globalData.dim
trace('HighScores scene:show', phase)
if phase == 'will' then
-- Code here runs when the scene is still off screen (but is about to come on screen)
local scoresTable = loadScores()
local score = nil
local words = nil
if event.params then
score = event.params.score
-- trace('score', score)
words = event.params.words
-- trace('words', words)
if score and words then
table.insert(scoresTable, {score=score, words=words})
table.sort(scoresTable, function(a,b) return a.score > b.score end) -- default comp uses <
if score > scoresTable[#scoresTable].score then
-- trace('writing scores')
saveScores(scoresTable)
-- else
-- trace('worthless new score')
end
end
end
local function _showScoreAndWord(thisScore, thisWord, yPos, color)
Ivory.new({
parent = sceneGroup,
x = dim.halfQ,
y = yPos,
text = tostring(thisScore),
color = color,
scale = 0.5,
})
local x = dim.firstTileX + (dim.halfQ * 3)
for j=1, string.len(thisWord) do
Ivory.new({
parent = sceneGroup,
x = x,
y = yPos,
text = string.sub(thisWord, j, j),
color = color,
scale = 0.5,
})
x = x + dim.halfQ
end
end
local y = dim.topInset + dim.halfQ
Util.banner(sceneGroup, y, globalData.mode .. ' HIGH SCORES')
y = y + dim.Q
for i = 1, 20 do
if scoresTable[i] then
-- show the highest scoring word, which has been sorted (when inserted) to the front
if scoresTable[i].score == score then
_showScoreAndWord(scoresTable[i].score, scoresTable[i].words[1], y, globalData.colorSelected)
else
_showScoreAndWord(scoresTable[i].score, scoresTable[i].words[1], y, globalData.colorTile)
end
y = y + dim.halfQ
end
end
-- show the user's pathetic effort if it's not in the top 20
if #words > 0 then
if score < scoresTable[20].score then
Util.sound('failure')
y = y + dim.halfQ
_showScoreAndWord(score, words[1], y, globalData.colorSelected)
else
Util.sound('complete')
end
end
if y > display.contentHeight then genericMore = Util.genericMore(sceneGroup, 'center') end
sceneGroup:addEventListener('touch', backTouch)
if not Runtime:addEventListener('key', scene) then
trace('ERROR: could not addEventListener key in HighScores scene:show')
end
elseif phase == 'did' then
-- Code here runs when the scene is entirely on screen
-- create a group for the tappy so it doesn't scroll with the background
toolbarGroup = display:newGroup()
Tappy.new({
parent = toolbarGroup,
x = dim.halfQ,
y = dim.topInset + dim.halfQ,
command = function()
Util.sound('ui')
composer.gotoScene('ModeMenu', {effect='slideRight'})
end,
text = '☰',
description = 'MENU'
}) -- '★'
end
end
-- hide()
function scene:hide(event)
local sceneGroup = self.view
local phase = event.phase
trace('HighScores scene:hide', phase)
if phase == 'will' then
-- Code here runs when the scene is on screen (but is about to go off screen)
sceneGroup:removeEventListener('touch', backTouch)
if not Runtime:removeEventListener('key', scene) then
trace('ERROR: could not removeEventListener key in HighScores scene:hide')
end
elseif phase == 'did' then
-- Code here runs immediately after the scene goes entirely off screen
toolbarGroup:removeSelf()
-- delete the scene so it gets built next time it's shown
composer.removeScene('HighScores')
end
end
-- destroy()
function scene:destroy(event)
local sceneGroup = self.view
-- Code here runs prior to the removal of scene's view
trace('HighScores scene:destroy')
end
function scene:key(event)
local phase = event.phase
if phase == 'up' then
if event.keyName == 'back' or event.keyName == 'deleteBack' then
Util.sound('ui')
composer.gotoScene('ModeMenu', {effect='slideRight'})
return true -- override the key
end
end
end
-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
scene:addEventListener('create', scene)
scene:addEventListener('show', scene)
scene:addEventListener('hide', scene)
scene:addEventListener('destroy', scene)
-- -----------------------------------------------------------------------------------
return scene