@@ -28,12 +28,12 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
2828 private let deathLabel2 = SKLabelNode ( text: " Click anywhere to try again. " )
2929 private let finalScoreLabel = SKLabelNode ( text: " Score: " )
3030
31- //Scoring
31+ //Scoring & levels
3232 private var lives = 5
3333 private var score = 0
34-
3534 private var currentLevel = 1
3635
36+ //this node detects if notes hit the bottom of the screen
3737 private var bottomDetector : SKShapeNode !
3838
3939 private var running = false
@@ -52,6 +52,7 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
5252
5353 public override func didMove( to view: SKView ) {
5454 setupNodes ( )
55+ assignNodeProperties ( )
5556 intro ( )
5657
5758 //delay 6 seconds so intro has time to complete
@@ -172,13 +173,42 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
172173 bottomDetector. physicsBody!. categoryBitMask = PhysicsCategory . Bottom
173174 bottomDetector. physicsBody!. contactTestBitMask = PhysicsCategory . Note
174175 bottomDetector. physicsBody!. affectedByGravity = false
176+
177+ }
178+
179+ //resets the game if you die
180+ func resetGame( ) {
181+
182+ //go back to the first level
183+ currentLevel = 1
184+
185+ //reset lives
186+ lives = 5
187+
188+ //remove the death menu labels
189+ deathLabel. removeFromParent ( )
190+ deathLabel2. removeFromParent ( )
191+ finalScoreLabel. removeFromParent ( )
175192
193+ //set the noeds back to thier starting posistions
176194 assignNodeProperties ( )
195+ //play intro
196+ intro ( )
197+
198+ //reset song array
199+ i = - 1
200+ self . song. clear ( )
201+
202+ //delay 6 seconds so intro has time to complete
203+ delay ( 6 ) {
204+ //setup & start generating the song
205+ self . song. setup ( level: self . currentLevel)
206+ self . generateSong ( )
207+ }
177208 }
178209
179210 //intro sequence when game starts
180211 func intro( ) {
181-
182212 score = 0
183213 scoreLabel. text = " \( score) "
184214
@@ -388,24 +418,21 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
388418 //bullet hit a note:
389419 if nodeBitmasks. contains ( PhysicsCategory . Note) && nodeBitmasks. contains ( PhysicsCategory . Bullet) {
390420
421+ //this big if-else statement essentially finds the x pos. of the collision and plays the right note
391422 if ( contact. bodyA. categoryBitMask == PhysicsCategory . Note) {
392-
393423 if let note = contact. bodyA. node {
394424 //access the length of the note we hit to play the correct sound
395425 let input = Int ( round ( note. position. x * 20 ) / 20 )
396426 let length = Double ( ( 10 * round( note. frame. size. height / 10.0 ) ) )
397427 Sound ( input: input, length: length) . playSound ( in: self )
398428 }
399-
400429 } else {
401-
402430 if let note = contact. bodyB. node {
403431 //access the length of the note we hit to play the correct sound
404432 let input = Int ( round ( note. position. x * 20 ) / 20 )
405433 let length = Double ( ( 10 * round( note. frame. size. height / 10.0 ) ) )
406434 Sound ( input: input, length: length) . playSound ( in: self )
407435 }
408-
409436 }
410437
411438 //add to the score & update label
@@ -436,7 +463,6 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
436463
437464 //take off one life
438465 adjustLives ( )
439-
440466 }
441467 }
442468
@@ -469,9 +495,7 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
469495
470496 running = false
471497 resetInProgress = false
472-
473- currentLevel = 1
474-
498+
475499 //remove nodes
476500 ship. isHidden = true
477501 scoreTextLabel. isHidden = true
@@ -517,6 +541,7 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
517541 //ANIMATE THIS LATER
518542 func winScreen( ) {
519543
544+ //stop user interation, but reset is not enabled because you can't reset after a win
520545 running = false
521546 resetInProgress = true
522547
@@ -529,8 +554,7 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
529554 life3. isHidden = true
530555 life4. isHidden = true
531556 life5. isHidden = true
532-
533- //ship.removeFromParent()
557+
534558 booster1. removeFromParent ( )
535559 booster2. removeFromParent ( )
536560 booster3. removeFromParent ( )
@@ -541,8 +565,7 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
541565 child. removeFromParent ( )
542566 }
543567 }
544-
545- //grow from center??
568+
546569 let winLabel = SKLabelNode ( text: " You Won! " )
547570 winLabel. fontName = " Helvetica Neue Light "
548571 winLabel. fontSize = 65
@@ -571,7 +594,7 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
571594 override public func mouseMoved( with event: NSEvent ) {
572595 let location = event. location ( in: self )
573596
574- //only move the ship if the game is still running
597+ //only move the ship if user interation is enabled
575598 if running {
576599 // move ship to mouse (only x values)
577600 ship. position. x = location. x
@@ -590,25 +613,7 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
590613 shootBeam ( )
591614
592615 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- }
616+ resetGame ( )
612617 }
613618 }
614619
0 commit comments