2020-12-05 00:04:40 +01:00
|
|
|
/* -------------------------------------------- */
|
2020-12-20 02:07:27 +01:00
|
|
|
import { HtmlUtility } from "./html-utility.js";
|
2021-03-31 01:15:55 +02:00
|
|
|
import { Misc } from "./misc.js";
|
2021-02-25 02:13:39 +01:00
|
|
|
import { RdDCombatManager } from "./rdd-combat.js";
|
2023-03-10 22:41:11 +01:00
|
|
|
import { Targets } from "./targets.js";
|
2020-12-05 00:04:40 +01:00
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
export class RdDTokenHud {
|
2020-12-20 02:07:27 +01:00
|
|
|
|
2021-05-04 21:22:25 +02:00
|
|
|
static init() {
|
|
|
|
// Integration du TokenHUD
|
2022-09-07 18:47:56 +02:00
|
|
|
Hooks.on('renderTokenHUD', (app, html, token) => { RdDTokenHud.addTokenHudExtensions(app, html, token._id) });
|
2021-01-10 00:30:37 +01:00
|
|
|
}
|
|
|
|
|
2021-01-05 15:28:05 +01:00
|
|
|
/* -------------------------------------------- */
|
2021-05-04 21:22:25 +02:00
|
|
|
static async removeExtensionHud(app, html, tokenId) {
|
|
|
|
html.find('.control-icon.rdd-combat').remove();
|
|
|
|
html.find('.control-icon.rdd-initiative').remove();
|
2021-01-05 15:28:05 +01:00
|
|
|
}
|
2020-12-05 00:04:40 +01:00
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
2023-03-10 22:33:05 +01:00
|
|
|
static async addExtensionHud(app, html, tokenId, isCombat) {
|
2021-01-05 15:28:05 +01:00
|
|
|
|
2020-12-20 21:54:09 +01:00
|
|
|
let token = canvas.tokens.get(tokenId);
|
2020-12-20 02:07:27 +01:00
|
|
|
let actor = token.actor;
|
2023-03-10 22:33:05 +01:00
|
|
|
app.hasExtension = true;
|
2023-03-10 22:41:11 +01:00
|
|
|
// soins
|
|
|
|
await RdDTokenHud.addExtensionHudSoins(html, actor);
|
|
|
|
|
2023-03-10 22:33:05 +01:00
|
|
|
if (isCombat) {
|
|
|
|
let combatant = game.combat.combatants.find(c => c.tokenId == tokenId);
|
|
|
|
if (!(combatant?.actor)) {
|
2022-07-03 15:32:20 +02:00
|
|
|
ui.notifications.warn(`Le combatant ${token.name} n'est pas associé à un acteur, impossible de déterminer ses actions de combat!`)
|
|
|
|
return;
|
2023-03-10 22:33:05 +01:00
|
|
|
}
|
|
|
|
let actions = RdDCombatManager.listActionsCombat(combatant);
|
|
|
|
// initiative
|
|
|
|
await RdDTokenHud.addExtensionHudInit(html, combatant, actions);
|
|
|
|
// combat
|
|
|
|
await RdDTokenHud.addExtensionHudCombat(html, combatant, actions);
|
2022-07-03 15:32:20 +02:00
|
|
|
}
|
2020-12-20 02:07:27 +01:00
|
|
|
|
2023-03-10 22:33:05 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static async addExtensionHudInit(html, combatant, actions) {
|
2021-05-04 21:22:25 +02:00
|
|
|
const hudData = {
|
2023-03-10 22:33:05 +01:00
|
|
|
combatant, actions,
|
2022-07-03 15:32:20 +02:00
|
|
|
commandes: [
|
|
|
|
{ name: "Autre action", command: 'autre' },
|
2023-03-10 22:33:05 +01:00
|
|
|
{ name: 'Initiative +1', command: 'inc', value: 0.01 },
|
2022-07-03 15:32:20 +02:00
|
|
|
{ name: 'Initiative -1', command: 'dec', value: -0.01 }]
|
2021-05-04 21:22:25 +02:00
|
|
|
};
|
|
|
|
const controlIconCombat = html.find('.control-icon[data-action=combat]');
|
2022-07-03 15:32:20 +02:00
|
|
|
await RdDTokenHud._configureSubMenu(controlIconCombat,
|
|
|
|
'systems/foundryvtt-reve-de-dragon/templates/hud-actor-init.html',
|
|
|
|
hudData,
|
2020-12-20 02:07:27 +01:00
|
|
|
(event) => {
|
2022-07-03 15:32:20 +02:00
|
|
|
let initCommand = event.currentTarget.attributes['data-command']?.value;
|
|
|
|
let combatantId = event.currentTarget.attributes['data-combatant-id']?.value;
|
|
|
|
if (initCommand) {
|
|
|
|
RdDTokenHud._initiativeCommand(initCommand, combatantId);
|
|
|
|
} else {
|
|
|
|
let index = event.currentTarget.attributes['data-action-index'].value;
|
2023-03-10 22:33:05 +01:00
|
|
|
let action = hudData.actions[index];
|
2022-07-03 15:32:20 +02:00
|
|
|
RdDCombatManager.rollInitiativeAction(combatantId, action);
|
2023-03-10 22:33:05 +01:00
|
|
|
}
|
2020-12-05 00:04:40 +01:00
|
|
|
});
|
2023-03-10 22:33:05 +01:00
|
|
|
}
|
2020-12-20 02:07:27 +01:00
|
|
|
|
2023-03-10 22:33:05 +01:00
|
|
|
static async addExtensionHudCombat(html, combatant, actions) {
|
|
|
|
const hudData = { combatant, actions, commandes: [] };
|
2021-05-04 21:22:25 +02:00
|
|
|
const controlIconTarget = html.find('.control-icon[data-action=target]');
|
|
|
|
await RdDTokenHud._configureSubMenu(controlIconTarget, 'systems/foundryvtt-reve-de-dragon/templates/hud-actor-attaque.html', hudData,
|
2020-12-20 02:07:27 +01:00
|
|
|
(event) => {
|
2022-07-03 15:32:20 +02:00
|
|
|
const actionIndex = event.currentTarget.attributes['data-action-index']?.value;
|
2023-03-10 22:33:05 +01:00
|
|
|
const action = hudData.actions[actionIndex];
|
2023-06-13 01:55:38 +02:00
|
|
|
if (action.action == 'possession') {
|
2023-03-10 22:33:05 +01:00
|
|
|
const possession = combatant.actor.getPossession(action.system.possessionid);
|
|
|
|
combatant.actor.conjurerPossession(possession);
|
2022-07-03 15:32:20 +02:00
|
|
|
}
|
|
|
|
else {
|
2023-03-10 22:33:05 +01:00
|
|
|
combatant.actor.rollArme(action);
|
2022-07-03 15:32:20 +02:00
|
|
|
}
|
2020-12-05 21:24:31 +01:00
|
|
|
});
|
2020-12-20 02:07:27 +01:00
|
|
|
}
|
|
|
|
|
2023-03-10 22:41:11 +01:00
|
|
|
static async addExtensionHudSoins(html, sourceActor) {
|
|
|
|
const target = Targets.getTarget({ warn: false });
|
|
|
|
if (target?.actor) {
|
2023-03-14 02:38:21 +01:00
|
|
|
const hudSoins = { blessures: target.actor.blessuresASoigner() ?? [] };
|
2023-03-10 22:41:11 +01:00
|
|
|
if (hudSoins.blessures.length > 0) {
|
|
|
|
// soins
|
|
|
|
const controlIconTarget = html.find('.control-icon[data-action=combat]');
|
|
|
|
await RdDTokenHud._configureSubMenu(controlIconTarget,
|
|
|
|
'systems/foundryvtt-reve-de-dragon/templates/hud-actor-soins.hbs',
|
|
|
|
hudSoins,
|
|
|
|
(event) => {
|
|
|
|
const blessureId = event.currentTarget.attributes['data-blessure-id']?.value;
|
|
|
|
sourceActor.rollSoins(target.actor, blessureId)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-03 15:32:20 +02:00
|
|
|
static _initiativeCommand(initCommand, combatantId) {
|
|
|
|
switch (initCommand) {
|
|
|
|
case 'inc': return RdDCombatManager.incDecInit(combatantId, 0.01);
|
|
|
|
case 'dec': return RdDCombatManager.incDecInit(combatantId, -0.01);
|
2023-03-10 22:33:05 +01:00
|
|
|
case 'autre': return RdDCombatManager.rollInitiativeAction(combatantId,
|
2022-09-07 18:47:56 +02:00
|
|
|
{ name: "Autre action", action: 'autre', system: { initOnly: true, competence: "Autre action" } });
|
2022-07-03 15:32:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-05 15:28:05 +01:00
|
|
|
/* -------------------------------------------- */
|
|
|
|
static async addTokenHudExtensions(app, html, tokenId) {
|
2023-03-10 22:33:05 +01:00
|
|
|
const controlIconCombat = html.find('.control-icon[data-action=combat]');
|
|
|
|
if (controlIconCombat.length > 0) {
|
|
|
|
controlIconCombat.click(event => {
|
|
|
|
if (event.currentTarget.className.includes('active')) {
|
|
|
|
RdDTokenHud.removeExtensionHud(app, html, tokenId);
|
|
|
|
} else {
|
|
|
|
setTimeout(() => RdDTokenHud.addExtensionHud(app, html, tokenId), 200);
|
|
|
|
}
|
|
|
|
});
|
2021-01-05 15:28:05 +01:00
|
|
|
|
2023-03-10 22:33:05 +01:00
|
|
|
const isCombat = controlIconCombat[0].className.includes('active');
|
|
|
|
RdDTokenHud.addExtensionHud(app, html, tokenId, isCombat);
|
2021-01-05 15:28:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-21 15:14:49 +01:00
|
|
|
/* -------------------------------------------- */
|
2020-12-20 21:54:09 +01:00
|
|
|
static async _configureSubMenu(insertionPoint, template, hudData, onMenuItem) {
|
|
|
|
const hud = $(await renderTemplate(template, hudData));
|
2020-12-20 02:07:27 +01:00
|
|
|
const list = hud.find('div.rdd-hud-list');
|
2023-03-10 22:33:05 +01:00
|
|
|
|
2021-05-04 21:22:25 +02:00
|
|
|
RdDTokenHud._toggleHudListActive(hud, list);
|
2023-03-10 22:33:05 +01:00
|
|
|
|
2021-05-04 21:22:25 +02:00
|
|
|
hud.find('img.rdd-hud-togglebutton').click(event => RdDTokenHud._toggleHudListActive(hud, list));
|
2020-12-20 02:07:27 +01:00
|
|
|
list.find('.rdd-hud-menu').click(onMenuItem);
|
|
|
|
|
|
|
|
insertionPoint.after(hud);
|
|
|
|
}
|
|
|
|
|
2021-05-04 21:22:25 +02:00
|
|
|
static _toggleHudListActive(hud, list) {
|
|
|
|
hud.toggleClass('active');
|
2023-01-10 22:11:16 +01:00
|
|
|
HtmlUtility.showControlWhen(list, hud.hasClass('active'));
|
2021-05-04 21:22:25 +02:00
|
|
|
}
|
2020-12-05 00:04:40 +01:00
|
|
|
}
|