using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; public class OpponentDragSpawner : MonoBehaviour, IRoundCallback, IManaChangeCallback, IDragDropListener { [SerializeField] Opponent opponent; [SerializeField] Color disabledPreviewSpriteColor = new Color(0.5f, 0.5f, 0.5f, 1f); [SerializeField] Color disabledCostTextColor = Color.red; [SerializeField] List<AudioClip> spawnOpponentSfx; Image previewSprite; Text costText; Color defaultCostTextColor; BaseCameraController cameraController; void Start() { previewSprite = transform.Find("OpponentImage").GetComponent<Image>(); costText = transform.Find("Cost").GetComponent<Text>(); defaultCostTextColor = costText.color; previewSprite.sprite = opponent.enemySprite; costText.text = opponent.enemyCost.ToString(); cameraController = Camera.main.GetComponent<BaseCameraController>(); var eventTrigger = GetComponent<EventTrigger>(); var beginDragEvent = new EventTrigger.Entry(); beginDragEvent.eventID = EventTriggerType.BeginDrag; beginDragEvent.callback.AddListener((data) => OnBeginDrag((PointerEventData)data)); eventTrigger.triggers.Add(beginDragEvent); StatsManager.instance.manaChangeCallbacks.Add(this); RoundController.instance.roundCallbacks.Add(this); UpdateDisplayStates(); } void OnBeginDrag(PointerEventData eventData) { if (!CanSpawnEnemy() || !StatsManager.instance.ModifyMana(-opponent.enemyCost)) { return; } var newEnemy = Instantiate(opponent.enemyPrefab); DragDestroyController.instance.HandoffEnemy(newEnemy, this); cameraController.PlayRandomGlobalAudioClip(spawnOpponentSfx); } void UpdateDisplayStates() { previewSprite.color = CanSpawnEnemy() ? Color.white : disabledPreviewSpriteColor; costText.color = StatsManager.instance.CanModifyMana(-opponent.enemyCost) ? defaultCostTextColor : disabledCostTextColor; } bool CanSpawnEnemy() { return !RoundController.instance.roundRunning && StatsManager.instance.CanModifyMana(-opponent.enemyCost); } public void OnDragDropped(GameObject enemy, bool validPosition) { if (!validPosition) { Destroy(enemy); StatsManager.instance.ModifyMana(enemy.GetComponent<Enemy>().opponentType.enemyCost); } } public void OnRoundStart() { UpdateDisplayStates(); } public void OnRoundEnd(bool won) { UpdateDisplayStates(); } public void OnManaChanged(int oldValue, int newValue) { UpdateDisplayStates(); } }