rework ActiveEffects

This commit is contained in:
Vincent Vandemeulebrouck
2021-06-29 00:56:25 +02:00
parent 3edc740d8c
commit e74f6b14d3
12 changed files with 110 additions and 154 deletions

View File

@ -32,7 +32,6 @@ import { DialogFabriquerPotion } from "./dialog-fabriquer-potion.js";
import { RollDataAjustements } from "./rolldata-ajustements.js";
import { DialogItemAchat } from "./dialog-item-achat.js";
import { RdDItem } from "./item.js";
/* -------------------------------------------- */
/**
* Extend the base Actor entity by defining a custom roll data structure which is ideal for the Simple system.
@ -42,7 +41,6 @@ export class RdDActor extends Actor {
/* -------------------------------------------- */
static init() {
Hooks.on("deleteActiveEffect", (effect, options, userId) => RdDActor.getParentActor(effect)?.onDeleteActiveEffect(effect, options));
Hooks.on("createActiveEffect", (effect, options, userId) => RdDActor.getParentActor(effect)?.onCreateActiveEffect(effect, options));
Hooks.on("preUpdateItem", (item, change, options, id) => RdDActor.getParentActor(item)?.onPreUpdateItem(item, change, options, id));
Hooks.on("createItem", (item, options, id) => RdDActor.getParentActor(item)?.onCreateItem(item, options, id));
@ -424,7 +422,7 @@ export class RdDActor extends Actor {
/* -------------------------------------------- */
getSurprise(isCombat = undefined) {
let niveauSurprise = Array.from(this.effects?.values() ?? [])
let niveauSurprise = this.getActiveEffects()
.map(effect => StatusEffects.valeurSurprise(effect.data, isCombat))
.reduce(Misc.sum(), 0);
if (niveauSurprise > 1) {
@ -1507,13 +1505,12 @@ export class RdDActor extends Actor {
/* -------------------------------------------- */
getSonne() {
let data = Misc.templateData(this);
return !this.isEntiteCauchemar() && (data.sante?.sonne?.value ?? false);
return this.getEffectByLabel("EFFECT.StatusStunned");
}
/* -------------------------------------------- */
getSonneRound() {
return !this.isEntiteCauchemar() && (Misc.templateData(this).sante.sonne?.round ?? false);
return Misc.templateData(this).sante.sonne?.round ?? -1;
}
/* -------------------------------------------- */
@ -1528,23 +1525,12 @@ export class RdDActor extends Actor {
/* -------------------------------------------- */
async setSonne(sonne = true) {
if (this.isEntiteCauchemar()) {
if (!game.combat || this.isEntiteCauchemar()) {
return;
}
let round = (sonne && game.combat) ? game.combat.current.round : -1; // Sauvegarde du round de sonné en cas de combat
await this.setStatusSonne(sonne);
await this.setStateSonne(sonne, round);
}
/* -------------------------------------------- */
async setStateSonne(sonne, round = -1) {
if (this.isEntiteCauchemar()) {
return;
}
let sonneData = duplicate(Misc.templateData(this).sante.sonne);
sonneData.value = sonne;
sonneData.round = round;
await this.update({ "data.sante.sonne": sonneData });
await this.setStatusEffect("EFFECT.StatusStunned", sonne, {
duration: (sonne && game.combat) ? { rounds: 2 } : undefined
});
}
/* -------------------------------------------- */
@ -1695,7 +1681,7 @@ export class RdDActor extends Actor {
}
await this.update({ "data.sante": sante });
if (this.isDead()) {
await this.addStatusEffectById('dead');
await this.setStatusEffect("EFFECT.StatusComma", true);
}
return result;
}
@ -3078,7 +3064,7 @@ export class RdDActor extends Actor {
ui.notifications.warn("Vous êtes déja dans les TMR....");
return
}
let demiReve = this.listeEffets(it => it.label == "Demi-rêve");
let demiReve = this.getActiveEffects(it => it.data.label == "Demi-rêve");
if (mode != 'visu' && demiReve.length > 0) {
ui.notifications.warn("Le joueur ou le MJ est déja dans les Terres Médianes avec ce personnage ! Visualisation uniquement");
mode = "visu"; // bascule le mode en visu automatiquement
@ -3094,7 +3080,7 @@ export class RdDActor extends Actor {
});
return;
}
await this.setStatusDemiReve(true);
await this.setStatusEffect("EFFECT.StatusDemiReve", true);
}
const actorData = Misc.data(this);
@ -3351,7 +3337,7 @@ export class RdDActor extends Actor {
count--;
} else {
// TODO: status effect dead
this.addStatusEffectById('dead');
this.setStatusEffect("EFFECT.StatusComma", true);
ChatMessage.create({
content: `<img class="chat-icon" src="icons/svg/skull.svg" alt="charge" />
<strong>${this.name} vient de succomber à une seconde blessure critique ! Que les Dragons gardent son Archétype en paix !</strong>`
@ -3990,109 +3976,64 @@ export class RdDActor extends Actor {
async onUpdateActor(update, options, actorId) {
const updatedEndurance = update?.data?.sante?.endurance;
if (updatedEndurance && options.diff) {
this.forceStatusEffectId('unconscious', updatedEndurance.value == 0);
}
}
/* -------------------------------------------- */
async onCreateActiveEffect(effect, options) {
switch (StatusEffects.statusId(effect)) {
case 'sonne':
await this.setStateSonne(true);
return;
await this.setStatusEffect("EFFECT.StatusUnconscious", updatedEndurance.value == 0);
}
}
/* -------------------------------------------- */
async onDeleteActiveEffect(effect, options) {
switch (StatusEffects.statusId(effect)) {
case 'sonne':
await this.setStateSonne(false);
switch (effect.label) {
case 'EFFECT.StatusStunned':
return;
}
}
/* -------------------------------------------- */
enleverTousLesEffets() {
const ids = Array.from(this.effects?.keys() ?? []);
this.deleteEmbeddedDocuments('ActiveEffect', ids);
getActiveEffects(matching = it => true) {
return Array.from(this.getEmbeddedCollection("ActiveEffect").values()).filter(it => matching(it));
}
/* -------------------------------------------- */
listeEffets(matching = it => true) {
const all = Array.from(this.effects?.values() ?? []);
const filtered = all.filter(it => matching(it.data));
return filtered;
getEffectByLabel(label) {
return this.getActiveEffects().find(it => it.data.label == label);
}
/* -------------------------------------------- */
async setStatusDemiReve(status) {
const demiReve = StatusEffects.demiReve();
if (status) {
await this.addStatusEffect(demiReve)
} else {
await this.deleteStatusEffect(demiReve)
}
getEffectById(id) {
return this.getActiveEffects().find(it => it.id == id);
}
/* -------------------------------------------- */
async setStatusSonne(sonne) {
if (this.isEntiteCauchemar()) {
async setStatusEffect(label, status, updates = {}) {
if (this.isEntiteCauchemar() || this.data.type == 'vehicule') {
return;
}
await this.forceStatusEffectId('sonne', sonne);
}
/* -------------------------------------------- */
async forceStatusEffectId(statusId, isSet) {
if (isSet) {
await this.addStatusEffectById(statusId);
console.log("setStatusEffect", label, status, updates)
const existing = this.getEffectByLabel(label);
if (existing) {
existing.delete();
}
else {
await this.deleteStatusEffectById(statusId);
if (status) {
const statusEffect = mergeObject(duplicate(StatusEffects.status(label)), updates);
await this.createEmbeddedDocuments("ActiveEffect", [statusEffect]);
}
}
/* -------------------------------------------- */
async deleteStatusEffectById(id) {
const ids = Array.from(this.effects?.values())
.filter(it => it.data.flags.core?.statusId == id)
.map(it => it.id);
console.log("Delete effect IDS1: ", this.effects, ids);
if (ids.length > 0) {
await this.deleteEmbeddedDocuments('ActiveEffect', ids);
enleverActiveEffectById(id) {
if (game.user.isGM){
const existing = this.getEffectById(id);
if (existing) {
existing.delete();
}
}
}
/* -------------------------------------------- */
async deleteStatusEffect(effect) {
const ids = Array.from(this.effects?.values())
.filter(it => StatusEffects.statusId(it.data) == StatusEffects.statusId(effect))
.map(it => it.id);
console.log("Delete effect 1: ", this.effects, ids);
if (ids.length > 0) {
await this.deleteEmbeddedDocuments('ActiveEffect', ids);
enleverTousLesEffets() {
if (game.user.isGM){
this.deleteEmbeddedDocuments('ActiveEffect', this.getActiveEffects().map(it => it.id));
}
}
/* -------------------------------------------- */
async addStatusEffectById(id) {
const statusEffect = CONFIG.statusEffects.find(it => it.id == id);
await this.addStatusEffect(statusEffect);
}
/* -------------------------------------------- */
async addStatusEffect(statusEffect) {
const effet = Misc.data(statusEffect);
await this.deleteStatusEffectById(effet.id);
effet.flags = effet.flags ?? { core: {} };
effet.flags.core.statusId = effet.id;
let effectArray = await this.createEmbeddedDocuments('ActiveEffect', [effet]);
//if (effectArray[0]) {
//await effectArray[0].setFlag('core', 'statusId', effet.id);
//}
}
/* -------------------------------------------- */
async onPreUpdateItem(item, change, options, id) {
const itemData = Misc.data(item);