From 9fac1d3dcc2c3b448407a6e14732697faa2d5ac0 Mon Sep 17 00:00:00 2001 From: Nathan Date: Sun, 13 Oct 2024 20:30:26 +0100 Subject: [PATCH] Jimmied in manaCost stuff, but not 100% working --- public/board.js | 121 ++++++++++++++++++++++++++++++------------------ 1 file changed, 76 insertions(+), 45 deletions(-) diff --git a/public/board.js b/public/board.js index 51ff2a4..706be98 100644 --- a/public/board.js +++ b/public/board.js @@ -624,65 +624,96 @@ class Board{ // Loop probably not needed, but may be for eg. 'play X cards from top of deck' for(let play = 0; play < cardsToPlay; play++){ - // Check there's space on board TODO: change to locationTo - // TODO: Normalise this for all element/player combos - let elementLength = getCurrentPositionAndLength(toElement, toPlayer)[1]; - // TODO: Compare against the maxSize for the boardElement - // this is fine(ish) for now, as just using hand - if(elementLength >= maxHandSize){ - alert('Board full '+elementLength+'/'+maxHandSize); - return 0; + + // Check there's space on the board/mana zone/shield/etc + if(!this.hasSpaceInBoardElement(toElement, toPlayer)){ + alert('No space in element'); + return false; } - // When done remove false && - // TODO: Rewrite for ECS - // Mana cost required and mana tapping for playing a card from hand, etc - if(false && cardPlayed.cost > playerMana.length){ - alert('Not enough mana'); - return 0; + + let itemKey = this.getItemKey(fromElement, positionFrom); + console.log(itemKey); + console.log(positionFrom); + + switch(fromElement){ + case 'hand': + // Mana cost required and mana tapping for playing a card from hand, etc + // The player casting/summoning should pay, ofc + let canPayMana = this.canPayMana(itemKey, fromPlayer) + if(canPayMana !== true){ + alert(canPayMana); + return false; + }else{ + // Tap mana and play TODO: TEMP SOLUTION + // TODO: May want to make this recursive, it's better and less bulky + // but still lots of dupe code + let items = this.ECSLoop('mana', fromPlayer, null, null); + for(let item = 0; item < items.length; item++){ + let mana = items[item]; + // For now just tapping the first untapped mana + // TODO: Tap mana of correct colour (also allow player to select in fut) + if(!this.isTapped(mana) && cardData[mana].colour == cardData[itemKey].colour){ + this.tapCard(mana); + }else{ + return false; + } + } + } } // Move from player0, position 0 (top) of deck, to hand, to pos(null/auto) for toPlayer this.addFromBoardElement(fromPlayer, positionFrom, fromElement, toElement, null, toPlayer); } + this.drawBoard(); + } - // Needs doing for playing cards from hand - if(false){ - let manaUsed = []; + getItemKey(boardElementId, listPositionId){ + let itemKey = this.ECSLoop(boardElementId, null, null, listPositionId); + if(itemKey.length < 1){ + alert('Could not find key'); + return false; + } + if(itemKey.length > 1){ + alert('Found more than one key'); + return false + } + // Return first index of array, aka the itemKey + return itemKey[0]; + } + + hasSpaceInBoardElement(toElement, toPlayer){ - if(cardPlayed.cost > playerMana.length){ - alert('Not enough mana'); - return 0; - }else{ - let canPlay = false; - let needsMana = 1; - let usedMana = 0; - playerMana.forEach(function(manaCard, key){ - if(cardPlayed.colour == manaCard.colour && manaCard.tapped == false && needsMana > usedMana){ - console.log(manaCard); - // Needs changing for multiple colour usage - // 2 red, 1 red + 1 blue, etc. - // Currently only gets one mana of the cards colour - manaUsed.push(key); - usedMana++; - canPlay = true; - } - }); + let elementLength = getCurrentPositionAndLength(toElement, toPlayer)[1]; - if(!canPlay){ - alert('Mana conditions not met, right click hand unit to play as mana'); - return 0; - } + if(elementLength >= maxHandSize){ + alert('Board full '+elementLength+'/'+maxHandSize); + return false; + } + return true; + } + canPayMana(itemToPlay, playerId){ + // Check player has enough mana + if(this.ECSLoop('mana', playerId, null, null).length < cardData[itemToPlay].cost){ + return 'Not enough mana'; } - // Tap mana to be used - manaUsed.forEach(function(cardKey, key){ - playerMana[cardKey].tapped = true; - }); - + // TODO: Check mana colours are available to play, then tap them + // TODO: Multicolours, and X of same colour checks + let manaUsed = []; + // For now, until adding colour, and other attributes to ECS modal + let manaRequired = cardData[itemToPlay].colour; + console.log(manaRequired); + let items = this.ECSLoop('mana', player[itemToPlay], null, null); + for(let item = 0; item < items.length; item++){ + let itemKey = items[item]; + if(cardData[itemKey].colour == manaRequired && !this.isTapped(itemKey)){ + manaUsed.push(itemKey); // This would be how I'd loop for X colour, or multi + return true; + } } - this.drawBoard(); + return 'Do not have correct mana requirements'; } inspectCard(cardToInspect){