| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import Phaser from 'phaser'; |
| import { type CardConfig } from '../scenes/BaseBattleScene'; |
|
|
| export class CardManager extends Phaser.Events.EventEmitter { |
| private scene: Phaser.Scene; |
| private deck: CardConfig[] = []; |
| private hand: CardConfig[] = []; |
| private discardPile: CardConfig[] = []; |
| private playedThisTurn: CardConfig[] = []; |
| private maxHandSize: number; |
|
|
| constructor(scene: Phaser.Scene, maxHandSize: number = 5) { |
| super(); |
| this.scene = scene; |
| this.maxHandSize = maxHandSize; |
| } |
|
|
| |
|
|
| |
| initDeck(cards: CardConfig[]): void { |
| this.deck = [...cards]; |
| } |
|
|
| |
| shuffle(): void { |
| Phaser.Utils.Array.Shuffle(this.deck); |
| this.emit('deckShuffled'); |
| } |
|
|
| |
|
|
| |
| drawToHand(count: number): CardConfig[] { |
| const drawn: CardConfig[] = []; |
| for (let i = 0; i < count && this.hand.length < this.maxHandSize; i++) { |
| if (this.deck.length === 0) { |
| this.reshuffleDiscard(); |
| if (this.deck.length === 0) { |
| this.emit('deckEmpty'); |
| break; |
| } |
| } |
| const card = this.deck.pop()!; |
| this.hand.push(card); |
| drawn.push(card); |
| this.emit('cardDrawn', card); |
| } |
| this.emit('handUpdated', [...this.hand]); |
| return drawn; |
| } |
|
|
| |
| playCard(card: CardConfig): boolean { |
| const index = this.hand.findIndex((c) => c.id === card.id); |
| if (index === -1) return false; |
| this.hand.splice(index, 1); |
| this.playedThisTurn.push(card); |
| this.emit('cardPlayed', card); |
| this.emit('handUpdated', [...this.hand]); |
| return true; |
| } |
|
|
| |
| discardFromHand(card: CardConfig): boolean { |
| const index = this.hand.findIndex((c) => c.id === card.id); |
| if (index === -1) return false; |
| this.hand.splice(index, 1); |
| this.discardPile.push(card); |
| this.emit('cardDiscarded', card); |
| this.emit('handUpdated', [...this.hand]); |
| return true; |
| } |
|
|
| |
| discardHand(): void { |
| this.discardPile.push(...this.hand); |
| this.hand = []; |
| this.emit('handUpdated', []); |
| } |
|
|
| |
| endTurn(): void { |
| this.discardPile.push(...this.playedThisTurn); |
| this.playedThisTurn = []; |
| } |
|
|
| |
|
|
| getHand(): CardConfig[] { |
| return [...this.hand]; |
| } |
| getDeckSize(): number { |
| return this.deck.length; |
| } |
| getDiscardSize(): number { |
| return this.discardPile.length; |
| } |
|
|
| |
|
|
| |
| private reshuffleDiscard(): void { |
| this.deck = [...this.discardPile]; |
| this.discardPile = []; |
| this.shuffle(); |
| } |
| } |
|
|