all repos — RPG @ 417c749cef61492ebc3c869d873c3c5dd78e09f8

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