forked from MarkHenryC/Stroople
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspiral.lua
286 lines (224 loc) · 6.23 KB
/
spiral.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
-- Mark H Carolan
--
-- Stroople project
-- © 2010 Mark H Carolan, Gregory S Hooper
module(..., package.seeall)
require "score"
require "languages"
require "globals"
local distractorTracker = 0
local correctorBias = 2
local direction = 1
local directionCounter = 0
local directionChangeForward = 200
local directionChangeBackward = 50
local rotateTimeout = 2
local rotateCounter = 1
local function newSpiral(resolution, radius, radiusX)
-- Radii:
local minRadius = 5
local maxRadius = radius or globals.H - globals.W4
local minRadiusX = 3
local maxRadiusX = radiusX or globals.W - globals.W4
-- How many times around
local nLoops = 3
local nPositions = resolution or 512
local incr = (nLoops * 360.0) / nPositions
local radIncr = (maxRadius - minRadius) / nPositions
local radIncrX = (maxRadiusX - minRadiusX) / nPositions
local radCntr = 0.0
local radCntrX = 0.0
local phase = 0.0
local posList = {} -- {x, y}
for i = 1, nPositions do
posList[i] =
{
(minRadiusX + radCntrX) * math.sin(math.rad(phase)),
(minRadius + radCntr) * math.cos(math.rad(phase))
}
-- Increase radius:
radCntr = radCntr + radIncr
radCntrX = radCntrX + radIncrX
-- Next angle step
phase = phase + incr
end
return posList
end
function createWords(params, resolution)
local words = display.newGroup()
words.direction = direction
words.directionCounter = directionCounter
words.firstRunThru = true
words.colorNames = params.colorNames
words.fontName = params.fontName
words.textHeight = params.textHeight or 32
words.points = 512
words.list = newSpiral(words.points)
words.items = 16
words.positions = words.points/words.items
words.scales = {}
words.scaleIncrement = 1.0 / words.points
for i = 1, words.points do
words.scales[i] = i * words.scaleIncrement
end
function words:scroll(offset)
local r, ix
for i = 1, self.numChildren do
r = self[i]
ix = r.index
if ix == 1 and self.direction == 1 then
r.isVisible = true
elseif ix == words.points then
if r.FLAG and self.direction == 1 then
db.print("FLAG")
self.firstRunThru = false
r.FLAG = false
elseif not self.firstRunThru then
if self.direction == -1 then
r.isVisible = true
end
end
end
r.x = globals.W2 + self.list[ix][1]
r.y = globals.H2 + self.list[ix][2]
r.xScale = 0.3 + self.scales[ix]
r.yScale = 0.4 + self.scales[ix]
if r.rotating then
r.rotation = r.rotation + 1
end
ix = ix + self.direction
if ix > self.points then
if r.isVisible then
r:recycle()
end
ix = 1
elseif ix < 1 then
if self.firstRunThru then
r.isVisible = false
elseif r.isVisible then
r:recycle(false)
end
ix = self.points
end
r.index = ix
end
self.directionCounter = self.directionCounter + 1
if self.direction == 1 and self.directionCounter >= directionChangeForward then
self.direction = -1
self.directionCounter = 0
elseif self.direction == -1 and self.directionCounter >= directionChangeBackward then
self.direction = 1
self.directionCounter = 0
end
end
function words:newWord(text, font, size)
local g = display.newGroup()
g.rotateTimeout = rotateTimeout
g.rotateCounter = rotateCounter
local shadow = display.newText(text, 0, 0, font, size)
shadow:setTextColor(0, 0, 0)
shadow.x = 6; shadow.y = 6
g:insert(shadow)
local t = display.newText(text, 0, 0, font, size)
t:setTextColor(255, 255, 255) -- white means unassigned
t.x = 0; t.y = 0
g:insert(t)
g.colorWordIndex = 0 -- zero means unassigned
g.colorDisplayIndex = 0
g.clicked = false
g:addEventListener("touch", g)
function g:setData(wi, ci)
self.colorWordIndex = wi
self.colorDisplayIndex = ci
local rgb = words.colorNames[ci][2]
self[1].text = words.colorNames[wi][1]
self[2].text = words.colorNames[wi][1]
self[2]:setTextColor(rgb[1], rgb[2], rgb[3])
self.clicked = false
end
function g:checkNotClicked()
if not self.clicked then
if self.colorWordIndex == self.colorDisplayIndex then
-- A correct item was not clicked
words.score = words.score - 1
score.miss(1)
end
end
end
function g:matching()
return self.colorWordIndex == self.colorDisplayIndex and true or false
end
function g:setRandomColor()
distractorTracker = distractorTracker + 1
if distractorTracker % globals.distractorProbability == 0 then
local d, ci = languages.getDistractor()
local rgb = d[2]
self[1].text = d[1]
self[2].text = d[1]
self[2]:setTextColor(rgb[1], rgb[2], rgb[3])
self.colorWordIndex = 0 -- as long as it doesn't match
self.colorDisplayIndex = ci
self.clicked = false
else
local colorWordIndex = math.random(1, #words.colorNames)
local pairing = math.random(1, #words.colorNames * correctorBias)
if pairing > #words.colorNames then
self:setData(colorWordIndex, colorWordIndex)
else
self:setData(colorWordIndex, pairing)
end
end
self.rotateCounter = self.rotateCounter + 1
if self.rotateCounter >= self.rotateTimeout then
self.rotation = math.random(1, 360)
self.rotating = true
end
end
function g:recycle(penalise)
if penalise then
self:checkNotClicked()
end
self.isVisible = true
self.clicked = false
self.rotation = 0
self:setRandomColor(#words.colorNames)
end
function g:touch(event)
if event.phase == "began" then
local t = event.target
if t.isVisible then
if t:matching() then
words.score = words.score + 1
score.add(1)
else
words.score = words.score - 1
score.sub(1)
end
t.clicked = true
t.isVisible = false
end
end
return true
end
return g
end
function words:init()
self.score = 0
for i = 1, self.items do
local r = self:newWord("system", self.fontName, self.textHeight)
r.index = 1 + (i-1) * self.positions
r.isVisible = false
if i == 1 then r.FLAG = true end
self:insert(r)
end
for i = 1, self.numChildren do
self[i]:setRandomColor(#self.colorNames)
end
end
function words:cleanup()
for i = self.numChildren, 1, -1 do
self[i]:removeSelf()
end
end
return words
end