-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWinScreen.lua
More file actions
355 lines (304 loc) · 10.9 KB
/
WinScreen.lua
File metadata and controls
355 lines (304 loc) · 10.9 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
-- WinScreen.lua
WinScreen = class(Screen)
function WinScreen:init(level,starTexs)
Screen.init(self)
Music.switch("Win")
self.level = level
self.numStars = self.level:starsEarned()
IO.saveScore(self.numStars,level.title)
-- stage
local stageState = {piles=level.levelData.stage,claw=level.levelData.claw}
self.stage = Stage(self,stageState)
self:add(self.stage)
-- make sure the random aspects of blocks remain the same for continuity
self.stage.crateConfigs = level.stage.crateConfigs
self.stage:copyState(level.stage)
-- floor
local obj = SpriteObj(0,359,768,21)
self:doDraw(obj,"Cargo Bot:Game Area Floor",-7)
-- roof
local obj = SpriteObj(0,687,768,26)
self:doDraw(obj,"Cargo Bot:Game Area Roof",-7)
-- stary background
for _,obj in ipairs(starTexs) do
obj:copyToScreen(self,-10)
self:add(obj)
end
-- stars
local starSpacing = 100
for i = 1,3 do
local x = WIDTH / 2 - starSpacing / 2 + (i-2) * starSpacing
local y = 780
if self.numStars < 4 then
local spr = "Cargo Bot:Star Filled"
if i > self.numStars then spr = "Cargo Bot:Star Empty" end
local obj = SpriteObj(x,y,95,92)
self:doDraw(obj,spr)
self:add(obj)
else
local obj = SpriteObj(x,y,95,92)
self:doDraw(obj,"Cargo Bot:Star Filled")
-- PULSING STARS
local starPulse = function(ds)
local w,h = obj:getSize()
obj:setSize(w+ds,h+ds)
local x,y = obj:getPos()
obj:translate(-ds/2,-ds/2)
end
local grow = false
local tweenerMaker
tweenerMaker = function()
-- reset the size to make sure the size doesnt diverge over time
if not grow then
local w,h = obj:getSize()
starPulse(95 - w)
end
local count = 0
local f = function()
count = count + 1
if count > 2 then
count = 0
if grow then starPulse(2) else starPulse(-2) end
end
end
grow = not grow
local tweener = Tweener(.7,f,tweenerMaker)
Tweener.add(tweener)
end
tweenerMaker()
end
end
-- fastforward button
local ffSprites = {}
ffSprites[true] = "Cargo Bot:Fast Button Active"
ffSprites[false] = "Cargo Bot:Fast Button Inactive"
self.ffB = Button(10,720,74,37)
self.ffB.fast = (self.stage.speed > 10)
self:doDraw(self.ffB,ffSprites[self.ffB.fast])
self:add(self.ffB)
-- called by the WinScreen class on fast events
self.ffB.setSprite = function(but)
self:doDraw(but,ffSprites[but.fast])
end
self.ffB.onEnded = function(but,t)
but.fast = not but.fast
Events.trigger("fast",but.fast)
end
-- next level button
self.nextB = Button(305,250,157,53) --305
--sprite("Cargo Bot:Menu Button")
self:doDraw(self.nextB,"Cargo Bot:Next Button")
self.nextB.onEnded = function(o,t) self:nextLevel() end
self:add(self.nextB)
-- replay button
self.replayB = Button(284,155,199,59)
self:doDraw(self.replayB,"Cargo Bot:Replay Button")
self.replayB.onEnded = function(o,t) self:replay() end
self:add(self.replayB)
-- menu button
self.menuB = Button(297,60,174,51) --297
self:doDraw(self.menuB,"Cargo Bot:Menu Button")
self.menuB.onEnded = function(o,t) self:menu() end
self:add(self.menuB)
-- play solution button
self.playB = Button(10,390,29,34)
self:doDraw(self.playB,"Cargo Bot:Play Solution Icon",1)
self.playB.onEnded = function(o,t) self:playSolution() end
self.playB:setTint(color(255,255,255,100))
self.playB:setExtras({left=100,right=0,top=10,bottom=10})
self:add(self.playB)
-- tip
self.tip1 = SpriteObj(20,260,137,103)
self:doDraw(self.tip1,"Cargo Bot:View Again Tip")
self:add(self.tip1)
-- stop button 1
local drawStop = function()
rectMode(CORNER)
strokeWidth(-1)
fill(255,255,255,100)
rect(0,0,30,30)
end
Screen.makeSprite("winStopB",drawStop,30,30)
self.stopB1 = Button(10,390,30,30)
self.stopB1.onEnded = function(o,t) self:stopPlaying() end
self.stopB1:setExtras({left=100,right=0,top=10,bottom=10})
-- record solution button
self.recordB = Button(715,390,45,31)
self:doDraw(self.recordB,"Cargo Bot:Record Solution Icon",1)
self.recordB.onEnded = function(o,t) self:recordSolution() end
self.recordB:setTint(color(255,255,255,100))
self:add(self.recordB)
-- tip
local obj = SpriteObj(645,270,114,93)
self:doDraw(obj,"Cargo Bot:Record Tip")
self:add(obj)
-- the second stop button
self.stopB2 = Button(715,390,30,30)
self.stopB2.onEnded = function(o,t) self:stopPlaying() end
--self.stopB2:setExtras({left=100,right=0,top=10,bottom=10})
-- YOU GOT IT
local gotItSpr = "yougotsprite"
local w,h = Screen.makeTextSprite(gotItSpr,"YOU GOT IT",{fontSize=120})
local obj = SpriteObj((WIDTH - w)/2,860,w,h)
self:doDraw(obj,gotItSpr)
self:add(obj)
-- shadow - could also use ShadowObj but this is simple enough
local obj = SpriteObj((WIDTH - w)/2-5,860-5,w,h)
self:doDraw(obj,gotItSpr,-1)
self:add(obj)
obj:setTint(color(40,40,40,100))
-- Message about number of stars earned
local msg = "You earned "..self.numStars.." stars!"
if self.numStars == 1 then msg = "You earned 1 star" end
if self.numStars == 4 then msg = "You found the shortest solution!" end
local fontArgs = {fontSize=35}
if self.numStars == 5 then
msg = "Congratulations, you found an unknown solution. Please upload to youtube!"
fontArgs = {
fontSize = 20,
fill = color(0, 255, 48, 255),
textWrapWidth = 400
}
end
-- now make the actual objs
local starMsgSpr = "startMsg"..self.numStars.."sprite"
local w,h = Screen.makeTextSprite(starMsgSpr,msg,fontArgs)
local obj = SpriteObj((WIDTH - w)/2,720,w,h)
self:doDraw(obj,starMsgSpr)
self:add(obj)
-- shadow - could also use ShadowObj but this is simple enough
local obj = SpriteObj((WIDTH - w)/2-3,720-3,w,h)
self:doDraw(obj,starMsgSpr,-1)
self:add(obj)
obj:setTint(color(40,40,40,100))
end
function WinScreen:tick()
Screen.tick(self)
if self.playing then self.level:run(self.stage) end -- so that won events only generate once
end
function WinScreen:bindEvents()
-- need to bind play so that the level knows it's now playing and it will start
-- executing the instructions
Events.bind("play",self.level,self.level.play)
Events.bind("play",self.level.program,self.level.program.play)
Events.bind("won",self,self.donePlaying)
Events.bind("fast",self,self.fast)
end
function WinScreen:fast()
self.ffB:setSprite()
end
function WinScreen:unbind()
Panel.unbind(self)
self.level:unbind() -- need this because we bound the play event to the level
self.level.program:unbind()
end
function WinScreen:playSolution()
-- show the program
self.level.program:undraw(self)
self.level.program:undraw(self.level)
local sol = self.level.program:solutionStr()
self.level.program:clear()
self.level.program:removeAll()
self.level.program.screen = self
self.level.program:makeFunctions()
self.level.program:setSolution(sol)
-- trigger events
Events.trigger("play",false) -- resets the stage state
Events.trigger("play",true) -- start playing
self.playing = true
-- remove the text buttons
self:undoDraw(self.nextB)
self:undoDraw(self.replayB)
self:undoDraw(self.menuB)
self:undoDraw(self.tip1)
self:remove(self.nextB)
self:remove(self.replayB)
self:remove(self.menuB)
self:remove(self.tip1)
-- change the play buttons to stop buttons
self:undoDraw(self.playB)
self:remove(self.playB)
self:doDraw(self.stopB1,"winStopB",1)
self:add(self.stopB1)
self:undoDraw(self.recordB)
self:remove(self.recordB)
self:doDraw(self.stopB2,"winStopB",1)
self:add(self.stopB2)
-- only the stop buttons are active
self:setActive(false)
self.active = true
self.stopB1.active = true
self.stopB2.active = true
self.ffB.active = true
self.level.program:setActive(false)
--self.program:setActiveRegisters({})
end
function WinScreen:donePlaying()
self.playing = false
if isRecording() then stopRecording() end
-- hide the program
self.level.program:undraw(self)
self.level.program:undraw(self.level)
local sol = self.level.program:solutionStr()
self.level.program:clear()
self.level.program:removeAll()
self.level.program.screen = self.level
self.level.program:makeFunctions()
self.level.program:makeMiscSprites()
self.level.program:setSolution(sol)
-- add the text buttons
self:doDraw(self.nextB,"Cargo Bot:Next Button")
self:doDraw(self.replayB,"Cargo Bot:Replay Button")
self:doDraw(self.menuB,"Cargo Bot:Menu Button")
self:doDraw(self.tip1,"Cargo Bot:View Again Tip")
self:add(self.nextB)
self:add(self.replayB)
self:add(self.menuB)
self:add(self.tip1)
-- change the buttons
self:doDraw(self.playB,"Cargo Bot:Play Solution Icon",1)
self:add(self.playB)
self:undoDraw(self.stopB1)
self:remove(self.stopB1)
self:doDraw(self.recordB,"Cargo Bot:Record Solution Icon",1)
self:add(self.recordB)
self:undoDraw(self.stopB2)
self:remove(self.stopB2)
self:setActive(true)
end
function WinScreen:stopPlaying()
Events.trigger("play",false) -- resets the stage state
self:donePlaying()
end
function WinScreen:recordSolution()
startRecording()
self:playSolution()
end
function WinScreen:replay()
self.level:setActive(true)
transitionScreen:start(self,self.level)
currentScreen = transitionScreen
transitionScreen.midCallback = function() Events.trigger("play",false) end
Music.switch("Tutorial")
end
function WinScreen:menu()
Events.trigger("levelSelect",self)
end
function WinScreen:nextLevel()
-- find the next level
local levelIdx
for idx,levelData in ipairs(levels) do
if levelData.name == self.level.title then
levelIdx = idx
break
end
end
levelIdx = math.min(levelIdx+1,#levels)
local levelData = levels[levelIdx]
local level = Level(levelData)
transitionScreen:start(self,level)
transitionScreen.endCallback = function()
level:addTutorial()
end
currentScreen = transitionScreen
end