-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameViewController.swift
189 lines (157 loc) · 7.03 KB
/
GameViewController.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
//
// GameViewController.swift
// HexWars
//
// Created by Aleksandr Grin on 8/5/17.
// Copyright © 2017 AleksandrGrin. All rights reserved.
//
import UIKit
import SpriteKit
import GameplayKit
import GoogleMobileAds
import DeviceKit
//Control the ads.
fileprivate let bottomBannerAdd:String = "ca-app-pub-5462309909970544/6750410601"
var sceneFilePath: String {
//1 - manager lets you examine contents of a files and folders in your app; creates a directory to where we are saving it
let manager = FileManager.default
//2 - this returns an array of urls from our documentDirectory and we take the first path
let url = manager.urls(for: .documentDirectory, in: .userDomainMask).first
//3 - creates a new path component and creates a new file called "Data" which is where we will store our Data array.
return (url!.appendingPathComponent("GameScene").path)
}
var stateFilePath: String {
//1 - manager lets you examine contents of a files and folders in your app; creates a directory to where we are saving it
let manager = FileManager.default
//2 - this returns an array of urls from our documentDirectory and we take the first path
let url = manager.urls(for: .documentDirectory, in: .userDomainMask).first
//3 - creates a new path component and creates a new file called "Data" which is where we will store our Data array.
let pathOut = url!.appendingPathComponent("GameState").path
return pathOut
}
class GameViewController: UIViewController {
var bottomBannerViewAd:GADBannerView?
var gameScene: GameScene?
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
}
override func loadView() {
super.loadView()
self.view = SKView()
self.view.bounds.size = CGSize(width: 375, height: 667)
self.navigationController!.isNavigationBarHidden = true
}
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
if self.gameScene != nil {
view.presentScene(self.gameScene)
self.gameScene!.parentViewController = self
}else{
let scene = GameScene(size: CGSize(width: 375, height: 730))
if Device.allDevicesWithSensorHousing.contains(Device.current){
scene.size = CGSize(width: 375, height: 700)
}else{
scene.size = CGSize(width: 375, height: 667)
}
scene.scaleMode = .fill
scene.parentViewController = self
// Present the scene
view.presentScene(scene)
view.ignoresSiblingOrder = true
self.gameScene = scene
}
//The ad setup can be run async just to make sure no performance is impacted.
DispatchQueue.main.async { [unowned self] in
self.initializeBannerAds()
}
}
}
func saveCurrentGame(){
if self.gameScene != nil {
NSKeyedArchiver.archiveRootObject(self.gameScene!, toFile: sceneFilePath)
GameState.sharedInstance().wasGameSaved = true
}else{
GameState.sharedInstance().wasGameSaved = false
}
}
override var shouldAutorotate: Bool {
return false
}
func presentGameOver(){
let gameOver = GameOverScreen()
let numTurns = self.gameScene!.currentTurn
if self.gameScene!.gameModel!.players!.count > 1 {
let winningPlayer = self.gameScene!.gameModel!.players!.first as! Player
gameOver.loadWinning(player: winningPlayer, turns: numTurns, didSurrender: true)
}else{
let winningPlayer = self.gameScene!.gameModel!.players!.first as! Player
gameOver.loadWinning(player: winningPlayer, turns: numTurns, didSurrender: false)
}
if GameState.sharedInstance().playerStatistics!.isGameBeingTracked == true{
self.updatePlayerStatistics()
}
let launchTime = DispatchTime.now() + 0.2
DispatchQueue.main.asyncAfter(deadline: launchTime, execute: {
self.navigationController?.pushViewController(gameOver, animated: true)
})
}
func showTutorial(){
let tutorial = TutorialScreen()
let launchTime = DispatchTime.now() + 0.2
self.gameScene!.isPaused = true
DispatchQueue.main.asyncAfter(deadline: launchTime, execute: {
self.navigationController?.pushViewController(tutorial, animated: true)
})
}
func updatePlayerStatistics(){
GameState.sharedInstance().playerStatistics!.gamesPlayedTotal! += 1
if (self.gameScene!.gameModel!.players!.first! as! Player).isPlayerHuman == true && self.gameScene!.gameModel!.players!.count == 1 {
GameState.sharedInstance().playerStatistics!.gamesWonTotal! += 1
if GameState.sharedInstance().playerStatistics!.fastestGameWin! != 0 {
GameState.sharedInstance().playerStatistics!.fastestGameWin! = min(self.gameScene!.currentTurn, GameState.sharedInstance().playerStatistics!.fastestGameWin!)
}else{
GameState.sharedInstance().playerStatistics!.fastestGameWin! = self.gameScene!.currentTurn
}
}else{
GameState.sharedInstance().playerStatistics!.gamesLostTotal! += 1
}
}
override var prefersStatusBarHidden: Bool {
return true
}
}
extension GameViewController:GADBannerViewDelegate {
private func initializeBannerAds(){
//Initialize bottom banner ad
self.bottomBannerViewAd = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)
self.bottomBannerViewAd!.delegate = self
//Banner is initially hidden.
self.bottomBannerViewAd!.alpha = 0.0
self.view.addSubview(self.bottomBannerViewAd!)
//These two constraints will center the ad banner and place it at the top safe area of the app.
NSLayoutConstraint(item: self.bottomBannerViewAd!, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1.0, constant: 0.0).isActive = true
if Device.allDevicesWithSensorHousing.contains(Device.current){
self.bottomBannerViewAd!.center.y = self.view.frame.height - self.bottomBannerViewAd!.frame.height*0.75
}else{
self.bottomBannerViewAd!.frame.origin.y = self.view.frame.height - self.bottomBannerViewAd!.frame.height
}
self.bottomBannerViewAd!.adUnitID = bottomBannerAdd
self.bottomBannerViewAd!.rootViewController = self
self.bottomBannerViewAd!.load(GADRequest())
}
func resetBannerAds(){
self.bottomBannerViewAd = nil
}
//Check if the app has recieved an ad. If it has then fade the ad banner in and display the ad.
func adViewDidReceiveAd(_ bannerView: GADBannerView) {
bannerView.alpha = 0
UIView.animate(withDuration: 0.5, animations: {
bannerView.alpha = 1
})
}
//If an ad has not appeared
func adView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: GADRequestError) {
print(error.localizedDescription)
}
}