fvtt-hero-system-6/modules/hero6-combat.js

151 lines
5.1 KiB
JavaScript
Raw Normal View History

2022-12-15 21:44:23 +01:00
import { Hero6Utility } from "./hero6-utility.js";
2023-04-06 16:57:19 +02:00
/* -------------------------------------------- */
export class Hero6CombatTracker extends CombatTracker {
/* -------------------------------------------- */
static get defaultOptions() {
var path = "systems/fvtt-hero-system-6/templates/apps/combat-tracker.hbs";
return foundry.utils.mergeObject(super.defaultOptions, {
template: path,
});
}
}
2022-12-15 21:44:23 +01:00
/* -------------------------------------------- */
export class Hero6Combat extends Combat {
2023-04-06 16:57:19 +02:00
/* -------------------------------------------- */
static init() {
Hooks.on("getCombatTrackerEntryContext", (html, options) => { Hero6Combat.pushMenuOptions(html, options); });
}
/* -------------------------------------------- */
static pushMenuOptions(html, options) {
let newOpt
for (let i = 0; i < options.length; i++) {
let option = options[i];
if (option.name == 'COMBAT.CombatantReroll') { // Replace !
option.name = "Hold action";
option.condition = true;
option.icon = '<i class="far fa-question-circle"></i>';
option.callback = target => {
Hero6Combat.holdAction(target.data('combatant-id'));
}
newOpt = duplicate(option)
}
}
newOpt.name = "Abort action"
newOpt.callback = target => {
Hero6Combat.abortAction(target.data('combatant-id'));
}
options.push( newOpt)
}
/* -------------------------------------------- */
static holdAction(combatantId) {
console.log("Combatant HOLD : ", combatantId)
const combatant = game.combat.combatants.get(combatantId)
combatant.setFlag("world", "hero6-hold-action", true)
combatant.update({name: combatant.name + " (H)"})
console.log("HOLD", combatant)
}
/* -------------------------------------------- */
static abortAction(html, combatantId) {
console.log("Combatant ABORT : ", combatantId);
const combatant = game.combat.combatants.get(combatantId);
combatant.setFlag("world", "hero6-abort-action", true)
combatant.update({name: combatant.name + " (A)"})
console.log("ABORT", combatant)
}
/* -------------------------------------------- */
constructor(data, context) {
super(data, context);
this.turnNumber = 1;
this.segmentNumber = 12;
}
/* -------------------------------------------- */
async computeInitiative(c) {
let id = c._id || c.id
if (c.actor.hasPhase(this.segmentNumber)) {
let baseInit = c.actor ? c.actor.getBaseInit() : - 1;
await this.updateEmbeddedDocuments("Combatant", [{ _id: id, initiative: baseInit }]);
} else {
await this.updateEmbeddedDocuments("Combatant", [{ _id: id, initiative: -1, visible: false, active: false }]);
}
console.log("Combatant", c)
}
2022-12-15 21:44:23 +01:00
/* -------------------------------------------- */
2023-04-06 16:57:19 +02:00
async rollInitiative(ids, formula = undefined, messageOptions = {}) {
2022-12-15 21:44:23 +01:00
ids = typeof ids === "string" ? [ids] : ids;
for (let cId = 0; cId < ids.length; cId++) {
2023-04-06 16:57:19 +02:00
const c = this.combatants.get(ids[cId])
await this.computeInitiative(c)
2022-12-15 21:44:23 +01:00
}
return this;
}
2023-04-06 16:57:19 +02:00
/* -------------------------------------------- */
nextRound() {
let turn = this.turn === null ? null : 0; // Preserve the fact that it's no-one's turn currently.
if (this.settings.skipDefeated && (turn !== null)) {
turn = this.turns.findIndex(t => !t.isDefeated);
if (turn === -1) {
ui.notifications.warn("COMBAT.NoneRemaining", { localize: true });
turn = 0;
}
}
let advanceTime = Math.max(this.turns.length - this.turn, 0) * CONFIG.time.turnTime;
advanceTime += CONFIG.time.roundTime;
let nextRound = this.round + 1;
let turnData = this.getFlag("world", "hero6-turn-data")
if (!turnData) {
turnData = { turnNumber: 1, segmentNumber: 12 }
this.setFlag("world", "hero6-turn-data", turnData)
}
turnData = duplicate(turnData)
turnData.segmentNumber -= 1
if (turnData.segmentNumber <= 0) {
turnData.segmentNumber = 12
turnData.turnNumber++
}
this.setFlag("world", "hero6-turn-data", turnData)
this.turnNumber = turnData.turnNumber;
this.segmentNumber = turnData.segmentNumber;
// Re-compute init of actors
for (let c of this.combatants) {
this.computeInitiative(c)
}
// Update the document, passing data through a hook first
const updateData = { round: nextRound, turn, segmentNumber: turnData.segmentNumber, turnNumber: turnData.turnNumber };
const updateOptions = { advanceTime, direction: 1 };
Hooks.callAll("combatRound", this, updateData, updateOptions);
return this.update(updateData, updateOptions);
}
/* -------------------------------------------- */
async _onCreateEmbeddedDocuments(type, documents, result, options, userId) {
console.log(">>>>", documents)
super._onCreateEmbeddedDocuments(type, documents, result, options, userId)
}
2022-12-15 21:44:23 +01:00
/* -------------------------------------------- */
_onUpdate(changed, options, userId) {
}
/* -------------------------------------------- */
static async checkTurnPosition() {
while (game.combat.turn > 0) {
await game.combat.previousTurn()
}
}
}