-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameOverScene.swift
More file actions
167 lines (144 loc) · 6.83 KB
/
GameOverScene.swift
File metadata and controls
167 lines (144 loc) · 6.83 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
//
// Created by Aleksandr Grin on 10/12/17.
// Copyright (c) 2017 AleksandrGrin. All rights reserved.
//
import Foundation
import SpriteKit
import UIKit
class GameOverScene: SKScene{
let fadeInDuration:Double = 1.0
weak var parentViewController:GameOverScreen?
var menuButtonRecognizer:UITapGestureRecognizer?
var tapSoundMaker:SKAudioNode?
override func didMove(to view: SKView) {
self.view!.ignoresSiblingOrder = true
setupBackGround(for: view)
displayGameOver(for: view){
self.createReturnBar(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!)
}
}
//If we win we should be done and restart.
GameState.sharedInstance().wasGameSaved = false
GameState.saveGameState()
}
private func setupBackGround(for view: SKView){
let backGroundColorImage = SKSpriteNode(texture: SKTexture(imageNamed: "Background"), size: view.frame.size)
backGroundColorImage.zPosition = -1
backGroundColorImage.anchorPoint = CGPoint(x: 0, y: 0)
self.addChild(backGroundColorImage)
}
private func displayGameOver(for view: SKView, completion: @escaping ()->()){
var textToUse:String = "Victory!"
if self.parentViewController!.didPlayerSurrender! == true{
textToUse = "Defeat!"
}
let gameOverText = SKLabelNode(text: "Player \(self.parentViewController!.winningPlayer!.playerId)")
gameOverText.alpha = 0.0
gameOverText.name = "victoryText"
gameOverText.fontColor = self.parentViewController!.winningPlayer!.color!
gameOverText.fontSize = 80
gameOverText.zPosition = 1
gameOverText.isHidden = false
gameOverText.fontName = "Arial-BoldMT"
gameOverText.position = CGPoint(x: view.frame.width / 2, y: view.frame.height / 2)
self.addChild(gameOverText)
let gameOverText2 = SKLabelNode(text: textToUse)
gameOverText2.alpha = 1.0
gameOverText2.name = "gameOverText2"
gameOverText2.zPosition = 1
gameOverText2.fontColor = self.parentViewController!.winningPlayer!.color!
gameOverText2.fontSize = 56
gameOverText2.isHidden = false
gameOverText2.fontName = "Arial-BoldMT"
gameOverText2.position.y = gameOverText2.position.y - 70
gameOverText.addChild(gameOverText2)
let entryAnimation = SKAction.group([SKAction.fadeAlpha(to: 1.0, duration: fadeInDuration),
SKAction.move(to: CGPoint(x: view.frame.width / 2, y: view.frame.height / 1.25), duration: fadeInDuration)])
gameOverText.run(entryAnimation){
completion()
}
let NumberOfTurns = SKLabelNode(text: "Number of Turns:")
NumberOfTurns.alpha = 0.0
NumberOfTurns.name = "NumberOfTurns"
NumberOfTurns.fontSize = 26
NumberOfTurns.zPosition = 1
NumberOfTurns.isHidden = false
NumberOfTurns.fontColor = self.parentViewController!.winningPlayer!.color!
NumberOfTurns.fontName = "Arial-BoldMT"
NumberOfTurns.position = CGPoint(x: view.frame.width / 2, y: 0)
self.addChild(NumberOfTurns)
let NumberOfTurns2 = SKLabelNode(text: "\(self.parentViewController!.winningTurns!)")
NumberOfTurns2.alpha = 1.0
NumberOfTurns2.name = "gameOverText2"
NumberOfTurns2.zPosition = 1
NumberOfTurns2.fontSize = 28
NumberOfTurns2.isHidden = false
NumberOfTurns2.fontColor = self.parentViewController!.winningPlayer!.color!
NumberOfTurns2.fontName = "Arial-BoldMT"
NumberOfTurns2.position.y = NumberOfTurns2.position.y - 40
NumberOfTurns.addChild(NumberOfTurns2)
let entryAnimation2 = SKAction.group([SKAction.fadeAlpha(to: 1.0, duration: fadeInDuration),
SKAction.move(to: CGPoint(x: view.frame.width / 2, y: view.frame.height / 2), duration: fadeInDuration)])
NumberOfTurns.run(entryAnimation2){
completion()
}
}
private func createReturnBar(for view: SKView){
let bottomBar = SKSpriteNode(texture: SKTexture(imageNamed: "TrackBar"))
bottomBar.alpha = 0
bottomBar.name = "saveBar"
bottomBar.anchorPoint = CGPoint(x: 0, y: 0.5)
bottomBar.isHidden = false
bottomBar.zPosition = 1
bottomBar.yScale = 2
bottomBar.position = CGPoint(x: 0, y: view.frame.height / 8)
self.addChild(bottomBar)
let returnButton = SpriteButton(button: SKTexture(imageNamed: "CrownIndicator"), buttonTouched: SKTexture(imageNamed: "CrownIndicator_TouchUpInside"))
returnButton.setButtonText(text: "Return to Menu")
returnButton.setButtonTextFont(size: 16)
returnButton.text!.position.y += 18
returnButton.alpha = 1.0
returnButton.name = "returnButton"
returnButton.anchorPoint = CGPoint(x: 0.5, y: 0)
returnButton.isHidden = false
returnButton.zPosition = 2
returnButton.xScale = 1.5
returnButton.position = CGPoint(x: bottomBar.frame.width / 2, y: -bottomBar.frame.height / 6)
bottomBar.addChild(returnButton)
bottomBar.run(SKAction.fadeAlpha(to: 1.0, duration: 1.0))
}
@objc func handleMenuTap(_ sender: UITapGestureRecognizer){
let tapped = sender.location(in: self.scene?.view)
let scenelocation = self.scene!.convertPoint(fromView: tapped)
let cameralocation = self.convert(scenelocation, from: self.scene!)
let touchedNodes = self.nodes(at: cameralocation)
if touchedNodes.isEmpty == false{
for button in touchedNodes {
if let buttonName = button.name {
switch buttonName {
case "returnButton":
if self.tapSoundMaker != nil {
self.tapSoundMaker!.run(SKAction.play())
}
(button as! SpriteButton).buttonTouchedUpInside(){
self.parentViewController!.returnToMainScreen()
}
return
default:
break
}
}
}
}
}
}