ds4/src/module/actor/actor.ts

117 lines
4.7 KiB
TypeScript
Raw Normal View History

import { ModifiableData } from "../common/common-data";
2020-12-29 00:33:43 +01:00
import { DS4Item } from "../item/item";
2021-01-26 03:55:18 +01:00
import { DS4Armor, DS4Shield, ItemType } from "../item/item-data";
import { DS4ActorData } from "./actor-data";
2020-12-23 18:23:26 +01:00
2021-01-31 19:57:14 +01:00
export class DS4Actor extends Actor<DS4ActorData, DS4Item> {
2020-12-23 16:52:20 +01:00
/** @override */
2020-12-23 18:23:26 +01:00
prepareDerivedData(): void {
2020-12-23 16:52:20 +01:00
const data = this.data;
const attributes = data.data.attributes;
Object.values(attributes).forEach(
(attribute: ModifiableData<number>) => (attribute.total = attribute.base + attribute.mod),
2020-12-23 16:52:20 +01:00
);
const traits = data.data.traits;
Object.values(traits).forEach((trait: ModifiableData<number>) => (trait.total = trait.base + trait.mod));
2020-12-23 16:52:20 +01:00
this._prepareCombatValues();
2020-12-23 16:52:20 +01:00
}
/**
* The list of item types that can be owned by this actor.
*/
get ownableItemTypes(): Array<ItemType> {
switch (this.data.type) {
case "character":
return [
"weapon",
"armor",
"shield",
"trinket",
"equipment",
"spell",
"talent",
"racialAbility",
"language",
"alphabet",
];
case "creature":
return ["weapon", "armor", "shield", "trinket", "equipment", "spell", "specialCreatureAbility"];
default:
2021-01-18 19:03:08 +01:00
return [];
}
}
/**
* Checks whether or not the given item type can be owned by the actor.
* @param itemType the item type to check
*/
canOwnItemType(itemType: ItemType): boolean {
return this.ownableItemTypes.includes(itemType);
}
/**
* Prepares the combat values of the actor.
*/
private _prepareCombatValues(): void {
const data = this.data.data;
const armorValueOfEquippedItems = this._calculateArmorValueOfEquippedItems();
data.combatValues.hitPoints.base =
(data.attributes.body.total ?? 0) + (data.traits.constitution.total ?? 0) + 10;
data.combatValues.defense.base =
(data.attributes.body.total ?? 0) + (data.traits.constitution.total ?? 0) + armorValueOfEquippedItems;
data.combatValues.initiative.base = (data.attributes.mobility.total ?? 0) + (data.traits.agility.total ?? 0);
data.combatValues.movement.base = (data.attributes.mobility.total ?? 0) / 2 + 1;
data.combatValues.meleeAttack.base = (data.attributes.body.total ?? 0) + (data.traits.strength.total ?? 0);
data.combatValues.rangedAttack.base =
(data.attributes.mobility.total ?? 0) + (data.traits.dexterity.total ?? 0);
data.combatValues.spellcasting.base =
(data.attributes.mind.total ?? 0) + (data.traits.aura.total ?? 0) - armorValueOfEquippedItems;
data.combatValues.targetedSpellcasting.base =
(data.attributes.mind.total ?? 0) + (data.traits.dexterity.total ?? 0) - armorValueOfEquippedItems;
Object.values(data.combatValues).forEach(
(combatValue: ModifiableData<number>) => (combatValue.total = combatValue.base + combatValue.mod),
);
data.combatValues.hitPoints.max = data.combatValues.hitPoints.total;
}
/**
* Calculates the total armor value of all equipped items.
*/
private _calculateArmorValueOfEquippedItems(): number {
return this.items
.filter((item) => ["armor", "shield"].includes(item.type))
.map((item) => item.data.data as DS4Armor | DS4Shield)
.filter((itemData) => itemData.equipped)
.map((itemData) => itemData.armorValue)
.reduce((a, b) => a + b, 0);
}
/**
* Handle how changes to a Token attribute bar are applied to the Actor.
* This only differs from the base implementation by also allowing negative values.
* @override
*/
2021-01-26 03:55:18 +01:00
async modifyTokenAttribute(attribute: string, value: number, isDelta = false, isBar = true): Promise<this> {
const current = getProperty(this.data.data, attribute);
// Determine the updates to make to the actor data
let updates: Record<string, number>;
if (isBar) {
if (isDelta) value = Math.min(Number(current.value) + value, current.max);
updates = { [`data.${attribute}.value`]: value };
} else {
if (isDelta) value = Number(current) + value;
updates = { [`data.${attribute}`]: value };
}
// Call a hook to handle token resource bar updates
const allowed = Hooks.call("modifyTokenAttribute", { attribute, value, isDelta, isBar }, updates);
return allowed !== false ? this.update(updates) : this;
}
2020-12-23 16:52:20 +01:00
}