all repos — RPG @ d7044262357209ad359fad4b2664315aea7096d0

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