Skip to content

Commit f69dfd0

Browse files
committed
made the basic scoring and lives functionality
1 parent 2b7c6ca commit f69dfd0

File tree

8 files changed

+124
-96
lines changed

8 files changed

+124
-96
lines changed
0 Bytes
Binary file not shown.
Binary file not shown.

WWDC 2018 Debug Xcode Project/WWDC SUBMISSION 2018.playground/Sources/CODE DUMP.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
//Dump code that may or may not be used in this file
22
/*
3+
4+
import Foundation
5+
6+
public class Scoring {
7+
8+
//var queuedNote = Song().noteArray[0]
9+
10+
public func checkCorrect(noteString: String) {
11+
//var noteChecking = noteString
12+
13+
// if(Song().noteArray[0] == queuedNote) {
14+
// print("correct note")
15+
// } else {
16+
print("note was wrong")
17+
18+
// var note = song.noteArray.index(of:"\(noteString)")
19+
// queuedNote = song.noteArray[note!]
20+
21+
//print("next note up is \(queuedNote)")
22+
//}
23+
}
24+
25+
}
26+
27+
328
/*
429
public func didBegin(_ contact: SKPhysicsContact) {
530

WWDC 2018 Debug Xcode Project/WWDC SUBMISSION 2018.playground/Sources/Extensions.swift

Lines changed: 11 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ import SpriteKit
66
//song.addDelay(length: 1)
77
//etc...
88

9-
//Delays Animations
9+
//Delays animations
1010
public func delay(_ delay:Double, closure:@escaping ()->()) {
1111
let when = DispatchTime.now() + delay
1212
DispatchQueue.main.asyncAfter(deadline: when, execute: closure)
1313
}
1414

15+
//Spawns new note to the scene
1516
public func addNoteWithOptions(height: CGFloat, xPosition: CGFloat, in scene: SKScene) {
1617

17-
var newNote = SKShapeNode(rect: CGRect(x: 0, y: 0, width: 43.75, height: height))
18+
let newNote = SKShapeNode(rect: CGRect(x: 0, y: 0, width: 43.75, height: height))
1819

1920
newNote.fillColor = .white
2021
//center y is set to length so that the end of the collision works properly
@@ -35,7 +36,7 @@ public func addNoteWithOptions(height: CGFloat, xPosition: CGFloat, in scene: SK
3536
scene.addChild(newNote)
3637

3738
//start moving down the screen
38-
let move = SKAction.moveBy(x: 0, y: -1500, duration: 15)
39+
let move = SKAction.moveBy(x: 0, y: -1500, duration: 15/5)
3940
newNote.run(move)
4041

4142
//to maintain performance, delete note nodes after they leave the screen.
@@ -44,52 +45,11 @@ public func addNoteWithOptions(height: CGFloat, xPosition: CGFloat, in scene: SK
4445
}
4546
}
4647

47-
/*
48-
public extension SKScene {
49-
/*
50-
public func addShapeNodeWithPhysicsBody(ofRect rect: CGRect, gravityEnabled: Bool, collisionBitMask: UInt32, categoryBitMask: UInt32, contactTestBitMask: UInt32, dynamic: Bool) {
51-
52-
var node = SKShapeNode(rect: rect)
53-
54-
node.fillColor = .white
55-
56-
node.physicsBody = SKPhysicsBody(rectangleOf: node.frame.size)
57-
node.physicsBody!.isDynamic = dynamic
58-
node.physicsBody!.affectedByGravity = gravityEnabled
59-
60-
node.physicsBody!.categoryBitMask = categoryBitMask
61-
node.physicsBody!.collisionBitMask = collisionBitMask
62-
node.physicsBody!.contactTestBitMask = contactTestBitMask
63-
64-
self.addChild(node)
65-
} */
66-
67-
public func addNoteWithOptions(height: CGFloat, xPosition: CGFloat, in scene: SKScene) {
68-
69-
var newNote = SKShapeNode(rect: CGRect(x: 0, y: 0, width: 43.75, height: height))
70-
71-
newNote.fillColor = .white
72-
//center y is set to length so that the end of the collision works properly
73-
newNote.physicsBody = SKPhysicsBody(rectangleOf: newNote.frame.size)
74-
75-
newNote.physicsBody!.isDynamic = false
76-
newNote.physicsBody!.affectedByGravity = false
77-
newNote.physicsBody!.usesPreciseCollisionDetection = true
78-
79-
newNote.physicsBody!.categoryBitMask = NoteCategory
80-
newNote.physicsBody!.collisionBitMask = 0
81-
newNote.physicsBody!.contactTestBitMask = 0x0
82-
83-
self.scene.addChild(newNote)
84-
85-
//start moving down the screen
86-
let move = SKAction.moveBy(x: 0, y: -1500, duration: 15)
87-
newNote.run(move)
88-
89-
//to maintain performance, delete note nodes after they leave the screen.
90-
DispatchQueue.main.asyncAfter(deadline: .now() + 15) {
91-
newNote.removeFromParent()
92-
}
93-
}
94-
} */
48+
struct PhysicsCategory {
49+
static let None : UInt32 = 0
50+
static let Bullet : UInt32 = 0b1 // 1
51+
static let Ship : UInt32 = 0b10 // 2
52+
static let Note : UInt32 = 0b100 // 3
53+
}
54+
9555

WWDC 2018 Debug Xcode Project/WWDC SUBMISSION 2018.playground/Sources/GameScene.swift

Lines changed: 81 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@ import Foundation
22
import SpriteKit
33

44
public 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)

WWDC 2018 Debug Xcode Project/WWDC SUBMISSION 2018.playground/Sources/Song.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ public class Song {
55
public var songArray = [(String, Int, Double)]()
66

77
//array of all the playable notes
8-
public var noteArray = [(String, Int, Double)]()
8+
public var noteArray = [String]()
99

1010
public func addNote(note: String, octave: Int, length: Double) {
1111
songArray.append((note,octave,length))
12-
//noteArray.append((note,octave,length))
12+
//noteArray.append(note)
1313
//print function for testing purposes
1414
//print(songArray)
1515
}

0 commit comments

Comments
 (0)