all repos — RPG @ 9639697424c1e8744f7291270ed4fcc647c4eee7

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