Create HeroDeck with state of cards
This commit is contained in:
parent
967c871ed9
commit
567b732f85
7 changed files with 175 additions and 17 deletions
102
Assets/Battle/HeroDeck.cs
Normal file
102
Assets/Battle/HeroDeck.cs
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class HeroDeck
|
||||
{
|
||||
public const int DEFAULT_HAND_DRAWN = 5;
|
||||
|
||||
public delegate void PileChangedAction();
|
||||
public event PileChangedAction OnDrawPileChanged;
|
||||
public event PileChangedAction OnHandPileChanged;
|
||||
public event PileChangedAction OnDiscardPileChanged;
|
||||
|
||||
// Piles are sorted from top to bottom.
|
||||
private List<BaseCard> drawPile;
|
||||
private List<BaseCard> handPile;
|
||||
private List<BaseCard> discardPile;
|
||||
|
||||
public HeroDeck(CardId[] cardIds) {
|
||||
this.drawPile = new List<BaseCard>();
|
||||
this.handPile = new List<BaseCard>();
|
||||
this.discardPile = new List<BaseCard>();
|
||||
|
||||
for (int i = 0; i < cardIds.Length; i++) {
|
||||
BaseCard newCard = CardDatabase.CreateCard(cardIds[i]);
|
||||
this.drawPile.Add(newCard);
|
||||
}
|
||||
this.drawPile.Shuffle();
|
||||
}
|
||||
|
||||
public List<BaseCard> GetHand() {
|
||||
return handPile;
|
||||
}
|
||||
|
||||
public void DrawHand() {
|
||||
for (int i = 0; i < DEFAULT_HAND_DRAWN; i++) {
|
||||
DrawCardInternal();
|
||||
}
|
||||
TriggerHandPileChanged();
|
||||
TriggerDrawPileChanged();
|
||||
}
|
||||
|
||||
public void DrawCard() {
|
||||
DrawCardInternal();
|
||||
TriggerHandPileChanged();
|
||||
TriggerDrawPileChanged();
|
||||
}
|
||||
|
||||
private void DrawCardInternal() {
|
||||
if (this.drawPile.Count == 0) {
|
||||
RemakeDrawPile();
|
||||
// If there is still no draw pile, probably because no cards were in the discard pile, just
|
||||
// skip the draw.
|
||||
if (this.drawPile.Count == 0) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
BaseCard drawnCard = this.drawPile[0];
|
||||
this.drawPile.RemoveAt(0);
|
||||
this.handPile.Add(drawnCard);
|
||||
}
|
||||
|
||||
public void DiscardPlayedCard(BaseCard card) {
|
||||
if (!this.handPile.Contains(card)) {
|
||||
throw new InvalidOperationException("Cannot discard card " + card.GetDisplayName()
|
||||
+ " because not in hand.");
|
||||
}
|
||||
|
||||
this.discardPile.Insert(0, card);
|
||||
this.handPile.Remove(card);
|
||||
TriggerHandPileChanged();
|
||||
TriggerDiscardPileChanged();
|
||||
}
|
||||
|
||||
public void DiscardHand() {
|
||||
this.discardPile.InsertRange(0, this.handPile);
|
||||
this.handPile.Clear();
|
||||
TriggerHandPileChanged();
|
||||
TriggerDiscardPileChanged();
|
||||
}
|
||||
|
||||
private void RemakeDrawPile() {
|
||||
this.drawPile.AddRange(this.discardPile);
|
||||
this.discardPile.Clear();
|
||||
|
||||
this.drawPile.Shuffle();
|
||||
TriggerDrawPileChanged();
|
||||
TriggerDiscardPileChanged();
|
||||
}
|
||||
|
||||
private void TriggerDrawPileChanged() {
|
||||
if (OnDrawPileChanged != null) OnDrawPileChanged();
|
||||
}
|
||||
private void TriggerHandPileChanged() {
|
||||
if (OnHandPileChanged != null) OnHandPileChanged();
|
||||
}
|
||||
private void TriggerDiscardPileChanged() {
|
||||
if (OnDiscardPileChanged != null) OnDiscardPileChanged();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue