Files
Generic/Assets/Scripts/Card.cs

218 lines
6.2 KiB
C#

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;
public 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<HandController>();
theCol = GetComponent<Collider>();
}
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);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (isSelected)
{
if (Physics.Raycast(ray, out hit, 100f, whatIsDesktop) && BattleController.instance.currentPhase == BattleController.TurnOrder.playerActive && BattleController.instance.battleEnded == false)
{
MoveToPoint(hit.point + new Vector3(0f, 2f, 0f), Quaternion.identity);
}
if (Input.GetMouseButtonDown(1) && BattleController.instance.battleEnded == false)
{
ReturnToHand();
}
if (Input.GetMouseButtonDown(0) && !justPressed && BattleController.instance.battleEnded == false)
{
if (Physics.Raycast(ray, out hit, 100f, whatIsPlacement))
{
CardPlacePoint selectedPoint = hit.collider.GetComponent<CardPlacePoint>();
if (selectedPoint.activeCard == null && selectedPoint.isPlayerPoint && BattleController.instance.battleEnded == false)
{
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 if (selectedPoint.isDiscardPoint && BattleController.instance.battleEnded == false)
{
DiscardCard(selectedPoint);
}
else
{
ReturnToHand();
}
}
else
{
ReturnToHand();
}
}
}
else
{
//DrawPile functionality
if (!isSelected && Input.GetMouseButtonDown(0) && Physics.Raycast(ray, out hit, 100f) && BattleController.instance.battleEnded == false)
{
if (hit.collider.CompareTag("DrawPile"))
{
DeckController.instance.DrawCardForMana();
}
}
}
justPressed = false;
}
public void MoveToPoint(Vector3 pointToMoveTo, Quaternion rotToMatch)
{
targetPoint = pointToMoveTo;
targetRot = rotToMatch;
}
private void OnMouseOver()
{
if (inHand && isPlayer && BattleController.instance.battleEnded == false)
{
MoveToPoint(theHC.cardPositions[handPosition] + new Vector3(0f, 1f, .5f), Quaternion.identity);
}
}
private void OnMouseExit()
{
if (inHand && isPlayer && BattleController.instance.battleEnded == false)
{
MoveToPoint(theHC.cardPositions[handPosition], theHC.minPos.rotation);
}
}
private void OnMouseDown()
{
if(inHand && BattleController.instance.currentPhase == BattleController.TurnOrder.playerActive && isPlayer && BattleController.instance.battleEnded == false)
{
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();
}
public void DiscardCard(CardPlacePoint discardPoint)
{
theHC.DiscardCard(this, discardPoint);
}
}