using UnityEngine; using TMPro; using UnityEngine.UI; public class Card : MonoBehaviour { public CardScriptableObject cardSO; public int currentHealth, attackPower, manaCost; public TMP_Text healthTxt, attackTxt, costTxt, nameTxt, actionDescriptionTxt, loreTxt; public Image characterArt, bgArt; public float moveSpeed = 5f, rotateSpeed = 540f; public bool inHand; public int handPosition; public Collider theCol; public LayerMask whatIsDesktop, whatIsPlacement; public CardPlacePoint assignedPlace; public bool isPlayer; public Animator anim; private bool isSelected; private Vector3 targetPoint; private Quaternion targetRot; private HandController theHC; private bool justPressed; // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { if(targetPoint == Vector3.zero) { targetPoint = transform.position; targetRot = transform.rotation; } SetupCard(); theHC = FindObjectOfType(); theCol = GetComponent(); } public void SetupCard() { currentHealth = cardSO.currentHealth; attackPower = cardSO.attackPower; manaCost = cardSO.manaCost; UpdateCardDisplay(); nameTxt.text = cardSO.cardName; actionDescriptionTxt.text = cardSO.actionDescription; loreTxt.text = cardSO.cardLore; characterArt.sprite = cardSO.characterSprite; bgArt.sprite = cardSO.bgSprite; } // Update is called once per frame void Update() { transform.position = Vector3.Lerp(transform.position, targetPoint, moveSpeed * Time.deltaTime); transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRot, rotateSpeed * Time.deltaTime); if(isSelected) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if(Physics.Raycast(ray, out hit, 100f, whatIsDesktop) && BattleController.instance.currentPhase == BattleController.TurnOrder.playerActive) { MoveToPoint(hit.point + new Vector3 (0f, 2f ,0f), Quaternion.identity); } if(Input.GetMouseButtonDown(1)) { ReturnToHand(); } if(Input.GetMouseButtonDown(0) && !justPressed) { if(Physics.Raycast(ray, out hit, 100f, whatIsPlacement)) { CardPlacePoint selectedPoint = hit.collider.GetComponent(); if(selectedPoint.activeCard == null && selectedPoint.isPlayerPoint) { if(BattleController.instance.playerMana >= manaCost) { selectedPoint.activeCard = this; assignedPlace = selectedPoint; MoveToPoint(selectedPoint.transform.position, Quaternion.identity); inHand = false; isSelected = false; theHC.RemoveCardFromHand(this); BattleController.instance.SpendPlayerMana(manaCost); } else { ReturnToHand(); UIController.instance.ShowManaWarning(); } } else { ReturnToHand(); } } else { ReturnToHand(); } } } justPressed = false; } public void MoveToPoint(Vector3 pointToMoveTo, Quaternion rotToMatch) { targetPoint = pointToMoveTo; targetRot = rotToMatch; } private void OnMouseOver() { if (inHand && isPlayer) { MoveToPoint(theHC.cardPositions[handPosition] + new Vector3(0f, 1f, .5f), Quaternion.identity); } } private void OnMouseExit() { if (inHand && isPlayer) { MoveToPoint(theHC.cardPositions[handPosition], theHC.minPos.rotation); } } private void OnMouseDown() { if(inHand && BattleController.instance.currentPhase == BattleController.TurnOrder.playerActive && isPlayer) { isSelected = true; theCol.enabled = false; justPressed = true; } } public void ReturnToHand() { isSelected = false; theCol.enabled = true; MoveToPoint(theHC.cardPositions[handPosition], theHC.minPos.rotation); } public void DamageCard(int damageAmount) { currentHealth -= damageAmount; if (currentHealth <= 0) { currentHealth = 0; assignedPlace.activeCard = null; MoveToPoint(BattleController.instance.discardPoint.position, BattleController.instance.discardPoint.rotation); anim.SetTrigger("Jump"); //Destroy(gameObject, 5f); } anim.SetTrigger("Hurt"); UpdateCardDisplay(); } public void UpdateCardDisplay() { healthTxt.text = currentHealth.ToString(); attackTxt.text = attackPower.ToString(); costTxt.text = manaCost.ToString(); } }