@@ -24,6 +24,10 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
2424 private var songLabel : SKLabelNode !
2525 private var scoreTextLabel : SKLabelNode !
2626
27+ private let deathLabel = SKLabelNode ( text: " You Died! " )
28+ private let deathLabel2 = SKLabelNode ( text: " Click anywhere to try again. " )
29+ private let finalScoreLabel = SKLabelNode ( text: " Score: " )
30+
2731 //Scoring
2832 private var lives = 5
2933 private var score = 0
@@ -33,6 +37,7 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
3337 private var bottomDetector : SKShapeNode !
3438
3539 private var running = false
40+ private var resetInProgress = true
3641
3742 //array of all contacts to be handled in the next frame
3843 var contactQueue = [ SKPhysicsContact] ( )
@@ -64,11 +69,11 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
6469 physicsWorld. contactDelegate = self
6570 }
6671
67- //sets up all initial properties of the nodes
68- func setupNodes( ) {
69- //ship node
70- ship = scene? . childNode ( withName: " ship " ) as? SKSpriteNode
72+
73+ func assignNodeProperties( ) {
74+ //center in the bottom
7175 ship. position. y = - 570
76+ ship. position. x = 0
7277
7378 //ship node physics body
7479 ship. physicsBody = SKPhysicsBody ( rectangleOf: ship. frame. size)
@@ -78,24 +83,61 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
7883 ship. physicsBody!. affectedByGravity = false
7984 ship. zPosition = 100
8085
81- //hearts
82- life1 = scene? . childNode ( withName: " life1 " ) as? SKShapeNode
83- life2 = scene? . childNode ( withName: " life2 " ) as? SKShapeNode
84- life3 = scene? . childNode ( withName: " life3 " ) as? SKShapeNode
85- life4 = scene? . childNode ( withName: " life4 " ) as? SKShapeNode
86- life5 = scene? . childNode ( withName: " life5 " ) as? SKShapeNode
87- scoreLabel = scene? . childNode ( withName: " score " ) as? SKLabelNode
88- scoreTextLabel = scene? . childNode ( withName: " scoreTextLabel " ) as? SKLabelNode
89-
86+ //set y posistions
9087 life1. position. y = - 550
9188 life2. position. y = - 550
9289 life3. position. y = - 550
9390 life4. position. y = - 550
9491 life5. position. y = - 550
9592
93+ //unhide nodes function later
94+ ship. isHidden = false
95+ life1. isHidden = false
96+ life2. isHidden = false
97+ life3. isHidden = false
98+ life4. isHidden = false
99+ life5. isHidden = false
100+ scoreTextLabel. isHidden = false
101+ scoreLabel. isHidden = false
102+
96103 scoreLabel. position. y = - 575
97104 scoreTextLabel. position. y = - 575
98105
106+ levelLabel. position. y = - 500
107+ songLabel. position. y = - 540
108+
109+ booster1. position. x = ship. position. x + 50
110+ booster1. position. y = ship. position. y
111+ booster1. particleScale = 0.1
112+ booster1. targetNode = self
113+
114+ booster2. position. x = ship. position. x - 50
115+ booster2. position. y = ship. position. y
116+ booster2. particleScale = 0.1
117+ booster2. targetNode = self
118+
119+ booster3. position. x = ship. position. x
120+ booster3. position. y = ship. position. y
121+ booster3. particleScale = 0.3
122+ booster3. targetNode = self
123+ }
124+
125+ //initialises the nodes
126+ func setupNodes( ) {
127+ //ship node
128+ ship = scene? . childNode ( withName: " ship " ) as? SKSpriteNode
129+
130+ //hearts
131+ life1 = scene? . childNode ( withName: " life1 " ) as? SKShapeNode
132+ life2 = scene? . childNode ( withName: " life2 " ) as? SKShapeNode
133+ life3 = scene? . childNode ( withName: " life3 " ) as? SKShapeNode
134+ life4 = scene? . childNode ( withName: " life4 " ) as? SKShapeNode
135+ life5 = scene? . childNode ( withName: " life5 " ) as? SKShapeNode
136+
137+ //score labels
138+ scoreLabel = scene? . childNode ( withName: " score " ) as? SKLabelNode
139+ scoreTextLabel = scene? . childNode ( withName: " scoreTextLabel " ) as? SKLabelNode
140+
99141 //stars emitter
100142 let starsPath = Bundle . main. path ( forResource: " stars " , ofType: " sks " ) !
101143 stars = NSKeyedUnarchiver . unarchiveObject ( withFile: starsPath) as! SKEmitterNode
@@ -110,51 +152,37 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
110152 asteroids. position. y = 500
111153 asteroids. targetNode = self
112154
155+ scene? . addChild ( stars)
156+ scene? . addChild ( asteroids)
157+
113158 //level labels
114159 levelLabel = scene? . childNode ( withName: " levelLabel " ) as? SKLabelNode
115160 songLabel = scene? . childNode ( withName: " songLabel " ) as? SKLabelNode
116161
117- levelLabel. position. y = - 500
118- songLabel. position. y = - 540
119-
120162 //ship booster effects
121163 let boosterPath = Bundle . main. path ( forResource: " booster " , ofType: " sks " ) !
122164 booster1 = NSKeyedUnarchiver . unarchiveObject ( withFile: boosterPath) as! SKEmitterNode
123-
124- booster1. position. x = ship. position. x + 50
125- booster1. position. y = ship. position. y
126- booster1. particleScale = 0.1
127- booster1. targetNode = self
128-
129165 booster2 = NSKeyedUnarchiver . unarchiveObject ( withFile: boosterPath) as! SKEmitterNode
130-
131- booster2. position. x = ship. position. x - 50
132- booster2. position. y = ship. position. y
133- booster2. particleScale = 0.1
134- booster2. targetNode = self
135-
136166 booster3 = NSKeyedUnarchiver . unarchiveObject ( withFile: boosterPath) as! SKEmitterNode
137-
138- booster3. position. x = ship. position. x
139- booster3. position. y = ship. position. y
140- booster3. particleScale = 0.3
141- booster3. targetNode = self
142-
167+
143168 //add noteDetector
144169 bottomDetector = scene? . childNode ( withName: " bottomDetector " ) as? SKShapeNode
145170 bottomDetector. physicsBody = SKPhysicsBody ( rectangleOf: bottomDetector. frame. size)
146171 bottomDetector. physicsBody!. collisionBitMask = PhysicsCategory . None
147172 bottomDetector. physicsBody!. categoryBitMask = PhysicsCategory . Bottom
148173 bottomDetector. physicsBody!. contactTestBitMask = PhysicsCategory . Note
149174 bottomDetector. physicsBody!. affectedByGravity = false
175+
176+ assignNodeProperties ( )
150177 }
151178
152179 //intro sequence when game starts
153180 func intro( ) {
154181
155- //add the emitters to the view
156- scene? . addChild ( stars)
157- scene? . addChild ( asteroids)
182+ score = 0
183+ scoreLabel. text = " \( score) "
184+
185+ //add the emitters to the ship
158186 scene? . addChild ( booster1)
159187 scene? . addChild ( booster2)
160188 scene? . addChild ( booster3)
@@ -392,69 +420,67 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
392420 //a note hit the ship:
393421 if nodeBitmasks. contains ( PhysicsCategory . Ship) && nodeBitmasks. contains ( PhysicsCategory . Note) {
394422
395- //ANIMATE LATER??
396-
397423 //make sure we delete the note and not the ship
398424 if ( contact. bodyA. categoryBitMask == PhysicsCategory . Note) {
399425 contact. bodyA. node!. removeFromParent ( )
400426 } else {
401427 contact. bodyB. node!. removeFromParent ( )
402428 }
403429
404- lives = lives - 1
405- switch lives {
406- case 4 :
407- life5. removeFromParent ( )
408- case 3 :
409- life4. removeFromParent ( )
410- case 2 :
411- life3. removeFromParent ( )
412- case 1 :
413- life2. removeFromParent ( )
414- case 0 :
415- life1. removeFromParent ( )
416- deathScreen ( )
417- print ( " ^ Re-run to try again " )
418- default :
419- print ( " this should not happen " )
420- }
430+ //take off one life
431+ adjustLives ( )
421432 }
422433
423434 //a note hit the bottom:
424435 if nodeBitmasks. contains ( PhysicsCategory . Bottom) && nodeBitmasks. contains ( PhysicsCategory . Note) {
436+
437+ //take off one life
438+ adjustLives ( )
425439
426- print ( " hit bottom " )
427-
428- lives = lives - 1
429- switch lives {
440+ }
441+ }
442+
443+ func adjustLives( ) {
444+ lives = lives - 1
445+ switch lives {
430446 case 4 :
431- life5. removeFromParent ( )
447+ // life5.removeFromParent()
448+ life5. isHidden = true
432449 case 3 :
433- life4. removeFromParent ( )
450+ // life4.removeFromParent()
451+ life4. isHidden = true
434452 case 2 :
435- life3. removeFromParent ( )
453+ // life3.removeFromParent()
454+ life3. isHidden = true
436455 case 1 :
437- life2. removeFromParent ( )
456+ // life2.removeFromParent()
457+ life2. isHidden = true
438458 case 0 :
439- life1. removeFromParent ( )
459+ //life1.removeFromParent()
460+ life1. isHidden = true
440461 deathScreen ( )
441- print ( " ^ Re-run to try again " )
442462 default :
443463 print ( " this should not happen " )
444- }
445464 }
446465 }
447466
448467 //ANIMATE THIS LATER
449468 func deathScreen( ) {
450- running = false
451-
452- //death sound effect
453469
454- //death animation
455- ship. removeFromParent ( )
456- scoreTextLabel. removeFromParent ( )
457- scoreLabel. removeFromParent ( )
470+ running = false
471+ resetInProgress = false
472+
473+ currentLevel = 1
474+
475+ //remove nodes
476+ ship. isHidden = true
477+ scoreTextLabel. isHidden = true
478+ scoreLabel. isHidden = true
479+ life1. isHidden = true
480+ life2. isHidden = true
481+ life3. isHidden = true
482+ life4. isHidden = true
483+ life5. isHidden = true
458484
459485 booster1. removeFromParent ( )
460486 booster2. removeFromParent ( )
@@ -466,20 +492,18 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
466492 child. removeFromParent ( )
467493 }
468494 }
469-
470- let deathLabel = SKLabelNode ( text: " You Died! " )
495+
471496 deathLabel. fontName = " Helvetica Neue Light "
472497 deathLabel. fontSize = 65
473498 deathLabel. fontColor = . white
474499 deathLabel. position = CGPoint ( x: 0 , y : 0 )
475500
476- let deathLabel2 = SKLabelNode ( text: " Re-run to try again. " )
477501 deathLabel2. fontName = " Helvetica Neue Light "
478502 deathLabel2. fontSize = 30
479503 deathLabel2. fontColor = . white
480504 deathLabel2. position = CGPoint ( x: 0 , y : - 40 )
481-
482- let finalScoreLabel = SKLabelNode ( text : " Score: \( score) " )
505+
506+ finalScoreLabel. text = " Score: \( score) "
483507 finalScoreLabel. fontName = " Helvetica Neue Light "
484508 finalScoreLabel. fontSize = 60
485509 finalScoreLabel. fontColor = . white
@@ -494,26 +518,23 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
494518 func winScreen( ) {
495519
496520 running = false
521+ resetInProgress = true
497522
498- //win animation (change later)
499-
500- //ship flys off screen??
501- ship. removeFromParent ( )
523+ //hide all nodes
524+ ship. isHidden = true
525+ scoreTextLabel. isHidden = true
526+ scoreLabel. isHidden = true
527+ life1. isHidden = true
528+ life2. isHidden = true
529+ life3. isHidden = true
530+ life4. isHidden = true
531+ life5. isHidden = true
532+
533+ //ship.removeFromParent()
502534 booster1. removeFromParent ( )
503535 booster2. removeFromParent ( )
504536 booster3. removeFromParent ( )
505-
506- //score will move to center??
507- scoreTextLabel. removeFromParent ( )
508- scoreLabel. removeFromParent ( )
509-
510- //life fade out?
511- life1. removeFromParent ( )
512- life2. removeFromParent ( )
513- life3. removeFromParent ( )
514- life4. removeFromParent ( )
515- life5. removeFromParent ( )
516-
537+
517538 // Removing existing notes
518539 for child in self . children {
519540 if child. name == " note " {
@@ -567,6 +588,28 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
567588 public override func mouseDown( with event: NSEvent ) {
568589 //shoots on click
569590 shootBeam ( )
591+
592+ if !running && !resetInProgress{
593+
594+ deathLabel. removeFromParent ( )
595+ deathLabel2. removeFromParent ( )
596+ finalScoreLabel. removeFromParent ( )
597+
598+ //reset()
599+
600+ assignNodeProperties ( )
601+ intro ( )
602+
603+ i = - 1
604+ self . song. clear ( )
605+
606+ //delay 6 seconds so intro has time to complete
607+ delay ( 6 ) {
608+ //setup & start generating the song
609+ self . song. setup ( level: self . currentLevel)
610+ self . generateSong ( )
611+ }
612+ }
570613 }
571614
572615 ////////////////////
0 commit comments