all repos — RPG @ 3551efcf4f46d0b1cbb9cdb7fff743c4354f4129

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