fvtt-pegasus-rpg/modules/pegasus-combat.js

197 lines
6.1 KiB
JavaScript
Raw Normal View History

2021-12-02 07:38:59 +01:00
import { PegasusUtility } from "./pegasus-utility.js";
2023-09-13 17:37:30 +02:00
/* -------------------------------------------- */
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,
});
}
2023-09-16 10:16:59 +02:00
/* -------------------------------------------- */
async getData() {
let combatData = await super.getData()
for (let t of combatData.turns) {
let c = game.combat.combatants.get(t.id)
let TICs = c.getFlag("world", "TICs")
if (TICs) {
t.TICs = TICs
} else {
t.TICs = []
}
}
console.log("CBT", combatData)
return combatData
}
2023-09-13 17:37:30 +02:00
/* -------------------------------------------- */
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)
})
2023-09-14 21:03:08 +02:00
html.find('.reset-npc-initiative').click(ev => {
game.combat.resetNPCInitiative()
})
2023-09-16 10:16:59 +02:00
html.find('.select-combat-actor').click(ev => {
let combatantId = $(ev.currentTarget).data("combatant-id")
game.combat.selectActor(combatantId)
})
2023-09-14 21:03:08 +02:00
2023-09-13 17:37:30 +02:00
}
}
2021-12-02 07:38:59 +01:00
/* -------------------------------------------- */
export class PegasusCombat extends Combat {
2023-09-13 17:37:30 +02:00
2021-12-02 07:38:59 +01:00
/* -------------------------------------------- */
2023-09-13 17:37:30 +02:00
async rollInitiative(ids, formula = undefined, messageOptions = {}) {
2021-12-02 07:38:59 +01:00
ids = typeof ids === "string" ? [ids] : ids;
for (let cId = 0; cId < ids.length; cId++) {
2021-12-20 11:54:19 +01:00
const c = this.combatants.get(ids[cId]);
let id = c._id || c.id;
2023-09-13 17:37:30 +02:00
let initBonus = c.actor ? c.actor.getInitiativeScore(this.id, id) : -1;
await this.updateEmbeddedDocuments("Combatant", [{ _id: id, initiative: initBonus }]);
2021-12-02 07:38:59 +01:00
}
return this;
}
2022-01-28 10:05:54 +01:00
2023-09-14 21:03:08 +02:00
/* -------------------------------------------- */
async resetNPCInitiative() {
2023-09-16 10:16:59 +02:00
for (let c of this.combatants) {
2023-09-14 21:03:08 +02:00
if (c.actor && c.actor.type == "npc") {
await this.updateEmbeddedDocuments("Combatant", [{ _id: c.id, initiative: -1 }]);
}
2023-09-16 10:16:59 +02:00
}
2023-09-14 21:03:08 +02:00
}
2023-09-13 17:37:30 +02:00
/* -------------------------------------------- */
isCharacter(combatantId) {
const combatant = game.combat.combatants.get(combatantId)
if (combatant) {
return combatant.actor.type == "character"
2023-09-14 21:03:08 +02:00
}
2023-09-13 17:37:30 +02:00
return false
}
/* -------------------------------------------- */
2023-09-16 10:16:59 +02:00
selectActor(combatantId) {
2023-09-13 17:37:30 +02:00
const combatant = game.combat.combatants.get(combatantId)
if (combatant) {
2023-09-16 10:16:59 +02:00
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} has been nominated to act, ${combatant.actor.name} choose which TIC you wish to activate!</div`
}
ChatMessage.create(chatData);
2023-09-13 17:37:30 +02:00
}
}
/* -------------------------------------------- */
2023-09-16 10:16:59 +02:00
async setTic(combatantId, rollData) {
2023-09-14 21:03:08 +02:00
if (!combatantId) {
2023-09-16 10:16:59 +02:00
return
2023-09-13 17:37:30 +02:00
}
const combatant = game.combat.combatants.get(combatantId)
if (combatant) {
2023-09-16 10:16:59 +02:00
await combatant.setFlag("world", "TICs", rollData.TICs)
2023-09-13 17:37:30 +02:00
}
}
2023-09-16 10:16:59 +02:00
2023-09-13 17:37:30 +02:00
/* -------------------------------------------- */
async revealTIC(num, combatantId) {
2023-09-16 10:16:59 +02:00
console.log('revealTIC', num, combatantId)
if (num == undefined || combatantId == undefined) {
2023-09-13 17:37:30 +02:00
return
}
const combatant = game.combat.combatants.get(combatantId)
if (combatant) {
2023-09-16 10:16:59 +02:00
console.log('revealTIC', num, combatantId, combatant)
let ticData = combatant.getFlag("world", "TICs")
2023-09-13 17:37:30 +02:00
if (ticData) {
2023-09-16 10:16:59 +02:00
console.log('revealTIC', num, combatantId, ticData)
num = Number(num)
ticData[num].revealed = true
ticData[num].displayed = true
combatant.setFlag("world", "TICs", 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[num].text}</div`
};
ChatMessage.create(chatData);
// Manage if all TIC has been ACTED
let allRevealed = true
for (let c of this.combatants) {
let TICs = c.getFlag("world", "TICs")
if (TICs) {
for (let t of TICs) {
if (!t.revealed) {
allRevealed = false
}
}
}
}
// If all TIC has been ACTED, display a message
if (allRevealed) {
let chatData = {
user: game.user.id,
rollMode: game.settings.get("core", "rollMode"),
content: `<div>All Characters have acted, All Characters who do not have Stun gain 1 Momentum!</div`
}
ChatMessage.create(chatData);
}
})
2023-09-13 17:37:30 +02:00
}
}
}
2023-09-16 10:16:59 +02:00
2023-09-14 21:03:08 +02:00
/* -------------------------------------------- */
nextRound() {
2023-09-16 10:16:59 +02:00
for (let c of this.combatants) {
let TICs = duplicate(c.getFlag("world", "TICs"))
for (let t of TICs) {
t.displayed = false
t.revealed = false
t.text = ""
}
c.setFlag("world", "TICs", TICs)
2023-09-14 21:03:08 +02:00
}
super.nextRound()
}
2023-09-16 10:16:59 +02:00
2022-01-28 10:05:54 +01:00
/* -------------------------------------------- */
_onUpdate(changed, options, userId) {
}
2022-03-11 14:00:14 +01:00
/* -------------------------------------------- */
static async checkTurnPosition() {
while (game.combat.turn > 0) {
await game.combat.previousTurn()
}
}
2022-01-28 10:05:54 +01:00
/* -------------------------------------------- */
2023-09-13 17:37:30 +02:00
static async decInitBy10(combatantId, value) {
2022-01-28 10:05:54 +01:00
const combatant = game.combat.combatants.get(combatantId)
let initValue = combatant.initiative + value
2022-02-17 19:17:24 +01:00
await game.combat.setInitiative(combatantId, initValue)
2023-09-13 17:37:30 +02:00
setTimeout(this.checkTurnPosition, 400) // The setInitiative is no more blocking for unknown reason
2022-01-28 10:05:54 +01:00
}
2021-12-02 07:38:59 +01:00
}