#57 Damage auf tokens #58

Merged
Byroks merged 1 commits from #57-damage-to-token into develop 2024-02-21 22:30:10 +01:00
2 changed files with 42 additions and 48 deletions

View File

@ -92,14 +92,14 @@ Hooks.once("init", async () => {
Handlebars.registerHelper("count", (object: any) => {
var length = 0;
for( var key in object ) {
if( object.hasOwnProperty(key) ) {
for (var key in object) {
if (object.hasOwnProperty(key)) {
++length;
}
}
return length;
});
// Default Sheet für Items definieren und das Standardsheet deaktivieren
Items.unregisterSheet("core", ItemSheet);
Items.registerSheet("midgard5", M5ItemSheet, { makeDefault: true });
@ -124,12 +124,10 @@ Hooks.on("getChatLogEntryContext", function (html, options) {
name: "LP & AP Schaden",
icon: '<i class="fas fa-tint"></i>',
condition: (li) => {
// Only show this context menu if there are re-rollable dice in the message
const damageRolls = li.find(".apply-damage").length;
const damageRolls = li.find(".damage").length;
// All must be true to show the reroll dialogue
// The button doesn't work for the GM right now, so we don't need to show it
return game["user"].character && damageRolls > 0;
return (game["user"].character || game["canvas"].tokens.controlled) && damageRolls > 0;
},
callback: (li) => applyDamage(li, 2),
},
@ -137,12 +135,10 @@ Hooks.on("getChatLogEntryContext", function (html, options) {
name: "AP Schaden",
icon: '<i class="fas fa-shield-alt"></i>',
condition: (li) => {
// Only show this context menu if there are re-rollable dice in the message
const damageRolls = li.find(".apply-damage").length;
const damageRolls = li.find(".damage").length;
// All must be true to show the reroll dialogue
// The button doesn't work for the GM right now, so we don't need to show it
return game["user"].character && damageRolls > 0;
return (game["user"].character || game["canvas"].tokens.controlled) && damageRolls > 0;
},
callback: (li) => applyDamage(li, 1),
},
@ -150,12 +146,10 @@ Hooks.on("getChatLogEntryContext", function (html, options) {
name: "LP & AP Heilen",
icon: '<i class="fas fa-heart"></i>',
condition: (li) => {
// Only show this context menu if there are re-rollable dice in the message
const damageRolls = li.find(".apply-damage").length;
const damageRolls = li.find(".heal").length;
// All must be true to show the reroll dialogue
// The button doesn't work for the GM right now, so we don't need to show it
return game["user"].character && damageRolls > 0;
return (game["user"].character || game["canvas"].tokens.controlled) && damageRolls > 0;
},
callback: (li) => applyDamage(li, -1),
},
@ -163,12 +157,10 @@ Hooks.on("getChatLogEntryContext", function (html, options) {
name: "AP Heilen",
icon: '<i class="far fa-heart"></i>',
condition: (li) => {
// Only show this context menu if there are re-rollable dice in the message
const damageRolls = li.find(".apply-damage").length;
const damageRolls = li.find(".heal").length;
// All must be true to show the reroll dialogue
// The button doesn't work for the GM right now, so we don't need to show it
return game["user"].character && damageRolls > 0;
return (game["user"].character || game["canvas"].tokens.controlled) && damageRolls > 0;
},
callback: (li) => applyDamage(li, -2),
}
@ -219,15 +211,6 @@ Hooks.on("updateCombat", function (combat: Combat, updateData: { round: number;
}
});
function limitHeal(heal: number, current: number, max: number): number {
if (current === max) {
return 0;
} else if (heal + current > max) {
return max - current;
}
return heal;
}
Hooks.on("renderCombatTracker", (combatTracker, html, context) => {
if (context.combat === null) {
html.find("h3.encounter-title")[0].innerHTML = game["i18n"].localize("midgard5.no-encounter");
@ -244,27 +227,29 @@ Hooks.once("ready", () => {
});
async function applyDamage(roll, direction) {
const damageValue = Array.from(roll.find(".apply-damage") as HTMLElement[])
console.log(roll, direction);
const damageValue = Array.from(roll.find(".apply") as HTMLElement[])
.map((x) => Math.max(0, Number(x.innerText)))
.reduce((prev, curr) => prev + curr, 0);
const userId = game["user"].character.id;
const viewedSceneId = game["user"].viewedScene;
const token = game["actors"].get(userId).getDependentTokens(viewedSceneId)[0]?.delta.syntheticActor;
const controlledTokens = game["canvas"].tokens.controlled;
const actor = game["user"].character;
if (token) {
switch (direction) {
case 2:
token["system"].lp.value -= Math.max(0, damageValue - token["system"].calc.stats.lpProtection.value);
case 1:
token["system"].ap.value -= Math.max(0, damageValue - token["system"].calc.stats.apProtection.value);
break;
case -1:
token["system"].lp.value += limitHeal(damageValue, token["system"].lp.value, token["system"].lp.max);
case -2:
token["system"].ap.value += limitHeal(damageValue, token["system"].ap.value, token["system"].ap.max);
}
token.render();
if (controlledTokens) {
controlledTokens.forEach((controlledToken) => {
let token = controlledToken.document.delta.syntheticActor;
switch (direction) {
case 2:
token["system"].lp.value -= Math.max(0, damageValue - token["system"].calc.stats.lpProtection.value);
case 1:
token["system"].ap.value -= Math.max(0, damageValue - token["system"].calc.stats.apProtection.value);
break;
case -1:
token["system"].lp.value += limitHeal(damageValue, token["system"].lp.value, token["system"].lp.max);
case -2:
token["system"].ap.value += limitHeal(damageValue, token["system"].ap.value, token["system"].ap.max);
}
token.render();
});
} else {
switch (direction) {
case 2:
@ -273,10 +258,19 @@ async function applyDamage(roll, direction) {
actor["system"].ap.value -= Math.max(0, damageValue - actor["system"].calc.stats.apProtection.value);
break;
case -1:
actor["system"].lp.value += limitHeal(damageValue, token["system"].lp.value, token["system"].lp.max);
actor["system"].lp.value += limitHeal(damageValue, actor["system"].lp.value, actor["system"].lp.max);
case -2:
actor["system"].ap.value += limitHeal(damageValue, token["system"].ap.value, token["system"].ap.max);
actor["system"].ap.value += limitHeal(damageValue, actor["system"].ap.value, actor["system"].ap.max);
}
actor.render();
}
}
function limitHeal(heal: number, current: number, max: number): number {
if (current === max) {
return 0;
} else if (heal + current > max) {
return max - current;
}
return heal;
}

View File

@ -82,7 +82,7 @@
<tr class="roll-row {{roll.css}}">
<td>{{roll.label}}</td>
<td class="roll-result">
<span class="roll-total {{#if (contains roll.label "Schaden")}} apply-damage{{/if}}">{{roll.totalStr}}</span>
<span class="roll-total {{#if (contains roll.label "Schaden")}} damage{{/if}}{{#if (contains roll.label "Heilung")}} heal{{/if}}">{{roll.totalStr}}</span>
<span class="roll-detail">{{roll.result}}</span>
</td>
</tr>