all repos — RPG @ caae07a322fba99ea546629731cbb7cbd7a8ad4b

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 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 = GameObject.Find("Player");
 68        lastPosition = player.transform.position;
 69        lastRotation = player.transform.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                fighting = "";
 99                killedEnemies = new List<string>();
100            }
101            Debug.Log("battle ended. fighting=" + fighting + ", currentLevel: " + currentLevel + ", killedEnemies:");
102            killedEnemies.ForEach(Console.WriteLine);
103            SceneManager.LoadScene("Level" + currentLevel);
104        } else //Lost Battle
105        {
106            SceneManager.LoadScene("GameOver");
107        }
108    }
109    #endregion
110
111    #region Cambio scena (evento)
112    private void sceneChanged(Scene arg0, LoadSceneMode arg1)
113    {
114        switch (arg0.name)
115        {
116            #region Inizio battaglia
117            case "Battle":
118                break;
119            #endregion
120
121            #region Battaglia vinta o ritorno all'overworld
122            case "Level0":
123            case "Level1":
124                player = FindObjectOfType<NoJumpController>().gameObject;
125
126                if(loaded || fighting != ""){
127                    player.transform.SetPositionAndRotation(lastPosition, lastRotation);
128                    loaded = false;
129                }
130                foreach (string enemy in killedEnemies)
131                {
132                    GameObject.Find(enemy).SetActive(false);
133                }
134                break;
135            #endregion
136
137            #region Battaglia persa
138            case "Menu":
139
140                break;
141            case "GameOver":
142
143                break;
144            #endregion
145
146            #region Altro
147            default:
148                break;
149                #endregion
150        }
151    }
152    #endregion
153
154    #region Getters, setters
155    public string GetEnemyType()
156    {
157        return enemyType;
158    }
159
160    public string GetPlayTime(){
161        return UI.getPlayTimeString(timer);
162    }
163    #endregion
164
165    #region Party Management
166    private void initParty(string[] names){
167        party = new Dictionary<string, Stats>();
168        foreach(string s in names)
169        {
170            addToParty(s);
171        }
172    }
173    public bool addToParty(string name)
174    {
175        if(!party.ContainsKey(name)){
176            party.Add(name, new Stats(name));
177            return true;
178        }
179        return false;
180    }
181    public bool removeFromParty(string name)
182    {
183        if(party.ContainsKey(name)){
184            party.Remove(name);
185        return true;
186        }
187        return false;
188    }
189    #endregion
190
191    #region Save and Load
192    public Save CreateSaveGameObject()
193    {
194        Transform t = FindObjectOfType<NoJumpController>().transform; //Find player
195        Save save = new Save();
196        save.timestamp = DateTime.Now;
197
198        save.currentLevel = currentLevel;
199        save.enemiesKilled = new List<string>(killedEnemies);
200        save.gold = gold;
201        save.party = new Dictionary<string, Stats>(party);
202        save.inventory = new List<ItemInfo>(inventory.getContents());
203        save.fighting = fighting;
204        save.posx = t.position.x;
205        save.posy = t.position.y;
206        save.posz = t.position.z;
207        save.rot0 = t.rotation.x;
208        save.rot1 = t.rotation.y;
209        save.rot2 = t.rotation.z;
210        save.rot3 = t.rotation.w;
211        save.timerfloat = timer;
212        //Debug.Log("saving scene Level" + currentLevel + ", fighting: " + fighting);
213        return save;
214    }
215
216    public void LoadManager(int cl, List<string> ke, int g, Dictionary<string, Stats> p, Inventory i, string f, Vector3 pos, Quaternion rot, float timerfloat)
217    {
218        loaded = true;
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}