This repository has been archived by the owner on Feb 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameManager.swift
219 lines (196 loc) · 6.86 KB
/
GameManager.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
//
// GameManager.swift
// Snake
//
// Created by Adrian Darian on 12/7/19.
// Copyright © 2019 Adrian Darian. All rights reserved.
//
import Foundation
import SpriteKit
class GameManager {
var scene: GameScene!
var nextTime: Double?
var timeExtension: Double = 0.15
var playerDirection: Int = 1
var currentScore: Int = 0
init(scene: GameScene) {
self.scene = scene
}
func initGame() {
// starting player position
scene.playerPositions.append((10, 10))
scene.playerPositions.append((10, 11))
scene.playerPositions.append((10, 12))
renderChange()
generateNewPoint()
}
func update(time: Double) {
if nextTime == nil {
nextTime = time + timeExtension
} else {
if time >= nextTime! {
nextTime = time + timeExtension
updatePlayerPosition()
checkForScore()
checkForDeath()
finishAnimation()
}
}
}
func renderChange() {
for (node, x, y) in scene.gameArray {
if contains(a: scene.playerPositions, v: (x,y)) {
node.fillColor = SKColor.cyan
} else {
node.fillColor = SKColor.clear
if scene.scorePos != nil {
if Int((scene.scorePos?.x)!) == y && Int((scene.scorePos?.y)!) == x {
node.fillColor = SKColor.red
}
}
}
}
}
func contains(a:[(Int, Int)], v:(Int,Int)) -> Bool {
let (c1, c2) = v
for (v1, v2) in a { if v1 == c1 && v2 == c2 { return true } }
return false
}
func swipe(ID: Int) {
if !(ID == 2 && playerDirection == 4) && !(ID == 4 && playerDirection == 2) {
if !(ID == 1 && playerDirection == 3) && !(ID == 3 && playerDirection == 1) {
if playerDirection != 0 {
playerDirection = ID
}
}
}
}
private func checkForDeath() {
if scene.playerPositions.count > 0 {
var arrayOfPositions = scene.playerPositions
let headOfSnake = arrayOfPositions[0]
arrayOfPositions.remove(at: 0)
if contains(a: arrayOfPositions, v: headOfSnake) {
playerDirection = 0
}
}
}
private func checkForScore() {
if scene.scorePos != nil {
let x = scene.playerPositions[0].0
let y = scene.playerPositions[0].1
if Int((scene.scorePos?.x)!) == y && Int((scene.scorePos?.y)!) == x {
currentScore += 1
scene.currentScore.text = "Score: \(currentScore)"
generateNewPoint()
scene.playerPositions.append(scene.playerPositions.last!)
scene.playerPositions.append(scene.playerPositions.last!)
scene.playerPositions.append(scene.playerPositions.last!)
}
}
}
private func updateScore() {
if currentScore > UserDefaults.standard.integer(forKey: "bestScore") {
UserDefaults.standard.set(currentScore, forKey: "bestScore")
}
currentScore = 0
scene.currentScore.text = "Score: 0"
scene.bestScore.text = "Best Score: \(UserDefaults.standard.integer(forKey: "bestScore"))"
}
private func generateNewPoint() {
var randomX = CGFloat(arc4random_uniform(19))
var randomY = CGFloat(arc4random_uniform(39))
scene.scorePos = CGPoint(x: randomX, y: randomY)
while contains(a: scene.playerPositions, v: (Int(randomX), Int(randomY))) {
randomX = CGFloat(arc4random_uniform(19))
randomY = CGFloat(arc4random_uniform(39))
}
}
private func finishAnimation() {
if playerDirection == 0 && scene.playerPositions.count > 0 {
var hasFinished = true
let headOfSnake = scene.playerPositions[0]
for position in scene.playerPositions {
if headOfSnake != position {
hasFinished = false
}
}
if hasFinished {
updateScore()
playerDirection = 4
// animation has completed
scene.scorePos = nil
scene.playerPositions.removeAll()
renderChange()
// return to menu
scene.currentScore.run(SKAction.scale(to: 0, duration: 0.4)) {
self.scene.currentScore.isHidden = true
}
scene.gameBG.run(SKAction.scale(to: 0, duration: 0.4)) {
self.scene.gameBG.isHidden = true
self.scene.gameLogo.isHidden = false
self.scene.gameLogo.run(SKAction.move(to: CGPoint(x: 0, y: (self.scene.frame.size.height / 2) - 200), duration: 0.5)) {
self.scene.playButton.isHidden = false
self.scene.playButton.run(SKAction.scale(to: 1, duration: 0.3))
self.scene.bestScore.run(SKAction.move(to: CGPoint(x: 0, y: self.scene.gameLogo.position.y - 50), duration: 0.3))
}
}
}
}
}
private func updatePlayerPosition() {
var xChange = -1
var yChange = 0
switch playerDirection {
case 1:
// left
xChange = -1
yChange = 0
break
case 2:
// up
xChange = 0
yChange = -1
break
case 3:
// right
xChange = 1
yChange = 0
break
case 4:
// down
xChange = 0
yChange = 1
break
case 0:
// dead
xChange = 0
yChange = 0
break
default:
break
}
if scene.playerPositions.count > 0 {
var start = scene.playerPositions.count - 1
while start > 0 {
scene.playerPositions[start] = scene.playerPositions[start - 1]
start -= 1
}
scene.playerPositions[0] = (scene.playerPositions[0].0 + yChange, scene.playerPositions[0].1 + xChange)
}
if scene.playerPositions.count > 0 {
let x = scene.playerPositions[0].1
let y = scene.playerPositions[0].0
if y > 40 {
scene.playerPositions[0].0 = 0
} else if y < 0 {
scene.playerPositions[0].0 = 40
} else if x > 20 {
scene.playerPositions[0].1 = 0
} else if x < 0 {
scene.playerPositions[0].1 = 20
}
}
renderChange()
}
}