@@ -3,7 +3,7 @@ import SpriteKit
33
44public class GameScene : SKScene , SKPhysicsContactDelegate {
55
6- //nodes
6+ //nodes:
77 private var ship : SKSpriteNode !
88
99 private var life1 : SKShapeNode !
@@ -36,8 +36,8 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
3636 //this node detects if notes hit the bottom of the screen
3737 private var bottomDetector : SKShapeNode !
3838
39- private var running = false
40- private var resetInProgress = true
39+ private var userInterationEnabled = false
40+ private var resettingEnabled = false
4141
4242 //array of all contacts to be handled in the next frame
4343 var contactQueue = [ SKPhysicsContact] ( )
@@ -51,8 +51,11 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
5151 }
5252
5353 public override func didMove( to view: SKView ) {
54+ //initialzise & setup node properties
5455 setupNodes ( )
5556 assignNodeProperties ( )
57+
58+ //play the intro animation
5659 intro ( )
5760
5861 //delay 6 seconds so intro has time to complete
@@ -62,9 +65,6 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
6265 self . generateSong ( )
6366 }
6467
65- //start w/ 3 lives
66- lives = 5
67-
6868 //setup physics
6969 physicsWorld. gravity = CGVector . zero
7070 physicsWorld. contactDelegate = self
@@ -208,8 +208,11 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
208208
209209 //intro sequence when game starts
210210 func intro( ) {
211+
212+ //reset score and lives (since this function is also called to reset the game)
211213 score = 0
212214 scoreLabel. text = " \( score) "
215+ lives = 5
213216
214217 //add the emitters to the ship
215218 scene? . addChild ( booster1)
@@ -236,14 +239,14 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
236239 self . scoreLabel. run ( moveLives)
237240 self . scoreTextLabel. run ( moveLives)
238241
239- //after these animations complete, start running the game
242+ //after these animations complete, enable user interaction
240243 delay ( 2 ) {
241- self . running = true
244+ self . userInterationEnabled = true
242245 }
243246 }
244247 }
245248
246- //animation that play between levels
249+ //animation that plays between levels
247250 func levelAnimation( level: String , song: String ) {
248251 //move the labels back to the bottom
249252 levelLabel. position. y = - 500
@@ -290,8 +293,8 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
290293 bullet. position. y = 7.5
291294 bullet. position. x = 7.5
292295
293- //only shoot bullets if the game is running
294- if running {
296+ //only shoot bullets if the game is userInterationEnabled
297+ if userInterationEnabled {
295298 beam. addChild ( bullet)
296299 scene? . addChild ( beam)
297300
@@ -345,18 +348,18 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
345348 print ( " this shouldn't happen " )
346349 }
347350
348- //is note checks if the note being created is not a rest
351+ //isNote checks if the note being created is not a rest
349352 if ( isNote) {
350353 //convert length (in beats) into height of note (1 beat is 100px in length)
351354 let noteHeight = length * Double( 100 )
352355
353356 //add the note to the scene
354- addNoteWithOptions ( height: CGFloat ( noteHeight) , xPosition: CGFloat ( x) , in : self )
357+ addNoteWithOptions ( height: CGFloat ( noteHeight) , xPosition: CGFloat ( x) )
355358 }
356359 }
357360
358361 //Spawns new note to the scene
359- public func addNoteWithOptions( height: CGFloat , xPosition: CGFloat , in scene : SKScene ) {
362+ public func addNoteWithOptions( height: CGFloat , xPosition: CGFloat ) {
360363
361364 let newNote = SKShapeNode ( rect: CGRect ( x: 0 , y: height/ 2 , width: 43.75 , height: height) )
362365
@@ -379,7 +382,7 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
379382 newNote. position. x = CGFloat ( xPosition)
380383 newNote. position. y = 500
381384
382- scene. addChild ( newNote)
385+ scene? . addChild ( newNote)
383386
384387 //start moving down the screen
385388 let move = SKAction . moveBy ( x: 0 , y: - 1500 , duration: 4 )
@@ -417,7 +420,7 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
417420 //bullet hit a note:
418421 if nodeBitmasks. contains ( PhysicsCategory . Note) && nodeBitmasks. contains ( PhysicsCategory . Bullet) {
419422
420- //this big if-else statement essentially finds the x pos. of the collision and plays the right note
423+ //this big if-else statement essentially finds the x pos. of the collision and plays the right note by calling the Sound.playSound() function
421424 if ( contact. bodyA. categoryBitMask == PhysicsCategory . Note) {
422425 if let note = contact. bodyA. node {
423426 //access the length of the note we hit to play the correct sound
@@ -469,33 +472,26 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
469472 lives = lives - 1
470473 switch lives {
471474 case 4 :
472- // life5.removeFromParent()
473475 life5. isHidden = true
474476 case 3 :
475- // life4.removeFromParent()
476477 life4. isHidden = true
477478 case 2 :
478- // life3.removeFromParent()
479479 life3. isHidden = true
480480 case 1 :
481- // life2.removeFromParent()
482481 life2. isHidden = true
483482 case 0 :
484- //life1.removeFromParent()
485483 life1. isHidden = true
486484 deathScreen ( )
487485 default :
488486 print ( " this should not happen " )
489487 }
490488 }
491489
492- //ANIMATE THIS LATER
493490 func deathScreen( ) {
494-
495- running = false
496- resetInProgress = false
491+ userInterationEnabled = false
492+ resettingEnabled = true
497493
498- //remove nodes
494+ //hide nodes
499495 ship. isHidden = true
500496 scoreTextLabel. isHidden = true
501497 scoreLabel. isHidden = true
@@ -516,6 +512,7 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
516512 }
517513 }
518514
515+ //display the death labels
519516 deathLabel. fontName = " Helvetica Neue Light "
520517 deathLabel. fontSize = 65
521518 deathLabel. fontColor = . white
@@ -537,12 +534,11 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
537534 scene? . addChild ( finalScoreLabel)
538535 }
539536
540- //ANIMATE THIS LATER
541537 func winScreen( ) {
542538
543539 //stop user interation, but reset is not enabled because you can't reset after a win
544- running = false
545- resetInProgress = true
540+ userInterationEnabled = false
541+ resettingEnabled = false
546542
547543 //hide all nodes
548544 ship. isHidden = true
@@ -565,6 +561,7 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
565561 }
566562 }
567563
564+ //display win labels
568565 let winLabel = SKLabelNode ( text: " You Won! " )
569566 winLabel. fontName = " Helvetica Neue Light "
570567 winLabel. fontSize = 65
@@ -594,7 +591,7 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
594591 let location = event. location ( in: self )
595592
596593 //only move the ship if user interation is enabled
597- if running {
594+ if userInterationEnabled {
598595 // move ship to mouse (only x values)
599596 ship. position. x = location. x
600597 //move the boosters along with the ship (keeping thier relative posistions)
@@ -611,7 +608,8 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
611608 //shoots on click
612609 shootBeam ( )
613610
614- if !running && !resetInProgress{
611+ //reset game if we're in the death menu & click
612+ if !userInterationEnabled && resettingEnabled{
615613 resetGame ( )
616614 }
617615 }
@@ -621,13 +619,15 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
621619 ////////////////////
622620
623621 let song = Song ( )
622+
623+ //this will keep track of how far we are in the song:
624624 var i = - 1
625625
626626 //this function takes the array of notes from the Song class and prepares to spawn them into the scene
627627 public func generateSong( ) {
628628
629629 //only generate the song if the game is not over
630- if running {
630+ if userInterationEnabled {
631631
632632 i = i + 1
633633 if i < song. songArray. count {
@@ -642,7 +642,6 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
642642
643643 //it was the end of the song
644644 else if ( ( song. songArray [ i] ) . 0 ) == " end " {
645-
646645 //play the next level
647646 currentLevel += 1
648647
@@ -652,7 +651,7 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
652651 delay ( 2 ) {
653652 //play level animation
654653 self . levelAnimation ( level: " \( self . currentLevel) " , song: songTitles [ self . currentLevel - 1 ] )
655- //clear songArray and repopulate with new song
654+ //clear songArray and re-populate with new song
656655 self . song. clear ( )
657656 self . song. setup ( level: self . currentLevel)
658657 //reset index
@@ -663,15 +662,10 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
663662 self . generateSong ( )
664663 }
665664 }
666- }
667-
668- //if song title doesn't exist; you either won the game or something went wrong
669- else {
670- //you probably won, but double check
671- if currentLevel > 4 /*maxLevels*/ {
672- //win animation will go here.
665+ } else {
666+ //if song title doesn't exist; you either won the game or something went wrong you probably won, but double check
667+ if currentLevel > 4 {
673668 delay ( 2 ) {
674- print ( " you completed the last level! WIN!! " )
675669 self . winScreen ( )
676670 }
677671 } else {
@@ -683,7 +677,8 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
683677 } else {
684678 //spawn note
685679 prepareNoteForSpawn ( note: ( ( song. songArray [ i] ) . 0 ) , length: ( ( song. songArray [ i] ) . 1 ) )
686- //delay the next iteration by length of not playing
680+
681+ //delay the next iteration by length of note
687682 delay ( Double ( ( song. songArray [ i] ) . 1 ) / 2 ) {
688683 self . generateSong ( )
689684 }
@@ -697,6 +692,7 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
697692 ///////////////////////
698693
699694 public override func update( _ currentTime: TimeInterval ) {
695+ //procces all the contacts that were detected last frame
700696 processContacts ( forUpdate: currentTime)
701697 }
702698}
0 commit comments