all repos — RPG @ 9639697424c1e8744f7291270ed4fcc647c4eee7

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

Assets/Scripts/Utility/UI.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.UI;
  8
  9public static class UI
 10{
 11    private static GameMaster gm = GameMaster.Instance;
 12    private static BattleUIManager battle = null;
 13    private static Coroutine text_coroutine;
 14
 15
 16    //hh, HH: 24-hour, 12-hour; MMMM, MM: March, 03
 17    public static string SaveDateFormat = "dd/MM/yyyy HH:mm";
 18    public static float TextSpeed = PlayerPrefs.GetFloat("TextSpeed", 0.015f);
 19    public static float BattleSpeed = PlayerPrefs.GetFloat("BattleSpeed", 1f);
 20    public static bool displaying_text = false;
 21
 22    public static void setSLButtonText(Button btn, int slot, bool load)
 23    {
 24        //Debug.Log("filling button " + btn.ToString());
 25        int seconds;
 26        DateTime dt = GetSlotDateTime(slot, out seconds);
 27        if(dt == default)
 28        {
 29            if(load)
 30            {
 31                btn.enabled = false;
 32            } else
 33            {
 34                btn.enabled = true;
 35            }
 36            btn.GetComponentInChildren<Text>().text = "Empty";
 37        } else
 38        {
 39            btn.GetComponentInChildren<Text>().text = dt.ToString(SaveDateFormat) + "\nPlay time " + Mathf.Floor(seconds / 60).ToString("00") + ":" + seconds.ToString("00");
 40            btn.enabled = true;
 41        }
 42        
 43    }
 44
 45    public static DateTime GetSlotDateTime(int slot, out int seconds)
 46    {
 47        if (File.Exists(Application.persistentDataPath + "/game" + slot + ".save"))
 48        {
 49            BinaryFormatter bf = new BinaryFormatter();
 50            FileStream file = File.Open(Application.persistentDataPath + "/game" + slot + ".save", FileMode.Open);
 51            Save save = (Save)bf.Deserialize(file);
 52            file.Close();
 53            seconds = Mathf.RoundToInt(save.timerfloat % 60);
 54            return save.timestamp;
 55        }
 56        else
 57        {
 58            seconds = 0;
 59            return default;
 60        }
 61    }
 62    public static bool ShowDamage(Fighter target, int danno, bool didCrit)
 63    {
 64        if (!battle)
 65        {
 66            battle = UnityEngine.Object.FindObjectOfType<BattleUIManager>();
 67        }
 68
 69        if (battle)
 70        {
 71            battle.ShowDamage(target, danno, didCrit);
 72            return true;
 73        }
 74        else
 75        {
 76            return false;
 77        }
 78    }
 79
 80    #region Slow Text
 81    public static void changeText(Text textComp, string content) //change any text slowly.
 82    {
 83        if(Time.timeScale == 0f)
 84        {
 85            textComp.text = content;
 86        } else
 87        {
 88            if (displaying_text)
 89            {
 90                stopText();
 91            }
 92            textComp.text = "";
 93            text_coroutine = gm.StartCoroutine(TypeText(textComp, content, null, null, TextSpeed));
 94        }
 95    }
 96
 97    public static void changeDialogue(Text textComp, string content, AudioSource source, AudioClip clip) //change text with sound
 98    {
 99        if (Time.timeScale == 0f)
100        {
101            textComp.text = content;
102        }
103        else
104        {
105            if (displaying_text)
106            {
107                stopText();
108
109            }
110            textComp.text = "";
111            text_coroutine = gm.StartCoroutine(TypeText(textComp, content, source, clip, TextSpeed));
112        }
113    }
114    public static void stopText()
115    {
116        displaying_text = false;
117        gm.StopCoroutine(text_coroutine);
118    }
119
120
121    public static IEnumerator TypeText(Text textComp, string message, AudioSource source, AudioClip clip, float seconds)
122    {
123        if(message == null)
124        {
125            yield break;
126        }
127        displaying_text = true;
128        int i = 0;
129        foreach (char letter in message.ToCharArray())
130        {
131            i++;
132            textComp.text += letter;
133            if (clip && i % 2 == 0)
134            {
135                source.PlayOneShot(clip);
136            }
137
138            yield return null;
139            yield return new WaitForSeconds(seconds);
140        }
141        displaying_text = false;
142    }
143    #endregion
144
145    public static void updateItems(UIManager ui, Inventory inv, Button[] item_buttons)
146    {
147        int i = 0;
148        foreach (ItemInfo it in inv.getContents())
149        {
150            if (i == item_buttons.Length)
151            {
152                break;
153            }
154
155            //show item_buttons
156            if (it.item.consumable)
157            {
158                item_buttons[i].GetComponent<ActionButton>().setDescription(it.GetDescription());
159                item_buttons[i].onClick.AddListener(() => ui.showTargets(it));
160                item_buttons[i].gameObject.SetActive(true);
161                item_buttons[i].GetComponentInChildren<Text>().text = it.ToString();
162                i++;
163            }
164        }
165        for (; i < item_buttons.Length; i++) //disable remaining item_buttons 
166        {
167            item_buttons[i].gameObject.SetActive(false);
168        }
169    }
170
171    public static void updateMeters(Stats s, Transform t)
172    {
173        t.GetComponent<Text>().text = s.Name + ":";
174
175        Transform tmphp = t.GetChild(0), tmpmp = null;
176
177        tmphp.GetComponent<Text>().text = (int)s.HP + "/" + (int)s.MaxHP + " HP";
178        tmphp.GetComponentInChildren<Slider>().value = (float)(s.HP / s.MaxHP);
179        if (s.MaxMP > 0)
180        {
181            tmpmp = t.GetChild(1);
182            tmpmp.GetComponent<Text>().text = (int)s.MP + "/" + (int)s.MaxMP + " MP";
183            tmpmp.GetComponentInChildren<Slider>().value = (float)(s.MP / s.MaxMP);
184        }
185    }
186
187    public static void showCursor(bool toggle){
188        Cursor.visible = toggle;
189        if(toggle){
190            Cursor.lockState = CursorLockMode.None;
191        } else {
192            Cursor.lockState = CursorLockMode.Locked;
193        }
194    }
195}