-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameState.swift
More file actions
115 lines (98 loc) · 4.17 KB
/
gameState.swift
File metadata and controls
115 lines (98 loc) · 4.17 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
//
// gameState.swift
// project-mars
//
// Created by Aleksandr Grin on 1/18/17.
// Copyright © 2017 AleksandrGrin. All rights reserved.
//
import Foundation
import SpriteKit
/// Singelton class that will keep persistant game data throughout the app.
class gameState: NSObject, NSCoding {
var GameSettings:gameSettings //Structure that stores all the game settings
var PlayerStats:gameStatistics //Structure that stores all game stats
var GameBoard:gameBoard //Contains the information of the gameboard
var GameLoop:gameLoop? //Stores the gameloop in its current state
var isGameInProgress:Bool = false
private static let Instance = loadgameState()
private override init(){
self.isGameInProgress = false
self.GameSettings = gameSettings()
self.PlayerStats = gameStatistics()
self.GameBoard = gameBoard()
}
class func sharedInstance() throws -> gameState{
return self.Instance
}
required init?(coder aDecoder: NSCoder) {
self.GameSettings = (aDecoder.decodeObject(forKey: "GameSettings") as? gameSettings)!
self.PlayerStats = (aDecoder.decodeObject(forKey: "PlayerStats") as? gameStatistics)!
self.GameBoard = (aDecoder.decodeObject(forKey: "GameBoard") as? gameBoard)!
self.GameLoop = aDecoder.decodeObject(forKey: "game") as? gameLoop
self.isGameInProgress = aDecoder.decodeBool(forKey: "inProgress")
}
func encode(with aCoder: NSCoder) {
do{
aCoder.encode(try gameState.sharedInstance().GameSettings, forKey: "GameSettings")
aCoder.encode(try gameState.sharedInstance().PlayerStats, forKey: "PlayerStats")
aCoder.encode(try gameState.sharedInstance().GameBoard, forKey: "GameBoard")
aCoder.encode(try gameState.sharedInstance().isGameInProgress, forKey: "inProgress")
aCoder.encode(try gameState.sharedInstance().GameLoop, forKey: "game")
}catch{ print("Error in gamestate Encode") }
}
class func loadgameState() ->gameState {
//gameState.deleteGameState()
let path = gameState.getFilePath()
if gameState.fileExistsAtPath(path: path){
if let rawData = NSData(contentsOfFile: path) {
/// do we get serialized data back from the attempted path?
/// if so, unarchive it into an AnyObject, and then convert to a GameData object
if(rawData.length != 0){
if let data = NSKeyedUnarchiver.unarchiveObject(with: rawData as Data) as? gameState {
return data
}
}
}
return gameState()
}else{
return gameState()
}
}
class func deleteGameState() {
let path = gameState.getFilePath()
let fileManager = FileManager.default
if gameState.fileExistsAtPath(path: path){
do { try fileManager.removeItem(atPath: path) }
catch {
print("Error in deleteGameState")
}
}
}
func save(){
do{ try gameState.sharedInstance().isGameInProgress = true } catch { print("Error in gamestate save") }
let path = gameState.getFilePath()
gameState.deleteGameState()
let saveData = NSKeyedArchiver.archivedData(withRootObject: self)
let location = URL(fileURLWithPath: path)
do { try saveData.write(to: location, options: .atomic) }
catch _ {
print("error on saveData")
}
}
class func fileExistsAtPath(path:String) -> Bool {
let fileManager = FileManager.default
if fileManager.fileExists(atPath: path) {
return true
}else{
fileManager.createFile(atPath: path, contents: nil, attributes: nil)
}
return true
}
class func getFilePath() -> String{
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0] as String
let fileName = "/gameState"
let path = documentsDirectory + "\(fileName).plist"
return path
}
}