all repos — RPG @ e473e7dcfb2acd82870d5f2ccbbd3d9539e07c45

Fully functional 3D turn based role playing game coded in C# and base Unity.

Assets/Scripts/Overworld/OverworldUIManager.cs (view raw)

  1using System;
  2using System.Collections;
  3using System.Collections.Generic;
  4using UnityEngine;
  5using UnityEngine.Events;
  6using UnityEngine.UI;
  7
  8public class OverworldUIManager : UIManager
  9{
 10    //info display
 11    private GameMaster gm;
 12    private SettingsManager sm;
 13    private int i = 0;
 14    private GoodGuy currentSource, currentTarget;
 15    private Action currentAction;
 16
 17    public int currentPanel = 0;
 18
 19    //inventory info
 20    [Header("Set panels here")]
 21    [SerializeField] private GameObject partyObject = null;
 22    [SerializeField] private List<GoodGuy> party;
 23    [SerializeField] private GameObject pause_panel = null;
 24    [SerializeField] private GameObject[] panel = null;
 25    public Text desc_text = null;
 26    public enum Panel
 27    {
 28        Main = 0,
 29        Inventory = 1,
 30        Abilities = 2,
 31        Status = 3,
 32        Save = 4,
 33        Load = 5,
 34        Settings = 6,
 35        Quit = 7,
 36        Description = 8
 37    }
 38
 39    public void showPause(bool onOff)
 40    {
 41        hideAllPanels();
 42        if (onOff)
 43        {
 44            pause_panel.SetActive(true);
 45        } else
 46        {
 47            currentPanel = 0;
 48            pause_panel.SetActive(false);
 49        }
 50    }
 51    void Start()
 52    {
 53        gm = GameMaster.Instance;
 54        sm = panel[(int)Panel.Settings].GetComponent<SettingsManager>();
 55        desc_text = panel[(int)Panel.Description].GetComponentInChildren<Text>();
 56
 57        //set party
 58        int i = 0;
 59        GoodGuy g;
 60        party = new List<GoodGuy>();
 61        foreach(Stats s in gm.Party)
 62        {
 63            g = AbilityDB.GetGoodGuy(partyObject, s);
 64            g.inBattle = false;
 65            g.stats = gm.Party[i];
 66            party.Add(g);
 67            i++;
 68        }
 69
 70        showPause(false);
 71    }
 72
 73    public void hideAllPanels()
 74    {
 75        for(i = 1; i < panel.Length; i++)
 76        {
 77            panel[i].SetActive(false);
 78        }
 79        desc_text.text = "";
 80    }
 81
 82    public void ShowPanel(int p) //shows and updates panels
 83    {
 84        if (p < 0 || p > 7)
 85            return;
 86        if (p == currentPanel)
 87        {
 88            //if equal, disable it
 89            panel[p].SetActive(false);
 90            panel[(int)Panel.Description].SetActive(false);
 91            currentPanel = 0;
 92            return;
 93        }
 94        if(currentPanel != 0)
 95        {
 96            //disable other panel, except main
 97            panel[currentPanel].SetActive(false);
 98            panel[(int)Panel.Description].SetActive(false);
 99        }
100        //enable new panel
101        panel[p].SetActive(true);
102        panel[(int)Panel.Description].SetActive(true);
103        currentPanel = p;
104
105        //Handle Panel
106        switch ((Panel)p)
107        {
108            case Panel.Inventory:
109                handleInventory();
110                break;
111            case Panel.Abilities:
112                handleAbilities();
113                break;
114            case Panel.Status:
115                handleStatus();
116                break;
117            case Panel.Save:
118                handleSave();
119                break;
120            case Panel.Load:
121                handleLoad();
122                break;
123            case Panel.Settings:
124                handleSettings();
125                break;
126            case Panel.Quit:
127                handleQuit();
128                break;
129            default:
130                break;
131        }
132    }
133
134    private void handleInventory()
135    { 
136        //get inventory buttons object
137        Transform tmp = panel[(int)Panel.Inventory].transform.GetChild(1);
138
139        Button[] item_buttons = new Button[tmp.childCount];
140        for(int i = 0; i < tmp.childCount; i++)
141        {
142            item_buttons[i] = tmp.GetChild(i).GetComponent<Button>();
143        }
144        UI.updateItems(this, gm.inventory, item_buttons);
145    }
146
147    private void handleAbilities()
148    {
149        updateSTPanel(panel[(int)Panel.Abilities].transform.GetChild(0), party, true);
150        selectedSource(null);
151    }
152    private void handleStatus()
153    {
154        panel[(int)Panel.Description].SetActive(false); //hide description
155        updateSTPanel(panel[(int)Panel.Status].transform.GetChild(0), party, false);
156    }
157    private void handleSave()
158    {
159        panel[(int)Panel.Description].SetActive(false); //hide description
160        
161        for(int i = 0; i < 3; i++)
162        {
163            UI.setSLButtonText(panel[(int)Panel.Save].transform.GetChild(i).GetComponent<Button>(), i);
164        }
165
166    }
167    private void handleLoad()
168    {
169        panel[(int)Panel.Description].SetActive(false); //hide description
170        
171        for (int i = 0; i < 3; i++)
172        {
173            UI.setSLButtonText(panel[(int)Panel.Load].transform.GetChild(i).GetComponent<Button>(), i);
174        }
175    }
176    private void handleSettings()
177    {
178
179    }
180    private void handleQuit()
181    {
182        panel[(int)Panel.Description].SetActive(false); //hide description
183    }
184
185    public override void showTargets(Action ac)
186    {
187        selectedAction(ac);
188    }
189
190    private void updateSTPanel(Transform panel, List<GoodGuy> list, bool source)
191    {
192
193        int i;
194        Transform [] btn = new Transform[panel.childCount];
195        for (i = 0; i < panel.childCount; i++)
196        {
197            btn[i] = panel.GetChild(i);
198        }
199
200        i = 0;
201        foreach(GoodGuy g in list)
202        {
203            if (i > btn.Length)
204                break;
205
206            UI.updateMeters(g.stats, btn[i].GetChild(0));
207
208            btn[i].GetComponent<Button>().onClick.RemoveAllListeners();
209            if (source)
210            {
211                btn[i].GetComponent<Button>().onClick.AddListener(() => selectedSource(g));
212            } else
213            {
214                btn[i].GetComponent<Button>().onClick.AddListener(() => selectedTarget(g, panel.gameObject));
215            }
216
217            i++;
218        }
219    }
220
221    private void selectedSource(GoodGuy s)
222    {
223        currentSource = s;
224        Transform t = panel[(int)Panel.Abilities].transform.GetChild(2);
225
226        int i = 0;
227        Button btn;
228        if (s != null)
229        {
230            
231            foreach (Ability a in s.stats.GetAbilities())
232            {
233                if (i > t.childCount)
234                    break;
235                if (a.ow_usable)
236                {
237                    btn = t.GetChild(i).GetComponent<Button>();
238                    btn.gameObject.SetActive(true);
239                    btn.GetComponentInChildren<Text>().text = a.ToString();
240                    btn.onClick.RemoveAllListeners();
241                    btn.GetComponent<ActionButton>().setDescription(a.GetDescription());
242                    btn.onClick.AddListener(() => selectedAction(a));
243                    i++;
244                }
245
246            }
247        }
248        if(i == 0 && s != null)
249        {
250            btn = t.GetChild(0).GetComponent<Button>();
251            btn.gameObject.SetActive(true);
252            btn.GetComponentInChildren<Text>().text = "None";
253            btn.enabled = false;
254            btn.onClick.RemoveAllListeners();
255            btn.GetComponent<ActionButton>().setDescription(s + " doesn't have any overworld abilities...");
256            i++;
257        }
258        for (; i < t.childCount; i++)
259        {
260            t.GetChild(i).gameObject.SetActive(false);
261        }
262    }
263
264    private void selectedAction(Action action)
265    {
266        currentAction = action;
267
268        Transform p = panel[currentPanel].transform;
269        Transform tp = null, sp = null;
270        if(action is Ability)
271        {
272            sp = p.GetChild(0);
273            tp = p.GetChild(3);
274        } else if (action is ItemInfo)
275        {
276            sp = null;
277            tp = p.GetChild(2);
278        } else
279        {
280            Debug.Log("What are you doing exactly");
281            return;
282        }
283
284        tp.gameObject.SetActive(true);
285        updateSTPanel(tp, party, false);
286        if (sp != null)
287        {
288            updateSTPanel(sp, party, true);
289        }
290
291        //also update sources panel if present
292    }
293
294    private void selectedTarget(GoodGuy s, GameObject target_panel)
295    {
296        if(currentAction != null)
297        {
298            bool output;
299            currentTarget = s;
300            string str = null;
301            switch (currentPanel)
302            {
303
304                case (int)Panel.Inventory:
305                    Debug.Log("Item: " + currentAction + ", Target: " + currentTarget);
306                    str = AbilityDB.Process(party[0], currentAction, currentTarget, true, out output);
307                    handleInventory();
308                    target_panel.SetActive(false);
309                    break;
310                case (int)Panel.Abilities:
311                    Debug.Log("Source: " + currentSource + ", Action: " + currentAction + ", Target: " + currentTarget);
312                    str = AbilityDB.Process(currentSource, currentAction, currentTarget, true, out output);
313                    target_panel.SetActive(false);
314                    updateSTPanel(panel[(int)Panel.Abilities].transform.GetChild(0), party, true);
315                    break;
316                default:
317                    Debug.Log("You shouldn't be here.");
318                    break;
319            }
320            
321            UI.changeText(desc_text, str);
322        }
323
324        if(currentPanel == (int)Panel.Status)
325        {
326            panel[(int)Panel.Status].transform.GetChild(2).GetComponent<Text>().text = s + "\n" + s.stats.ToString() + "\n\n\n" + s.description;
327        }
328    }
329}