From 1e82ae04b948515d5d9c189f89e51b82690425e6 Mon Sep 17 00:00:00 2001 From: Nathan Date: Tue, 29 Oct 2024 00:12:02 +0000 Subject: [PATCH] Add combat logic for taunt/flight/reach passives --- public/board.js | 65 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/public/board.js b/public/board.js index f71ec43..e3c0d4f 100644 --- a/public/board.js +++ b/public/board.js @@ -1045,6 +1045,12 @@ class Board{ let atkAttacker = this.attackOf(itemAttacking); let atkDefender = this.attackOf(itemDefending); + // Check if the attack can target the defender + if(!this.canAttackDefender(itemAttacking, itemDefending)){ + alert('Cannot target card.'); + break; + } + // Does Attacker kill Defender if(atkDefender <= atkAttacker){ this.sendToGrave(itemDefending); @@ -1062,6 +1068,13 @@ class Board{ break; case 'shield': + + // Check shield can be targetted (not defended by taunt/block unit) + if(!this.canAttackDirect(itemAttacking, player[itemDefending])){ + alert('Cannot attack shield for some reason'); + break; + } + // If the shield is tapped 'destroy' it if(this.isTapped(itemDefending)){ if(!this.destroyShield(itemDefending)){ @@ -1081,6 +1094,58 @@ class Board{ this.drawBoard(); } + canAttackDefender(itemAttacking, itemDefending){ + + // Currently all there is is that flight cannot + // be attacked by anything but flight and reach + + // If defender has flight + if(flight[itemDefending]){ + // And attack doesn't have either flight or reach + if(flight[itemAttacking] === undefined && reach[itemAttacking] === undefined){ + return false; + } + } + + return true; + + + } + canAttackDirect(itemAttacking, playerDefending){ + + // Defender must not be tapped, as you can ignore tapped units + let itemsToBlock = this.getItems('board', playerDefending, null, null); + for(let item = 0; item < itemsToBlock.length; item++){ + + let defender = itemsToBlock[item]; + + // Defender cannot block if tapped + if(this.isTapped(defender)){ + continue; + } + + // If the defender doesn't have taunt, direct attacks can occur + if(taunt[defender] == undefined){ + continue; + } + + // If the attacked doesn't have flight (or another means of bypassing taunt) + // it can't attack direct + if(!flight[itemAttacking]){ + return false; + } + + // If the unitAttacking has flight, it can be blocked by a taunt+flight + // or giant+flight + if(flight[itemAttacking] && flight[defender]){ + return false; + } + + } + + return true; + + } attackOf(itemKey){ // TODO: Change this to ECSey element when added return cardData[itemKey];