2022-11-23 21:42:39 +01:00
|
|
|
import { ENTITE_NONINCARNE } from "./constants.js";
|
|
|
|
import { DialogSelectTarget } from "./dialog-select-target.js";
|
|
|
|
|
|
|
|
export class Targets {
|
|
|
|
static listTargets() {
|
|
|
|
return Array.from(game.user.targets);
|
|
|
|
}
|
|
|
|
|
|
|
|
static hasTargets() {
|
|
|
|
return Targets.listTargets().length > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static extractTokenData(target) {
|
|
|
|
if (!target) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
return { id: target.id, name: target.document.name, img: target.document.texture.src ?? target.actor.img ?? 'icons/svg/mystery-man.svg' };
|
|
|
|
}
|
|
|
|
|
|
|
|
static isTargetEntite(target) {
|
|
|
|
return target?.actor.type == 'entite' && target?.actor.system.definition.typeentite == ENTITE_NONINCARNE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static async selectOneToken(onSelectTarget = target => { }) {
|
|
|
|
const targets = Targets.listTargets();
|
|
|
|
switch (targets.length) {
|
|
|
|
case 0: return;
|
|
|
|
case 1:
|
|
|
|
onSelectTarget(targets[0]);
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
const tokens = targets.map(it => Targets.extractTokenData(it))
|
|
|
|
const html = await renderTemplate("systems/foundryvtt-reve-de-dragon/templates/dialog-select-target.html", {
|
|
|
|
tokens: tokens
|
|
|
|
});
|
|
|
|
new DialogSelectTarget(html, onSelectTarget, targets).render(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-09 22:47:58 +01:00
|
|
|
static getTarget(options = { warn: true }) {
|
2022-11-23 21:42:39 +01:00
|
|
|
const targets = Targets.listTargets();
|
|
|
|
switch (targets.length) {
|
|
|
|
case 1:
|
|
|
|
return targets[0];
|
|
|
|
case 0:
|
2023-03-09 22:47:58 +01:00
|
|
|
if (options.warn) ui.notifications.warn("Vous devez choisir une cible à attaquer!");
|
2022-11-23 21:42:39 +01:00
|
|
|
break;
|
|
|
|
default:
|
2023-03-09 22:47:58 +01:00
|
|
|
if (options.warn) ui.notifications.warn("Vous devez choisir une cible (et <strong>une seule</strong>) à attaquer!");
|
2022-11-23 21:42:39 +01:00
|
|
|
}
|
2023-03-09 22:47:58 +01:00
|
|
|
return undefined;
|
2022-11-23 21:42:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|