VanRtkDbg/Assets/Scripts/Deck.cs

217 lines
7.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Deck : MonoBehaviour
{
public List<CardData> cardDataList;
//public List<CardData> drawList;
public List<GameObject> drawDisplayList;
//public List<CardData> handList;
//public List<GameObject> handDisplayList;
//public List<CardData> discardList;
public List<GameObject> discardDisplayList;
public List<CardData> wasteList;
public int randomSeed;
public GameObject handCardTemplate;
public GameObject cardDisplayTemplate;
public GameObject drawListContent;
public GameObject discardListContent;
private int cardInHandAmount = 0;
// Card Limit
public int maxCardinHandLimit = 10;
public int maxCardRemainLimit = 5;
private GameObject reachCardinHandLimitWarningDisplay;
private float warningDisplayCountDown;
private float warningDisplayTime = 1.0f;
// Start is called before the first frame update
void Start()
{
reachCardinHandLimitWarningDisplay = transform.Find("Content/LimitWarning").gameObject;
reachCardinHandLimitWarningDisplay.SetActive(false);
randomSeed = 200;
Random.InitState(randomSeed);
for (int i = 0; i < cardDataList.Count; i++)
{
//drawList.Add(cardDataList[i]);
GameObject tempDrawDisplay = Instantiate(cardDisplayTemplate.gameObject, transform.Find("DrawListContent/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);
drawDisplayList.Add(tempDrawDisplay);
}
drawListContent.SetActive(false);
discardListContent.SetActive(false);
}
// 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 (cardInHandAmount > maxCardinHandLimit - 1)
{
ActivateCardInHandLimitWarning();
return;
}
if (drawDisplayList.Count == 0)
{
Shuffle();
}
if (drawDisplayList.Count == 0)
{
return;
}
int randomIndex = Random.Range(0, drawDisplayList.Count);
GameObject cardObj = Instantiate(handCardTemplate.gameObject, transform.Find("Content/HandList"));
var tempCardData = drawDisplayList[randomIndex].transform.Find("Card").gameObject.GetComponent<CardDisplay>().cardData;
cardObj.transform.Find("Card").gameObject.GetComponent<CardDisplay>().SetCardData(tempCardData);
cardObj.transform.Find("Button").gameObject.GetComponent<Image>().sprite = tempCardData.artwork;
cardObj.SetActive(true);
cardInHandAmount++;
//handDisplayList.Add(cardObj);
//handList.Add(drawList[randomIndex]);
Destroy(drawDisplayList[randomIndex]);
drawDisplayList.RemoveAt(randomIndex);
}
}
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)
{
GameObject tempDiscardDisplay = Instantiate(cardDisplayTemplate.gameObject, transform.Find("DiscardListContent/Viewport/Content"));
tempDiscardDisplay.SetActive(true);
tempDiscardDisplay.transform.Find("Card").gameObject.GetComponent<CardDisplay>().SetCardData(card.transform.Find("Card").GetComponent<CardDisplay>().cardData);
tempDiscardDisplay.transform.Find("Button").gameObject.GetComponent<Image>().sprite = card.transform.Find("Card").GetComponent<CardDisplay>().cardData.artwork;
discardDisplayList.Add(tempDiscardDisplay);
//discardList.Add(card.transform.Find("Card").gameObject.gameObject.GetComponent<CardDisplay>().cardData);
//handDisplayList.Find(card);
Destroy(card);
cardInHandAmount--;
}
// When unit dead
public void AddACardIntoDiscardList(CardData card)
{
}
public void Shuffle()
{
// Put all cards in drawList into discardList
List<CardData> tempCardDataList = new List<CardData>();
for (int i = 0; i < drawDisplayList.Count; i++)
{
tempCardDataList.Add(drawDisplayList[i].transform.Find("Card").GetComponent<CardDisplay>().cardData);
Destroy(drawDisplayList[i]);
}
drawDisplayList.Clear();
//drawList.Clear();
for (int i = 0; i < tempCardDataList.Count; i++)
{
GameObject tempCardDisplay = Instantiate(cardDisplayTemplate.gameObject,transform.Find("DrawListContent/Viewport/Content"));
tempCardDisplay.transform.Find("Card").gameObject.GetComponent<CardDisplay>().SetCardData(tempCardDataList[i]);
tempCardDisplay.transform.Find("Button").gameObject.GetComponent<Image>().sprite = tempCardDataList[i].artwork;
tempCardDisplay.SetActive(true);
drawDisplayList.Add(tempCardDisplay);
}
tempCardDataList.Clear();
while (discardDisplayList.Count != 0)
{
int index = Random.Range(0, discardDisplayList.Count);
GameObject tempCardDisplay = Instantiate(cardDisplayTemplate.gameObject, transform.Find("DrawListContent/Viewport/Content"));
CardData tempCardData = discardDisplayList[index].transform.Find("Card").GetComponent<CardDisplay>().cardData;
tempCardDisplay.transform.Find("Card").gameObject.GetComponent<CardDisplay>().SetCardData(tempCardData);
tempCardDisplay.transform.Find("Button").gameObject.GetComponent<Image>().sprite = tempCardData.artwork;
tempCardDisplay.SetActive(true);
drawDisplayList.Add(tempCardDisplay);
Destroy(discardDisplayList[index]);
discardDisplayList.RemoveAt(index);
};
Debug.LogError(discardDisplayList.Count);
discardDisplayList.Clear();
}
public void ActivateDisplayDrawList()
{
drawListContent.SetActive(true);
}
public void DeactivateDisplayDrawList()
{
drawListContent.SetActive(false);
}
public void ActivateDisplayDiscardList()
{
discardListContent.SetActive(true);
}
public void DeactivateDisplayDiscardList()
{
discardListContent.SetActive(false);
}
private void ActivateCardInHandLimitWarning()
{
reachCardinHandLimitWarningDisplay.SetActive(true);
warningDisplayCountDown = warningDisplayTime;
}
private void DeactivateCardInHandLimitWarning()
{
reachCardinHandLimitWarningDisplay.SetActive(false);
}
public void EndRound()
{
if (cardInHandAmount > maxCardRemainLimit)
{
}
}
}