2022-11-23 21:42:39 +01:00
|
|
|
import { ENTITE_NONINCARNE } from "./constants.js";
|
2023-11-21 16:03:26 +01:00
|
|
|
import { DialogSelect } from "./dialog-select.js";
|
2022-11-23 21:42:39 +01:00
|
|
|
|
|
|
|
export class Targets {
|
|
|
|
static listTargets() {
|
|
|
|
return Array.from(game.user.targets);
|
|
|
|
}
|
|
|
|
|
|
|
|
static hasTargets() {
|
|
|
|
return Targets.listTargets().length > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static extractTokenData(target) {
|
2023-11-21 16:03:26 +01:00
|
|
|
return { id: target?.id, name: target?.document.name, img: target?.document.texture.src ?? target?.actor.img ?? 'icons/svg/mystery-man.svg' };
|
2022-11-23 21:42:39 +01:00
|
|
|
}
|
2024-10-25 23:28:09 +02:00
|
|
|
static buildActorTokenData(tokenId, actor) {
|
|
|
|
return { id: tokenId, name: actor.name, img: actor.img ?? 'icons/svg/mystery-man.svg' };
|
|
|
|
}
|
2022-11-23 21:42:39 +01:00
|
|
|
static isTargetEntite(target) {
|
|
|
|
return target?.actor.type == 'entite' && target?.actor.system.definition.typeentite == ENTITE_NONINCARNE;
|
|
|
|
}
|
|
|
|
|
2024-10-25 23:28:09 +02:00
|
|
|
static async selectOneTargetToken(onSelectTarget = target => { }) {
|
|
|
|
const targets = Targets.listTargets()
|
2022-11-23 21:42:39 +01:00
|
|
|
switch (targets.length) {
|
2024-10-25 23:28:09 +02:00
|
|
|
case 0:
|
|
|
|
return
|
2022-11-23 21:42:39 +01:00
|
|
|
case 1:
|
2024-10-25 23:28:09 +02:00
|
|
|
onSelectTarget(targets[0])
|
|
|
|
return
|
2022-11-23 21:42:39 +01:00
|
|
|
default:
|
|
|
|
{
|
2023-11-21 16:03:26 +01:00
|
|
|
const selectData = {
|
|
|
|
title: "Choisir une cible",
|
|
|
|
label: "Choisir une seule des cibles",
|
|
|
|
list: targets.map(it => Targets.extractTokenData(it))
|
|
|
|
};
|
2024-10-25 23:28:09 +02:00
|
|
|
DialogSelect.select(selectData, onSelectTarget)
|
2022-11-23 21:42:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|