Skip to content

Instantly share code, notes, and snippets.

@Kooshaba
Created August 3, 2023 10:58
Show Gist options
  • Save Kooshaba/b61d62ed1e213f9bc34fb42a1298635e to your computer and use it in GitHub Desktop.
Save Kooshaba/b61d62ed1e213f9bc34fb42a1298635e to your computer and use it in GitHub Desktop.
Sky Strife Frenzy Plugin
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