all repos — RPG @ e473e7dcfb2acd82870d5f2ccbbd3d9539e07c45

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