-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptionsMenuScene.swift
More file actions
403 lines (365 loc) · 21.8 KB
/
OptionsMenuScene.swift
File metadata and controls
403 lines (365 loc) · 21.8 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//
// OptionsMenuScene.swift
// HexWars
//
// Created by Aleksandr Grin on 9/30/17.
// Copyright © 2017 AleksandrGrin. All rights reserved.
//
import Foundation
import SpriteKit
class OptionsMenuScene: SKScene {
weak var parentViewController:OptionsMenu?
var menuButtonRecognizer:UITapGestureRecognizer?
var tapSoundMaker:SKAudioNode?
let fadeDuration = 0.25
var possiblePlayerColors:Array<SKColor> = [.red, .blue, .green, .yellow, .purple, .orange, .cyan, .magenta, .brown]
override func didMove(to view: SKView) {
setupBackGround(for: view)
setupMenuStyle(for: view){
self.setupTopMenuButtons(for: view){}
self.setupOptionsButtons(for: view){}
}
menuButtonRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleMenuTap))
self.scene?.view?.addGestureRecognizer(menuButtonRecognizer!)
if GameState.sharedInstance().mainPlayerOptions!.chosenSoundToggle! == .soundOn {
if let path = Bundle.main.url(forResource: "TapSound", withExtension: "wav", subdirectory: "Sounds"){
self.tapSoundMaker = SKAudioNode(url: path)
self.tapSoundMaker!.autoplayLooped = false
self.tapSoundMaker!.isPositional = false
self.tapSoundMaker!.run(SKAction.changeVolume(to: 0.10, duration: 0))
self.addChild(self.tapSoundMaker!)
}
}
}
private func setupBackGround(for view: SKView){
let backGroundColorImage = SKSpriteNode(texture: SKTexture(imageNamed: "Background"), size: view.frame.size)
backGroundColorImage.zPosition = -2
backGroundColorImage.anchorPoint = CGPoint(x: 0, y: 0)
let backGroundStyleImage = SKSpriteNode(texture: SKTexture(imageNamed: "BackGroundStyleV1"), size: view.frame.size)
backGroundStyleImage.zPosition = -1
backGroundStyleImage.anchorPoint = CGPoint(x: 0.5, y: 0.5)
backGroundStyleImage.colorBlendFactor = 1.0
backGroundStyleImage.blendMode = .add
backGroundStyleImage.name = "backGroundStyleImage"
backGroundStyleImage.color = GameState.sharedInstance().mainPlayerOptions!.chosenPlayerColor!.getColorFromCode()
self.addChild(backGroundColorImage)
self.addChild(backGroundStyleImage)
animateBackGround(for: view)
}
private func animateBackGround(for view: SKView){
let path = CGMutablePath()
let refRect = CGRect(x: view.frame.width / 2 - 12, y: view.frame.height / 2 - 12, width: 24, height: 24)
path.addEllipse(in: refRect)
let bkg1 = self.childNode(withName: "backGroundStyleImage") as! SKSpriteNode
let animation = SKAction.repeatForever(SKAction.follow(path, asOffset: false, orientToPath: false, speed: 7))
bkg1.run(animation)
}
private func setupMenuStyle(for view: SKView, completion: @escaping ()->()){
let trackBarAnimationTime = 0.20
var moveIn = SKAction.move(to: CGPoint(x: 0, y: view.frame.height - 80), duration: trackBarAnimationTime)
var entryAnimation = SKAction.sequence([SKAction.group([SKAction.fadeAlpha(to: 1, duration: trackBarAnimationTime), moveIn])])
let setupTopArray = [[1, 1, 1],
[1, 1, 1]]
let setupBotArray = [[0, 0, 1],
[1, 1, 1],
[1, 1, 1],
[1, 1, 1],
[1, 1, 1],
[1, 1, 0]]
let topTrackBar = tileUIBuilder.createTileBar(scene: self, enterFromLeft: false, name: "topBar", yPosition: view.frame.height - 80)
topTrackBar.run(entryAnimation){
tileUIBuilder.generateTilesUsingArray(scene: self, configuration: setupTopArray, atTrack: topTrackBar, evenIndented: false, enterFromLeft: false, offset: nil){ }
}
moveIn = SKAction.move(to: CGPoint(x: 0, y: view.frame.height / 2 ), duration: trackBarAnimationTime)
entryAnimation = SKAction.sequence([SKAction.wait(forDuration: 0.3), SKAction.group([SKAction.fadeAlpha(to: 1, duration: trackBarAnimationTime), moveIn])])
let bottomTrackBar1 = tileUIBuilder.createTileBar(scene: self, enterFromLeft: true, name: "midBar", yPosition: view.frame.height / 2 )
bottomTrackBar1.run(entryAnimation){
tileUIBuilder.generateTilesUsingArray(scene: self, configuration: setupBotArray, atTrack: bottomTrackBar1, evenIndented: false, enterFromLeft: false, offset: 60){
}
}
moveIn = SKAction.move(to: CGPoint(x: 0, y: bottomTrackBar1.position.y - 120), duration: trackBarAnimationTime)
entryAnimation = SKAction.sequence([SKAction.wait(forDuration: 1.2), SKAction.group([SKAction.fadeAlpha(to: 1, duration: trackBarAnimationTime), moveIn])])
let bottomTrackBar2 = tileUIBuilder.createTileBar(scene: self, enterFromLeft: false, name: "lowBar", yPosition: bottomTrackBar1.position.y - 120)
bottomTrackBar2.run(entryAnimation){
completion()
}
}
private func setupTopMenuButtons(for view: SKView, completion: @escaping ()->()){
let returnButton = SpriteButton(button: SKTexture(imageNamed: "RegularHex"), buttonTouched: SKTexture(imageNamed: "RegularHex_TouchUpInside"))
returnButton.setButtonText(text: "Return")
returnButton.setButtonTextFont(size: 18)
returnButton.text!.position.y += 5
self.addChild(returnButton)
returnButton.name = "returnButton"
returnButton.alpha = 0
returnButton.zPosition = 2
returnButton.anchorPoint = CGPoint(x: 0.5, y: 0.5)
if let buttonPosition = self.childNode(withName: "transparent_c0_r1_topBar")?.position{
returnButton.position = buttonPosition
}
returnButton.run(SKAction.fadeAlpha(to: 1, duration: fadeDuration)){
completion()
}
}
private func setupOptionsButtons(for view: SKView, completion: @escaping ()->()){
let soundOnButton = SpriteButton(button: SKTexture(imageNamed: "RegularHex"), buttonTouched: SKTexture(imageNamed: "RegularHex_TouchUpInside"))
soundOnButton.setButtonText(text: "Sound \n\nON")
soundOnButton.setButtonTextFont(size: 18)
soundOnButton.text!.position.y += 5
self.addChild(soundOnButton)
soundOnButton.name = "SoundOnButton"
soundOnButton.alpha = 0
soundOnButton.zPosition = 2
soundOnButton.anchorPoint = CGPoint(x: 0.5, y: 0.5)
soundOnButton.addButtonVariation(text: "Sound \n\nOFF")
if let buttonPosition = self.childNode(withName: "transparent_c0_r1_midBar")?.position{
soundOnButton.position = buttonPosition
}
let playerColorButton = SpriteButton(button: SKTexture(imageNamed: "RegularHex"), buttonTouched: SKTexture(imageNamed: "RegularHex_TouchUpInside"))
playerColorButton.setButtonText(text: "Player \n\nColor")
playerColorButton.setButtonTextFont(size: 18)
playerColorButton.text!.position.y += 5
self.addChild(playerColorButton)
playerColorButton.name = "playerColorButton"
playerColorButton.alpha = 0
playerColorButton.zPosition = 2
playerColorButton.anchorPoint = CGPoint(x: 0.5, y: 0.5)
if let buttonPosition = self.childNode(withName: "transparent_c2_r0_midBar")?.position{
playerColorButton.position = buttonPosition
}
let playerPieceButton = SpriteButton(button: SKTexture(imageNamed: "RegularHex"), buttonTouched: SKTexture(imageNamed: "RegularHex_TouchUpInside"))
playerPieceButton.setButtonText(text: "Theme")
playerPieceButton.setButtonTextFont(size: 20)
playerPieceButton.text!.position.y += 5
self.addChild(playerPieceButton)
playerPieceButton.name = "playerPieceButton"
playerPieceButton.alpha = 0
playerPieceButton.zPosition = 2
playerPieceButton.anchorPoint = CGPoint(x: 0.5, y: 0.5)
if let buttonPosition = self.childNode(withName: "transparent_c2_r4_midBar")?.position{
playerPieceButton.position = buttonPosition
}
let playerWallButton = SpriteButton(button: SKTexture(imageNamed: "RegularHex"), buttonTouched: SKTexture(imageNamed: "RegularHex_TouchUpInside"))
playerWallButton.setButtonText(text: "Wall \n\nType")
playerWallButton.setButtonTextFont(size: 18)
playerWallButton.text!.position.y += 5
self.addChild(playerWallButton)
playerWallButton.name = "playerWallButton"
playerWallButton.alpha = 0
playerWallButton.zPosition = 2
playerWallButton.anchorPoint = CGPoint(x: 0.5, y: 0.5)
if let buttonPosition = self.childNode(withName: "transparent_c1_r3_midBar")?.position{
playerWallButton.position = buttonPosition
}
let playerColorCycle = SpriteButton(button: SKTexture(imageNamed: "ColorCycleWhitened"), buttonTouched: SKTexture(imageNamed: "ColorCycleWhitened_TouchUpInside"))
self.addChild(playerColorCycle)
playerColorCycle.name = "playerColorCycle"
playerColorCycle.alpha = 1
playerColorCycle.zPosition = 2
playerColorCycle.anchorPoint = CGPoint(x: 0.5, y: 0.5)
playerColorCycle.isHidden = true
playerColorCycle.blendMode = .alpha
playerColorCycle.colorBlendFactor = 1.0
playerColorCycle.color = GameState.sharedInstance().mainPlayerOptions!.chosenPlayerColor!.getColorFromCode()
if let buttonPosition = self.childNode(withName: "transparent_c1_r1_midBar")?.position{
playerColorCycle.position = buttonPosition
}
soundOnButton.run(SKAction.fadeAlpha(to: 1, duration: fadeDuration))
playerWallButton.run(SKAction.fadeAlpha(to: 1, duration: fadeDuration))
playerColorButton.run(SKAction.fadeAlpha(to: 1, duration: fadeDuration))
playerPieceButton.run(SKAction.fadeAlpha(to: 1, duration: fadeDuration)){
completion()
}
let playerPieceCycle = SpriteButton(button: SKTexture(imageNamed: "RegularHexRTab"), buttonTouched: SKTexture(imageNamed: "RegularHexRTab_TouchUpInside"))
playerPieceCycle.setButtonText(text: "Medieval")
playerPieceCycle.setButtonTextFont(size: 14)
playerPieceCycle.text!.position.y += 5
self.addChild(playerPieceCycle)
playerPieceCycle.name = "playerPieceCycle"
playerPieceCycle.alpha = 1
playerPieceCycle.zPosition = 2
playerPieceCycle.anchorPoint = CGPoint(x: 0.5, y: 0.5)
playerPieceCycle.isHidden = true
playerPieceCycle.addButtonVariation(text: "Sci-FI")
playerPieceCycle.addButtonVariation(text: "Modern")
playerPieceCycle.iterateButtonVariation(toPosition: GameState.sharedInstance().mainPlayerOptions!.chosenGameTheme!.rawValue){}
if let buttonPosition = self.childNode(withName: "transparent_c1_r5_midBar")?.position{
playerPieceCycle.position = buttonPosition
}
let playerWallCycle = SpriteButton(button: SKTexture(imageNamed: "RegularHexRTab"), buttonTouched: SKTexture(imageNamed: "RegularHexRTab_TouchUpInside"))
playerWallCycle.setButtonText(text: "Wall A")
playerWallCycle.setButtonTextFont(size: 14)
playerWallCycle.text!.position.y += 5
self.addChild(playerWallCycle)
playerWallCycle.name = "playerWallCycle"
playerWallCycle.alpha = 1
playerWallCycle.zPosition = 2
playerWallCycle.anchorPoint = CGPoint(x: 0.5, y: 0.5)
playerWallCycle.isHidden = true
playerWallCycle.addButtonVariation(text: "Wall B")
playerWallCycle.addButtonVariation(text: "Wall C")
playerWallCycle.iterateButtonVariation(toPosition: GameState.sharedInstance().mainPlayerOptions!.chosenWallType!.rawValue){}
if let buttonPosition = self.childNode(withName: "transparent_c1_r4_midBar")?.position{
playerWallCycle.position = buttonPosition
}
}
func resetCycleButtons(){
self.childNode(withName: "playerPieceCycle")?.isHidden = true
self.childNode(withName: "playerColorCycle")?.isHidden = true
self.childNode(withName: "playerWallCycle")?.isHidden = true
}
@objc func handleMenuTap(_ sender: UITapGestureRecognizer){
let tapped = sender.location(in: self.scene?.view)
let locationInScene = self.convertPoint(fromView: tapped)
for button in (self.scene?.children)! {
if button.contains(locationInScene){
if let buttonName = button.name {
switch buttonName {
case "returnButton":
if self.tapSoundMaker != nil {
self.tapSoundMaker!.run(SKAction.play())
}
(button as! SpriteButton).buttonTouchedUpInside(){
self.parentViewController?.returnToMenu()
}
return
case "SoundOnButton":
if self.tapSoundMaker != nil {
self.tapSoundMaker!.run(SKAction.play())
}
(button as! SpriteButton).iterateButtonVariation(toPosition: nil){
(self.childNode(withName: "SoundOnButton")! as! SpriteButton).buttonTouchedUpInside(){
if (button as! SpriteButton).currentVariation == 1{
// SoundOff
GameState.sharedInstance().mainPlayerOptions!.chosenSoundToggle = SoundToggle.soundOff
self.tapSoundMaker = nil
for vc in self.parentViewController!.navigationController!.childViewControllers {
if vc is MainScreen {
(((vc as! MainScreen).view as! SKView).scene as! MainMenuScene).backGroundMusicMaker!.run(SKAction.pause())
(((vc as! MainScreen).view as! SKView).scene as! MainMenuScene).tapSoundMaker = nil
}
}
}else{
// SoundOn
GameState.sharedInstance().mainPlayerOptions!.chosenSoundToggle = SoundToggle.soundOn
if let path = Bundle.main.url(forResource: "TapSound", withExtension: "wav", subdirectory: "Sounds"){
self.tapSoundMaker = SKAudioNode(url: path)
self.tapSoundMaker!.autoplayLooped = false
self.tapSoundMaker!.isPositional = false
self.tapSoundMaker!.run(SKAction.changeVolume(to: 0.10, duration: 0))
self.addChild(self.tapSoundMaker!)
}
for vc in self.parentViewController!.navigationController!.childViewControllers {
if vc is MainScreen {
(((vc as! MainScreen).view as! SKView).scene as! MainMenuScene).backGroundMusicMaker!.run(SKAction.play())
(((vc as! MainScreen).view as! SKView).scene as! MainMenuScene).reinitSoundMaker()
}
}
}
}
}
return
case "playerColorButton":
if self.tapSoundMaker != nil {
self.tapSoundMaker!.run(SKAction.play())
}
(button as! SpriteButton).buttonTouchedUpInside(){
if self.childNode(withName: "playerColorCycle")?.isHidden == true {
self.childNode(withName: "playerColorCycle")?.isHidden = false
}else{
self.childNode(withName: "playerColorCycle")?.isHidden = true
}
}
return
case "playerWallButton":
if self.tapSoundMaker != nil {
self.tapSoundMaker!.run(SKAction.play())
}
(button as! SpriteButton).buttonTouchedUpInside(){
if self.childNode(withName: "playerWallCycle")?.isHidden == true {
self.childNode(withName: "playerWallCycle")?.isHidden = false
}else{
self.childNode(withName: "playerWallCycle")?.isHidden = true
}
}
return
case "playerWallCycle":
if self.tapSoundMaker != nil {
self.tapSoundMaker!.run(SKAction.play())
}
(button as! SpriteButton).iterateButtonVariation(toPosition: nil){
(self.childNode(withName: "playerWallCycle")! as! SpriteButton).buttonTouchedUpInside(){
if self.childNode(withName: "playerWallCycle")?.isHidden == false {
if (button as! SpriteButton).currentVariation != nil{
GameState.sharedInstance().mainPlayerOptions!.chosenWallType = WallType(rawValue: (button as! SpriteButton).currentVariation!)
}
}
}
}
return
case "playerPieceButton":
if self.tapSoundMaker != nil {
self.tapSoundMaker!.run(SKAction.play())
}
(button as! SpriteButton).buttonTouchedUpInside(){
if self.childNode(withName: "playerPieceCycle")?.isHidden == true {
self.childNode(withName: "playerPieceCycle")?.isHidden = false
}else{
self.childNode(withName: "playerPieceCycle")?.isHidden = true
}
}
return
case "playerPieceCycle":
if self.tapSoundMaker != nil {
self.tapSoundMaker!.run(SKAction.play())
}
(button as! SpriteButton).iterateButtonVariation(toPosition: nil){
(self.childNode(withName: "playerPieceCycle")! as! SpriteButton).buttonTouchedUpInside(){
if self.childNode(withName: "playerPieceCycle")?.isHidden == false {
if (button as! SpriteButton).currentVariation != nil{
GameState.sharedInstance().mainPlayerOptions!.chosenGameTheme = GameTheme(rawValue: (button as! SpriteButton).currentVariation!)
}
}
}
}
return
case "playerColorCycle":
if self.tapSoundMaker != nil {
self.tapSoundMaker!.run(SKAction.play())
}
(self.childNode(withName: "playerColorCycle")! as! SpriteButton).buttonTouchedUpInside(){
if self.childNode(withName: "playerColorCycle")?.isHidden == false {
let currentColor:UIColor = (self.childNode(withName: "playerColorCycle")! as! SKSpriteNode).color
var currentIndex = self.possiblePlayerColors.index(of: currentColor)
if currentIndex == nil {
currentIndex = 8 //Deals with the floating point rounding error on brown
}
if currentIndex! + 1 >= self.possiblePlayerColors.count {
currentIndex = 0
(self.childNode(withName: "playerColorCycle")! as! SKSpriteNode).color = self.possiblePlayerColors[currentIndex!]
}else{
currentIndex! += 1
(self.childNode(withName: "playerColorCycle")! as! SKSpriteNode).color = self.possiblePlayerColors[currentIndex!]
}
if currentIndex != nil {
GameState.sharedInstance().mainPlayerOptions!.chosenPlayerColor = PlayerColor(rawValue: currentIndex!)
(self.childNode(withName: "//backGroundStyleImage") as! SKSpriteNode).color = PlayerColor(rawValue: currentIndex!)!.getColorFromCode()
}
}
}
return
default:
break
}
}
}
}
if self.childNode(withName: "playerPieceCycle") != nil && self.childNode(withName: "playerColorCycle") != nil {
if self.nodes(at: locationInScene).contains(self.childNode(withName: "playerPieceCycle")!) == false {
if self.nodes(at: locationInScene).contains(self.childNode(withName: "playerColorCycle")!) == false {
resetCycleButtons()
}
}
}
}
}