all repos — RPG @ a40a52248252949030dbbef05eb6f438175b8d4e

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