forked from xdy/twodsix-foundryvtt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change so that damage can be dealt to both linked and unlinked tokens. The change restructures how damage is being dealt and adds a new hook for recalulating the hits when the characteristics change. Fix xdy#232
- Loading branch information
Showing
5 changed files
with
98 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import TwodsixActor from "../entities/TwodsixActor"; | ||
import { mergeDeep } from "../utils/utils"; | ||
|
||
function getCurrentHits(target: Record<string, any>, ...args: Record<string, any>[]) { | ||
const characteristics = mergeDeep(target, ...args); | ||
const hitsCharacteristics = ["strength", "dexterity", "endurance"]; | ||
|
||
return Object.entries(characteristics).reduce((hits, [key, chr]) => { | ||
if (hitsCharacteristics.includes(key)) { | ||
hits.value += chr.value-chr.damage; | ||
hits.max += chr.value; | ||
} | ||
return hits; | ||
}, {value: 0, max: 0}); | ||
} | ||
|
||
Hooks.on('preUpdateActor', async (actor:TwodsixActor, update:Record<string, any>) => { | ||
if (update.data?.characteristics) { | ||
update.data.hits =getCurrentHits(actor.data.data.characteristics, update.data.characteristics); | ||
} | ||
}); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
Hooks.on('preUpdateToken', async (scene, token:Record<string, any>, update:Record<string, any>) => { | ||
if (update.actorData?.data?.characteristics) { | ||
const actor = TwodsixActor.collection.get(token.actorId); | ||
update.actorData.data.hits = getCurrentHits( | ||
actor.data.data.characteristics, | ||
token.actorData.data.characteristics, | ||
update.actorData.data.characteristics | ||
); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// https://stackoverflow.com/a/34749873 | ||
/** | ||
* Simple object check. | ||
* @param item | ||
* @returns {boolean} | ||
*/ | ||
export function isObject(item: unknown):boolean { | ||
return (item && typeof item === 'object' && !Array.isArray(item)); | ||
} | ||
|
||
/** | ||
* Deep merge two objects. | ||
* @param target | ||
* @param ...sources | ||
*/ | ||
export function mergeDeep(target:Record<string, any>, ...sources:Record<string, any>[]):Record<string, any> { | ||
if (!sources.length) { | ||
return target; | ||
} | ||
const source = sources.shift(); | ||
|
||
if (isObject(target) && isObject(source)) { | ||
for (const key in source) { | ||
if (isObject(source[key])) { | ||
if (!target[key]) { | ||
Object.assign(target, { [key]: {} }); | ||
} | ||
mergeDeep(target[key], source[key]); | ||
} else { | ||
Object.assign(target, { [key]: source[key] }); | ||
} | ||
} | ||
} | ||
|
||
return mergeDeep(target, ...sources); | ||
} |