@@ -159,4 +159,164 @@ extension Queue: MutableCollection {
159
159
}
160
160
161
161
162
+ //: The next step is to implement interactive scene for the data structure:
163
+
164
+ import SpriteKit
165
+ import PlaygroundSupport
166
+
167
+ class QueueScene : SKScene {
168
+
169
+ var queueElements : Queue < QueueElementNode > ! {
170
+ didSet {
171
+ if queueElements != nil {
172
+ numberOfElements. text = " \( queueElements. count) animals "
173
+ }
174
+ }
175
+ }
176
+ lazy var numberOfElements : SKLabelNode = {
177
+ let label = SKLabelNode ( text: " 0 animals " )
178
+ label. position = CGPoint ( x: 500 , y: 300 )
179
+ label. color = . gray
180
+ label. fontSize = 28
181
+ label. verticalAlignmentMode = . center
182
+ return label
183
+ } ( )
184
+
185
+ override func didMove( to view: SKView ) {
186
+ queueElements = createRandomQeueueElementNodeArray ( for: 4 )
187
+ appearenceAnimation ( )
188
+
189
+ dequeueButton ( )
190
+ enqueueButton ( )
191
+ label ( )
192
+ description ( )
193
+
194
+ addChild ( numberOfElements)
195
+ }
196
+
197
+ func label( ) {
198
+ let nameLabel = SKLabelNode ( text: " Queue " )
199
+ nameLabel. position = CGPoint ( x: 300 , y: 760 )
200
+ nameLabel. color = . gray
201
+ nameLabel. fontSize = 64
202
+ nameLabel. verticalAlignmentMode = . center
203
+ addChild ( nameLabel)
204
+ }
205
+
206
+ func description( ) {
207
+ let nameLabel = SKLabelNode ( text: " Limit is 7 animals " )
208
+ nameLabel. position = CGPoint ( x: 300 , y: 700 )
209
+ nameLabel. color = . darkGray
210
+ nameLabel. fontSize = 24
211
+ nameLabel. verticalAlignmentMode = . center
212
+ addChild ( nameLabel)
213
+ }
214
+
215
+ func dequeueButton( ) {
216
+ let popButton = SKSpriteNode ( color: . white, size: CGSize ( width: 120 , height: 50 ) )
217
+ popButton. position = CGPoint ( x: 100 , y: 700 )
218
+ popButton. name = " pop "
219
+
220
+ let popLabel = SKLabelNode ( text: " Dequeue " )
221
+ popLabel. fontColor = SKColor . darkGray
222
+ popLabel. fontSize = 24
223
+
224
+ popLabel. verticalAlignmentMode = SKLabelVerticalAlignmentMode . center
225
+
226
+ popButton. addChild ( popLabel)
227
+ addChild ( popButton)
228
+ }
229
+
230
+ func enqueueButton( ) {
231
+ let pushButton = SKSpriteNode ( color: . white, size: CGSize ( width: 120 , height: 50 ) )
232
+ pushButton. position = CGPoint ( x: 500 , y: 700 )
233
+ pushButton. name = " push "
234
+
235
+ let pushLabel = SKLabelNode ( text: " Enqueue " )
236
+ pushLabel. fontColor = SKColor . darkGray
237
+ pushLabel. fontSize = 24
238
+ pushLabel. verticalAlignmentMode = SKLabelVerticalAlignmentMode . center
239
+
240
+ pushButton. addChild ( pushLabel)
241
+ addChild ( pushButton)
242
+ }
243
+
244
+ func appearenceAnimation( ) {
245
+
246
+ // Animate creation of the stack of books
247
+
248
+ let appearAction = SKAction . unhide ( )
249
+
250
+ for (index, book) in queueElements. enumerated ( ) {
251
+ book. position = CGPoint . init ( x: 300 , y: 500 )
252
+ book. isHidden = true
253
+ addChild ( book)
254
+
255
+ let moveDown = SKAction . move ( to: CGPoint ( x: 300 , y: CGFloat ( 80 * CGFloat( index + 1 ) ) ) , duration: 1.5 )
256
+ let waitAction = SKAction . wait ( forDuration: TimeInterval ( index * 2 ) )
257
+ let sequence = SKAction . sequence ( [ waitAction, appearAction, moveDown] )
258
+ book. run ( sequence)
259
+ }
260
+ }
261
+
262
+ func createRandomQeueueElementNodeArray( for numberOfElements: Int ) -> Queue < QueueElementNode > {
263
+ var nodes : Queue < QueueElementNode > = Queue ( )
264
+
265
+ for _ in 0 ..< numberOfElements {
266
+ let node = QueueElementNode ( )
267
+ nodes. enqueue ( element: node)
268
+ }
269
+ return nodes
270
+ }
271
+
272
+ // MARK: - Overrides
273
+
274
+ override func touchesBegan( _ touches: Set < UITouch > , with event: UIEvent ? ) {
275
+ guard let selfLocation = touches. first? . location ( in: self ) else {
276
+ return
277
+ }
278
+ let nodes = self . nodes ( at: selfLocation)
279
+
280
+ for node in nodes {
281
+ if node. name == " pop " {
282
+ // pop action
283
+ let element = queueElements. dequeue ( )
284
+ let moveUpAction = SKAction . move ( by: CGVector ( dx: 0 , dy: - 200 ) , duration: 0.5 )
285
+ let removeAction = SKAction . removeFromParent ( )
286
+ let moveQueueDown = SKAction . run {
287
+ let moveDownAction = SKAction . move ( by: CGVector ( dx: 0 , dy: - 80 ) , duration: 0.5 )
288
+ self . queueElements. forEach ( { ( node) in
289
+ node. run ( moveDownAction)
290
+ } )
291
+ }
292
+ let actionSequence = SKAction . sequence ( [ moveUpAction, removeAction, moveQueueDown] )
293
+ element? . run ( actionSequence)
294
+ } else if node. name == " push " , queueElements. count < 7 {
295
+ let node = QueueElementNode ( )
296
+ node. position = CGPoint . init ( x: 300 , y: 600 )
297
+ node. isHidden = true
298
+ addChild ( node)
299
+ queueElements. enqueue ( element: node)
300
+
301
+ let appearAction = SKAction . unhide ( )
302
+ let moveDown = SKAction . move ( to: CGPoint ( x: 300 , y: CGFloat ( 80 * CGFloat( queueElements. count) ) ) , duration: 1.5 )
303
+ let waitAction = SKAction . wait ( forDuration: TimeInterval ( 1 ) )
304
+ let sequence = SKAction . sequence ( [ waitAction, appearAction, moveDown] )
305
+ node. run ( sequence)
306
+ }
307
+
308
+ }
309
+ }
310
+
311
+ }
312
+
313
+ let skScene = QueueScene ( size: CGSize ( width: 600 , height: 800 ) )
314
+ skScene. backgroundColor = . black
315
+
316
+
317
+ let skView = SKView ( frame: CGRect ( x: 0 , y: 0 , width: 600 , height: 800 ) )
318
+ skView. presentScene ( skScene)
319
+ PlaygroundPage . current. liveView = skView
320
+
321
+
162
322
//: [Next](@next)
0 commit comments