123 lines
3.5 KiB
C#
123 lines
3.5 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class BattleDeck<BaseCardT> where BaseCardT : BaseCard
|
|
{
|
|
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<BaseCardT> drawPile;
|
|
private List<BaseCardT> handPile;
|
|
private List<BaseCardT> discardPile;
|
|
|
|
public BattleDeck(CardDatabase<BaseCardT> cardDatabase, int[] cardIds, BattleParticipant[] cardOwners) {
|
|
this.drawPile = new List<BaseCardT>();
|
|
this.handPile = new List<BaseCardT>();
|
|
this.discardPile = new List<BaseCardT>();
|
|
|
|
for (int i = 0; i < cardIds.Length; i++) {
|
|
BaseCardT newCard = cardDatabase.CreateCard(cardIds[i], cardOwners[i]);
|
|
this.drawPile.Add(newCard);
|
|
}
|
|
this.drawPile.Shuffle();
|
|
}
|
|
|
|
public List<BaseCardT> GetDrawPile() {
|
|
return drawPile;
|
|
}
|
|
|
|
public List<BaseCardT> GetHandPile() {
|
|
return handPile;
|
|
}
|
|
|
|
public List<BaseCardT> GetDiscardPile() {
|
|
return discardPile;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
BaseCardT drawnCard = this.drawPile[0];
|
|
this.drawPile.RemoveAt(0);
|
|
this.handPile.Add(drawnCard);
|
|
}
|
|
|
|
public void RemoveCardFromHand(BaseCardT card) {
|
|
if (!this.handPile.Contains(card)) {
|
|
throw new InvalidOperationException("Cannot discard card " + card.GetDisplayName()
|
|
+ " because not in hand.");
|
|
}
|
|
|
|
this.handPile.Remove(card);
|
|
TriggerHandPileChanged();
|
|
}
|
|
|
|
public void MoveCardToDiscard(BaseCardT card) {
|
|
this.discardPile.Insert(0, card);
|
|
TriggerDiscardPileChanged();
|
|
}
|
|
|
|
public void DiscardHand() {
|
|
this.discardPile.InsertRange(0, this.handPile);
|
|
this.handPile.Clear();
|
|
TriggerHandPileChanged();
|
|
TriggerDiscardPileChanged();
|
|
}
|
|
|
|
public void PutCardOnTopOfDrawPile(BaseCardT card) {
|
|
this.drawPile.Insert(0, card);
|
|
TriggerDrawPileChanged();
|
|
}
|
|
|
|
public void ShuffleDrawPile() {
|
|
this.drawPile.Shuffle();
|
|
TriggerDrawPileChanged();
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|