mourning-ember / src / main / java / dev / llan / model / cards / Hand.java
Hand.java
Raw
package dev.llan.model.cards;

import dev.llan.utils.MathUtil;

import static dev.llan.model.cards.Constants.*;

public class Hand {
  private IndexedCard[] cards;
  private Deck deck;

  public Hand() {
    cards = new IndexedCard[MAX_HAND_CARDS];
  }

  public void initHand(CardCollection collection) {
    this.updateDeck(collection);
    cards = deck.selectHand();
    for(int i = 0; i < cards.length; i++) {
      int currentIndex = i;
      cards[i].card().setOnPlayed(() -> this.playedCard(currentIndex));
    }
  }

  private void updateDeck(CardCollection cardCollection) {
    deck = cardCollection.createNewDeck();
  }

  public Card cardAt(int index) {
    assert MathUtil.within(index, 0, MAX_HAND_CARDS - 1);
    return cards[index].card();
  }

  public void playedCard(int index) {
    assert MathUtil.within(index, 0, MAX_HAND_CARDS - 1);
    cards[index].card().clearOnPlayed();
    deck.addUnusedCard(cards[index].card());
    cards[index] = deck.selectOne();
    cards[index].card().setOnPlayed(() -> this.playedCard(index));
  }

  public void updatePlayableCards(double mana) {
    for(IndexedCard card : cards) {
      if(card.card().getCost() > mana) {
        card.card().setCanPlay(false);
      } else {
        card.card().setCanPlay(true);
      }
    }
  }
  
  public Hand clone(CardCollection clonedCollection) {
    Hand clone = new Hand();

    for(int i = 0; i < MAX_HAND_CARDS; i++) {
      int index = this.cards[i].deckIndex();
      clone.cards[i] = new IndexedCard(clonedCollection.getCardAt(index), index);
    }

    return clone;
  }
}