@@ -2,16 +2,19 @@ import Foundation
22import SpriteKit
33
44public class GameScene : SKScene , SKPhysicsContactDelegate {
5+
56 //Nodes
67 private var ship : SKSpriteNode !
78 private var noteSpawner : SKSpriteNode !
89 private var life1 : SKSpriteNode !
910 private var life2 : SKSpriteNode !
1011 private var life3 : SKSpriteNode !
12+ private var scoreLabel : SKLabelNode !
1113
1214 //Scoring
13- private var lives : Int !
14-
15+ private var lives = 3
16+ private var score = 0
17+
1518 //Physics Global Vars
1619 let BulletCategory : UInt32 = 0x1 << 0
1720 let ShipCategory : UInt32 = 0x1 << 1
@@ -20,33 +23,45 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
2023 //array of all contacts to be handled in the next frame
2124 var contactQueue = [ SKPhysicsContact] ( )
2225
26+
2327 public override func sceneDidLoad( ) {
2428 size = CGSize ( width: 700 , height: 1000 )
2529 }
2630
2731 public override func didMove( to view: SKView ) {
28- //setup & start playing the song
32+ //setup & start generating the song
2933 setupSong ( )
30- playSong ( )
34+ generateSong ( )
3135
36+ setupNodes ( )
37+
3238 //start w/ 3 lives
3339 lives = 3
3440
35- //setup nodes
41+ physicsWorld. gravity = CGVector . zero
42+ physicsWorld. contactDelegate = self
43+ }
44+
45+ //Sets up nodes at beginning of game
46+ func setupNodes( ) {
3647 ship = self . scene? . childNode ( withName: " ship " ) as? SKSpriteNode
3748 noteSpawner = self . scene? . childNode ( withName: " noteSpawner " ) as? SKSpriteNode
38-
49+
3950 life1 = self . scene? . childNode ( withName: " life1 " ) as? SKSpriteNode
4051 life2 = self . scene? . childNode ( withName: " life2 " ) as? SKSpriteNode
4152 life3 = self . scene? . childNode ( withName: " life3 " ) as? SKSpriteNode
53+ scoreLabel = self . scene? . childNode ( withName: " score " ) as? SKLabelNode
4254
4355 ship. physicsBody = SKPhysicsBody ( rectangleOf: ship. frame. size)
44- ship. physicsBody!. collisionBitMask = 0
4556 ship. physicsBody!. affectedByGravity = false
57+
58+ ship. physicsBody!. collisionBitMask = PhysicsCategory . None
59+ ship. physicsBody!. categoryBitMask = PhysicsCategory . Ship
60+ ship. physicsBody!. contactTestBitMask = PhysicsCategory . Note
4661
4762 let starsPath = Bundle . main. path ( forResource: " stars " , ofType: " sks " ) !
4863 let stars = NSKeyedUnarchiver . unarchiveObject ( withFile: starsPath) as! SKEmitterNode
49-
64+
5065 stars. position. y = 500
5166 stars. targetNode = self
5267
@@ -59,9 +74,6 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
5974 asteroids. targetNode = self
6075
6176 self . scene? . addChild ( asteroids)
62-
63- physicsWorld. gravity = CGVector . zero
64- physicsWorld. contactDelegate = self
6577 }
6678
6779 func shootBeam( ) {
@@ -74,7 +86,7 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
7486 beam. physicsBody!. affectedByGravity = false
7587
7688 beam. physicsBody!. categoryBitMask = BulletCategory
77- beam. physicsBody!. collisionBitMask = 0
89+ beam. physicsBody!. collisionBitMask = PhysicsCategory . None
7890 beam. physicsBody!. contactTestBitMask = NoteCategory
7991
8092 beam. physicsBody!. usesPreciseCollisionDetection = true
@@ -89,6 +101,8 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
89101 }
90102
91103 public func spawnNote( note: String , octave: Int , length: Double ) {
104+
105+ let noteWidth = 43.75
92106 var noteHeight : Double
93107 var x : Double = 0
94108
@@ -145,28 +159,68 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
145159 //MARK:Physics and Contacts//
146160 /////////////////////////////
147161
148- public func setupPhysics( ) {
149- //do I even need this?
162+ func processContacts( forUpdate currentTime: CFTimeInterval ) {
163+ for contact in contactQueue {
164+ handle ( contact)
165+
166+ if let index = contactQueue. index ( of: contact) {
167+ contactQueue. remove ( at: index)
168+ }
169+ }
150170 }
151-
171+
152172 public func handle( _ contact: SKPhysicsContact ) {
153173 if contact. bodyA. node? . parent == nil || contact. bodyB. node? . parent == nil {
154174 return
155175 }
156176
157177 let nodeBitmasks = [ contact. bodyA. categoryBitMask, contact. bodyB. categoryBitMask]
158178
159- if nodeBitmasks. contains ( NoteCategory) && nodeBitmasks. contains ( BulletCategory) {
160- //bullet hit note
161-
179+ if nodeBitmasks. contains ( PhysicsCategory . Note) && nodeBitmasks. contains ( PhysicsCategory . Bullet) {
180+ //bullet hit a note
181+
182+ //access the length of the note we hit to play the correct sound
162183 if let note = contact. bodyA. node {
163- let length = ( 10 * round( Double ( note. frame. size. height / 10.0 ) ) )
184+ let length = Double ( ( 10 * round( note. frame. size. height / 10.0 ) ) )
164185 Sound ( input: Double ( note. position. x) , length: length) . playSound ( in: self )
165186 }
187+
188+ score += 1
189+ scoreLabel. text = " \( score) "
190+ print ( score)
191+
166192 contact. bodyA. node!. removeFromParent ( )
167193 contact. bodyB. node!. removeFromParent ( )
168194 }
195+
196+ if nodeBitmasks. contains ( PhysicsCategory . Ship) && nodeBitmasks. contains ( PhysicsCategory . Note) {
197+ //a note hit the ship.
198+
199+ //contact.bodyA.node!.removeFromParent()
200+ //contact.bodyB.node!.removeFromParent()
201+
202+ if ( contact. bodyA. categoryBitMask == PhysicsCategory . Note) {
203+ contact. bodyA. node!. removeFromParent ( )
204+ } else {
205+ contact. bodyB. node!. removeFromParent ( )
206+ }
207+
208+ //handle lives
209+ if ( lives > 1 ) {
210+ lives = lives - 1
211+ print ( lives)
212+ if lives == 2 {
213+ //life3.removeFromParent()
214+ } else if lives == 1 {
215+ //life2.removeFromParent()
216+ }
217+ } else {
218+ // life1.removeFromParent()
219+ print ( " he DEAD " )
220+ }
221+ }
169222 }
223+
170224
171225 public func didBegin( _ contact: SKPhysicsContact ) {
172226 contactQueue. append ( contact)
@@ -178,10 +232,11 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
178232
179233 //FIX THIS
180234
181- var notesPlayed = 0
182- var nextNote : String !
183- var targetNote : String !
235+ // var notesPlayed = 0
236+ // var nextNote : String!
237+ // var targetNote : String!
184238
239+ /*
185240 public func checkCorrect(noteToCheck note: String) {
186241 //not working b/c multiple nodes are hitting the same note
187242 if(note == (song.noteArray[notesPlayed]).0) {
@@ -190,7 +245,7 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
190245 //print("wrong")
191246 }
192247 notesPlayed += 1
193- }
248+ } */
194249
195250 //////////////////////////////
196251 //MARK: Mouse Event Handlers//
@@ -243,21 +298,21 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
243298 }
244299 }
245300
246- public func playSong ( ) {
301+ public func generateSong ( ) {
247302 //because of the functionailty of my delay function, a for loop would not work properly
248303 i = i + 1
249304 if i < ( song. songArray. count - 1 ) {
250305 if ( ( song. songArray [ i] ) . 0 ) == " N/A " {
251306 //delay here
252307 delay ( Double ( ( song. songArray [ i] ) . 2 ) ) {
253- self . playSong ( )
308+ self . generateSong ( )
254309 }
255310 } else {
256311 //spawn note
257312 spawnNote ( note: ( ( song. songArray [ i] ) . 0 ) , octave: ( ( song. songArray [ i] ) . 1 ) , length: ( ( song. songArray [ i] ) . 2 ) )
258313 //delay
259314 delay ( Double ( ( song. songArray [ i] ) . 2 ) ) {
260- self . playSong ( )
315+ self . generateSong ( )
261316 }
262317 }
263318 }
@@ -266,16 +321,6 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
266321 ///////////////////////
267322 //MARK: Update Frames//
268323 ///////////////////////
269-
270- func processContacts( forUpdate currentTime: CFTimeInterval ) {
271- for contact in contactQueue {
272- handle ( contact)
273-
274- if let index = contactQueue. index ( of: contact) {
275- contactQueue. remove ( at: index)
276- }
277- }
278- }
279324
280325 public override func update( _ currentTime: TimeInterval ) {
281326 processContacts ( forUpdate: currentTime)
0 commit comments