-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameScene.swift
344 lines (215 loc) · 10.6 KB
/
GameScene.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//
// GameScene.swift
// MySwift
//
// Created by ISHFAQ HUSSAIN on 19/06/16.
// Copyright (c) 2016 ISHFAQ HUSSAIN. All rights reserved.
//
import SpriteKit
struct PhysicsCatagory {
static let Ghost : UInt32 = 0x1 << 1
static let Ground : UInt32 = 0x1 << 2
static let Wall : UInt32 = 0x1 << 3
static let score : UInt32 = 0x1 << 4
}
class GameScene: SKScene, SKPhysicsContactDelegate {
var Ground = SKSpriteNode()
var Ghost = SKSpriteNode()
var wallPair = SKNode()
var moveAndRemove = SKAction()
var gameStarted = Bool()
var score = Int()
let scoreLbl = SKLabelNode()
var died = Bool()
var restartBTN = SKSpriteNode()
func restartScene(){
self.removeAllChildren()
self.removeAllActions()
died = false
gameStarted = false
score = 0
createScene()
}
func createScene(){
self.physicsWorld.contactDelegate = self
for i in 0..<2 {
let background = SKSpriteNode(imageNamed: "Background")
background.anchorPoint = CGPointZero
background.position = CGPointMake(CGFloat(i) * self.frame.width, 0)
background.name = "background"
background.size = (self.view?.bounds.size)!
self.addChild(background)
}
scoreLbl.position = CGPoint(x: 50, y: self.frame.height / 2 + self.frame.height / 2.5)
scoreLbl.text = "0 $"
print("Scores")
scoreLbl.fontName = "FlappyText"
scoreLbl.zPosition = 5
scoreLbl.fontSize = 60
self.addChild(scoreLbl)
Ground = SKSpriteNode(imageNamed: "Ground")
Ground.setScale(0.5)
Ground.position = CGPoint(x: self.frame.width / 2, y: 0 + Ground.frame.height / 2)
Ground.physicsBody = SKPhysicsBody(rectangleOfSize: Ground.size)
Ground.physicsBody?.categoryBitMask = PhysicsCatagory.Ground
Ground.physicsBody?.collisionBitMask = PhysicsCatagory.Ghost
Ground.physicsBody?.contactTestBitMask = PhysicsCatagory.Ghost
Ground.physicsBody?.affectedByGravity = false
Ground.physicsBody?.dynamic = false
Ground.zPosition = 3
self.addChild(Ground)
Ghost = SKSpriteNode(imageNamed: "Ghost")
Ghost.size = CGSize(width: 60, height: 70)
Ghost.position = CGPoint(x: self.frame.width / 2 - Ghost.frame.width, y: self.frame.height / 2)
Ghost.physicsBody = SKPhysicsBody(circleOfRadius: Ghost.frame.height / 2)
Ghost.physicsBody?.categoryBitMask = PhysicsCatagory.Ghost
Ghost.physicsBody?.collisionBitMask = PhysicsCatagory.Ground | PhysicsCatagory.Wall
Ghost.physicsBody?.contactTestBitMask = PhysicsCatagory.Ground | PhysicsCatagory.Wall | PhysicsCatagory.score
Ghost.physicsBody?.affectedByGravity = false
Ghost.physicsBody?.dynamic = true
Ghost.zPosition = 2
self.addChild(Ghost)
}
override func didMoveToView(view: SKView) {
createScene()
}
func createBTN(){
restartBTN = SKSpriteNode(imageNamed: "RestartBtn")
restartBTN.size = CGSizeMake(100, 50)
restartBTN.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
restartBTN.zPosition = 6
restartBTN.setScale(0)
self.addChild(restartBTN)
restartBTN.runAction(SKAction.scaleTo(1.0, duration: 0.3))
}
func didBeginContact(contact: SKPhysicsContact) {
let firstBody = contact.bodyA
let secondBody = contact.bodyB
if firstBody.categoryBitMask == PhysicsCatagory.score && secondBody.categoryBitMask == PhysicsCatagory.Ghost{
score++
scoreLbl.text = "\(score)"
firstBody.node?.removeFromParent()
}
else if firstBody.categoryBitMask == PhysicsCatagory.Ghost && secondBody.categoryBitMask == PhysicsCatagory.score {
score++
scoreLbl.text = "\(score)"
secondBody.node?.removeFromParent()
}
else if firstBody.categoryBitMask == PhysicsCatagory.Ghost && secondBody.categoryBitMask == PhysicsCatagory.Wall || firstBody.categoryBitMask == PhysicsCatagory.Wall && secondBody.categoryBitMask == PhysicsCatagory.Ghost{
enumerateChildNodesWithName("wallPair", usingBlock: ({
(node, error) in
node.speed = 0
self.removeAllActions()
}))
if died == false{
died = true
createBTN()
}
}
else if firstBody.categoryBitMask == PhysicsCatagory.Ghost && secondBody.categoryBitMask == PhysicsCatagory.Ground || firstBody.categoryBitMask == PhysicsCatagory.Ground && secondBody.categoryBitMask == PhysicsCatagory.Ghost{
enumerateChildNodesWithName("wallPair", usingBlock: ({
(node, error) in
node.speed = 0
self.removeAllActions()
}))
if died == false{
died = true
createBTN()
}
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if gameStarted == false{
gameStarted = true
Ghost.physicsBody?.affectedByGravity = true
let spawn = SKAction.runBlock({
() in
self.createWalls()
})
let delay = SKAction.waitForDuration(1.5)
let SpawnDelay = SKAction.sequence([spawn, delay])
let spawnDelayForever = SKAction.repeatActionForever(SpawnDelay)
self.runAction(spawnDelayForever)
let distance = CGFloat(self.frame.width + wallPair.frame.width)
let movePipes = SKAction.moveByX(-distance - 50, y: 0, duration: NSTimeInterval(0.008 * distance))
let removePipes = SKAction.removeFromParent()
moveAndRemove = SKAction.sequence([movePipes, removePipes])
Ghost.physicsBody?.velocity = CGVectorMake(0, 0)
Ghost.physicsBody?.applyForce(CGVectorMake(0, 0))
Ghost.physicsBody?.applyImpulse(CGVectorMake(0, 90))
}
else{
if died == true{
}
else{
Ghost.physicsBody?.velocity = CGVectorMake(0, 0)
Ghost.physicsBody?.applyForce(CGVectorMake(0, 0))
Ghost.physicsBody?.applyImpulse(CGVectorMake(0, 70))
}
}
for touch in touches{
let location = touch.locationInNode(self)
if died == true{
if restartBTN.containsPoint(location){
restartScene()
}
}
}
}
func createWalls(){
let scoreNode = SKSpriteNode(imageNamed: "Coin")
scoreNode.size = CGSize(width: 50, height: 50)
scoreNode.position = CGPoint(x: self.frame.width + 25, y: self.frame.height / 2)
scoreNode.physicsBody = SKPhysicsBody(rectangleOfSize: scoreNode.size)
scoreNode.physicsBody?.affectedByGravity = false
scoreNode.physicsBody?.dynamic = false
scoreNode.physicsBody?.categoryBitMask = PhysicsCatagory.score
scoreNode.physicsBody?.collisionBitMask = 0
scoreNode.physicsBody?.contactTestBitMask = PhysicsCatagory.Ghost
scoreNode.color = SKColor.blueColor()
wallPair = SKNode()
wallPair.name = "wallPair"
let topWall = SKSpriteNode(imageNamed: "Wall")
let btmWall = SKSpriteNode(imageNamed: "Wall")
topWall.position = CGPoint(x: self.frame.width + 25, y: self.frame.height / 2 + 350)
btmWall.position = CGPoint(x: self.frame.width + 25, y: self.frame.height / 2 + 150)
topWall.setScale(0.15)
btmWall.setScale(0.15)
topWall.physicsBody = SKPhysicsBody(rectangleOfSize: topWall.size)
topWall.physicsBody?.categoryBitMask = PhysicsCatagory.Wall
topWall.physicsBody?.collisionBitMask = PhysicsCatagory.Ghost
topWall.physicsBody?.contactTestBitMask = PhysicsCatagory.Ghost
topWall.physicsBody?.dynamic = true
topWall.physicsBody?.affectedByGravity = false
btmWall.physicsBody = SKPhysicsBody(rectangleOfSize: btmWall.size)
btmWall.physicsBody?.categoryBitMask = PhysicsCatagory.Wall
btmWall.physicsBody?.collisionBitMask = PhysicsCatagory.Ghost
btmWall.physicsBody?.contactTestBitMask = PhysicsCatagory.Ghost
btmWall.physicsBody?.dynamic = true
btmWall.physicsBody?.affectedByGravity = false
topWall.zRotation = CGFloat(M_PI)
wallPair.addChild(topWall)
wallPair.addChild(btmWall)
wallPair.zPosition = 1
let randomPosition = CGFloat.random(min: -240, max: 140)
wallPair.position.y = wallPair.position.y + randomPosition
wallPair.addChild(scoreNode)
wallPair.runAction(moveAndRemove)
self.addChild(wallPair)
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
if gameStarted == true{
if died == false{
enumerateChildNodesWithName("background", usingBlock: ({
(node, error) in
let bg = node as! SKSpriteNode
bg.position = CGPoint(x: bg.position.x - 2, y: bg.position.y)
if bg.position.x <= -bg.size.width {
bg.position = CGPointMake(bg.position.x + bg.size.width * 2, bg.position.y)
}
}))
}
}
}
}