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