all repos — RPG @ 53bc27fededf9e41b98fb5249608168b93ceda09

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