SpellSelectionUI.cs 9.1 KB

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