all repos — RPG @ dd5f6156ac54813c7cb4bff643887514808b3930

Fully functional 3D turn based role playing game coded in C# and base Unity.

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