From 3c1309a63adcc0e6aba4d37b3dc063536b79a91b Mon Sep 17 00:00:00 2001 From: Nathan Date: Sat, 12 Oct 2024 11:03:09 +0100 Subject: [PATCH] Add max hand size check ECS --- public/board.js | 52 +++++++++++++++---------------------------------- 1 file changed, 16 insertions(+), 36 deletions(-) diff --git a/public/board.js b/public/board.js index 0a9164d..df3d084 100644 --- a/public/board.js +++ b/public/board.js @@ -469,6 +469,8 @@ class Board{ // Move itemKey fromPosition in elementFrom to toPosition in elementTo // Can also switch item between players + + // Loop all items, get the item with boardElement elementFrom and player playerFrom for(let itemKey = 0; itemKey < item.length; itemKey++){ // Get the item from the element if(itemKey in boardElement && boardElement[itemKey] == elementFrom){ @@ -539,53 +541,31 @@ class Board{ } // Draw a card, traditional TCG drawACard(cardsToDraw = 1){ - - for(let draw = 0; draw < cardsToDraw; draw++){ - // Move from player0, position 0 (top) of deck, to hand - this.addFromBoardElement(0, 0, 'deck', 'hand', null, null); - } - - if(false){ - // For loop so that animations will play each time (when they exist) for(let draw = 0; draw < cardsToDraw; draw++){ - if(playerHand.length >= maxHandSize){ - alert('Hand full '+playerHand.length+'/'+maxHandSize); + // Check there's space in hand + // TODO: Normalise this for all element/player combos + let elementLength = getCurrentPositionAndLength('hand', 0)[1]; + if(elementLength >= maxHandSize){ + alert('Hand full '+elementLength+'/'+maxHandSize); return 0; } - - // Random card from deck, remove from deck, add to hand - let cardToDraw = Math.floor(Math.random() * deckCount); - let cardDrawn = playerDeck[cardToDraw]; - // Remove from deck - playerDeck.splice(cardToDraw, 1); - // Add to hand - playerHand.push(cardDrawn); - this.drawBoard(); - } + // Move from player0, position 0 (top) of deck, to hand + this.addFromBoardElement(0, 0, 'deck', 'hand', null, null); } } drawACardOpponent(cardsToDraw = 1){ for(let draw = 0; draw < cardsToDraw; draw++){ - // Move from player0, position 0 (top) of deck, to hand - this.addFromBoardElement(1, 0, 'deck', 'hand', null, null); - } + // Move from player1, position 0 (top) of deck, to hand - if(false){ - for(let draw = 0; draw < cardsToDraw; draw++){ - if(opponentHand.length >= maxHandSize){ - alert('Hand full '+opponentHand.length+'/'+maxHandSize); + // Check there's space in hand + // TODO: Normalise this for all element/player combos + let elementLength = getCurrentPositionAndLength('hand', 1)[1]; + if(elementLength >= maxHandSize){ + alert('Hand full '+elementLength+'/'+maxHandSize); return 0; } - // Random card from deck, remove from deck, add to hand - let cardToDraw = Math.floor(Math.random() * deckCountOpponent); - let cardDrawn = opponentDeck[cardToDraw]; - // Remove from deck - opponentDeck.splice(cardToDraw, 1); - // Add to hand - opponentHand.push(cardDrawn); - this.drawBoard(); - } + this.addFromBoardElement(1, 0, 'deck', 'hand', null, null); } }