Assets/Scripts/Utility/GameMaster.cs (view raw)
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.IO;
5using System.Runtime.Serialization.Formatters.Binary;
6using UnityEngine;
7using UnityEngine.SceneManagement;
8
9public class GameMaster : Singleton<GameMaster>
10{
11 #region Pubblica
12 private Dictionary<string, Stats> party;
13 public List<Stats> Party
14 {
15 get
16 {
17 List<Stats> l = new List<Stats>();
18 foreach(KeyValuePair<string, Stats> kv in party)
19 {
20 l.Add(kv.Value);
21 }
22 return l;
23 }
24 }
25
26 [Header("Inventory")]
27 public int gold = 0;
28 public Inventory inventory;
29
30 #endregion
31 #region Private
32 private float timer;
33 private BattleManager battle;
34 private GameObject player;
35 private List<string> killedEnemies = new List<string>();
36 private string fighting = "";
37 private string enemyType;
38 private Vector3 lastPosition;
39 private Quaternion lastRotation;
40 public bool loaded = false;
41
42 public int currentLevel = 0;
43
44 private string[] partyNames = { "Paladin", "Archer", "Boxer", "Mage" };
45 private List<string> bossNames = new List<string>{ "Brady", "Ganfaul" };
46 private Stats tempStats;
47 #endregion
48
49 private void Start()
50 {
51 //start timer
52 timer = 0;
53
54 //create party
55 party = new Dictionary<string, Stats>();
56
57 //add members
58 foreach(string st in partyNames)
59 {
60 addToParty(st);
61 }
62
63 //create inventory
64 inventory = new Inventory();
65 inventory.generateItem(new ItemInfo(new BottledBlessing(), 2));
66 inventory.generateItem(new ItemInfo(new BottledMiracle(), 1));
67 inventory.generateItem(new ItemInfo(new Potion(), 1));
68
69 SceneManager.sceneLoaded += sceneChanged;
70 }
71 private void Update()
72 {
73 timer += Time.deltaTime;
74 }
75
76 #region InizioBattaglia
77 public void EnterBattle(string enemyName, string enemyType)
78 {
79 fighting = enemyName;
80 this.enemyType = enemyType;
81 player = GameObject.Find("Player");
82 lastPosition = player.transform.position;
83 lastRotation = player.transform.rotation;
84
85 SceneManager.LoadScene("Battle");
86 }
87 #endregion
88
89 #region FineBattaglia
90 public void EndBattle(bool didYouWin)
91 {
92 if (didYouWin) //Won Battle
93 {
94 battle = FindObjectOfType<BattleManager>();
95
96 //save party stats for later battles
97 foreach (Fighter f in battle.fighters)
98 {
99 if(party.TryGetValue(f.ToString(), out tempStats)) //save the reference to party's stats in tempStats
100 {
101 tempStats.HP = f.stats.HP;
102 tempStats.MP = f.stats.MP;
103 }
104 }
105 gold += battle.enemyGold;
106 inventory.addInventory(battle.enemyItems);
107
108 killedEnemies.Add(fighting);
109
110 if(bossNames.Contains(fighting)){
111 currentLevel++;
112 fighting = "";
113 killedEnemies = new List<string>();
114 }
115 Debug.Log("battle ended. fighting=" + fighting + ", currentLevel: " + currentLevel + ", killedEnemies:");
116 killedEnemies.ForEach(Console.WriteLine);
117 SceneManager.LoadScene("Level" + currentLevel);
118 } else //Lost Battle
119 {
120 SceneManager.LoadScene("GameOver");
121 }
122 }
123 #endregion
124
125 #region Cambio scena (evento)
126 private void sceneChanged(Scene arg0, LoadSceneMode arg1)
127 {
128 switch (arg0.name)
129 {
130 #region Inizio battaglia
131 case "Battle":
132 break;
133 #endregion
134 #region Battaglia vinta o ritorno all'overworld
135 case "Level0":
136 case "Level1":
137 case "Level2":
138 player = FindObjectOfType<NoJumpController>().gameObject;
139
140 if (fighting == "") //first spawn
141 {
142 if (!loaded)
143 {
144 lastPosition = player.transform.position;
145 lastRotation = player.transform.rotation;
146 }
147 }
148 else
149 { //return from battle
150 foreach (string enemy in killedEnemies)
151 {
152 GameObject.Find(enemy).SetActive(false);
153 }
154 }
155 player.transform.SetPositionAndRotation(lastPosition, lastRotation);
156 break;
157 #endregion
158 #region Battaglia persa
159 case "Menu":
160
161 break;
162 case "GameOver":
163
164 break;
165 #endregion
166 #region Altro
167 default:
168 break;
169 #endregion
170 }
171 }
172 #endregion
173
174 #region Getters, setters
175 public string GetEnemyType()
176 {
177 return enemyType;
178 }
179 #endregion
180
181 #region Party Management
182 public void addToParty(string name)
183 {
184 party.Add(name, new Stats(name));
185 }
186 public void removeFromParty(string name)
187 {
188 party.Remove(name);
189 }
190 #endregion
191
192 #region Save and Load
193 public Save CreateSaveGameObject()
194 {
195 Transform t = FindObjectOfType<NoJumpController>().transform; //Find player
196 Save save = new Save();
197 save.timestamp = DateTime.Now;
198
199 save.currentLevel = currentLevel;
200 save.enemiesKilled = new List<string>(killedEnemies);
201 save.gold = gold;
202 save.party = new Dictionary<string, Stats>(party);
203 save.inventory = new List<ItemInfo>(inventory.getContents());
204 save.fighting = fighting;
205 save.posx = t.position.x;
206 save.posy = t.position.y;
207 save.posz = t.position.z;
208 save.rot0 = t.rotation.x;
209 save.rot1 = t.rotation.y;
210 save.rot2 = t.rotation.z;
211 save.rot3 = t.rotation.w;
212 save.timerfloat = timer;
213 return save;
214 }
215
216 public void LoadManager(int cl, List<string> ke, int g, Dictionary<string, Stats> p, Inventory i, string f, Vector3 pos, Quaternion rot, float timerfloat)
217 {
218 loaded = true;
219 currentLevel = cl;
220 killedEnemies = ke;
221 gold = g;
222 party = p;
223 inventory = i;
224 fighting = f;
225 lastPosition = pos;
226 lastRotation = rot;
227 timer = timerfloat; //i may need to initialize this with new first.
228 }
229
230 public void resetGame()
231 {
232 loaded = false;
233 currentLevel = 0;
234 killedEnemies = new List<string>();
235 gold = 0;
236 party = new Dictionary<string, Stats>();
237 foreach(string s in partyNames)
238 {
239 party.Add(s, new Stats(s));
240 }
241 inventory = new Inventory();
242 fighting = "";
243 timer = 0;
244 }
245 #endregion
246}