import LethalFantasyActorSheet from "./base-actor-sheet.mjs" import LethalFantasyRoll from "../../documents/roll.mjs" export default class LethalFantasyCharacterSheet extends LethalFantasyActorSheet { /** @override */ static DEFAULT_OPTIONS = { classes: ["character"], position: { width: 1150, height: 780, }, window: { contentClasses: ["character-content"], }, actions: { createEquipment: LethalFantasyCharacterSheet.#onCreateEquipment, rangedAttackDefense: LethalFantasyCharacterSheet.#onRangedAttackDefense, }, } /** @override */ static PARTS = { main: { template: "systems/fvtt-lethal-fantasy/templates/character-main.hbs", }, tabs: { template: "templates/generic/tab-navigation.hbs", }, skills: { template: "systems/fvtt-lethal-fantasy/templates/character-skills.hbs", }, weapons: { template: "systems/fvtt-lethal-fantasy/templates/character-weapons.hbs", }, spells: { template: "systems/fvtt-lethal-fantasy/templates/character-spells.hbs", }, miracles: { template: "systems/fvtt-lethal-fantasy/templates/character-miracles.hbs", }, biography: { template: "systems/fvtt-lethal-fantasy/templates/character-biography.hbs", }, } /** @override */ tabGroups = { sheet: "skills", } /** * Prepare an array of form header tabs. * @returns {Record>} */ #getTabs() { let tabs = { skills: { id: "skills", group: "sheet", icon: "fa-solid fa-shapes", label: "LETHALFANTASY.Label.skills" }, weapons: { id: "weapons", group: "sheet", icon: "fa-solid fa-swords", label: "LETHALFANTASY.Label.weapons" }, biography: { id: "biography", group: "sheet", icon: "fa-solid fa-book", label: "LETHALFANTASY.Label.biography" }, } if (this.actor.system.biodata.magicUser) { tabs.spells = { id: "spells", group: "sheet", icon: "fa-sharp-duotone fa-solid fa-wand-magic-sparkles", label: "LETHALFANTASY.Label.spells" } } if (this.actor.system.biodata.clericUser) { tabs.miracles = { id: "miracles", group: "sheet", icon: "fa-sharp-duotone fa-solid fa-hands-praying", label: "LETHALFANTASY.Label.miracles" } } for (const v of Object.values(tabs)) { v.active = this.tabGroups[v.group] === v.id v.cssClass = v.active ? "active" : "" } return tabs } /** @override */ async _prepareContext() { const context = await super._prepareContext() context.tabs = this.#getTabs() return context } _generateTooltip(type, target) { } /** @override */ async _preparePartContext(partId, context) { const doc = this.document switch (partId) { case "main": break case "skills": context.tab = context.tabs.skills context.skills = doc.itemTypes.skill context.gifts = doc.itemTypes.gift context.vulnerabilities = doc.itemTypes.vulnerability break case "spells": context.tab = context.tabs.spells context.spells = doc.itemTypes.spell context.hasSpells = context.spells.length > 0 break case "miracles": context.tab = context.tabs.miracles context.miracles = doc.itemTypes.miracle context.hasMiracles = context.miracles.length > 0 break case "weapons": context.tab = context.tabs.weapons context.weapons = doc.itemTypes.weapon context.armors = doc.itemTypes.armor context.equipments = doc.itemTypes.equipment break case "biography": context.tab = context.tabs.biography context.enrichedDescription = await TextEditor.enrichHTML(doc.system.description, { async: true }) context.enrichedNotes = await TextEditor.enrichHTML(doc.system.notes, { async: true }) break } return context } // #region Drag-and-Drop Workflow /** * Callback actions which occur when a dragged element is dropped on a target. * @param {DragEvent} event The originating DragEvent * @protected */ async _onDrop(event) { if (!this.isEditable || !this.isEditMode) return const data = TextEditor.getDragEventData(event) // Handle different data types switch (data.type) { case "Item": const item = await fromUuid(data.uuid) return this._onDropItem(item) } } async #onDropPathItem(item) { await this.document.addPath(item) } static async #onRangedAttackDefense(event, target) { const hasTarget = false let roll = await LethalFantasyRoll.promptRangedDefense({ actorId: this.actor.id, actorName: this.actor.name, actorImage: this.actor.img }) if (!roll) return null await roll.toMessage({}, { rollMode: roll.options.rollMode }) } static #onCreateEquipment(event, target) { } getBestWeaponClassSkill(skills, rollType, multiplier = 1.0) { let maxValue = 0 let goodSkill = skills[0] for (let s of skills) { if (rollType === "weapon-attack") { if (s.system.weaponBonus.attack > maxValue) { maxValue = Number(s.system.weaponBonus.attack) goodSkill = s } } if (rollType === "weapon-defense") { if (s.system.weaponBonus.defense > maxValue) { maxValue = Number(s.system.weaponBonus.defense) goodSkill = s } } if (rollType.includes("weapon-damage")) { if (s.system.weaponBonus.damage > maxValue) { maxValue = Number(s.system.weaponBonus.damage) goodSkill = s } } } goodSkill.weaponSkillModifier = maxValue * multiplier return goodSkill } /** * Handles the roll action triggered by user interaction. * * @param {PointerEvent} event The event object representing the user interaction. * @param {HTMLElement} target The target element that triggered the roll. * * @returns {Promise} A promise that resolves when the roll action is complete. * * @throws {Error} Throws an error if the roll type is not recognized. * * @description This method checks the current mode (edit or not) and determines the type of roll * (save, resource, or damage) based on the target element's data attributes. It retrieves the * corresponding value from the document's system and performs the roll. */ async _onRoll(event, target) { if (this.isEditMode) return const rollType = event.target.dataset.rollType let rollTarget let rollKey = event.target.dataset.rollKey switch (rollType) { case "challenge": rollTarget = foundry.utils.duplicate(this.document.system.challenges[rollKey]) rollTarget.rollKey = rollKey break case "save": rollTarget = foundry.utils.duplicate(this.document.system.saves[rollKey]) rollTarget.rollKey = rollKey rollTarget.rollDice = event.target.dataset?.rollDice break case "spell": rollTarget = this.actor.items.find((i) => i.type === "spell" && i.id === rollKey) rollTarget.rollKey = rollKey break case "miracle": rollTarget = this.actor.items.find((i) => i.type === "miracle" && i.id === rollKey) rollTarget.rollKey = rollKey break case "skill": rollTarget = this.actor.items.find((i) => i.type === "skill" && i.id === rollKey) rollTarget.rollKey = rollKey if (rollTarget.system.category === "weapon") { ui.notifications.warn(game.i18n.localize("LETHALFANTASY.Notifications.rollFromWeapon")) return } break case "weapon-damage-small": case "weapon-damage-medium": case "weapon-attack": case "weapon-defense": let weapon = this.actor.items.find((i) => i.type === "weapon" && i.id === rollKey) let skill let skills = this.actor.items.filter((i) => i.type === "skill" && i.name.toLowerCase() === weapon.name.toLowerCase()) if (skills.length > 0) { skill = this.getBestWeaponClassSkill(skills, rollType, 1.0) } else { skills = this.actor.items.filter((i) => i.type === "skill" && i.name.toLowerCase().replace(" skill", "") === weapon.name.toLowerCase()) if (skills.length > 0) { skill = this.getBestWeaponClassSkill(skills, rollType, 1.0) } else { skills = this.actor.items.filter((i) => i.type === "skill" && i.system.weaponClass === weapon.system.weaponClass) if (skills.length > 0) { skill = this.getBestWeaponClassSkill(skills, rollType, 0.5) } else { skills = this.actor.items.filter((i) => i.type === "skill" && i.system.weaponClass.includes(SYSTEM.WEAPON_CATEGORIES[weapon.system.weaponClass])) if (skills.length > 0) { skill = this.getBestWeaponClassSkill(skills, rollType, 0.25) } else { ui.notifications.warn(game.i18n.localize("LETHALFANTASY.Notifications.skillNotFound")) return } } } } if (!weapon || !skill) { console.error("Weapon or skill not found", weapon, skill) ui.notifications.warn(game.i18n.localize("LETHALFANTASY.Notifications.skillNotFound")) return } rollTarget = skill rollTarget.weapon = weapon rollTarget.weaponSkillModifier = skill.weaponSkillModifier rollTarget.rollKey = rollKey rollTarget.combat = foundry.utils.duplicate(this.actor.system.combat) break default: ui.notifications.error(game.i18n.localize("LETHALFANTASY.Notifications.rollTypeNotFound") + String(rollType)) break } // In all cases rollTarget.magicUser = this.actor.system.biodata.magicUser rollTarget.actorModifiers = foundry.utils.duplicate(this.actor.system.modifiers) await this.document.system.roll(rollType, rollTarget) } // #endregion }