VanRtkDbg/Assets/Scripts/Deck.cs

158 lines
5.2 KiB
C#
Raw Normal View History

2022-03-16 14:59:40 -07:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
2022-03-16 14:59:40 -07:00
public class Deck : MonoBehaviour
{
public List<CardData> cardDataList;
2022-03-16 14:59:40 -07:00
public List<CardData> drawList;
// Control Card Display
public List<GameObject> drawDisplayList;
2022-03-16 14:59:40 -07:00
public List<CardData> handList;
2022-03-16 14:59:40 -07:00
public List<CardData> discardList;
// Control Card Display
public List<GameObject> discardDisplayList;
2022-03-16 14:59:40 -07:00
public List<CardData> wasteList;
public int randomSeed;
public GameObject handCardTemplate;
public GameObject cardDisplayTemplate;
public GameObject drawListContent;
public GameObject discardListContent;
2022-03-16 14:59:40 -07:00
// Start is called before the first frame update
void Start()
{
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"));
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);
2022-03-16 14:59:40 -07:00
}
// Update is called once per frame
void Update()
{
}
public void DrawCards(int amount)
{
for (int i = 0; i < amount; i++)
{
if (drawList.Count == 0)
{
Shuffle();
}
if(drawList.Count==0)
{
return;
}
int randomIndex = Random.Range(0, discardList.Count);
GameObject cardObj = Instantiate(handCardTemplate.gameObject, transform.Find("Content/HandList"));
cardObj.SetActive(true);
cardObj.transform.Find("Card").gameObject.GetComponent<CardDisplay>().SetCardData(drawList[randomIndex]);
cardObj.transform.Find("Button").gameObject.GetComponent<Image>().sprite = drawList[randomIndex].artwork;
drawList.RemoveAt(randomIndex);
Destroy(drawDisplayList[randomIndex]);
drawDisplayList.RemoveAt(randomIndex);
2022-03-16 14:59:40 -07:00
}
}
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
//{
discardList.Add(handList[index]);
2022-03-16 14:59:40 -07:00
//}
handList.RemoveAt(index);
}
public void UseThisCard(GameObject card)
{
GameObject tempDiscardDisplay = Instantiate(cardDisplayTemplate.gameObject, transform.Find("DiscardListContent"));
tempDiscardDisplay.SetActive(true);
tempDiscardDisplay.transform.Find("Card").gameObject.GetComponent<CardDisplay>().SetCardData(card.transform.Find("Card").gameObject.gameObject.GetComponent<CardDisplay>().cardData);
tempDiscardDisplay.transform.Find("Button").gameObject.GetComponent<Image>().sprite = card.transform.Find("Card").gameObject.gameObject.GetComponent<CardDisplay>().cardData.artwork;
discardDisplayList.Add(tempDiscardDisplay);
discardList.Add(card.transform.Find("Card").gameObject.gameObject.GetComponent<CardDisplay>().cardData);
Destroy(card);
}
// When unit dead
2022-03-16 14:59:40 -07:00
public void AddACardIntoDiscardList(CardData card)
{
discardList.Add(card);
}
public void Shuffle()
2022-03-16 14:59:40 -07:00
{
// Put all cards in drawList into discardList
for (int i = 0; i < drawList.Count; i++)
2022-03-16 14:59:40 -07:00
{
discardList.Add(drawList[i]);
Destroy(drawDisplayList[i]);
2022-03-16 14:59:40 -07:00
}
drawDisplayList.Clear();
drawList.Clear();
2022-03-16 14:59:40 -07:00
while (discardList.Count != 0)
2022-03-16 14:59:40 -07:00
{
int index = Random.Range(0, discardList.Count);
drawList.Add(discardList[index]);
GameObject tempCardDisplay = Instantiate(cardDisplayTemplate.gameObject, transform.Find("DrawListContent"));
tempCardDisplay.transform.Find("Card").gameObject.GetComponent<CardDisplay>().SetCardData(discardList[index]);
tempCardDisplay.transform.Find("Button").gameObject.GetComponent<Image>().sprite = discardList[index].artwork;
tempCardDisplay.SetActive(true);
drawDisplayList.Add(tempCardDisplay);
2022-03-16 14:59:40 -07:00
discardList[index] = discardList[discardList.Count - 1];
discardList.RemoveAt(discardList.Count - 1);
};
for (int i = 0; i < discardDisplayList.Count; i++)
{
Destroy(discardDisplayList[i]);
}
2022-03-16 14:59:40 -07:00
}
public void ActivateDisplayDrawList()
{
drawListContent.SetActive(true);
}
2022-03-16 14:59:40 -07:00
public void DeactivateDisplayDrawList()
{
drawListContent.SetActive(false);
}
public void ActivateDisplayDiscardList()
{
discardListContent.SetActive(true);
}
public void DeactivateDisplayDiscardList()
{
discardListContent.SetActive(false);
}
2022-03-16 14:59:40 -07:00
}