Skip to content

Commit 43a2a53

Browse files
committed
made some UI improvements
1 parent f69dfd0 commit 43a2a53

File tree

7 files changed

+87
-36
lines changed

7 files changed

+87
-36
lines changed
0 Bytes
Binary file not shown.
Binary file not shown.
-17.7 KB
Loading

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

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

9+
//Where should I put these?
10+
911
//Delays animations
1012
public func delay(_ delay:Double, closure:@escaping ()->()) {
1113
let when = DispatchTime.now() + delay
@@ -25,9 +27,9 @@ public func addNoteWithOptions(height: CGFloat, xPosition: CGFloat, in scene: SK
2527
newNote.physicsBody!.affectedByGravity = false
2628
newNote.physicsBody!.usesPreciseCollisionDetection = true
2729

28-
newNote.physicsBody!.categoryBitMask = 0x1 << 2
29-
newNote.physicsBody!.collisionBitMask = 0
30-
newNote.physicsBody!.contactTestBitMask = 0x0
30+
newNote.physicsBody!.categoryBitMask = PhysicsCategory.Note
31+
newNote.physicsBody!.collisionBitMask = PhysicsCategory.None
32+
newNote.physicsBody!.contactTestBitMask = PhysicsCategory.Ship
3133

3234
//change to CGPoint later; easier to read this way for now
3335
newNote.position.x = CGFloat(xPosition)

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

Lines changed: 81 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,23 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
55

66
//Nodes
77
private var ship: SKSpriteNode!
8-
private var noteSpawner: SKSpriteNode!
8+
99
private var life1: SKSpriteNode!
1010
private var life2: SKSpriteNode!
1111
private var life3: SKSpriteNode!
1212
private var scoreLabel : SKLabelNode!
13-
13+
14+
private var booster1 : SKEmitterNode!
15+
private var booster2 : SKEmitterNode!
16+
private var booster3 : SKEmitterNode!
17+
1418
//Scoring
1519
private var lives = 3
1620
private var score = 0
1721

18-
//Physics Global Vars
19-
let BulletCategory: UInt32 = 0x1 << 0
20-
let ShipCategory: UInt32 = 0x1 << 1
21-
let NoteCategory: UInt32 = 0x1 << 2
22-
2322
//array of all contacts to be handled in the next frame
2423
var contactQueue = [SKPhysicsContact]()
25-
26-
24+
2725
public override func sceneDidLoad() {
2826
size = CGSize(width: 700, height: 1000)
2927
}
@@ -44,21 +42,51 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
4442

4543
//Sets up nodes at beginning of game
4644
func setupNodes() {
45+
//ship node
4746
ship = self.scene?.childNode(withName: "ship") as? SKSpriteNode
48-
noteSpawner = self.scene?.childNode(withName: "noteSpawner") as? SKSpriteNode
47+
ship.physicsBody = SKPhysicsBody(rectangleOf: ship.frame.size)
48+
ship.physicsBody!.collisionBitMask = PhysicsCategory.None
49+
ship.physicsBody!.categoryBitMask = PhysicsCategory.Ship
50+
ship.physicsBody!.contactTestBitMask = PhysicsCategory.Note
51+
ship.physicsBody!.affectedByGravity = false
52+
//ship.physicsBody!.isDynamic = false
53+
ship.zPosition = 100
4954

5055
life1 = self.scene?.childNode(withName: "life1") as? SKSpriteNode
5156
life2 = self.scene?.childNode(withName: "life2") as? SKSpriteNode
5257
life3 = self.scene?.childNode(withName: "life3") as? SKSpriteNode
5358
scoreLabel = self.scene?.childNode(withName: "score") as? SKLabelNode
5459

55-
ship.physicsBody = SKPhysicsBody(rectangleOf: ship.frame.size)
56-
ship.physicsBody!.affectedByGravity = false
57-
58-
ship.physicsBody!.collisionBitMask = PhysicsCategory.None
59-
ship.physicsBody!.categoryBitMask = PhysicsCategory.Ship
60-
ship.physicsBody!.contactTestBitMask = PhysicsCategory.Note
60+
//ship booster effects
61+
let boosterPath = Bundle.main.path(forResource: "booster", ofType: "sks")!
62+
booster1 = NSKeyedUnarchiver.unarchiveObject(withFile: boosterPath) as! SKEmitterNode
63+
64+
booster1.position.x = ship.position.x + 50
65+
booster1.position.y = ship.position.y - 30
66+
booster1.particleScale = 0.1
67+
booster1.targetNode = self
68+
69+
self.scene?.addChild(booster1)
70+
71+
booster2 = NSKeyedUnarchiver.unarchiveObject(withFile: boosterPath) as! SKEmitterNode
72+
73+
booster2.position.x = ship.position.x - 50
74+
booster2.position.y = ship.position.y - 30
75+
booster2.particleScale = 0.1
76+
booster2.targetNode = self
77+
78+
self.scene?.addChild(booster2)
79+
80+
booster3 = NSKeyedUnarchiver.unarchiveObject(withFile: boosterPath) as! SKEmitterNode
81+
82+
booster3.position.x = ship.position.x
83+
booster3.position.y = ship.position.y - 30
84+
booster3.particleScale = 0.3
85+
booster3.targetNode = self
6186

87+
self.scene?.addChild(booster3)
88+
89+
//stars emitter
6290
let starsPath = Bundle.main.path(forResource: "stars", ofType: "sks")!
6391
let stars = NSKeyedUnarchiver.unarchiveObject(withFile: starsPath) as! SKEmitterNode
6492

@@ -67,36 +95,46 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
6795

6896
self.scene?.addChild(stars)
6997

98+
//asteroid emiiter
7099
let asteroidsPath = Bundle.main.path(forResource: "asteroid", ofType: "sks")!
71100
let asteroids = NSKeyedUnarchiver.unarchiveObject(withFile: asteroidsPath) as! SKEmitterNode
72101

73102
asteroids.position.y = 500
74103
asteroids.targetNode = self
75104

76105
self.scene?.addChild(asteroids)
106+
77107
}
78108

79109
func shootBeam() {
110+
//beam is invisible, but is the node that tracks collisions
80111
let beam = SKShapeNode(rect: CGRect(x: 0, y: 0, width: 15, height: 15))
112+
beam.fillColor = .clear
113+
beam.strokeColor = .clear
114+
115+
//bulletPath is the emitter that makes the bullet look like it does
116+
let bulletPath = Bundle.main.path(forResource: "bullet", ofType: "sks")!
117+
let bullet = NSKeyedUnarchiver.unarchiveObject(withFile: bulletPath) as! SKEmitterNode
81118

82-
beam.fillColor = .red
83-
119+
//assign physics body properties
84120
beam.physicsBody = SKPhysicsBody(rectangleOf: beam.frame.size)
85121
beam.physicsBody!.isDynamic = true
86122
beam.physicsBody!.affectedByGravity = false
87-
88-
beam.physicsBody!.categoryBitMask = BulletCategory
123+
beam.physicsBody!.categoryBitMask = PhysicsCategory.Bullet
89124
beam.physicsBody!.collisionBitMask = PhysicsCategory.None
90-
beam.physicsBody!.contactTestBitMask = NoteCategory
91-
125+
beam.physicsBody!.contactTestBitMask = PhysicsCategory.Note
92126
beam.physicsBody!.usesPreciseCollisionDetection = true
93127

94128
//change to CGPoint later; easier to read this wy for now
95129
beam.position.y = ship.position.y + 50
96130
beam.position.x = ship.position.x
131+
bullet.position.y = 7.5
132+
bullet.position.x = 7.5
97133

134+
beam.addChild(bullet)
98135
self.scene?.addChild(beam)
99136

137+
//shoot the bullet
100138
beam.physicsBody!.applyImpulse(CGVector(dx: 0.0, dy: 10.0))
101139
}
102140

@@ -106,9 +144,10 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
106144
var noteHeight : Double
107145
var x: Double = 0
108146

109-
//always start off assuming we have a note and not a delay
147+
//always start off assuming we have a note and not a delay, this will be proved otherwise in the switch statement
110148
var isNote = true
111149

150+
//Based on the note we have, spawn it in at the correct x position
112151
switch note {
113152
case "C":
114153
x = noteWidth * -8
@@ -140,6 +179,7 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
140179
x = noteWidth * 6
141180
case "N/A":
142181
x = 0
182+
isNote = false
143183
default:
144184
isNote = false
145185
print("this shouldn't happen")
@@ -196,6 +236,8 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
196236
if nodeBitmasks.contains(PhysicsCategory.Ship) && nodeBitmasks.contains(PhysicsCategory.Note) {
197237
//a note hit the ship.
198238

239+
print("aaa")
240+
199241
//contact.bodyA.node!.removeFromParent()
200242
//contact.bodyB.node!.removeFromParent()
201243

@@ -255,6 +297,12 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
255297
let location = event.location(in:self)
256298
// move ship to mouse (only x values)
257299
ship.position.x = location.x
300+
booster1.position.x = ship.position.x + 50
301+
booster1.position.y = ship.position.y
302+
booster2.position.x = ship.position.x - 50
303+
booster2.position.y = ship.position.y
304+
booster3.position.x = ship.position.x
305+
booster3.position.y = ship.position.y
258306
}
259307

260308
public override func mouseDown(with event: NSEvent) {
@@ -276,25 +324,25 @@ public class GameScene: SKScene, SKPhysicsContactDelegate {
276324
// .quarter = .25, half = .5, and so on; fix later
277325
for _ in 0...5 {
278326
song.addNote(note: "A", octave: 1, length: 0.25)
279-
song.addDelay(length: 1)
327+
song.addDelay(length: 0.25)
280328
song.addNote(note: "C", octave: 1, length: 0.5)
281-
song.addDelay(length: 1)
329+
song.addDelay(length: 0.25)
282330
song.addNote(note: "D", octave: 1, length: 0.25)
283-
song.addDelay(length: 1)
331+
song.addDelay(length: 0.25)
284332
song.addNote(note: "E", octave: 1, length: 0.25)
285-
song.addDelay(length: 1)
333+
song.addDelay(length: 0.25)
286334
song.addNote(note: "F", octave: 1, length: 0.25)
287-
song.addDelay(length: 1)
335+
song.addDelay(length: 0.25)
288336
song.addNote(note: "A2", octave: 1, length: 0.25)
289-
song.addDelay(length: 1)
337+
song.addDelay(length: 0.25)
290338
song.addNote(note: "C2", octave: 1, length: 0.5)
291-
song.addDelay(length: 1)
339+
song.addDelay(length: 0.25)
292340
song.addNote(note: "D2", octave: 1, length: 0.25)
293-
song.addDelay(length: 1)
341+
song.addDelay(length: 0.25)
294342
song.addNote(note: "E2", octave: 1, length: 0.25)
295-
song.addDelay(length: 1)
343+
song.addDelay(length: 0.25)
296344
song.addNote(note: "F2", octave: 1, length: 0.25)
297-
song.addDelay(length: 1)
345+
song.addDelay(length: 0.25)
298346
}
299347
}
300348

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ struct Sound {
66
var noteString: String
77

88
init(input: Double, length: Double) {
9+
910
noteString = ""
1011

1112
switch input {

0 commit comments

Comments
 (0)