Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add combat event hooks #222

Merged
merged 6 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion module/checks/accuracy-check.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CHECK_ROLL } from './default-section-order.mjs';
import { SYSTEM } from '../helpers/config.mjs';
import { Flags } from '../helpers/flags.mjs';
import { CommonSections } from './common-sections.mjs';
import { CommonEvents } from './common-events.mjs';
import { CheckConfiguration } from './check-configuration.mjs';

function handleGenericBonus(actor, modifiers) {
Expand Down Expand Up @@ -38,6 +39,11 @@ const onProcessCheck = (check, actor, item) => {
if (type === 'accuracy') {
const configurer = CheckConfiguration.configure(check);
configurer.modifyTargetedDefense((value) => value ?? 'def');
if (critical) {
configurer.addTraits('critical');
} else if (fumble) {
configurer.addTraits('fumble');
}
configurer.modifyTargets((targets) => {
if (targets?.length) {
const targetedDefense = CheckConfiguration.inspect(check).getTargetedDefense();
Expand Down Expand Up @@ -80,7 +86,6 @@ const onProcessCheck = (check, actor, item) => {
function onRenderCheck(data, checkResult, actor, item, flags) {
if (checkResult.type === 'accuracy') {
const inspector = CheckConfiguration.inspect(checkResult);

const accuracyData = inspector.getAccuracyData();
const damageData = inspector.getDamageData();

Expand All @@ -97,6 +102,7 @@ function onRenderCheck(data, checkResult, actor, item, flags) {
/** @type TargetData[] */
const targets = inspector.getTargets();
CommonSections.damage(data, actor, item, targets, flags, accuracyData, damageData);
CommonEvents.attack(inspector, actor, item);

(flags[SYSTEM] ??= {})[Flags.ChatMessage.Item] ??= item.toObject();
}
Expand Down
58 changes: 58 additions & 0 deletions module/checks/check-configuration.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const TARGETS = 'targets';
const TARGETED_DEFENSE = 'targetedDefense';
const DIFFICULTY = 'difficulty';
const DAMAGE = 'damage';
const TRAITS = 'traits';

/**
*
Expand Down Expand Up @@ -68,6 +69,9 @@ const configure = (check) => {
};

class CheckConfigurer {
/**
* @type {CheckV2, CheckResultV2}
*/
#check;

constructor(check) {
Expand Down Expand Up @@ -103,6 +107,18 @@ class CheckConfigurer {
return this.#check.additionalData[DAMAGE];
}

/**
* @param {String[]|String} traits
* @returns {CheckConfigurer}
*/
addTraits(...traits) {
if (!this.#check.additionalData[TRAITS]) {
this.#check.additionalData[TRAITS] = [];
}
traits.forEach((t) => this.#check.additionalData[TRAITS].push(t.toLowerCase()));
return this;
}

/**
* @param {FUItem} item
* @param {FUActor} actor
Expand All @@ -112,6 +128,22 @@ class CheckConfigurer {
return this.addModelAccuracyBonuses(item.system, actor);
}

/**
* @description Add the common traits of a weapon
* @param {WeaponDataModel} system
*/
addWeaponTraits(system) {
return this.addTraits(system.category.value, system.type.value, system.hands.value);
}

/**
* @description Add the common traits of an NPC attack
* @param {BasicItemDataModel} system
*/
addAttackTraits(system) {
return this.addTraits(system.type.value, system.damageType.value);
}

/**
* @param {DataModel} model
* @param {FUActor} actor
Expand Down Expand Up @@ -367,8 +399,12 @@ const inspect = (check) => {

/**
* @description Given a {@link CheckResultV2} object, provides additional information from it
* @remarks Provides read-only access, to be used after {@linkcode CheckConfigurer}
*/
class CheckInspector {
/**
* @type CheckResultV2
*/
#check;

constructor(check) {
Expand Down Expand Up @@ -410,6 +446,13 @@ class CheckInspector {
return this.#check.additionalData[DIFFICULTY] ?? null;
}

/**
* @return {String[]|null}
*/
getTraits() {
return this.#check.additionalData[TRAITS] ?? null;
}

/**
* @return {TargetData[]|null}
*/
Expand All @@ -424,6 +467,20 @@ class CheckInspector {
return this.getTargets() || [];
}

/**
* @returns {Boolean}
*/
isCritical() {
return this.getCheck().critical;
}

/**
* @returns {Boolean}
*/
isFumble() {
return this.getCheck().fumble;
}

/**
* @remarks Used for templating
*/
Expand Down Expand Up @@ -475,6 +532,7 @@ class CheckInspector {
total: damage.total,
type: damage.type,
extra: damage.extra,
traits: this.getTraits(),
},
translation: {
damageTypes: FU.damageTypes,
Expand Down
Loading