-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInterSceneDataController.cs
133 lines (111 loc) · 4.05 KB
/
InterSceneDataController.cs
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
using UnityEngine;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public class InterSceneDataController : MonoBehaviour {
bool sceneIsArea = false;
bool firstupdate = true;
GameObject player;
// Use this for initialization
void Start () {
if (GameObject.Find ("AreaMarker") != null) {
sceneIsArea = true;
player = GameObject.FindGameObjectWithTag ("Player");
if (InterSceneData.main.destinatedSpawn == "LAST") {
player.transform.position = new Vector2 (InterSceneData.main.posX, InterSceneData.main.posY);
InterSceneData.main.destinatedSpawn = "DEFAULT";
} else if (InterSceneData.main.destinatedSpawn == "TRAINER") {
player.transform.position = new Vector2 (InterSceneData.main.posX, InterSceneData.main.posY);
InterSceneData.main.destinatedSpawn = "DEFAULT";
StartCoroutine (GameObject.Find ("trainer_" + InterSceneData.main.battle_trainer).GetComponent<Trainer> ().defeated ());
} else if (InterSceneData.main.destinatedSpawn != "DEFAULT") {
GameObject spawn = GameObject.Find (InterSceneData.main.destinatedSpawn);
player.transform.position = spawn.transform.position;
InterSceneData.main.destinatedSpawn = "DEFAULT";
}
}
}
// Update is called once per frame
void Update () {
if (firstupdate) {
InterSceneData.main.lastScene = Application.loadedLevelName;
if (sceneIsArea) {
InterSceneData.main.lastArea = Application.loadedLevelName;
}
firstupdate = false;
}
if (sceneIsArea) {
if (player == null) {
ReloadPlayerObject ();
}
InterSceneData.main.posX = player.transform.position.x;
InterSceneData.main.posY = player.transform.position.y;
InterSceneData.main.heading = player.GetComponent<PlayerMovementController> ().heading;
}
InterSceneData.main.minutesPlayed += Time.deltaTime / 60;
}
void onDisable () {
InterSceneData.main.lastScene = Application.loadedLevelName;
if (sceneIsArea) {
InterSceneData.main.lastArea = Application.loadedLevelName;
}
}
void ReloadPlayerObject () {
player = GameObject.FindGameObjectWithTag ("Player");
}
public void Save () {
SaveData dat = new SaveData ();
dat.posX = InterSceneData.main.posX;
dat.posY = InterSceneData.main.posY;
dat.headingX = InterSceneData.main.heading.x;
dat.headingY = InterSceneData.main.heading.y;
dat.lastScene = InterSceneData.main.lastScene;
dat.lastArea = InterSceneData.main.lastArea;
dat.destinatedSpawn = InterSceneData.main.destinatedSpawn;
dat.playerName = InterSceneData.main.playerName;
dat.minutesPlayed = InterSceneData.main.minutesPlayed;
dat.money = InterSceneData.main.money;
dat.badges = InterSceneData.main.badges;
BinaryFormatter form = new BinaryFormatter ();
FileStream of = File.Create (Application.persistentDataPath + "/save.dat");
form.Serialize (of, dat);
of.Close ();
}
public bool Load () {
if (File.Exists (Application.persistentDataPath + "/save.dat")) {
BinaryFormatter form = new BinaryFormatter ();
FileStream inf = File.OpenRead (Application.persistentDataPath + "/save.dat");
SaveData dat = (SaveData)form.Deserialize (inf);
inf.Close ();
InterSceneData.main.posX = dat.posX;
InterSceneData.main.posY = dat.posY;
InterSceneData.main.heading.y = dat.headingY;
InterSceneData.main.heading.x = dat.headingX;
InterSceneData.main.lastScene = dat.lastScene;
InterSceneData.main.lastArea = dat.lastArea;
InterSceneData.main.destinatedSpawn = dat.destinatedSpawn;
InterSceneData.main.playerName = dat.playerName;
InterSceneData.main.minutesPlayed = dat.minutesPlayed;
InterSceneData.main.money = dat.money;
InterSceneData.main.badges = dat.badges;
return true;
} else {
return false;
}
}
}
[System.Serializable]
class SaveData {
// This is basically a non-MonoBehaviour copy of InterSceneData
public float posX;
public float posY;
public float headingX;
public float headingY;
public string lastScene;
public string lastArea;
public string destinatedSpawn;
public string playerName;
public float minutesPlayed;
public int money;
public int badges;
}