all repos — RPG @ 6c05a2dbbbf1a595de392cda9fc6b624274a08d9

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 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
161    public string GetTimerString(){
162        int seconds = Mathf.RoundToInt(timer % 60);
163        int minutes = Mathf.RoundToInt(seconds / 60);
164        return minutes.ToString("00") + ":" + seconds.ToString("00");
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        currentLevel = cl;
222        killedEnemies = ke;
223        gold = g;
224        party = p;
225        inventory = i;
226        fighting = f;
227        lastPosition = pos;
228        lastRotation = rot;
229        timer = timerfloat;
230        //Debug.Log("loading scene Level" + currentLevel + ", fighting: " + fighting);
231    }
232
233    public void resetGame()
234    {
235        currentLevel = 0;
236        killedEnemies = new List<string>();
237        gold = 0;
238        initParty(partyNames);
239        inventory = new Inventory();
240        fighting = "";
241        timer = 0;
242    }
243    #endregion
244}