-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFoundWords.lua
More file actions
246 lines (194 loc) · 7.21 KB
/
FoundWords.lua
File metadata and controls
246 lines (194 loc) · 7.21 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
-- FoundWords.lua
-- https://docs.coronalabs.com/guide/programming/06/index.html
local composer = require('composer')
local scene = composer.newScene()
-- local widget = require('widget')
local const = require 'constants'
local globalData = require 'globalData'
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 tappiesGroup
-- -----------------------------------------------------------------------------------
-- 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 dim = globalData.dim
local sceneGroup = self.view
-- Code here runs when the scene is first created but has not yet appeared on screen
-- trace('scene height is', sceneGroup.height)
-- sceneGroup.height = display.actualContentHeight * 2
-- trace('scene height is', sceneGroup.height)
Util.setBackground(sceneGroup)
local function _displayRow(y, i, word, color)
local score = 0
-- local xNumber = dim.firstTileX + dim.halfQ
local xScore = dim.firstTileX + dim.halfQ
local xLetter = dim.firstTileX + (dim.halfQ * 3)
-- if type(globalData.mode) == 'number' then
-- Tile.createLittleGraphics(sceneGroup, xNumber, y, tostring(i), color)
-- xScore = xScore + dim.halfQ * 2
-- xLetter = xLetter + dim.halfQ * 2
-- end
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 rect = display.newRect(sceneGroup, dim.toolbarX, dim.toolbarY, dim.toolbarWidth, dim.toolbarHeight)
rect:setFillColor(unpack(const.COLORS.uibackground))
]]
local y = dim.topInset + dim.halfQ
Util.banner(sceneGroup, y, 'WORDS YOU FOUND')
y = y + dim.Q
for i,word in ipairs(globalData.grid.humanFoundWords) do
_displayRow(y, i, word, globalData.colorSelected)
y = y + dim.halfQ
end
if globalData.grid.swapLoss > 0 then
y = y + dim.Q
Util.banner(sceneGroup, y, string.format('-%u SWAP POINTS', globalData.grid.swapLoss))
y = y + dim.Q
end
if #globalData.grid.robotFoundWords > 0 then
y = y + dim.Q
Util.banner(sceneGroup, y, 'WORDS ROBOTO FOUND')
y = y + dim.Q
for i,word in ipairs(globalData.grid.robotFoundWords) do
_displayRow(y, i, word, globalData.colorRoboto)
y = y + dim.halfQ
end
end
if y > display.contentHeight then genericMore = Util.genericMore(sceneGroup, 'center') end
local Tappies = {
{element='back', label='<', subtitle='BACK', command=function()
Util.sound('ui')
composer.hideOverlay('slideLeft') -- default is recycleOnly=false, so overlay scene will be completely removed, including its scene object
globalData.grid:resumeCountdown()
end},
{element='finish', label='Fin', subtitle='FINISH', command=function()
Util.sound('ui')
composer.hideOverlay('slideLeft') -- default is recycleOnly=false, so overlay scene will be completely removed, including its scene object
globalData.grid:gameOver()
end},
}
-- create a group for the tappies so they doesn't scroll with the background
tappiesGroup = display:newGroup()
-- sceneGroup:insert(tappiesGroup)
local tappies = {}
for i=1,#Tappies do
local tp = Tappies[i]
tappies[tp.element] = Tappy.new({
parent = tappiesGroup,
x = Util.mapValue(i, 1, #Tappies, dim.halfQ, display.actualContentWidth - dim.halfQ),
y = dim.toolbarY,
command = tp.command,
text = tp.label,
description = tp.subtitle
})
end
if const.VARIANT[globalData.mode].robot then -- TODO this is ugly and smelly
tappies.finish:enable(false)
end
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 FoundWords scene:hide')
end
elseif phase == 'did' then
-- Code here runs immediately after the scene goes entirely off screen
tappiesGroup:removeSelf()
-- delete the scene so it gets built next time it's shown
-- composer.removeScene('FoundWords')
-- "FoundWords's was not removed because it does not exist. Use composer.loadScene() or composer.gotoScene()"
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
Util.sound('ui')
composer.hideOverlay('slideLeft')
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