-
Notifications
You must be signed in to change notification settings - Fork 0
/
LevelUnit.swift
executable file
·101 lines (69 loc) · 3.15 KB
/
LevelUnit.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
import Foundation
import SpriteKit
class LevelUnit:SKNode {
var imageName:String = ""
var backgroundSprite:SKSpriteNode = SKSpriteNode()
var width:CGFloat = 0
var height:CGFloat = 0
var type:LevelType = LevelType.ground
var xAmount:CGFloat = 1 //essentially this is our speed
var direction:CGFloat = 1 //will be saved as either 1 or -1
var numberOfObjectsInLevel:UInt32 = 0
var offscreenCounter:Int = 0 //anytime an object goes offscreen we add to this, for resetting speed purposes
var maxObjectsInLevelUnit:UInt32 = 2
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
init (width:CGFloat, height:CGFloat, xPos:CGFloat, yPos:CGFloat, isFirst:Bool) {
super.init()
zPosition = -1;
position = CGPoint(x: xPos, y: yPos);
self.width = width;
self.height = height;
let diceRoll = arc4random_uniform(5)
if (diceRoll == 0) {
imageName = "Wild_West_Background1"
} else if (diceRoll == 1) {
imageName = "Wild_West_Background2"
} else if (diceRoll == 2) {
imageName = "Wild_West_Background1"
} else if (diceRoll == 3) {
imageName = "Wild_West_Background2"
} else if (diceRoll == 4) {
if (isFirst == false) {
imageName = "toxic_bg2"
type = LevelType.water
} else {
imageName = "Wild_West_Background2"
}
}
let theSize:CGSize = CGSize(width: width, height: height)
let tex:SKTexture = SKTexture(imageNamed: imageName)
backgroundSprite = SKSpriteNode(texture: tex, color: SKColor.clear, size: theSize)
self.addChild(backgroundSprite)
self.name = "levelUnit"
//self.position = CGPointMake(backgroundSprite.size.width / 2, 0)
backgroundSprite.physicsBody = SKPhysicsBody(rectangleOf: backgroundSprite.size, center:CGPoint(x: 0, y: -backgroundSprite.size.height * 0.88))
backgroundSprite.physicsBody!.isDynamic = false
backgroundSprite.physicsBody!.restitution = 0
if (type == LevelType.water) {
backgroundSprite.physicsBody!.categoryBitMask = BodyType.water.rawValue
backgroundSprite.physicsBody!.contactTestBitMask = BodyType.water.rawValue
zPosition = 400
} else if (type == LevelType.ground){
backgroundSprite.physicsBody!.categoryBitMask = BodyType.ground.rawValue
backgroundSprite.physicsBody!.contactTestBitMask = BodyType.ground.rawValue
}
if ( isFirst == false ) {
createObstacle()
}
}
// create obstacles on level unit
func createObstacle() {
numberOfObjectsInLevel = arc4random_uniform(maxObjectsInLevelUnit) + 1 // shouldn't be zero
for _ in 0 ..< Int(numberOfObjectsInLevel) {
let obstacle:Object = Object(type: type, spreadWidth: width, spreadHeight: height)
addChild(obstacle)
}
}
}