VanRtkDbg/Assets/Scripts/Deck.cs

220 lines
7.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class Deck : MonoBehaviour
{
public List<CardData> cardDataList;
public int randomSeed;
private GameObject cardTemplate;
private GameObject handListContent;
private GameObject drawListContent;
private GameObject discardListContent;
// Card Limit
public int maxCardinHandLimit = 10;
public int maxCardRemainLimit = 5;
private GameObject reachCardinHandLimitWarningDisplay;
private float warningDisplayCountDown;
private float warningDisplayTime = 1.0f;
private GameObject discardHoldingListContent;
private TextMeshProUGUI discardWarningDisplayText;
// Start is called before the first frame update
void Start()
{
drawListContent = transform.Find("DrawList/Viewport/Content").gameObject;
drawListContent.transform.parent.parent.gameObject.SetActive(false);
discardListContent = transform.Find("DiscardList/Viewport/Content").gameObject;
discardListContent.transform.parent.parent.gameObject.SetActive(false);
handListContent = transform.Find("Content/HandList/Viewport/Content").gameObject;
cardTemplate = transform.Find("CardTemplate").gameObject;
reachCardinHandLimitWarningDisplay = transform.Find("Content/LimitWarning").gameObject;
reachCardinHandLimitWarningDisplay.SetActive(false);
discardHoldingListContent = transform.Find("Content/DiscardHoldingList/Viewport/HoldingListContent").gameObject;
discardHoldingListContent.transform.parent.parent.gameObject.SetActive(false);
discardWarningDisplayText = transform.Find("Content/DiscardHoldingList/Viewport/DiscardWarning/Text").gameObject.GetComponent<TextMeshProUGUI>();
randomSeed = 200;
Random.InitState(randomSeed);
for (int i = 0; i < cardDataList.Count; i++)
{
GameObject tempDrawDisplay = Instantiate(cardTemplate.gameObject, transform.Find("DrawList/Viewport/Content"));
tempDrawDisplay.transform.Find("Card").gameObject.GetComponent<CardDisplay>().SetCardData(cardDataList[i]);
tempDrawDisplay.transform.Find("Button").gameObject.GetComponent<Image>().sprite = cardDataList[i].artwork;
tempDrawDisplay.SetActive(true);
}
}
// Update is called once per frame
void Update()
{
}
private void FixedUpdate()
{
if (warningDisplayCountDown >= 0.0f)
{
warningDisplayCountDown -= Time.deltaTime;
if (warningDisplayCountDown <= 0.0f)
{
DeactivateCardInHandLimitWarning();
}
}
}
public void DrawCards(int amount)
{
for (int i = 0; i < amount; i++)
{
if (handListContent.transform.childCount > maxCardinHandLimit - 1)
{
ActivateCardInHandLimitWarning();
return;
}
if (drawListContent.transform.childCount == 0)
{
Shuffle();
}
if (drawListContent.transform.childCount == 0)
{
return;
}
int randomIndex = Random.Range(0, drawListContent.transform.childCount);
drawListContent.transform.GetChild(randomIndex).transform.SetParent(handListContent.transform);
}
}
public void UseACard(int index)
{
// TODO: Add card type into card data
// Chech if the card is waste card;
//if (handList[index].cardTags == wastCard)
//{
// wasteList.Add(handList[index]);
//}
//else
//{
//}
}
public void UseThisCard(GameObject card)
{
if(card.transform.parent == handListContent.transform)
{
if (!discardHoldingListContent.transform.parent.parent.gameObject.activeSelf)
{
// Use card in hand
card.transform.SetParent(discardListContent.transform);
}
else if (handListContent.transform.childCount > maxCardRemainLimit)
{
// Put card in discard holding list
card.transform.SetParent(discardHoldingListContent.transform);
}
}
else if (card.transform.parent == discardHoldingListContent.transform)
{
card.transform.SetParent(handListContent.transform);
}
else if (card.transform.parent == drawListContent.transform)
{
// card in draw list
Debug.Log("Card in Draw list");
}
else if (card.transform.parent == discardListContent.transform)
{
// card in discard list
Debug.Log("Card in Discard list");
}
}
// When unit dead
public void AddACardIntoDiscardList(CardData card)
{
}
public void Shuffle()
{
while (drawListContent.transform.childCount != 0)
{
drawListContent.transform.GetChild(drawListContent.transform.childCount - 1).SetParent(discardListContent.transform);
}
while (discardListContent.transform.childCount != 0)
{
int index = Random.Range(0, discardListContent.transform.childCount);
discardListContent.transform.GetChild(index).transform.SetParent(drawListContent.transform);
};
}
public void ActivateDisplayDrawList()
{
drawListContent.transform.parent.parent.gameObject.SetActive(true);
}
public void DeactivateDisplayDrawList()
{
drawListContent.transform.parent.parent.gameObject.SetActive(false);
}
public void ActivateDisplayDiscardList()
{
discardListContent.transform.parent.parent.gameObject.SetActive(true);
}
public void DeactivateDisplayDiscardList()
{
discardListContent.transform.parent.parent.gameObject.SetActive(false);
}
private void ActivateCardInHandLimitWarning()
{
reachCardinHandLimitWarningDisplay.SetActive(true);
warningDisplayCountDown = warningDisplayTime;
}
private void DeactivateCardInHandLimitWarning()
{
reachCardinHandLimitWarningDisplay.SetActive(false);
}
public void EndRound()
{
if (handListContent.transform.childCount > maxCardRemainLimit)
{
ActivateDiscardWarning();
}
}
private void ActivateDiscardWarning()
{
discardHoldingListContent.transform.parent.parent.gameObject.SetActive(true);
discardWarningDisplayText.text = string.Format("Choose {0} Card(s) to Discard", handListContent.transform.childCount - maxCardRemainLimit);
}
public void DiscardHoldingListCards()
{
while (discardHoldingListContent.transform.childCount != 0)
{
discardHoldingListContent.transform.GetChild(discardHoldingListContent.transform.childCount - 1).SetParent(discardListContent.transform);
}
if (handListContent.transform.childCount <= maxCardRemainLimit)
{
discardHoldingListContent.transform.parent.parent.gameObject.SetActive(false);
}
}
}