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