-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobotEnd.lua
More file actions
359 lines (287 loc) · 9.37 KB
/
RobotEnd.lua
File metadata and controls
359 lines (287 loc) · 9.37 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
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
-- RobotEnd.lua
local composer = require('composer')
local scene = composer.newScene()
local json = require 'json'
local const = require 'constants'
local globalData = require 'globalData'
local Dim = require 'Dim'
local Ivory = require 'Ivory'
local Tappy = require 'Tappy'
local Util = require 'Util'
-- -----------------------------------------------------------------------------------
-- Code outside of the scene event functions below will only be executed ONCE unless
-- the scene is removed entirely (not recycled) via 'composer.removeScene()'
-- -----------------------------------------------------------------------------------
local genericMore
local filePath = system.pathForFile('ROBOTO_stats.json', system.DocumentsDirectory)
local function loadStats()
local stats
local file, msg = io.open(filePath, 'r')
if file then
local contents = file:read('*a')
io.close(file)
trace('robot stats loaded from', filePath)
stats = json.decode(contents)
else
trace('cannot open', filePath, msg)
end
return stats or {
gamesWon = 0,
gamesLost = 0,
bestScore = 0,
worstScore = 9999,
currStreak = 0,
bestStreak = 0,
worstStreak = 0,
}
end
local function saveStats(stats)
local file, msg = io.open(filePath, 'w')
if file then
file:write(json.encode(stats, {indent=true}))
trace('robot stats written to', filePath)
io.close(file)
else
trace('cannot open', filePath, msg)
end
end
local toolbarGroup
-- -----------------------------------------------------------------------------------
-- Scene event functions
-- -----------------------------------------------------------------------------------
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
-- 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
Util.setBackground(sceneGroup)
globalData.dim = Dim.new(7,7)
local dim = globalData.dim
--[[
local function _titleRow(y, s)
local titleGroup = display.newGroup()
-- the first tile is dim.quarterQ over to the right
titleGroup.x = display.contentCenterX - (string.len(s) * dim.quarterQ) - dim.quarterQ
titleGroup.y = y
sceneGroup:insert(titleGroup)
local x = dim.halfQ
for i=1, string.len(s) do
Ivory.new({
parent = titleGroup,
x = x,
y = 0,
text = string.sub(s, i, i),
scale = 0.5,
})
x = x + dim.halfQ
end
end
]]
local function _doubletext(y, s, n)
local txt = display.newText({
parent = sceneGroup,
text = s .. tostring(n),
x = display.contentCenterX,
y = y,
font = const.FONTS.ACME,
fontSize = dim.Q / 3,
align = 'center',
})
-- txt.anchorX = 1
txt:setFillColor(0,0,0)
end
local function _displayRow(y, word, color)
local score = 0
local xScore = dim.firstTileX + dim.halfQ
local xLetter = dim.firstTileX + (dim.halfQ * 3)
for j=1, string.len(word) do
local letter = string.sub(word, j, j)
score = score + const.SCRABBLE_SCORES[letter]
Ivory.new({
parent = sceneGroup,
x = xLetter,
y = y,
text = letter,
color = color,
scale = 0.5,
})
xLetter = xLetter + dim.halfQ
end
Ivory.new({
parent = sceneGroup,
x = xScore,
y = y,
text = tostring(score * string.len(word)),
color = color,
scale = 0.5,
})
end
local stats = loadStats()
local y = dim.topInset + dim.halfQ
if event.params.humanScore > event.params.robotScore then
-- _titleRow(y, 'YOU WON')
Util.banner(sceneGroup, y, 'YOU WON')
Util.sound('complete')
stats.gamesWon = stats.gamesWon + 1
if stats.currStreak < 0 then
stats.currStreak = 1
else
stats.currStreak = stats.currStreak + 1
end
if stats.currStreak > stats.bestStreak then
stats.bestStreak = stats.currStreak
end
elseif event.params.humanScore < event.params.robotScore then
-- _titleRow(y, 'YOU LOSE')
Util.banner(sceneGroup, y, 'YOU LOSE')
Util.sound('failure')
stats.gamesLost = stats.gamesLost + 1
if stats.currStreak > 0 then
stats.currStreak = -1
else
stats.currStreak = stats.currStreak - 1
end
if stats.currStreak < stats.worstStreak then
stats.worstStreak = stats.currStreak
end
else
-- _titleRow(y, 'GAME TIED')
Util.banner(sceneGroup, y, 'GAME TIED')
end
if event.params.humanScore > stats.bestScore then
stats.bestScore = event.params.humanScore
end
if event.params.humanScore < stats.worstScore then
stats.worstScore = event.params.humanScore
end
y = y + dim.Q
-- _text(y, string.format('GAMES WON: %u', stats.gamesWon))
_doubletext(y, 'GAMES WON : ', stats.gamesWon)
y = y + dim.halfQ
-- _text(y, string.format('GAMES LOST: %u', stats.gamesLost))
_doubletext(y, 'GAMES LOST : ', stats.gamesLost)
y = y + dim.Q
-- _text(y, string.format('BEST SCORE: %u', stats.bestScore))
_doubletext(y, 'BEST SCORE : ', stats.bestScore)
y = y + dim.halfQ
-- _text(y, string.format('WORST SCORE: %u', stats.worstScore))
_doubletext(y, 'WORST SCORE : ', stats.worstScore)
y = y + dim.Q
-- _text(y, string.format('CURRENT STREAK: %d', stats.currStreak))
_doubletext(y, 'CURRENT STREAK : ', stats.currStreak)
y = y + dim.halfQ
-- _text(y, string.format('BEST STREAK: %d', stats.bestStreak))
_doubletext(y, 'BEST STREAK : ', stats.bestStreak)
y = y + dim.halfQ
-- _text(y, string.format('WORST STREAK: %d', stats.worstStreak))
_doubletext(y, 'WORST STREAK : ', stats.worstStreak)
y = y + dim.halfQ
saveStats(stats)
y = y + dim.halfQ
Util.banner(sceneGroup, y, 'WORDS YOU FOUND')
y = y + dim.Q
for _,word in ipairs(event.params.humanFoundWords) do
_displayRow(y, word, globalData.colorSelected)
y = y + dim.halfQ
end
if event.params.swapLoss > 0 then
y = y + dim.halfQ
Util.banner(sceneGroup, y, string.format('-%u SWAP POINTS', event.params.swapLoss))
y = y + dim.Q
end
y = y + dim.halfQ
Util.banner(sceneGroup, y, 'WORDS ROBOTO FOUND')
y = y + dim.Q
for _,word in ipairs(event.params.robotFoundWords) do
_displayRow(y, word, globalData.colorRoboto)
y = y + dim.halfQ
end
if y > display.contentHeight then genericMore = Util.genericMore(sceneGroup, 'center') end
-- 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
-- show()
function scene:show(event)
local sceneGroup = self.view
local phase = event.phase
if phase == 'will' then
-- Code here runs when the scene is still off screen (but is about to come on screen)
sceneGroup:addEventListener('touch', backTouch)
if not Runtime:addEventListener('key', scene) then
trace('ERROR: could not addEventListener key in FoundWords scene:show')
end
elseif phase == 'did' then
-- Code here runs when the scene is entirely on screen
end
end
-- hide()
function scene:hide(event)
local sceneGroup = self.view
local phase = event.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 RobotEnd 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('RobotEnd')
end
end
-- destroy()
function scene:destroy(event)
local sceneGroup = self.view
-- Code here runs prior to the removal of scene's view
end
function scene:key(event)
local phase = event.phase
if phase == 'up' then
if event.keyName == 'back' or event.keyName == 'deleteBack' then
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