Unity肉鸽卡牌开发记录03-牌库手牌坟墓场
创建CardAreaManager类,管理对局中牌库、手牌、坟墓场之间牌张的移动:
- 对局开始时,从deckListSO(CardList,SO)中读取玩家的卡组
- 对局之中,管理牌库、手牌、坟墓场之间卡牌的移动:
- 手牌->牌库:将手牌洗回牌库(任意类型的牌张、特定类型的牌张、特定的法术力,包括大于小于等于),可以洗回牌库的特定位置(牌库顶,牌库底,牌库中)
- 手牌->坟墓场:弃掉手牌
- 牌库->手牌:抓牌、检索
- 牌库->坟墓场:磨牌、检索入坟
- 坟墓场->手牌
- 坟墓场->牌库:洗回牌库
- 持有一个allCardListSO(CardList,SO)字段,可以让其他类(主要是手牌)获取卡牌信息
- 持有deckListSO、handListSO、graveyardListSO(CardList,SO)字段,后续如果有保存游戏需求可以使用(此功能待定)
- 持有handList、deckList、graveyardList(
List<Card>)字段,用于管理对局中的手牌、牌库、坟墓场。其中deckList在游戏开始时读取deckListSO来获取玩家的牌库
- CardAreaManager类只负责处理卡牌在三个区域之间的移动,不负责卡牌的显示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CardAreaManager : MonoBehaviour { #region ScriptableObjects private CardList handListSO; private CardList deckListSO; private CardList graveyardListSO;
[SerializeField] [Header("所有卡牌listSO")] private CardList allCardListSO; public CardList AllCardList { get => allCardListSO; } #endregion
#region Datas private List<Card> handList = new List<Card>(); private List<Card> deckList = new List<Card>(); private List<Card> graveyardList = new List<Card>(); #endregion
public static CardAreaManager instance;
private void Awake() { if(instance == null) { instance = this; } else { Destroy(this); } handListSO = Resources.Load<CardList>("ScriptableObj/HandList"); graveyardListSO = Resources.Load<CardList>("ScriptableObj/GraveyardList"); deckListSO = Resources.Load<CardList>("ScriptableObj/DeckList"); deckListSO.cardList.ForEach(card => deckList.Add(card)); ShuffleDeck(); }
public static CardAreaManager GetInstance() { return instance; } }
public enum DeckPosition { Top, Middle, Bottom }
|
管理牌张移动示例:手牌区域操作与洗牌算法:
具体的操作就是判断是否符合条件,符合的话就将卡牌从一个List移除,并加入另一个List
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
| using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CardAreaManager : MonoBehaviour { #region 手牌区域操作 public void PutCardFormHandToDeck(Card card,DeckPosition deckPosition) { switch (deckPosition) { case DeckPosition.Top: deckList.Insert(0, card); handList.Remove(card); break; case DeckPosition.Middle: deckList.Add(card); ShuffleDeck(); handList.Remove(card); break; case DeckPosition.Bottom: deckList.Add(card); handList.Remove(card); break; default: deckList.Insert(0, card); handList.Remove(card); break; } CardRevealTool.GetInstance().RemoveCardFromHandZone(card); CardRevealTool.GetInstance().ListHand(); }
public void DiscardACard(Card card) { graveyardList.Add(card); handList.Remove(card); CardRevealTool.GetInstance().RemoveCardFromHandZone(card); CardRevealTool.GetInstance().ListHand(); }
public int GetHandNum() { return deckList.Count; }
public List<Card> GetAllHand() { return handList; }
public List<Card> GetAllHandWithType(CardType type) { List<Card> typeHand = new List<Card>(); foreach (Card item in handList) { if(item.type == type) { typeHand.Add(item); } } return typeHand; }
public List<Card> GetAllHandWithCertainCost(int cost) { List<Card> costHand = new List<Card>(); foreach (Card item in handList) { if (item.pointCost == cost) { costHand.Add(item); } } return costHand; }
public List<Card> GetAllHandWithLessCost(int cost) { List<Card> costHand = new List<Card>(); foreach (Card item in handList) { if (item.pointCost < cost) { costHand.Add(item); } } return costHand; }
public List<Card> GetAllHandWithLargerCost(int cost) { List<Card> costHand = new List<Card>(); foreach (Card item in handList) { if (item.pointCost > cost) { costHand.Add(item); } } return costHand; }
public void PlayACard(Card card) { handList.Remove(card); Debug.Log("打出一张卡:"+card.cardName); CardRevealTool.GetInstance().ListHand(); } #endregion
#region 牌库区域操作 public void ShuffleDeck() { int cardInDeck = deckList.Count; for (int shuffleTime = 0; shuffleTime < 2; shuffleTime++) { for (int i = 0; i < cardInDeck; i++) { int randInt = Random.Range(i, cardInDeck - 1); Card temp = deckList[i]; deckList[i] = deckList[randInt]; deckList[randInt] = temp; } } } #endregion
public static CardAreaManager GetInstance() { return instance; } }
public enum DeckPosition { Top, Middle, Bottom }
|
TODO:
- [ ] 手牌、牌库与坟墓场的读档与存档功能(待定)