-
Notifications
You must be signed in to change notification settings - Fork 24
/
Menu.swift
executable file
·224 lines (170 loc) · 8.38 KB
/
Menu.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//
// Menu.swift
// Orbit 7
//
// Created by Aaron Ackerman on 1/17/15.
// Copyright (c) 2015 Mav3r1ck. All rights reserved.
//
import Foundation
import SpriteKit
import AVFoundation
var gameLevel: Int?
var gameSpeed: UInt32?
var gamelvlLable = ""
var gameObjective: Int!
class Menu: SKScene {
var audioPlayer = AVAudioPlayer()
let gameScene = GameScene()
let mygameLabel = SKLabelNode()
let sunNode = SKSpriteNode(imageNamed: "sunDot@2x")
let redNode = SKSpriteNode(imageNamed: "redDot@2x")
let greenNode = SKSpriteNode(imageNamed: "greenDot@2x")
let purpleNode = SKSpriteNode(imageNamed: "purpleDot@2x")
let blueNode = SKSpriteNode(imageNamed: "blueDot@2x")
override func didMoveToView(view: SKView) {
var alertSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Spiraling",ofType: "wav")!)
var error:NSError?
audioPlayer = AVAudioPlayer(contentsOfURL: alertSound, error: &error)
audioPlayer.prepareToPlay()
audioPlayer.play()
audioPlayer.numberOfLoops = -1
// Level Label
mygameLabel.fontName = "DIN Condensed"
mygameLabel.fontSize = 40
mygameLabel.fontColor = SKColor.whiteColor()
mygameLabel.position = CGPoint(x: size.width/2, y: size.height/1.29)
mygameLabel.text = "Orbit 7"
addChild(mygameLabel)
// Add Stars!
let stars = SKEmitterNode(fileNamed: "Stars")
stars.position = CGPointMake(400, 200)
addChild(stars)
// Background Color
backgroundColor = SKColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 1)
func addOrbitingToShape(shape: SKSpriteNode, orbitSize: CGSize, rotateDegrees: Double, reverseOrbit: Bool, duration: NSTimeInterval) {
let orbitPathOrigin = CGPoint(x: view.center.x - orbitSize.width / 2, y: view.center.y - orbitSize.height / 2)
let orbitPathRect = CGRect(origin: orbitPathOrigin, size: orbitSize)
let orbitPath = CGPathCreateWithEllipseInRect(orbitPathRect, nil)
var transform = CGAffineTransformIdentity
transform = CGAffineTransformTranslate(transform, view.center.x, view.center.y)
transform = CGAffineTransformRotate(transform, CGFloat(rotateDegrees * M_PI / 180))
transform = CGAffineTransformTranslate(transform, -view.center.x, -view.center.y)
let rotatedOrbitPath = CGPathCreateCopyByTransformingPath(orbitPath, &transform)
let shapeLayer = CAShapeLayer()
shapeLayer.path = rotatedOrbitPath
shapeLayer.strokeColor = UIColor.clearColor().CGColor
shapeLayer.lineWidth = 0.5
shapeLayer.lineDashPattern = [5, 1.5]
shapeLayer.fillColor = UIColor.clearColor().CGColor
view.layer.addSublayer(shapeLayer)
var spriteKitTransform = CGAffineTransformIdentity
spriteKitTransform = CGAffineTransformTranslate(spriteKitTransform, view.center.x, view.center.y)
spriteKitTransform = CGAffineTransformRotate(spriteKitTransform, CGFloat(90 * M_PI / 180))
spriteKitTransform = CGAffineTransformTranslate(spriteKitTransform, -view.center.x, -view.center.y)
let spriteKitPath = CGPathCreateCopyByTransformingPath(shapeLayer.path, &spriteKitTransform)
if reverseOrbit == false {
let orbitActionTwo = SKAction.followPath(spriteKitPath, asOffset: false, orientToPath: true, duration: duration)
shape.runAction(SKAction.repeatActionForever(orbitActionTwo))
} else if reverseOrbit == true {
let orbitActionTwo = SKAction.followPath(spriteKitPath, asOffset: false, orientToPath: true, duration: duration)
shape.runAction(SKAction.repeatActionForever(orbitActionTwo.reversedAction()))
}
}
// Sun Dot
sunNode.position = CGPoint(x: size.width/2, y:size.height/2)
sunNode.name = "Sun"
addChild(sunNode)
// Green Asteroid
let greenCircleOrbitSize = CGSize(width: 200 , height: 200)
addOrbitingToShape(greenNode, greenCircleOrbitSize, 90, false, 8)
greenNode.name = "greenDot"
addChild(greenNode)
// Red Asteroid
let redCircleOrbitSize = CGSize(width: sunNode.xScale + 280, height: sunNode.yScale + 500)
addOrbitingToShape(redNode, redCircleOrbitSize, 330, false, 5)
redNode.name = "redDot"
addChild(redNode)
// Blue Asteroid
let blueCircleOrbitSize = CGSize(width: 500 , height: 540)
addOrbitingToShape(blueNode, blueCircleOrbitSize, 90, true, 5)
blueNode.name = "blueDot"
addChild(blueNode)
// Purple Asteroid
let purpleCircleOrbitSize = CGSize(width: 700 , height: 650)
addOrbitingToShape(purpleNode, purpleCircleOrbitSize, 45, true, 15)
purpleNode.name = "purpleDot"
addChild(purpleNode)
}
// Game Levels
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
let touchLocation = (touches.first as? UITouch)?.locationInNode(self)
let node = self.nodeAtPoint(touchLocation!)
audioPlayer.stop()
if node.name == "Sun" {
runAction(SKAction.sequence([
SKAction.waitForDuration(1.0),
SKAction.runBlock() {
gamelvlLable = "Level One"
let reveal = SKTransition.crossFadeWithDuration(0.5)
let scene = GameScene(size: self.size)
self.view?.presentScene(scene, transition:reveal)
gameSpeed = 10
gameLevel = 1
gameObjective = 50
}
]))
} else if node.name == "greenDot" {
runAction(SKAction.sequence([
SKAction.waitForDuration(1.0),
SKAction.runBlock() {
gamelvlLable = "Level Two"
let reveal = SKTransition.crossFadeWithDuration(0.5)
let scene = GameScene(size: self.size)
self.view?.presentScene(scene, transition:reveal)
gameSpeed = 8
gameLevel = 2
gameObjective = 100
}
]))
} else if node.name == "redDot" {
runAction(SKAction.sequence([
SKAction.waitForDuration(1.0),
SKAction.runBlock() {
gamelvlLable = "Level Three"
let reveal = SKTransition.crossFadeWithDuration(0.5)
let scene = GameScene(size: self.size)
self.view?.presentScene(scene, transition:reveal)
gameSpeed = 6
gameLevel = 3
gameObjective = 150
}
]))
} else if node.name == "blueDot" {
runAction(SKAction.sequence([
SKAction.waitForDuration(1.0),
SKAction.runBlock() {
gamelvlLable = "Level Four"
let reveal = SKTransition.crossFadeWithDuration(0.5)
let scene = GameScene(size: self.size)
self.view?.presentScene(scene, transition:reveal)
gameSpeed = 4
gameLevel = 4
gameObjective = 200
}
]))
} else if node.name == "purpleDot" {
runAction(SKAction.sequence([
SKAction.waitForDuration(1.0),
SKAction.runBlock() {
gamelvlLable = "Level Five"
let reveal = SKTransition.crossFadeWithDuration(0.5)
let scene = GameScene(size: self.size)
self.view?.presentScene(scene, transition:reveal)
gameSpeed = 2
gameLevel = 5
gameObjective = 250
}
]))
}
}
}