| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections.Generic;
- using TMPro;
- using System;
- /// <summary>
- /// UI for selecting spells to cast in battle
- /// </summary>
- public class SpellSelectionUI : MonoBehaviour
- {
- [Header("UI References")]
- public Canvas selectionCanvas;
- public Transform spellListContainer;
- public GameObject spellButtonPrefab;
- public Button cancelButton;
- public TextMeshProUGUI titleText;
- [Header("Settings")]
- public int maxSpellsToShow = 6;
- private Character currentCharacter;
- private List<GameObject> spellButtons = new List<GameObject>();
- // Events
- public event Action<string> OnSpellSelected;
- public event Action OnSelectionCancelled;
- void Awake()
- {
- if (selectionCanvas == null)
- selectionCanvas = GetComponentInChildren<Canvas>();
- SetVisible(false);
- }
- void Start()
- {
- if (cancelButton != null)
- {
- cancelButton.onClick.AddListener(() =>
- {
- OnSelectionCancelled?.Invoke();
- HideSelection();
- });
- }
- }
- void Update()
- {
- if (selectionCanvas.gameObject.activeInHierarchy)
- {
- if (Input.GetKeyDown(KeyCode.Escape))
- {
- OnSelectionCancelled?.Invoke();
- HideSelection();
- }
- }
- }
- public void ShowSpellSelection(Character character)
- {
- currentCharacter = character;
- if (titleText != null)
- titleText.text = $"{character.CharacterName} - Select Spell";
- PopulateSpellList();
- SetVisible(true);
- Debug.Log($"✨ Spell selection shown for {character.CharacterName}");
- }
- public void HideSelection()
- {
- SetVisible(false);
- ClearSpellList();
- currentCharacter = null;
- Debug.Log("✨ Spell selection hidden");
- }
- private void PopulateSpellList()
- {
- ClearSpellList();
- var spells = GetCharacterSpells();
- for (int i = 0; i < spells.Count && i < maxSpellsToShow; i++)
- {
- CreateSpellButton(spells[i], i);
- }
- }
- private List<SpellData> GetCharacterSpells()
- {
- // Placeholder implementation - return sample spells
- // TODO: Get actual spells from character's spell list/class abilities
- return new List<SpellData>
- {
- new SpellData {
- name = "Magic Missile",
- description = "Deals 15 magic damage to target",
- manaCost = 5,
- requiresTarget = true,
- isAoE = false,
- icon = null
- },
- new SpellData {
- name = "Heal",
- description = "Restores 25 HP to target",
- manaCost = 8,
- requiresTarget = true,
- isAoE = false,
- icon = null
- },
- new SpellData {
- name = "Fireball",
- description = "Explosive fire damage in area",
- manaCost = 12,
- requiresTarget = true,
- isAoE = true,
- icon = null
- },
- new SpellData {
- name = "Shield",
- description = "Increases AC by 2 for 5 turns",
- manaCost = 6,
- requiresTarget = false,
- isAoE = false,
- icon = null
- },
- new SpellData {
- name = "Lightning Bolt",
- description = "Chain lightning hits multiple enemies",
- manaCost = 10,
- requiresTarget = true,
- isAoE = true,
- icon = null
- },
- new SpellData {
- name = "Cure Disease",
- description = "Removes negative effects",
- manaCost = 7,
- requiresTarget = true,
- isAoE = false,
- icon = null
- }
- };
- }
- private void CreateSpellButton(SpellData spell, int index)
- {
- GameObject buttonObj = Instantiate(spellButtonPrefab, spellListContainer);
- // Get or create components
- Button button = buttonObj.GetComponent<Button>();
- if (button == null)
- button = buttonObj.AddComponent<Button>();
- // Find or create text components
- TextMeshProUGUI nameText = buttonObj.transform.Find("SpellName")?.GetComponent<TextMeshProUGUI>();
- TextMeshProUGUI descText = buttonObj.transform.Find("Description")?.GetComponent<TextMeshProUGUI>();
- TextMeshProUGUI costText = buttonObj.transform.Find("ManaCost")?.GetComponent<TextMeshProUGUI>();
- Image iconImage = buttonObj.transform.Find("Icon")?.GetComponent<Image>();
- // Create text components if they don't exist
- if (nameText == null)
- {
- GameObject nameObj = new GameObject("SpellName");
- nameObj.transform.SetParent(buttonObj.transform, false);
- nameText = nameObj.AddComponent<TextMeshProUGUI>();
- RectTransform nameRect = nameText.GetComponent<RectTransform>();
- nameRect.anchorMin = new Vector2(0.1f, 0.7f);
- nameRect.anchorMax = new Vector2(0.7f, 0.9f);
- nameRect.offsetMin = Vector2.zero;
- nameRect.offsetMax = Vector2.zero;
- nameText.fontSize = 14;
- nameText.fontStyle = FontStyles.Bold;
- nameText.alignment = TextAlignmentOptions.Left;
- }
- if (descText == null)
- {
- GameObject descObj = new GameObject("Description");
- descObj.transform.SetParent(buttonObj.transform, false);
- descText = descObj.AddComponent<TextMeshProUGUI>();
- RectTransform descRect = descText.GetComponent<RectTransform>();
- descRect.anchorMin = new Vector2(0.1f, 0.3f);
- descRect.anchorMax = new Vector2(0.9f, 0.7f);
- descRect.offsetMin = Vector2.zero;
- descRect.offsetMax = Vector2.zero;
- descText.fontSize = 10;
- descText.alignment = TextAlignmentOptions.Left;
- descText.color = Color.gray;
- }
- if (costText == null)
- {
- GameObject costObj = new GameObject("ManaCost");
- costObj.transform.SetParent(buttonObj.transform, false);
- costText = costObj.AddComponent<TextMeshProUGUI>();
- RectTransform costRect = costText.GetComponent<RectTransform>();
- costRect.anchorMin = new Vector2(0.7f, 0.7f);
- costRect.anchorMax = new Vector2(0.9f, 0.9f);
- costRect.offsetMin = Vector2.zero;
- costRect.offsetMax = Vector2.zero;
- costText.fontSize = 12;
- costText.alignment = TextAlignmentOptions.Right;
- costText.color = Color.cyan;
- }
- // Set content
- nameText.text = spell.name;
- descText.text = spell.description;
- costText.text = $"{spell.manaCost} MP";
- // Add spell type indicators
- if (spell.isAoE)
- {
- nameText.text += " (AoE)";
- }
- if (spell.requiresTarget)
- {
- descText.text += " [Requires Target]";
- }
- if (iconImage != null && spell.icon != null)
- iconImage.sprite = spell.icon;
- // Check if character can cast this spell (placeholder)
- bool canCast = CanCharacterCastSpell(spell);
- button.interactable = canCast;
- if (!canCast)
- {
- // Dim the button for unusable spells
- var colors = button.colors;
- colors.normalColor = Color.gray;
- colors.disabledColor = Color.gray;
- button.colors = colors;
- nameText.color = Color.gray;
- descText.color = Color.gray;
- costText.color = Color.gray;
- }
- // Setup button click
- button.onClick.AddListener(() =>
- {
- if (canCast)
- {
- OnSpellSelected?.Invoke(spell.name);
- HideSelection();
- }
- });
- spellButtons.Add(buttonObj);
- }
- private bool CanCharacterCastSpell(SpellData spell)
- {
- // Placeholder logic - check if character has enough mana, knows spell, etc.
- // TODO: Implement actual spell casting requirements
- if (currentCharacter == null)
- return false;
- // For now, assume character can cast if they have enough "mana"
- // (We'll need to add a mana system to characters later)
- return true;
- }
- private void ClearSpellList()
- {
- foreach (var button in spellButtons)
- {
- if (button != null)
- DestroyImmediate(button);
- }
- spellButtons.Clear();
- }
- private void SetVisible(bool visible)
- {
- if (selectionCanvas != null)
- selectionCanvas.gameObject.SetActive(visible);
- }
- [System.Serializable]
- private class SpellData
- {
- public string name;
- public string description;
- public int manaCost;
- public bool requiresTarget;
- public bool isAoE;
- public Sprite icon;
- }
- }
|