Assets/Scripts/Battle/BattleUIManager.cs (view raw)
1using System.Collections;
2using System.Collections.Generic;
3using UnityEngine;
4using UnityEngine.UI;
5using static UnityEngine.UI.Button;
6
7public class BattleUIManager : UIManager
8{
9 private BattleManager battle;
10 private GameMaster gm;
11
12 //smart input variables
13 private const string itemsGuiName = "Items";
14 private const int maxAbilityNumber = 6;
15 private const int maxTargetNumber = 5;
16 private const int maxItemNumber = 9;
17
18 private Button[] ability_buttons;
19 private Button[] item_buttons;
20 private Button[] target_buttons;
21 private List<Ability> abilities;
22 private Coroutine text_coroutine;
23 private int i = 0;
24
25
26 public Text status;
27 public GameObject status_panel;
28 public GameObject results_panel;
29 public Text drops;
30 public GameObject input_panel;
31 public GameObject targets_panel;
32 public GameObject items_panel;
33
34 private void Start()
35 {
36 gm = GameMaster.Instance;
37 battle = GetComponent<BattleManager>();
38
39 /*
40 status_panel = GameObject.Find("status_panel");
41 status = status_panel.GetComponentInChildren<Text>();
42 input_panel = GameObject.Find("Input_UI");
43 items_panel = GameObject.Find("items_panel");
44 targets_panel = GameObject.Find("targets_panel");
45 results_panel = GameObject.Find("Results_UI");
46 drops = GameObject.Find("drops_text").GetComponent<Text>();
47 */
48
49 input_panel.SetActive(false);
50 targets_panel.SetActive(false);
51 items_panel.SetActive(false);
52 results_panel.SetActive(false);
53
54 ability_buttons = input_panel.transform.GetComponentsInChildren<Button>();
55 target_buttons = targets_panel.GetComponentsInChildren<Button>();
56 item_buttons = items_panel.GetComponentsInChildren<Button>();
57
58
59 #region Set target buttons
60 foreach (Fighter f in battle.fighters)
61 {
62 if (f.fighter_number < 0 || f.fighter_number >= maxTargetNumber)
63 {
64 i = maxTargetNumber - 1;
65 }
66 else
67 {
68 i = f.fighter_number;
69 }
70 target_buttons[i].GetComponentInChildren<Text>().text = f.ToString();
71 }
72 foreach (Button b in target_buttons)
73 {
74 if (b.GetComponentInChildren<Text>().text == "")
75 {
76 b.gameObject.SetActive(false);
77 }
78 }
79 #endregion
80 }
81
82 public void showAbilities(bool show) //show abilities menu
83 {
84 if (show && input_panel.activeSelf == false)
85 {
86 input_panel.SetActive(true);
87
88 //read abilities from current fighter
89 abilities = battle.turn.stats.GetAbilities();
90
91 i = 0;
92 foreach(Ability a in abilities)
93 {
94 if(i == maxAbilityNumber - 1)
95 {
96 break;
97 }
98
99 //show ability_buttons
100 ability_buttons[i].gameObject.SetActive(true);
101 ability_buttons[i].GetComponentInChildren<Text>().text = a.ToString();
102 ability_buttons[i].GetComponent<ActionButton>().setDescription(a.GetDescription());
103 ability_buttons[i].onClick.AddListener(() => selectTarget(a));
104 i++;
105 }
106
107 //add Items entry for everybody
108 ability_buttons[i].gameObject.SetActive(true);
109 ability_buttons[i].GetComponentInChildren<Text>().text = itemsGuiName;
110 ability_buttons[i].GetComponent<ActionButton>().setDescription("Use an item.");
111 ability_buttons[i].onClick.AddListener(() => showItems(true));
112 i++;
113
114 for (; i < maxAbilityNumber; i++)
115 {
116 //disable remaining ability_buttons
117 ability_buttons[i].gameObject.SetActive(false);
118 }
119
120 } else if (!show && input_panel.activeSelf == true)
121 {
122 input_panel.SetActive(false);
123 }
124 }
125
126
127
128 private void showItems(bool onOff)
129 {
130 if (onOff)
131 {
132 items_panel.SetActive(true);
133 targets_panel.SetActive(false);
134 UI.updateItems(this, gm.inventory, item_buttons);
135 } else
136 {
137 items_panel.SetActive(false);
138 }
139
140 }
141
142 public override void showTargets(Action ac) //show targets menu for abilities
143 {
144 targets_panel.SetActive(true);
145 if(ac is Ability)
146 {
147 items_panel.SetActive(false);
148 }
149
150 //set current buttons
151 foreach (Fighter f in battle.fighters)
152 {
153 if (f.fighter_number < 0 || f.fighter_number >= maxTargetNumber)
154 {
155 i = maxTargetNumber - 1;
156 }
157 else
158 {
159 i = f.fighter_number;
160 }
161
162 target_buttons[i].onClick.RemoveAllListeners();
163 target_buttons[i].onClick.AddListener(() => sendInput(ac, f));
164 }
165 }
166
167 private void sendInput(Action ac, Fighter target)
168 {
169 //hide panel
170 targets_panel.SetActive(false);
171 items_panel.SetActive(false);
172 input_panel.SetActive(false);
173
174 //send input
175 battle.getInput(ac, target);
176 }
177
178 private void selectTarget(Ability a) //asks user the target for an ability
179 {
180 if (a.hasTarget)
181 {
182 UI.changeText(status, battle.turn + ": Select target for " + a.guiName);
183 showTargets(a);
184 } else
185 {
186 sendInput(a, null);
187 }
188 }
189
190 public void showResults(int gold, Inventory items) //shows drops list at the end of battle
191 {
192 drops.text = gold + "g\n";
193
194 if (items != null)
195 {
196 foreach (ItemInfo it in items.getContents())
197 {
198 drops.text += "\n" + it.ToString();
199 }
200 }
201 //hide status, show results_panel
202 status_panel.SetActive(false);
203 results_panel.SetActive(true);
204 }
205
206 public void ShowDamage(Fighter f, int damage, bool crit) //shows damage tooltip above a fighter. TODO: handle text color (green for healing)
207 {
208 StartCoroutine(showDamageLate(f, damage, crit, UI.BattleSpeed, UI.BattleSpeed * 0.5f));
209 }
210
211 #region Damage Tooltip stuff
212 private IEnumerator showDamageLate(Fighter f, int damage, bool crit, float offset, float duration)
213 {
214 //get tooltip
215 Text tmptext = f.damage_tooltip.GetComponent<Text>();
216 //wait for attack animation to finish
217 yield return new WaitForSeconds(offset);
218
219 //show tooltip
220 tmptext.text = "";
221 tmptext.color = new Color(1f, 0.5f, 0f);
222 if (damage == 0)
223 {
224 tmptext.text += "Miss!";
225 }
226 else
227 {
228 if(damage < 0)
229 {
230 //target was healed
231 damage *= -1;
232 tmptext.color = Color.green;
233 } else
234 {
235 //target was damaged
236 f.animator.SetTrigger("Damage");
237 if (crit)
238 {
239 tmptext.color = Color.red;
240 }
241 }
242 tmptext.text += damage;
243 }
244
245
246 //wait, then hide tooltip
247 yield return new WaitForSeconds(duration);
248 f.damage_tooltip.GetComponent<Text>().text = "";
249
250 }
251 #endregion
252
253}