Add rest dice tracking for the Sync actors
This commit is contained in:
parent
4544516c5c
commit
1002b1387c
8 changed files with 186 additions and 24 deletions
60
module/documents/Actor/Sync.mjs
Normal file
60
module/documents/Actor/Sync.mjs
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
import { syncMilestones, syncDice } from "../../config.mjs";
|
||||
|
||||
/** @this {Actor} */
|
||||
async function useRestDie() {
|
||||
let addToSync = await (new Roll(syncDice)).evaluate();
|
||||
await addToSync.toMessage({
|
||||
speaker: ChatMessage.getSpeaker({ actor: this.actor }),
|
||||
flavor: `Sync Restoration`,
|
||||
});
|
||||
this.update({
|
||||
"system.rest_dice": this.system.rest_dice - 1,
|
||||
"system.value": this.system.value + addToSync.total,
|
||||
});
|
||||
};
|
||||
|
||||
/** @this {Actor} */
|
||||
async function _preUpdate(data, options) {
|
||||
if (options.diff) {
|
||||
if (data.system?.value != null) {
|
||||
let currentSync = this.system.value;
|
||||
let newSync = data.system.value;
|
||||
|
||||
let minSync = Math.min(currentSync, newSync);
|
||||
let maxSync = Math.max(currentSync, newSync);
|
||||
let milestones = syncMilestones.filter(
|
||||
m => minSync < m.value && m.value <= maxSync
|
||||
);
|
||||
|
||||
if (milestones.length > 0) data.system.rest_dice ??= this.system.rest_dice;
|
||||
|
||||
for (const milestone of milestones) {
|
||||
// Damage
|
||||
if (newSync < currentSync) {
|
||||
if (!this.system.milestones_hit.has(milestone.value)) {
|
||||
data.system.rest_dice += 1;
|
||||
this.system.milestones_hit.add(milestone.value);
|
||||
};
|
||||
}
|
||||
|
||||
// Healing
|
||||
else if (newSync > currentSync) {
|
||||
if (
|
||||
this.system.milestones_hit.has(milestone.value)
|
||||
&& milestone.andReturn
|
||||
&& milestone.value <= newSync
|
||||
) {
|
||||
this.system.milestones_hit.delete(milestone.value);
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
data.system.milestones_hit = [ ...this.system.milestones_hit ];
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
export default {
|
||||
_preUpdate,
|
||||
useRestDie,
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue