-
Notifications
You must be signed in to change notification settings - Fork 1
/
GameScene.swift
333 lines (265 loc) · 9.81 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
//
// GameScene.swift
// ShootingGallery
//
// Created by Julian Moorhouse on 12/08/2019.
// Copyright © 2019 Mindwarp Consultancy Ltd. All rights reserved.
//
import SpriteKit
import GameplayKit
class GameScene: SKScene, SKPhysicsContactDelegate{
var grass: SKSpriteNode!
var waterBg: SKSpriteNode!
var waterFg: SKSpriteNode!
var reloadLabel: SKLabelNode!
var scoreLabel: SKLabelNode!
var remainLabel: SKLabelNode!
var gameOver: SKSpriteNode!
var startGameLabel: SKLabelNode!
var timerBottom: Timer?
var timerMiddle: Timer?
var timerTop: Timer?
var timerRemain: Timer?
var timerIntervalBottom: Double = 1.0
var timerIntervalMiddle: Double = 0.7
var timerIntervalTop: Double = 0.5
var isGameOver = false
var soundsFiles = ["shot.wav", "empty.wav", "reload.wav", "whack.caf", "whackBad.caf"]
enum sounds: Int {
case shot = 0
case empty = 1
case reload = 2
case whackGood = 3
case whackBad = 4
}
// Sounds for preload
var sound: [SKAction] = []
var score: Int = 0 {
didSet {
scoreLabel.text = "Score: \(score)"
}
}
var countDown: Int = 60 {
didSet {
remainLabel.text = "Time Left: \(countDown)"
}
}
var reloadMode: Bool = false {
didSet {
if reloadMode {
reloadLabel.text = "Reload"
} else {
reloadLabel.text = ""
}
}
}
class Bullet {
var x: Int
var node: SKSpriteNode?
var used: Bool?
init(x: Int) {
self.x = x
self.used = false
}
}
var bullets: [Bullet] = [Bullet(x: 983), Bullet(x: 962), Bullet(x: 941), Bullet(x: 920), Bullet(x: 899), Bullet(x: 878)]
override func didMove(to view: SKView) {
backgroundColor = UIColor.init(red: 0.75, green: 0.84, blue: 0.96, alpha: 1)
for item in soundsFiles {
sound.append(SKAction.playSoundFileNamed(item, waitForCompletion: false))
}
grass = SKSpriteNode(imageNamed: "grass-trees")
grass.position = CGPoint(x: 512, y: 480)
grass.setScale(1.28)
grass.zPosition = 10
addChild(grass)
waterBg = SKSpriteNode(imageNamed: "water-bg")
waterBg.position = CGPoint(x: 512, y: 260)
waterBg.setScale(1.28)
waterBg.zPosition = 20
addChild(waterBg)
waterFg = SKSpriteNode(imageNamed: "water-fg")
waterFg.position = CGPoint(x: 512, y: 100)
waterFg.setScale(1.28)
waterFg.zPosition = 30
addChild(waterFg)
reloadLabel = SKLabelNode(fontNamed: "Chalkduster")
reloadLabel.text = ""
reloadLabel.position = CGPoint(x: 930, y: 90)
reloadLabel.zPosition = 50
addChild(reloadLabel)
startGameLabel = SKLabelNode(fontNamed: "Chalkduster")
startGameLabel.text = "Start Game"
startGameLabel.position = CGPoint(x: 512, y: 90)
startGameLabel.zPosition = 50
startGameLabel.horizontalAlignmentMode = .center
//addChild(startGameLabel)
scoreLabel = SKLabelNode(fontNamed: "Chalkduster")
scoreLabel.text = "Score: 0"
scoreLabel.position = CGPoint(x: 100, y: 20)
scoreLabel.zPosition = 50
addChild(scoreLabel)
gameOver = SKSpriteNode(imageNamed: "game-over")
gameOver.position = CGPoint(x: 512, y: 384)
gameOver.zPosition = 50
remainLabel = SKLabelNode(fontNamed: "Chalkduster")
remainLabel.text = "Time Left: 60"
remainLabel.position = CGPoint(x: 20, y: 730)
remainLabel.horizontalAlignmentMode = .left
remainLabel.zPosition = 50
addChild(remainLabel)
physicsWorld.gravity = .zero
physicsWorld.contactDelegate = self // tell us when contacts happen
for bullet in bullets {
bullet.node = SKSpriteNode(imageNamed: "shotFull")
bullet.node?.position = CGPoint(x: bullet.x, y: 43)
bullet.node?.zPosition = 50
addChild(bullet.node!)
}
startGame()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else { return }
let location = touch.location(in: self)
let tappedNodes = nodes(at: location)
var hitSomething = false
if isGameOver {
if tappedNodes.contains(startGameLabel) {
startGame()
}
return
}
if !bullets.contains(where: {( $0.used == false )}) {
if tappedNodes.contains(reloadLabel) && reloadLabel.text != "" {
//reloadMode.toggle()
resetBullets()
run(SKAction.playSoundFileNamed("reload.wav", waitForCompletion: false))
}
return
}
useBullet()
for node in tappedNodes {
guard let item = node.parent as? SpriteNode else { continue }
let sprite = item.sprite!
if (sprite.name == "bad") {
run(sound[sounds.whackBad.rawValue])
} else if (sprite.name == "good") {
run(sound[sounds.whackGood.rawValue])
let goodScore = SKLabelNode(fontNamed: "Chalkduster")
goodScore.text = "+\(item.scoreAmount)"
goodScore.horizontalAlignmentMode = .center
goodScore.zPosition = 40
let x = location.x - sprite.frame.width / 2
let y = location.y + sprite.frame.height / 2
goodScore.position = CGPoint(x: x, y: y)
addChild(goodScore)
score += item.scoreAmount
goodScore.run(fadeGroup())
}
if (sprite.name == "bad") || (sprite.name == "good") {
hitSomething = true
sprite.run(fadeGroup())
score -= 10
}
}
if !hitSomething {
run(sound[sounds.shot.rawValue])
}
}
override func update(_ currentTime: TimeInterval) {
for node in children {
if let item = node as? SpriteNode {
let x = item.sprite.position.x
if item.row == 0 || item.row == 2 {
if x <= 0 {
item.sprite.removeFromParent()
}
} else {
if x >= 1024 {
item.sprite.removeFromParent()
}
}
}
}
}
func fadeGroup() -> SKAction {
let scale = SKAction.scale(to: 0.1, duration: 0.5)
let fade = SKAction.fadeOut(withDuration: 0.5)
return SKAction.group([scale, fade])
}
@objc func topRow() {
timerTop?.invalidate()
let sprite = SpriteNode()
sprite.configure(row: 2, yPos: 545, xPos: 1024, slide: -500, zPosition: 9)
addChild(sprite)
timerTop = Timer.scheduledTimer(timeInterval: timerIntervalTop, target: self, selector: #selector(topRow), userInfo: nil, repeats: true)
timerIntervalTop = 0.5
}
@objc func middleRow() {
timerMiddle?.invalidate()
let sprite = SpriteNode()
sprite.configure(row: 1, yPos: 392, xPos: 0, slide: 350, zPosition: 19)
addChild(sprite)
timerMiddle = Timer.scheduledTimer(timeInterval: timerIntervalMiddle, target: self, selector: #selector(middleRow), userInfo: nil, repeats: true)
timerIntervalMiddle = 0.7
}
@objc func bottomRow() {
timerBottom?.invalidate()
let sprite = SpriteNode()
sprite.configure(row: 0, yPos: 239, xPos: 1024, slide: -300, zPosition: 29)
addChild(sprite)
timerBottom = Timer.scheduledTimer(timeInterval: timerIntervalBottom, target: self, selector: #selector(bottomRow), userInfo: nil, repeats: true)
timerIntervalBottom = 1.0
}
func useBullet() {
var counter = 0
for bullet in bullets {
counter += 1
if let used = bullet.used {
if !used {
bullet.node?.texture = SKTexture(imageNamed: "shotEmpty")
bullet.used = true
if counter == 6 {
reloadMode.toggle()
}
break
}
}
}
}
@objc func countDownTimer() {
countDown -= 1
if countDown <= 0 {
timerRemain?.invalidate()
addChild(gameOver)
isGameOver = true
addChild(startGameLabel)
} else if countDown <= 3 {
timerTop?.invalidate()
timerMiddle?.invalidate()
timerBottom?.invalidate()
}
}
func resetTimer() {
timerRemain = Timer.scheduledTimer(timeInterval: 0.7, target: self, selector: #selector(countDownTimer), userInfo: nil, repeats: true)
}
func resetBullets() {
bullets = bullets.map({ (bullet) -> Bullet in
bullet.used = false
bullet.node?.texture = SKTexture(imageNamed: "shotFull")
return bullet
})
reloadMode = false
}
func startGame() {
countDown = 60
score = 0
gameOver.removeFromParent()
startGameLabel.removeFromParent()
topRow()
middleRow()
bottomRow()
isGameOver = false
resetTimer()
}
}