-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameplayScene.swift
134 lines (97 loc) · 3.77 KB
/
GameplayScene.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
//
// GameplayScene.swift
// Jack The Giant
//
// Created by Jamie Armour on 4/06/17.
// Copyright © 2017 Jamie Armour. All rights reserved.
//
import SpriteKit
class GameplayScene: SKScene {
var player: Player?
var canMove = false;
var moveLeft = false;
var center: CGFloat?
var mainCamera: SKCameraNode?
private var cameraDistanceBeforeCreatingNewClouds = CGFloat();
var bg1: BGClass?;
var bg2: BGClass?;
var bg3: BGClass?;
var distanceBetweenClouds = CGFloat(240);
var minX = CGFloat(-148);
var maxX = CGFloat(148);
var cloudsController = CloudsController();
override func didMove(to view: SKView){
initializeVariables();
}
func initializeVariables () {
center = (self.scene?.size.width)! / (self.scene?.size.height)!;
//gets the main Player
player = self.childNode(withName: "Player") as? Player!;
//inits the Player and animations - obviously
player?.initialisePlayerAndAnimations();
//gets the Main Camera
mainCamera = self.childNode(withName: "Main Camera") as? SKCameraNode!;
bg1 = self.childNode(withName: "BG 1") as? BGClass!;
bg2 = self.childNode(withName: "BG 2") as? BGClass!;
bg3 = self.childNode(withName: "BG 3") as? BGClass!;
cloudsController.arrangeCloudsInScene(scene: self.scene!, distanceBetweenClouds: 240, center: center!, minX: minX, maxX: maxX, initialClouds: true);
cameraDistanceBeforeCreatingNewClouds = mainCamera!.position.y - 400;
}
override func update(_ currentTime: TimeInterval) {
//takes place on every frame
managePlayer();
moveCamera();
manageBackgrounds();
createNewClouds();
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
//like touch start
for touch in touches {
let location = touch.location(in: self);
if location.x > center! {
moveLeft = false;
player?.animatePlayer(moveLeft: moveLeft);
} else {
moveLeft = true;
player?.animatePlayer(moveLeft: moveLeft);
}
}
canMove = true;
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
//like touch stop
canMove = false;
player?.stopPlayerAnimation();
}
func managePlayer () {
if canMove {
player?.movePlayer(moveLeft: moveLeft);
}
}
func moveCamera () {
//slowly move the camera
self.mainCamera?.position.y -= 3;
}
func createNewClouds () {
if cameraDistanceBeforeCreatingNewClouds > mainCamera!.position.y {
cameraDistanceBeforeCreatingNewClouds = mainCamera!.position.y - 400;
cloudsController.arrangeCloudsInScene(scene: self.scene!, distanceBetweenClouds: distanceBetweenClouds, center: center!, minX: minX, maxX: maxX, initialClouds: false);
checkForChildsOutOfScreen();
}
}
private func checkForChildsOutOfScreen() {
for child in children {
if child.position.y > mainCamera!.position.y + self.scene!.size.height {
let childName = child.name?.components(separatedBy: " ");
if childName![0] != "BG" {
child.removeFromParent();
}
}
}
}
func manageBackgrounds () {
bg1?.moveBackground(camera: mainCamera!);
bg2?.moveBackground(camera: mainCamera!);
bg3?.moveBackground(camera: mainCamera!);
}
}