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