Files
duneinichecker/scripts/combat/Combat2d20.js
2026-03-02 12:02:33 +01:00

88 lines
2.0 KiB
JavaScript

const KEY = "dune";
export default class Combat2d20 extends Combat {
get combatantsTurnDone() {
return this.getFlag(KEY, "combatantsTurnDone") ?? [];
}
get combatantsTurnsDoneThisRound() {
const combatantsTurnDone = this.combatantsTurnDone;
return combatantsTurnDone[this.round] ?? {};
}
async rollInitiative() {
return this;
}
async setTurn(newTurn) {
this.turn = newTurn;
// Update the document, passing data through a hook first
const updateData = {round: this.round, turn: newTurn};
const updateOptions = {advanceTime: CONFIG.time.turnTime, direction: 1};
Hooks.callAll("combatTurn", this, updateData, updateOptions);
return this.update(updateData, updateOptions);
}
setupTurns() {
// Determine the turn order and the current turn
const turns = this.combatants.contents;
// Sort alphabetically by name first
turns.sort((a, b) => {
if (a.name < b.name) {
return -1;
}
if (a.name > b.name) {
return 1;
}
return 0;
});
// Then make sure the player characters are first
turns.sort(
(a, b) => Number(b.hasPlayerOwner) - Number(a.hasPlayerOwner)
);
if (this.turn !== null) this.turn =
Math.clamp(this.turn, 0, turns.length - 1);
// Update state tracking
let c = turns[this.turn];
this.current = {
round: this.round,
turn: this.turn,
combatantId: c ? c.id : null,
tokenId: c ? c.tokenId : null,
};
// One-time initialization of the previous state
if (!this.previous) this.previous = this.current;
// Return the array of prepared turns
return this.turns = turns;
}
async toggleTurnDone(combatantId) {
if (!game.user.isGM) return;
if (!this.started) return;
const combatantsTurnsDoneThisRound = this.combatantsTurnsDoneThisRound;
const turnDone = !(combatantsTurnsDoneThisRound[combatantId] ?? false);
combatantsTurnsDoneThisRound[combatantId] = turnDone;
const combatantsTurnDone = this.combatantsTurnDone;
combatantsTurnDone[this.round] = combatantsTurnsDoneThisRound;
this.setFlag(KEY, "combatantsTurnDone", combatantsTurnDone);
}
}