SpellSelectionUI.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections.Generic;
  4. using TMPro;
  5. using System;
  6. /// <summary>
  7. /// UI for selecting spells to cast in battle
  8. /// </summary>
  9. public class SpellSelectionUI : MonoBehaviour
  10. {
  11. [Header("UI References")]
  12. public Canvas selectionCanvas;
  13. public Transform spellListContainer;
  14. public GameObject spellButtonPrefab;
  15. public Button cancelButton;
  16. public TextMeshProUGUI titleText;
  17. [Header("Settings")]
  18. public int maxSpellsToShow = 6;
  19. private Character currentCharacter;
  20. private List<GameObject> spellButtons = new List<GameObject>();
  21. // Events
  22. public event Action<string> OnSpellSelected;
  23. public event Action OnSelectionCancelled;
  24. void Awake()
  25. {
  26. if (selectionCanvas == null)
  27. selectionCanvas = GetComponentInChildren<Canvas>();
  28. SetVisible(false);
  29. }
  30. void Start()
  31. {
  32. if (cancelButton != null)
  33. {
  34. cancelButton.onClick.AddListener(() =>
  35. {
  36. OnSelectionCancelled?.Invoke();
  37. HideSelection();
  38. });
  39. }
  40. }
  41. void Update()
  42. {
  43. if (selectionCanvas.gameObject.activeInHierarchy)
  44. {
  45. if (Input.GetKeyDown(KeyCode.Escape))
  46. {
  47. OnSelectionCancelled?.Invoke();
  48. HideSelection();
  49. }
  50. }
  51. }
  52. public void ShowSpellSelection(Character character)
  53. {
  54. currentCharacter = character;
  55. if (titleText != null)
  56. titleText.text = $"{character.CharacterName} - Select Spell";
  57. PopulateSpellList();
  58. SetVisible(true);
  59. }
  60. public void HideSelection()
  61. {
  62. SetVisible(false);
  63. ClearSpellList();
  64. currentCharacter = null;
  65. }
  66. private void PopulateSpellList()
  67. {
  68. ClearSpellList();
  69. var spells = GetCharacterSpells();
  70. for (int i = 0; i < spells.Count && i < maxSpellsToShow; i++)
  71. {
  72. CreateSpellButton(spells[i], i);
  73. }
  74. }
  75. private List<SpellData> GetCharacterSpells()
  76. {
  77. // Placeholder implementation - return sample spells
  78. // TODO: Get actual spells from character's spell list/class abilities
  79. return new List<SpellData>
  80. {
  81. new SpellData {
  82. name = "Magic Missile",
  83. description = "Deals 15 magic damage to target",
  84. manaCost = 5,
  85. requiresTarget = true,
  86. isAoE = false,
  87. icon = null
  88. },
  89. new SpellData {
  90. name = "Heal",
  91. description = "Restores 25 HP to target",
  92. manaCost = 8,
  93. requiresTarget = true,
  94. isAoE = false,
  95. icon = null
  96. },
  97. new SpellData {
  98. name = "Fireball",
  99. description = "Explosive fire damage in area",
  100. manaCost = 12,
  101. requiresTarget = true,
  102. isAoE = true,
  103. icon = null
  104. },
  105. new SpellData {
  106. name = "Shield",
  107. description = "Increases AC by 2 for 5 turns",
  108. manaCost = 6,
  109. requiresTarget = false,
  110. isAoE = false,
  111. icon = null
  112. },
  113. new SpellData {
  114. name = "Lightning Bolt",
  115. description = "Chain lightning hits multiple enemies",
  116. manaCost = 10,
  117. requiresTarget = true,
  118. isAoE = true,
  119. icon = null
  120. },
  121. new SpellData {
  122. name = "Cure Disease",
  123. description = "Removes negative effects",
  124. manaCost = 7,
  125. requiresTarget = true,
  126. isAoE = false,
  127. icon = null
  128. }
  129. };
  130. }
  131. private void CreateSpellButton(SpellData spell, int index)
  132. {
  133. GameObject buttonObj = Instantiate(spellButtonPrefab, spellListContainer);
  134. // Get or create components
  135. Button button = buttonObj.GetComponent<Button>();
  136. if (button == null)
  137. button = buttonObj.AddComponent<Button>();
  138. // Find or create text components
  139. TextMeshProUGUI nameText = buttonObj.transform.Find("SpellName")?.GetComponent<TextMeshProUGUI>();
  140. TextMeshProUGUI descText = buttonObj.transform.Find("Description")?.GetComponent<TextMeshProUGUI>();
  141. TextMeshProUGUI costText = buttonObj.transform.Find("ManaCost")?.GetComponent<TextMeshProUGUI>();
  142. Image iconImage = buttonObj.transform.Find("Icon")?.GetComponent<Image>();
  143. // Create text components if they don't exist
  144. if (nameText == null)
  145. {
  146. GameObject nameObj = new GameObject("SpellName");
  147. nameObj.transform.SetParent(buttonObj.transform, false);
  148. nameText = nameObj.AddComponent<TextMeshProUGUI>();
  149. RectTransform nameRect = nameText.GetComponent<RectTransform>();
  150. nameRect.anchorMin = new Vector2(0.1f, 0.7f);
  151. nameRect.anchorMax = new Vector2(0.7f, 0.9f);
  152. nameRect.offsetMin = Vector2.zero;
  153. nameRect.offsetMax = Vector2.zero;
  154. nameText.fontSize = 14;
  155. nameText.fontStyle = FontStyles.Bold;
  156. nameText.alignment = TextAlignmentOptions.Left;
  157. }
  158. if (descText == null)
  159. {
  160. GameObject descObj = new GameObject("Description");
  161. descObj.transform.SetParent(buttonObj.transform, false);
  162. descText = descObj.AddComponent<TextMeshProUGUI>();
  163. RectTransform descRect = descText.GetComponent<RectTransform>();
  164. descRect.anchorMin = new Vector2(0.1f, 0.3f);
  165. descRect.anchorMax = new Vector2(0.9f, 0.7f);
  166. descRect.offsetMin = Vector2.zero;
  167. descRect.offsetMax = Vector2.zero;
  168. descText.fontSize = 10;
  169. descText.alignment = TextAlignmentOptions.Left;
  170. descText.color = Color.gray;
  171. }
  172. if (costText == null)
  173. {
  174. GameObject costObj = new GameObject("ManaCost");
  175. costObj.transform.SetParent(buttonObj.transform, false);
  176. costText = costObj.AddComponent<TextMeshProUGUI>();
  177. RectTransform costRect = costText.GetComponent<RectTransform>();
  178. costRect.anchorMin = new Vector2(0.7f, 0.7f);
  179. costRect.anchorMax = new Vector2(0.9f, 0.9f);
  180. costRect.offsetMin = Vector2.zero;
  181. costRect.offsetMax = Vector2.zero;
  182. costText.fontSize = 12;
  183. costText.alignment = TextAlignmentOptions.Right;
  184. costText.color = Color.cyan;
  185. }
  186. // Set content
  187. nameText.text = spell.name;
  188. descText.text = spell.description;
  189. costText.text = $"{spell.manaCost} MP";
  190. // Add spell type indicators
  191. if (spell.isAoE)
  192. {
  193. nameText.text += " (AoE)";
  194. }
  195. if (spell.requiresTarget)
  196. {
  197. descText.text += " [Requires Target]";
  198. }
  199. if (iconImage != null && spell.icon != null)
  200. iconImage.sprite = spell.icon;
  201. // Check if character can cast this spell (placeholder)
  202. bool canCast = CanCharacterCastSpell(spell);
  203. button.interactable = canCast;
  204. if (!canCast)
  205. {
  206. // Dim the button for unusable spells
  207. var colors = button.colors;
  208. colors.normalColor = Color.gray;
  209. colors.disabledColor = Color.gray;
  210. button.colors = colors;
  211. nameText.color = Color.gray;
  212. descText.color = Color.gray;
  213. costText.color = Color.gray;
  214. }
  215. // Setup button click
  216. button.onClick.AddListener(() =>
  217. {
  218. if (canCast)
  219. {
  220. OnSpellSelected?.Invoke(spell.name);
  221. HideSelection();
  222. }
  223. });
  224. spellButtons.Add(buttonObj);
  225. }
  226. private bool CanCharacterCastSpell(SpellData spell)
  227. {
  228. // Placeholder logic - check if character has enough mana, knows spell, etc.
  229. // TODO: Implement actual spell casting requirements
  230. if (currentCharacter == null)
  231. return false;
  232. // For now, assume character can cast if they have enough "mana"
  233. // (We'll need to add a mana system to characters later)
  234. return true;
  235. }
  236. private void ClearSpellList()
  237. {
  238. foreach (var button in spellButtons)
  239. {
  240. if (button != null)
  241. DestroyImmediate(button);
  242. }
  243. spellButtons.Clear();
  244. }
  245. private void SetVisible(bool visible)
  246. {
  247. if (selectionCanvas != null)
  248. selectionCanvas.gameObject.SetActive(visible);
  249. }
  250. [System.Serializable]
  251. private class SpellData
  252. {
  253. public string name;
  254. public string description;
  255. public int manaCost;
  256. public bool requiresTarget;
  257. public bool isAoE;
  258. public Sprite icon;
  259. }
  260. }