Skip to content

Commit 6f19ff7

Browse files
committed
added a basic death screen
1 parent 30114b6 commit 6f19ff7

File tree

4 files changed

+98
-49
lines changed

4 files changed

+98
-49
lines changed

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

Lines changed: 97 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
2525
//Scoring
2626
private var lives = 3
2727
private var score = 0
28+
29+
private var running = false
2830

2931
//array of all contacts to be handled in the next frame
3032
var contactQueue = [SKPhysicsContact]()
@@ -154,6 +156,11 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
154156
self.life3.run(moveLives)
155157
self.scoreLabel.run(moveLives)
156158
self.scoreTextLabel.run(moveLives)
159+
160+
//after these animations complete, start the game
161+
delay(2){
162+
self.running = true
163+
}
157164
}
158165
}
159166

@@ -201,11 +208,13 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
201208
bullet.position.y = 7.5
202209
bullet.position.x = 7.5
203210

204-
beam.addChild(bullet)
205-
scene?.addChild(beam)
211+
if running {
212+
beam.addChild(bullet)
213+
scene?.addChild(beam)
206214

207-
//shoot the bullet
208-
beam.physicsBody!.applyImpulse(CGVector(dx: 0.0, dy: 10.0))
215+
//shoot the bullet
216+
beam.physicsBody!.applyImpulse(CGVector(dx: 0.0, dy: 10.0))
217+
}
209218
}
210219

211220
public func prepareNoteForSpawn(note: String, length: Double) {
@@ -270,9 +279,12 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
270279
let newNote = SKShapeNode(rect: CGRect(x: 0, y: height/2, width: 43.75, height: height))
271280

272281
newNote.fillColor = .white
282+
newNote.name = "note"
283+
273284
//center y is set to length so that the end of the collision works properly
274285
newNote.physicsBody = SKPhysicsBody(rectangleOf: newNote.frame.size)
275286

287+
//set other physics properties
276288
newNote.physicsBody!.isDynamic = false
277289
newNote.physicsBody!.affectedByGravity = false
278290
newNote.physicsBody!.usesPreciseCollisionDetection = true
@@ -291,10 +303,12 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
291303
let move = SKAction.moveBy(x: 0, y: -1500, duration: 2)
292304
newNote.run(move)
293305

306+
294307
//to maintain performance, delete note nodes after they leave the screen.
295308
delay(2) {
296309
newNote.removeFromParent()
297310
}
311+
298312
}
299313

300314
/////////////////////////////
@@ -350,26 +364,75 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
350364

351365
//a note hit the ship:
352366
if nodeBitmasks.contains(PhysicsCategory.Ship) && nodeBitmasks.contains(PhysicsCategory.Note) {
367+
368+
//ANIMATE LATER??
369+
370+
//make sure we delete the note and not the ship
371+
if(contact.bodyA.categoryBitMask == PhysicsCategory.Note) {
372+
contact.bodyA.node!.removeFromParent()
373+
} else {
374+
contact.bodyB.node!.removeFromParent()
375+
}
376+
353377
//adjust lives
354378
if (lives > 1) {
355379
//lost life sound effect
356380
lives = lives - 1
357381
if lives == 2 {
382+
//animate?
358383
//lost life sound effect
359384
life3.removeFromParent()
360385
} else if lives == 1 {
361386
//lost life sound effect
387+
//animate?
362388
life2.removeFromParent()
363389
}
364390
} else {
365391
life1.removeFromParent()
366392
//death screen
367-
//death sound effect
393+
deathScreen()
394+
368395
print("he DEAD")
369396
}
370397
}
371398
}
372399

400+
func deathScreen() {
401+
running = false
402+
//death sound effect
403+
404+
//death animation
405+
ship.removeFromParent()
406+
scoreTextLabel.removeFromParent()
407+
scoreLabel.removeFromParent()
408+
409+
booster1.removeFromParent()
410+
booster2.removeFromParent()
411+
booster3.removeFromParent()
412+
413+
// Removing existing notes
414+
for child in self.children {
415+
if child.name == "note" {
416+
child.removeFromParent()
417+
}
418+
}
419+
420+
let deathLabel = SKLabelNode(text: "You Died!")
421+
deathLabel.fontName = "Helvetica Neue Light"
422+
deathLabel.fontSize = 65
423+
deathLabel.fontColor = .white
424+
deathLabel.position = CGPoint(x: 0, y : 0)
425+
426+
let deathLabel2 = SKLabelNode(text: "Re-run to try again.")
427+
deathLabel2.fontName = "Helvetica Neue Light"
428+
deathLabel2.fontSize = 30
429+
deathLabel2.fontColor = .white
430+
deathLabel2.position = CGPoint(x: 0, y : -40)
431+
432+
scene?.addChild(deathLabel)
433+
scene?.addChild(deathLabel2)
434+
}
435+
373436
//when we detect a collision, add it to our queue to be handled in the next frame.
374437
public func didBegin(_ contact: SKPhysicsContact) {
375438
contactQueue.append(contact)
@@ -381,15 +444,19 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
381444

382445
override public func mouseMoved(with event: NSEvent) {
383446
let location = event.location(in:self)
384-
// move ship to mouse (only x values)
385-
ship.position.x = location.x
386-
//move the boosters along with the ship (keeping thier relative posistions)
387-
booster1.position.x = ship.position.x + 50
388-
booster1.position.y = ship.position.y
389-
booster2.position.x = ship.position.x - 50
390-
booster2.position.y = ship.position.y
391-
booster3.position.x = ship.position.x
392-
booster3.position.y = ship.position.y
447+
448+
//only move the ship if the game is still running
449+
if running {
450+
// move ship to mouse (only x values)
451+
ship.position.x = location.x
452+
//move the boosters along with the ship (keeping thier relative posistions)
453+
booster1.position.x = ship.position.x + 50
454+
booster1.position.y = ship.position.y
455+
booster2.position.x = ship.position.x - 50
456+
booster2.position.y = ship.position.y
457+
booster3.position.x = ship.position.x
458+
booster3.position.y = ship.position.y
459+
}
393460
}
394461

395462
public override func mouseDown(with event: NSEvent) {
@@ -518,19 +585,22 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
518585
var i = -1
519586
//this function takes the array of notes from the Song class and prepares to spawn them into the scene
520587
public func generateSong() {
521-
i = i + 1
522-
if i < (song.songArray.count ) {
523-
if ((song.songArray[i]).0) == "N/A" {
524-
//delay the next iteration by delay amount
525-
delay(Double((song.songArray[i]).1)) {
526-
self.generateSong()
527-
}
528-
} else {
529-
//spawn note
530-
prepareNoteForSpawn(note: ((song.songArray[i]).0), length: ((song.songArray[i]).1))
531-
//delay the next iteration by length of not playing
532-
delay(Double((song.songArray[i]).1)/2) {
533-
self.generateSong()
588+
//only generate the song if the game is not over
589+
if running {
590+
i = i + 1
591+
if i < (song.songArray.count ) {
592+
if ((song.songArray[i]).0) == "N/A" {
593+
//delay the next iteration by delay amount
594+
delay(Double((song.songArray[i]).1)) {
595+
self.generateSong()
596+
}
597+
} else {
598+
//spawn note
599+
prepareNoteForSpawn(note: ((song.songArray[i]).0), length: ((song.songArray[i]).1))
600+
//delay the next iteration by length of not playing
601+
delay(Double((song.songArray[i]).1)/2) {
602+
self.generateSong()
603+
}
534604
}
535605
}
536606
}

WWDC 2018 Debug Xcode Project/WWDC SUBMISSION 2018.playground/Sources/Intro View.swift

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,6 @@ public class IntroView: SKScene {
1111
public override func didMove(to view: SKView) {
1212

1313
}
14-
/*override public func mouseMoved(with event: NSEvent) {
15-
let location = event.location(in:self)
16-
// move ship to mouse (only x values)
17-
ship.position.x = location.x
18-
//move the boosters along with the ship (keeping thier relative posistions)
19-
booster1.position.x = ship.position.x + 50
20-
booster1.position.y = ship.position.y
21-
booster2.position.x = ship.position.x - 50
22-
booster2.position.y = ship.position.y
23-
booster3.position.x = ship.position.x
24-
booster3.position.y = ship.position.y
25-
}*/
26-
27-
public override func mouseDown(with event: NSEvent) {
28-
//shoots on click
29-
print("click")
30-
self.backgroundColor = .clear
31-
}
32-
33-
// called when user touches anywhere on the view and dismisses it via animation.
34-
3514
}
3615

3716

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ public class Song {
1212
//adds a rest in the song array, note is assigned "N/A" which will be recognised as a delay
1313
public func addDelay(length: Double) {
1414
songArray.append(("N/A",length))
15-
}
15+
}
1616
}

0 commit comments

Comments
 (0)