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

177 lines
5.5 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() {
2023-06-30 17:21:11 +02:00
let path = "systems/fvtt-hero-system-6/templates/apps/combat-tracker.hbs";
2023-04-06 16:57:19 +02:00
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
/* -------------------------------------------- */
2023-06-30 21:11:29 +02:00
static ready() {
2023-04-06 16:57:19 +02:00
Hooks.on("getCombatTrackerEntryContext", (html, options) => { Hero6Combat.pushMenuOptions(html, options); });
2023-06-30 21:11:29 +02:00
game.combat.settings.resource = "characteristics.spd.value";
2023-04-06 16:57:19 +02:00
}
2023-06-30 21:11:29 +02:00
2023-04-06 16:57:19 +02:00
/* -------------------------------------------- */
static pushMenuOptions(html, options) {
let newOpt
for (let i = 0; i < options.length; i++) {
let option = options[i];
if (option.name == 'COMBAT.CombatantReroll') { // Replace !
2023-06-30 21:11:29 +02:00
option.name = "Hold/Unhold action";
2023-04-06 16:57:19 +02:00
option.condition = true;
option.icon = '<i class="far fa-question-circle"></i>';
option.callback = target => {
Hero6Combat.holdAction(target.data('combatant-id'));
}
2023-06-30 21:11:29 +02:00
//newOpt = duplicate(option)
2023-04-06 16:57:19 +02:00
}
}
2023-06-30 21:11:29 +02:00
//options.push(newOpt)
2023-04-06 16:57:19 +02:00
}
/* -------------------------------------------- */
static holdAction(combatantId) {
const combatant = game.combat.combatants.get(combatantId)
2023-06-30 21:11:29 +02:00
combatant.actor.holdAction()
2023-04-06 16:57:19 +02:00
}
/* -------------------------------------------- */
constructor(data, context) {
super(data, context);
2023-06-30 17:21:11 +02:00
this.turnNumber = 0;
2023-04-06 16:57:19 +02:00
this.segmentNumber = 12;
}
/* -------------------------------------------- */
2023-06-30 21:11:29 +02:00
async startCombat() {
game.combat.settings.resource = "characteristics.spd.value";
let updList = []
for (let c of this.combatants) {
this.computeInitiative(c, updList)
await c.actor.cleanCombat()
}
if (updList.length > 0) {
await this.updateEmbeddedDocuments("Combatant", updList);
}
super.startCombat();
}
/* -------------------------------------------- */
computeInitiative(c, updList) {
2023-04-06 16:57:19 +02:00
let id = c._id || c.id
2023-06-30 21:11:29 +02:00
if (c.actor.hasPhase(this.segmentNumber) || c.actor.getHoldAction()) {
let baseInit = c.actor ? c.actor.getBaseInit() : 0;
let name = c.actor.name
if (c.actor.getHoldAction()) {
name = c.actor.name + " (H)"
}
if (c.actor.getAbortAction()) {
name = c.actor.name + " (A)"
}
updList.push({ _id: id, name: name, initiative: baseInit, holdAction: c.holdAction })
2023-04-06 16:57:19 +02:00
} else {
2023-06-30 21:11:29 +02:00
updList.push({ _id: id, name: name, initiative: 0, holdAction: c.holdAction })
2023-04-06 16:57:19 +02:00
}
}
2023-06-30 21:11:29 +02:00
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;
2023-06-30 21:11:29 +02:00
console.log("Roll INIT")
let updList = []
2022-12-15 21:44:23 +01:00
for (let cId = 0; cId < ids.length; cId++) {
2023-04-06 16:57:19 +02:00
const c = this.combatants.get(ids[cId])
2023-06-30 21:11:29 +02:00
this.computeInitiative(c, updList)
}
if (updList.length > 0) {
await this.updateEmbeddedDocuments("Combatant", updList);
2022-12-15 21:44:23 +01:00
}
return this;
}
2023-06-30 21:11:29 +02:00
/* -------------------------------------------- */
async rebuildInitiative() {
let updList = []
for (let c of this.combatants) {
this.computeInitiative(c, updList)
}
console.log(this.combatants, updList)
if (updList.length > 0) {
await this.updateEmbeddedDocuments("Combatant", updList);
}
}
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) {
2023-06-30 17:21:11 +02:00
turnData = { turnNumber: 0, segmentNumber: 12 }
2023-04-06 16:57:19 +02:00
this.setFlag("world", "hero6-turn-data", turnData)
}
turnData = duplicate(turnData)
2023-06-30 17:21:11 +02:00
turnData.segmentNumber += 1
if (turnData.segmentNumber > 12) {
turnData.segmentNumber = 1
2023-04-06 16:57:19 +02:00
turnData.turnNumber++
}
this.setFlag("world", "hero6-turn-data", turnData)
this.turnNumber = turnData.turnNumber;
this.segmentNumber = turnData.segmentNumber;
// Re-compute init of actors
2023-06-30 21:11:29 +02:00
this.rebuildInitiative()
2023-04-06 16:57:19 +02:00
// 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) {
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()
}
}
}