You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
4.5 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Threading; //导入命名空间,类Thread就在此空间中
public class DeckManager : MonoBehaviour
{
// Start is called before the first frame update
public Transform deckPanel;//卡组显示区域
public Transform libraryPanel;//玩家卡牌仓库
public GameObject deckPrefab;//卡组显示的卡
public GameObject cardPrefab;//卡库显示的卡
public GameObject DataManager;
private PlayerData PlayerData;//玩家数据
private CardStore CardStore;
private Dictionary<int, GameObject> libraryDic = new Dictionary<int, GameObject>();
private Dictionary<int, GameObject> deckDic = new Dictionary<int, GameObject>();
void Start()
{
PlayerData = DataManager.GetComponent<PlayerData>();
CardStore = DataManager.GetComponent<CardStore>();
//CardStore.LoadCardData();
//PlayerData.LoadPlayerData();
//Thread.Sleep(500); //延时x ms.
UpdateLibrary();
UpdateDeck();
////测试,显示成功
//GameObject cardInstance = Instantiate(cardPrefab, libraryPanel);
//cardInstance.GetComponent<CardDisplay>().card = CardStore.RandomCard();
}
// Update is called once per frame
void Update()
{
}
public void UpdateLibrary()
{
//更新仓库
for (int i = 0; i < PlayerData.playerCards.Length ; i++)
{
if (PlayerData.playerCards[i] > 0)//库存大于零
{
GameObject newCard = Instantiate(cardPrefab, libraryPanel);
//newCard.GetComponent<CardCounter>().counter.text = PlayerData.playerCards[i].ToString();
newCard.GetComponent<CardCounter>().SetCounter(PlayerData.playerCards[i]);
newCard.GetComponent<CardDisplay>().card = CardStore.cardList[i];
libraryDic.Add(i, newCard);
}
}
}
public void UpdateDeck()
{
//更新卡组
for (int i = 0; i < PlayerData.playerDeck.Length; i++)
{
if (PlayerData.playerDeck[i] > 0)//库存大于零
{
GameObject newCard = Instantiate(deckPrefab, deckPanel);
newCard.GetComponent<CardCounter>().SetCounter(PlayerData.playerDeck[i]);
newCard.GetComponent<CardDisplay>().card = CardStore.cardList[i];
deckDic.Add(i, newCard);
}
}
}
public void UpdateCard(CardState _state,int _id)
{
if(_state == CardState.Deck)
{
PlayerData.playerDeck[_id]--;
PlayerData.playerCards[_id]++;
if(!deckDic[_id].GetComponent<CardCounter>().SetCounter(-1))
{
deckDic.Remove(_id);
}
if (libraryDic.ContainsKey(_id))
{
//能直接加吗?为什么不能
libraryDic[_id].GetComponent<CardCounter>().SetCounter(1);
}
else
{
CreatCard(_id, CardState.Library);
}
}
else if(_state == CardState.Library)
{
PlayerData.playerDeck[_id]++;
PlayerData.playerCards[_id]--;
if (deckDic.ContainsKey(_id))
{
//deckDic[_id]--;能直接减吗?为什么不能
deckDic[_id].GetComponent<CardCounter>().SetCounter(1);
}
else
{
CreatCard(_id, CardState.Deck);
}
if(!libraryDic[_id].GetComponent<CardCounter>().SetCounter(-1))
{
libraryDic.Remove(_id);
}
}
}
public void CreatCard(int _id,CardState _cardState)
{
Transform targetPanel;
GameObject targetPrefab;
var refData = PlayerData.playerCards;
Dictionary<int, GameObject> targetDic = libraryDic;
if(_cardState == CardState.Library)
{
targetPanel = libraryPanel;
targetPrefab = cardPrefab;
}
else
{
targetPanel= deckPanel;
targetPrefab = deckPrefab;
refData = PlayerData.playerDeck;
targetDic = deckDic;
}
GameObject newCard = Instantiate(targetPrefab, targetPanel);
newCard.GetComponent<CardCounter>().SetCounter(refData[_id]);
newCard.GetComponent<CardDisplay>().card = CardStore.cardList[_id];
targetDic.Add(_id, newCard);
}
}