Created
August 3, 2023 10:58
-
-
Save Kooshaba/b61d62ed1e213f9bc34fb42a1298635e to your computer and use it in GitHub Desktop.
Sky Strife Frenzy Plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { | |
network: { | |
match, | |
}, | |
utils: { | |
isOwnedByCurrentPlayer, | |
manhattan, | |
}, | |
} = networkLayer; | |
const { | |
api: { | |
canMoveToAndAttack, | |
canAttack, | |
move, | |
attack, | |
}, | |
} = localLayer; | |
const { | |
scenes: { | |
Main: { | |
input, | |
}, | |
} | |
} = phaserLayer; | |
input.onKeyPress(keys => keys.has("P"), () => { | |
const selectedEntity = [...runQuery([Has(Selected)])].find(e => isOwnedByCurrentPlayer(e)); | |
if(!selectedEntity) return; | |
console.log("Frenzy mode!"); | |
const enemyUnits = [...runQuery([Has(Combat), HasValue(Position, { z: match })])].filter(e => !isOwnedByCurrentPlayer(e)); | |
if(!enemyUnits.length) return; | |
let closestEnemy = undefined; | |
let closestDistance = Infinity; | |
for(const enemy of enemyUnits) { | |
const distance = manhattan(getComponentValueStrict(Position, selectedEntity), getComponentValueStrict(Position, enemy)); | |
if(distance < closestDistance) { | |
closestEnemy = enemy; | |
closestDistance = distance; | |
} | |
} | |
if(!closestEnemy) return; | |
if(canAttack(selectedEntity, closestEnemy)) { | |
attack(selectedEntity, closestEnemy); | |
return; | |
} | |
const closestUnblockedPosition = canMoveToAndAttack(selectedEntity, closestEnemy); | |
if(closestUnblockedPosition) { | |
move(selectedEntity, closestUnblockedPosition, closestEnemy); | |
return; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment