all repos — RPG @ 1be4b62bb8e62efb733d4a69a1b1690c1999981f

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