-
Notifications
You must be signed in to change notification settings - Fork 0
/
LevelManager.swift
281 lines (220 loc) · 10.3 KB
/
LevelManager.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
//
// LevelManager.swift
// FingersRevenge
//
// Created by student on 10/3/16.
// Copyright © 2016 Austin Willoughby / Peter Lockhart. All rights reserved.
//
import Foundation
import SpriteKit
class LevelManager{
private var startHeight:Int
private var tilesAcross:Int
private var unitSize:Int
init(){
tilesAcross = 10
unitSize = 1080 / tilesAcross
startHeight = 2200
}
//Load an array of rectanglesprites from a map identifier
func loadMap(map:String)->[RectangleSprite]{
var chunkMap:[String] = map.characters.split{$0 == ","}.map(String.init)
if chunkMap.last != "E" {
chunkMap.append("E")
}
var chunks = [RectangleSprite]()
var currentStartHeight: Int = startHeight
for i in 0 ..< chunkMap.count{
let levelChunk: [String] = getChunkAtIndex(chunk: chunkMap[i])
let chunkInfo = loadChunk(map: levelChunk, startingHeight: currentStartHeight)
let currentChunk = chunkInfo.0
currentStartHeight = chunkInfo.1
chunks.append(contentsOf: currentChunk)
}
return chunks
}
//Load a chunk from a map string[], give starting height to know where it is in the world
func loadChunk(map:[String], startingHeight: Int)->(newChunk: [RectangleSprite], height: Int){
var currentLine = map[0]
let xSize: Int = tilesAcross
let ySize: Int = map.count
var button:RectangleSprite?
var gates:[RectangleSprite] = [RectangleSprite]()
var chunk = [RectangleSprite]()
for y in 0 ..< ySize{
currentLine = map[ySize - y - 1]
for x in 0 ..< xSize{
var tempRect:RectangleSprite
let index = currentLine.index(currentLine.startIndex, offsetBy: x)
switch currentLine[index]
{
case "O": //Generic obstacle
tempRect = generateRectDetails(fill: randomGreenColor(), stroke: SKColor.clear, name: "obstacle", xValue: x, yValue: y, startHeight: startingHeight, elementID: "O")
chunk.append(tempRect)
case "L": //Finish line (graphic)
tempRect = generateRectDetails(fill: SKColor.white, stroke: SKColor.clear, name: "obstacle", xValue: x, yValue: y, startHeight: startingHeight, elementID: "L")
chunk.append(tempRect)
case "F": //Finish line (collider)
tempRect = generateRectDetails(fill: SKColor.clear, stroke: SKColor.clear, name: "obstacle", xValue: x, yValue: y, startHeight: startingHeight, elementID: "F")
chunk.append(tempRect)
case "G": //Gate Obstacle
tempRect = generateRectDetails(fill: randomLightGrayColor(), stroke: SKColor.clear, name: "obstacle", xValue: x, yValue: y, startHeight: startingHeight, elementID: "G")
chunk.append(tempRect)
gates.append(tempRect)
case "B": //Button Obstacle
tempRect = generateRectDetails(fill: SKColor.red, stroke: SKColor.clear, name: "obstacle", xValue: x, yValue: y, startHeight: startingHeight, elementID: "B")
chunk.append(tempRect)
tempRect.isButton = true
button = tempRect
case "U": //Unbreakable rects
tempRect = generateRectDetails(fill: randomDarkGrayColor(), stroke: SKColor.clear, name: "obstacle", xValue: x, yValue: y, startHeight: startingHeight, elementID: "U")
chunk.append(tempRect)
default:
break
}
}
}
if(button != nil && gates.count > 0){
button?.gates = gates
}
return (chunk, startingHeight + (ySize * unitSize))
}
//Handle generating the rectangle information
func generateRectDetails(fill: SKColor, stroke: SKColor, name: String, xValue: Int, yValue: Int, startHeight: Int, elementID: String) -> RectangleSprite
{
let tempRect = RectangleSprite(size: CGSize(width: unitSize, height: unitSize), fillColor: fill, strokeColor: stroke)
tempRect.name = name
tempRect.elementID = elementID
let x = (xValue * unitSize) + (unitSize / 2)
let y = startHeight + (yValue * unitSize)
tempRect.position = CGPoint(x: CGFloat(x), y: CGFloat(y))
switch elementID
{
case "O":
tempRect.physicsBody = SKPhysicsBody.init(polygonFrom: tempRect.path!)
tempRect.physicsBody?.isDynamic = false
tempRect.physicsBody?.categoryBitMask = CollisionMask.wall
tempRect.physicsBody?.collisionBitMask = CollisionMask.none
case "L": break
//For now we arent doing anything
case "F":
tempRect.physicsBody = SKPhysicsBody.init(polygonFrom: tempRect.path!)
tempRect.physicsBody?.isDynamic = false
tempRect.physicsBody?.categoryBitMask = CollisionMask.finish
tempRect.physicsBody?.collisionBitMask = CollisionMask.none
case "G":
tempRect.physicsBody = SKPhysicsBody.init(polygonFrom: tempRect.path!)
tempRect.physicsBody?.isDynamic = false
tempRect.physicsBody?.categoryBitMask = CollisionMask.gate
tempRect.physicsBody?.collisionBitMask = CollisionMask.none
case "B":
tempRect.physicsBody = SKPhysicsBody.init(polygonFrom: tempRect.path!)
tempRect.physicsBody?.isDynamic = false
tempRect.physicsBody?.categoryBitMask = CollisionMask.button
tempRect.physicsBody?.collisionBitMask = CollisionMask.none
case "U":
tempRect.physicsBody = SKPhysicsBody.init(polygonFrom: tempRect.path!)
tempRect.physicsBody?.isDynamic = false
tempRect.physicsBody?.categoryBitMask = CollisionMask.unbreakable
tempRect.physicsBody?.collisionBitMask = CollisionMask.none
default:
break
}
return tempRect
}
//MARK: Random Colors
func randomGreenColor() -> SKColor{
var h:CGFloat, s:CGFloat, l:CGFloat
let hRange:CGFloat = 140.0 - 90.0
let sRange:CGFloat = 100.0 - 50.0
let lRange:CGFloat = 60.0 - 40.0
h = (90.0 + CGFloat(((CGFloat(arc4random_uniform(101))/100.0) * hRange)))/3.6/100.0
// s = (50.0 + CGFloat(((CGFloat(arc4random_uniform(101))/100.0) * sRange)))/100.0
s = 100.0/100.0
l = (60.0 + CGFloat(((CGFloat(arc4random_uniform(101))/100.0) * lRange)))/100.0
return SKColor(hue: h, saturation: s, brightness: l, alpha: 1)
}
func randomLightGrayColor() -> SKColor{
var l:CGFloat
let lRange:CGFloat = 90.0 - 70.0
l = (70.0 + CGFloat(((CGFloat(arc4random_uniform(101))/100.0) * lRange)))/100.0
return SKColor(hue: 0, saturation: 0, brightness: l, alpha: 1)
}
func randomDarkGrayColor() -> SKColor{
var l:CGFloat
let lRange:CGFloat = 40.0 - 20.0
l = (20.0 + CGFloat(((CGFloat(arc4random_uniform(101))/100.0) * lRange)))/100.0
return SKColor(hue: 0, saturation: 0, brightness: l, alpha: 1)
}
//Get a random chunk back
func randomChunk(currentChunk: Int, endChunk: Int) -> [RectangleSprite]{
if(currentChunk > endChunk){
return loadChunk(map: LevelChunks.end, startingHeight: 1920).0
}
return loadChunk(map: getChunkAtIndex(chunk: String(randRange(lower: 1, upper: 17))),startingHeight: 1920).0
}
//Get the chunk at a given index
func getChunkAtIndex(chunk: String) -> [String]{
var levelChunk: [String]
switch(chunk){
case "1":
levelChunk = LevelChunks.one
case "2":
levelChunk = LevelChunks.two
case "3":
levelChunk = LevelChunks.three
case "4":
levelChunk = LevelChunks.four
case "5":
levelChunk = LevelChunks.five
case "6":
levelChunk = LevelChunks.six
case "7":
levelChunk = LevelChunks.seven
case "8":
levelChunk = LevelChunks.eight
case "9":
levelChunk = LevelChunks.nine
case "10":
levelChunk = LevelChunks.ten
case "11":
levelChunk = LevelChunks.eleven
case "12":
levelChunk = LevelChunks.twelve
case "13":
levelChunk = LevelChunks.thirteen
case "14":
levelChunk = LevelChunks.fourteen
case "15":
levelChunk = LevelChunks.fifteen
case "16":
levelChunk = LevelChunks.sixteen
case "17":
levelChunk = LevelChunks.seventeen
default:
levelChunk = LevelChunks.end
}
return levelChunk
}
//Get the level at index
func getLevelAtIndex(index: Int) -> String{
switch index{
case 1:
return LevelMaps.one
case 2:
return LevelMaps.two
default:
return LevelMaps.three
}
}
//Generate random number
func randRange (lower: UInt32 , upper: UInt32) -> UInt32 {
return lower + arc4random_uniform(upper - lower + 1)
}
}
//Maps
struct LevelMaps{
static let one :String = "1,1,2,2,3,4,5,5"
static let two :String = "6,7,8,9,10,10,11,11,12,13,12,13"
static let three :String = "14,14,15,15,16,16,17,17"
}