-
Notifications
You must be signed in to change notification settings - Fork 1
/
GameScene.swift
131 lines (106 loc) · 4.46 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
//
// GameScene.swift
// WhackAPenguin
//
// Created by Julian Moorhouse on 09/08/2019.
// Copyright © 2019 Mindwarp Consultancy Ltd. All rights reserved.
//
import SpriteKit
class GameScene: SKScene {
var slots = [WhackSlot]()
var gameScore: SKLabelNode!
var finalScore: SKLabelNode!
var popupTime = 0.85
var numRounds = 0
var score = 0 {
didSet {
gameScore.text = "Score: \(score)"
}
}
override func didMove(to view: SKView) {
let background = SKSpriteNode(imageNamed: "whackBackground")
background.position = CGPoint(x: 512, y: 384) // In the middle of parent view
background.blendMode = .replace
background.zPosition = -1
addChild(background)
gameScore = SKLabelNode(fontNamed: "Chalkduster")
gameScore.text = "Score: 0"
gameScore.position = CGPoint(x: 8, y: 8) // Bottom left - remember different from ui kit
gameScore.horizontalAlignmentMode = .left
gameScore.fontSize = 48
addChild(gameScore)
// Draw slot grid
for i in 0..<5 { createSlot(at: CGPoint(x: 100 + (i * 170), y: 410))}
for i in 0..<4 { createSlot(at: CGPoint(x: 180 + (i * 170), y: 320))}
for i in 0..<5 { createSlot(at: CGPoint(x: 100 + (i * 170), y: 230))}
for i in 0..<4 { createSlot(at: CGPoint(x: 180 + (i * 170), y: 140))}
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
[weak self] in
self?.createEnemy()
}
}
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)
for node in tappedNodes {
guard let whackSlot = node.parent?.parent as? WhackSlot else { continue }
if !whackSlot.isVisible { continue }
if whackSlot.isHit { continue }
whackSlot.hit()
if node.name == "charFriend" {
// they shouldn't have whacked this penguin
score -= 5
run(SKAction.playSoundFileNamed("whackBad.caf", waitForCompletion: false))
} else if node.name == "charEnemy" {
// they should have wached this one
whackSlot.charNode.xScale = 0.85
whackSlot.charNode.yScale = 0.85
score += 1
run(SKAction.playSoundFileNamed("whack.caf", waitForCompletion: false))
}
}
}
func createSlot(at position: CGPoint) {
let slot = WhackSlot()
slot.configure(at: position)
addChild(slot)
slots.append(slot)
}
func createEnemy() {
numRounds += 1
if numRounds >= 30 {
for slot in slots {
slot.hide()
}
let gameOver = SKSpriteNode(imageNamed: "gameOver")
gameOver.position = CGPoint(x: 512, y: 384)
gameOver.zPosition = 1
addChild(gameOver)
run(SKAction.playSoundFileNamed("gameOver.caf", waitForCompletion: false))
finalScore = SKLabelNode(fontNamed: "Chalkduster")
finalScore.text = "Your final score is \(score)"
finalScore.position = CGPoint(x: 512, y: 290) // Bottom left - remember different from ui kit
finalScore.horizontalAlignmentMode = .center
finalScore.fontSize = 48
finalScore.zPosition = 1
addChild(finalScore)
return
}
popupTime *= 0.991 // Make enemy create more quickly / aka harder
slots.shuffle()
slots[0].show(hideTime: popupTime)
// Randomly show multiple penguins
if Int.random(in: 0...12) > 4 { slots[1].show(hideTime: popupTime)}
if Int.random(in: 0...12) > 8 { slots[2].show(hideTime: popupTime)}
if Int.random(in: 0...12) > 10 { slots[3].show(hideTime: popupTime)}
if Int.random(in: 0...12) > 11 { slots[4].show(hideTime: popupTime)}
let minDelay = popupTime / 2.0
let maxDelay = popupTime * 2
let delay = Double.random(in: minDelay...maxDelay)
DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
[weak self] in
self?.createEnemy()
}
}
}