fvtt-pegasus-rpg/modules/pegasus-combat.js
2023-09-14 23:23:16 +02:00

151 lines
4.8 KiB
JavaScript

import { PegasusUtility } from "./pegasus-utility.js";
/* -------------------------------------------- */
export class PegasusCombatTracker extends CombatTracker {
/* -------------------------------------------- */
static get defaultOptions() {
let path = "systems/fvtt-pegasus-rpg/templates/pegasus-combat-tracker.html";
return foundry.utils.mergeObject(super.defaultOptions, {
template: path,
});
}
/* -------------------------------------------- */
activateListeners(html) {
super.activateListeners(html)
html.find('.combat-tracker-tic').click(ev => {
let ticNum = $(ev.currentTarget).data("tic-num")
let combatantId = $(ev.currentTarget).data("combatant-id")
game.combat.revealTIC(ticNum, combatantId)
})
html.find('.reset-npc-initiative').click(ev => {
game.combat.resetNPCInitiative()
})
}
}
/* -------------------------------------------- */
export class PegasusCombat extends Combat {
/* -------------------------------------------- */
async rollInitiative(ids, formula = undefined, messageOptions = {}) {
ids = typeof ids === "string" ? [ids] : ids;
for (let cId = 0; cId < ids.length; cId++) {
const c = this.combatants.get(ids[cId]);
let id = c._id || c.id;
let initBonus = c.actor ? c.actor.getInitiativeScore(this.id, id) : -1;
await this.updateEmbeddedDocuments("Combatant", [{ _id: id, initiative: initBonus }]);
}
return this;
}
/* -------------------------------------------- */
async resetNPCInitiative() {
for(let c of this.combatants) {
if (c.actor && c.actor.type == "npc") {
await this.updateEmbeddedDocuments("Combatant", [{ _id: c.id, initiative: -1 }]);
}
}
}
/* -------------------------------------------- */
isCharacter(combatantId) {
const combatant = game.combat.combatants.get(combatantId)
if (combatant) {
return combatant.actor.type == "character"
}
return false
}
/* -------------------------------------------- */
async setTic(combatantId, rollData) {
if (!combatantId) {
return
}
const combatant = game.combat.combatants.get(combatantId)
if (combatant) {
await combatant.setFlag("world", "tic1", { revealed: false, text: rollData.tic1, displayed: false })
await combatant.setFlag("world", "tic2", { revealed: false, text: rollData.tic2, displayed: false })
}
}
/* -------------------------------------------- */
getTIC(num, combatantId) {
if (!combatantId) {
return ""
}
const combatant = game.combat.combatants.get(combatantId)
if (combatant) {
let ticData = combatant.getFlag("world", "tic" + num)
if (ticData) {
/* returns if revealed */
if (ticData.revealed && ticData.displayed) {
return "ACTED"
}
if (ticData.revealed && !ticData.displayed) {
ticData.displayed = true
combatant.setFlag("world", "tic" + num, ticData ).then(() => {
let chatData = {
user: game.user.id,
alias: combatant.actor.name,
rollMode: game.settings.get("core", "rollMode"),
whisper: [game.user.id].concat(ChatMessage.getWhisperRecipients('GM')),
content: `<div>${combatant.actor.name} is performing ${ticData.text}</div`
};
ChatMessage.create(chatData);
return "ACTED"
})
}
}
}
return "TIC " + num
}
/* -------------------------------------------- */
async revealTIC(num, combatantId) {
if (!num || !combatantId) {
return
}
const combatant = game.combat.combatants.get(combatantId)
if (combatant) {
let ticData = combatant.getFlag("world", "tic" + num)
if (ticData) {
ticData.revealed = true
await combatant.setFlag("world", "tic" + num, ticData)
}
}
}
/* -------------------------------------------- */
nextRound() {
for(let c of this.combatants) {
c.setFlag("world", "tic1", { revealed: false, text: "", displayed: false })
c.setFlag("world", "tic2", { revealed: false, text: "", displayed: false })
}
super.nextRound()
}
/* -------------------------------------------- */
_onUpdate(changed, options, userId) {
}
/* -------------------------------------------- */
static async checkTurnPosition() {
while (game.combat.turn > 0) {
await game.combat.previousTurn()
}
}
/* -------------------------------------------- */
static async decInitBy10(combatantId, value) {
const combatant = game.combat.combatants.get(combatantId)
let initValue = combatant.initiative + value
await game.combat.setInitiative(combatantId, initValue)
setTimeout(this.checkTurnPosition, 400) // The setInitiative is no more blocking for unknown reason
}
}