473 lines
14 KiB
TypeScript
473 lines
14 KiB
TypeScript
import Logger from "../../utils/Logger";
|
|
import { M5Character } from "../actors/M5Character";
|
|
import { M5Item } from "../items/M5Item";
|
|
import { M5ItemType, M5SkillLearned, M5SkillType, M5SkillUnlearned } from "../M5Base";
|
|
import { M5Roll } from "../rolls/M5Roll";
|
|
|
|
export default class M5CharacterSheet extends ActorSheet {
|
|
static get defaultOptions() {
|
|
return mergeObject(super.defaultOptions, {
|
|
template: "systems/midgard5/templates/sheets/character/main.hbs",
|
|
width: 1000,
|
|
height: 800,
|
|
classes: ["midgard5", "sheet", "character"],
|
|
tabs: [
|
|
{
|
|
navSelector: ".sheet-navigation",
|
|
contentSelector: ".sheet-content",
|
|
initial: "base_values",
|
|
},
|
|
],
|
|
});
|
|
}
|
|
|
|
// get template() {
|
|
// return "systems/midgard5/templates/character_sheet/main.hbs"
|
|
// }Options extends ActorSheet.Options = ActorSheet.Options, Data extends object = ActorSheet.Data<Options>
|
|
|
|
override getData(options?: Partial<ActorSheet.Options>): ActorSheet.Data<ActorSheet.Options> | Promise<ActorSheet.Data<ActorSheet.Options>> {
|
|
const actor = this.actor as M5Character;
|
|
//console.log("Sheet getData", (actor as any).data)
|
|
return Promise.resolve(super.getData(options)).then((context) => {
|
|
actor.prepareDerivedData();
|
|
|
|
context.actor = (actor as any).toObject(false);
|
|
context.data = (actor as any).system;
|
|
|
|
//console.log("Sheet Promise", context.actor, context.data)
|
|
return context;
|
|
});
|
|
}
|
|
|
|
override setPosition(options = {}) {
|
|
const position = super.setPosition(options);
|
|
const fillerWidth = this.element.find(".attributes .filler:first").width();
|
|
this.element.find(".attributes .attribute-filler-fixed").width(fillerWidth);
|
|
return position;
|
|
}
|
|
|
|
override activateListeners(html: JQuery) {
|
|
super.activateListeners(html);
|
|
|
|
html.find(".update-lp").on("click", async (event) => {
|
|
const valueStr = event.target.dataset["value"];
|
|
const value = parseInt(valueStr);
|
|
this.actor.update({
|
|
data: {
|
|
lp: {
|
|
value: value + 1,
|
|
},
|
|
},
|
|
});
|
|
});
|
|
|
|
html.find(".update-ap").on("click", async (event) => {
|
|
const valueStr = event.target.dataset["value"];
|
|
const value = parseInt(valueStr);
|
|
this.actor.update({
|
|
data: {
|
|
ap: {
|
|
value: value + 1,
|
|
},
|
|
},
|
|
});
|
|
});
|
|
|
|
html.find(".roll-attribute-button").on("click", async (event) => {
|
|
let target = event.target.closest("[data-attribute]") as HTMLElement;
|
|
let attributeValue = target ? parseInt(target.dataset.value) : null;
|
|
let attributeStr = target ? target.dataset.attribute : null;
|
|
|
|
const roll = M5Roll.fromAttributeValue(this.actor, attributeStr, attributeValue);
|
|
await roll.toMessage();
|
|
});
|
|
|
|
html.find(".edit-item").on("click", async (event) => {
|
|
let target = event.target.closest("[data-item-id]") as HTMLElement;
|
|
let itemId = target ? target.dataset.itemId : null;
|
|
|
|
const context = this.actor as any;
|
|
const item = context.items.get(itemId);
|
|
console.log("edit-item", item);
|
|
item.sheet.render(true);
|
|
});
|
|
|
|
html.find(".quantity-increase").on("click", async (event) => {
|
|
let target = event.target.closest("[data-item-id]") as HTMLElement;
|
|
let itemId = target ? target.dataset.itemId : null;
|
|
|
|
const context = this.actor as any;
|
|
const item = context.items.get(itemId);
|
|
if (!item.system.quantity) {
|
|
item.system.quantity = 0;
|
|
}
|
|
item.update({
|
|
data: {
|
|
quantity: item.system.quantity + 1,
|
|
},
|
|
});
|
|
this.render();
|
|
});
|
|
|
|
html.find(".quantity-decrease").on("click", async (event) => {
|
|
let target = event.target.closest("[data-item-id]") as HTMLElement;
|
|
let itemId = target ? target.dataset.itemId : null;
|
|
|
|
const context = this.actor as any;
|
|
const item = context.items.get(itemId);
|
|
if (item.system.quantity > 0) {
|
|
item.update({
|
|
data: {
|
|
quantity: item.system.quantity - 1,
|
|
},
|
|
});
|
|
}
|
|
this.render();
|
|
});
|
|
|
|
html.find(".roll-consumable-item").on("click", async (event) => {
|
|
let target = event.target.closest("[data-item-id]") as HTMLElement;
|
|
let itemId = target ? target.dataset.itemId : null;
|
|
|
|
const context = this.actor as any;
|
|
const item = context.items.get(itemId);
|
|
if (item.system.quantity > 0) {
|
|
item.update({
|
|
data: {
|
|
quantity: item.system.quantity - 1,
|
|
},
|
|
});
|
|
}
|
|
|
|
await item.roll();
|
|
this.render();
|
|
});
|
|
|
|
html.find(".item-delete").on("click", async (event) => {
|
|
let target = event.target.closest("[data-item-id]") as HTMLElement;
|
|
let itemId = target ? target.dataset.itemId : null;
|
|
|
|
const context = this.actor as any;
|
|
const item = context.items.get(itemId);
|
|
item.delete();
|
|
this.render(false);
|
|
});
|
|
|
|
html.find(".roll-learned-button").on("click", async (event) => {
|
|
let target = event.target.closest("[data-item-id]") as HTMLElement;
|
|
let skillId = target ? target.dataset.itemId : null;
|
|
|
|
const actor = this.actor as any;
|
|
const item = actor.items.get(skillId) as M5Item;
|
|
await item.roll();
|
|
});
|
|
|
|
html.find(".roll-general-button").on("click", async (event) => {
|
|
let target = event.target.closest("[data-skill]") as HTMLElement;
|
|
let skillName = target ? target.dataset.skill : null;
|
|
|
|
const data = this.actor.system;
|
|
const unlearnedSkill = data.skills.general[skillName] as M5SkillUnlearned;
|
|
|
|
const roll = M5Roll.fromUnlearnedSkill(this.actor, unlearnedSkill, skillName);
|
|
await roll.toMessage();
|
|
});
|
|
|
|
html.find(".learn-button").on("click", async (event) => {
|
|
let target = event.target.closest("[data-skill]") as HTMLElement;
|
|
let skillName = target ? target.dataset.skill : null;
|
|
|
|
const data = this.actor.system;
|
|
const unlearnedSkill = data.skills.general[skillName] as M5SkillUnlearned;
|
|
|
|
const character = this.actor as M5Character;
|
|
character.createSkill((game as Game).i18n.localize("midgard5." + skillName)).then((skill) => {
|
|
const item = skill as any;
|
|
item.update({
|
|
data: {
|
|
fw: unlearnedSkill.initial,
|
|
attribute: unlearnedSkill.attribute,
|
|
skill: skillName,
|
|
type: "general",
|
|
},
|
|
});
|
|
});
|
|
});
|
|
|
|
html.find(".roll-weapon-button").on("click", async (event) => {
|
|
let target = event.target.closest("[data-item-id]") as HTMLElement;
|
|
let itemId = target ? target.dataset.itemId : null;
|
|
|
|
const context = this.actor as any;
|
|
const item = context.items.get(itemId) as M5Item;
|
|
await item.roll();
|
|
this.render();
|
|
});
|
|
|
|
html.find(".roll-brawl-button").on("click", async (event) => {
|
|
const roll = M5Roll.brawl(this.actor);
|
|
await roll.toMessage();
|
|
});
|
|
|
|
html.find(".roll-perception-button").on("click", async (event) => {
|
|
const roll = M5Roll.perception(this.actor);
|
|
await roll.toMessage();
|
|
});
|
|
|
|
html.find(".roll-drinking-button").on("click", async (event) => {
|
|
const roll = M5Roll.drinking(this.actor);
|
|
await roll.toMessage();
|
|
});
|
|
|
|
html.find(".roll-defense-button").on("click", async (event) => {
|
|
const roll = M5Roll.defense(this.actor);
|
|
await roll.toMessage();
|
|
});
|
|
|
|
html.find(".roll-resistanceMind-button").on("click", async (event) => {
|
|
const roll = M5Roll.resistanceMind(this.actor);
|
|
await roll.toMessage();
|
|
});
|
|
|
|
html.find(".roll-resistanceBody-button").on("click", async (event) => {
|
|
const roll = M5Roll.resistanceBody(this.actor);
|
|
await roll.toMessage();
|
|
});
|
|
|
|
html.find(".change-equipped").on("click", async (event) => {
|
|
let target = event.target.closest("[data-item-id]") as HTMLElement;
|
|
let itemId = target ? target.dataset.itemId : null;
|
|
|
|
const context = this.actor as any;
|
|
const item = context.items.get(itemId);
|
|
item.update({
|
|
data: {
|
|
equipped: !item.system.equipped,
|
|
},
|
|
});
|
|
|
|
this.render();
|
|
});
|
|
|
|
html.find(".add-item").on("click", async (event) => {
|
|
const data = this.actor.system;
|
|
|
|
const character = this.actor as M5Character;
|
|
character.createItem((game as Game).i18n.localize("TYPES.Item.item"), M5ItemType.ITEM, { quantity: 1 });
|
|
});
|
|
|
|
html.find(".add-weapon").on("click", async (event) => {
|
|
const data = this.actor.system;
|
|
|
|
const character = this.actor as M5Character;
|
|
character.createItem((game as Game).i18n.localize("TYPES.Item.weapon"), M5ItemType.WEAPON);
|
|
});
|
|
|
|
html.find(".add-defensiveWeapon").on("click", async (event) => {
|
|
const data = this.actor.system;
|
|
|
|
const character = this.actor as M5Character;
|
|
character.createItem((game as Game).i18n.localize("TYPES.Item.defensiveWeapon"), M5ItemType.DEFENSIVE_WEAPON);
|
|
});
|
|
|
|
html.find(".add-armor").on("click", async (event) => {
|
|
const data = this.actor.system;
|
|
|
|
const character = this.actor as M5Character;
|
|
character.createItem((game as Game).i18n.localize("TYPES.Item.armor"), M5ItemType.ARMOR);
|
|
});
|
|
|
|
html.find(".add-container").on("click", async (event) => {
|
|
const data = this.actor.system;
|
|
|
|
const character = this.actor as M5Character;
|
|
character.createItem((game as Game).i18n.localize("TYPES.Item.container"), M5ItemType.CONTAINER, { quantity: 1 });
|
|
});
|
|
|
|
html.find(".add-innate-skill").on("click", async (event) => {
|
|
const data = this.actor.system;
|
|
|
|
const character = this.actor as M5Character;
|
|
character.createItem((game as Game).i18n.localize("midgard5.innate-ability"), M5ItemType.SKILL, {
|
|
type: M5SkillType.INNATE,
|
|
});
|
|
});
|
|
|
|
html.find(".add-general-skill").on("click", async (event) => {
|
|
const data = this.actor.system;
|
|
|
|
const character = this.actor as M5Character;
|
|
character.createItem((game as Game).i18n.localize("TYPES.Item.skill"), M5ItemType.SKILL, {
|
|
type: M5SkillType.GENERAL,
|
|
});
|
|
});
|
|
|
|
html.find(".add-combat-skill").on("click", async (event) => {
|
|
const data = this.actor.system;
|
|
|
|
const character = this.actor as M5Character;
|
|
character.createItem((game as Game).i18n.localize("midgard5.weapon-skill"), M5ItemType.SKILL, {
|
|
type: M5SkillType.COMBAT,
|
|
});
|
|
});
|
|
|
|
html.find(".add-language-skill").on("click", async (event) => {
|
|
const data = this.actor.system;
|
|
|
|
const character = this.actor as M5Character;
|
|
character.createItem((game as Game).i18n.localize("midgard5.language"), M5ItemType.SKILL, {
|
|
type: M5SkillType.LANGUAGE,
|
|
});
|
|
});
|
|
|
|
html.find(".add-spell").on("click", async (event) => {
|
|
const data = this.actor.system;
|
|
|
|
const character = this.actor as M5Character;
|
|
character.createItem((game as Game).i18n.localize("TYPES.Item.spell"), M5ItemType.SPELL, { process: "none" });
|
|
});
|
|
|
|
html.find(".add-kampfkunst").on("click", async (event) => {
|
|
const data = this.actor.system;
|
|
|
|
const character = this.actor as M5Character;
|
|
character.createItem((game as Game).i18n.localize("TYPES.Item.kampfkunst"), M5ItemType.KAMPFKUNST, {
|
|
type: "angriff",
|
|
variante: "anstuermen",
|
|
});
|
|
});
|
|
|
|
html.find(".add-effect").on("click", async (event) => {
|
|
const data = this.actor.system;
|
|
|
|
const character = this.actor as M5Character;
|
|
character.createItem((game as Game).i18n.localize("TYPES.Item.effect"), M5ItemType.EFFECT);
|
|
});
|
|
|
|
html.find(".join-combat").on("click", async (event) => {
|
|
if (!!game["combat"]) {
|
|
let combatant = game["combat"].getCombatantByActor(this.actor._id);
|
|
if (!combatant) {
|
|
await game["combat"].createEmbeddedDocuments("Combatant", [{ actorId: this.actor._id }]);
|
|
combatant = game["combat"].getCombatantByActor(this.actor._id);
|
|
}
|
|
|
|
const initiatives = {
|
|
_id: combatant._id,
|
|
initiative: this.actor.system.calc.attributes.gw.value,
|
|
};
|
|
|
|
await game["combat"].updateEmbeddedDocuments("Combatant", [initiatives]);
|
|
}
|
|
});
|
|
|
|
html.find(".ranged-combat").on("click", async (event) => {
|
|
if (!!game["combat"]) {
|
|
let combatant = game["combat"].getCombatantByActor(this.actor._id);
|
|
if (!combatant) {
|
|
await game["combat"].createEmbeddedDocuments("Combatant", [{ actorId: this.actor._id }]);
|
|
combatant = game["combat"].getCombatantByActor(this.actor._id);
|
|
}
|
|
|
|
const initiatives = {
|
|
_id: combatant._id,
|
|
initiative: 0.01 * this.actor.system.calc.attributes.gw.value,
|
|
};
|
|
|
|
await game["combat"].updateEmbeddedDocuments("Combatant", [initiatives]);
|
|
}
|
|
});
|
|
|
|
html.find(".spell-combat").on("click", async (event) => {
|
|
if (!!game["combat"]) {
|
|
let combatant = game["combat"].getCombatantByActor(this.actor._id);
|
|
if (!combatant) {
|
|
await game["combat"].createEmbeddedDocuments("Combatant", [{ actorId: this.actor._id }]);
|
|
combatant = game["combat"].getCombatantByActor(this.actor._id);
|
|
}
|
|
|
|
const initiatives = {
|
|
_id: combatant._id,
|
|
initiative: 0.001 * this.actor.system.calc.attributes.gw.value,
|
|
};
|
|
|
|
await game["combat"].updateEmbeddedDocuments("Combatant", [initiatives]);
|
|
}
|
|
});
|
|
|
|
// Drag & Drop
|
|
const dragDrop = new DragDrop({
|
|
dragSelector: ".items-list .item",
|
|
dropSelector: null,
|
|
permissions: {
|
|
dragstart: this._canDragStart.bind(this),
|
|
drop: this._canDragDrop.bind(this),
|
|
},
|
|
callbacks: {
|
|
dragstart: this._onTransferItemDragStart.bind(this),
|
|
drop: this._onTransferItemDrop.bind(this),
|
|
},
|
|
});
|
|
dragDrop.bind(html[0]);
|
|
}
|
|
|
|
_canDragStart(selector) {
|
|
return this.options.editable && this.actor.isOwner;
|
|
}
|
|
|
|
_canDragDrop(selector) {
|
|
return true;
|
|
}
|
|
|
|
_onTransferItemDragStart(event) {
|
|
const li = event.target;
|
|
$(event.target).attr("data-item-actorid", this.actor.id);
|
|
const item = this.actor.items.get(li.dataset.itemId);
|
|
|
|
const dragData = {
|
|
type: "Transfer",
|
|
actorId: this.actor.id,
|
|
data: item.toObject(false),
|
|
tokenId: null,
|
|
};
|
|
|
|
if (this.actor.isToken) dragData.tokenId = this.actor.token.id;
|
|
|
|
event.dataTransfer.setData("text/plain", JSON.stringify(dragData));
|
|
}
|
|
|
|
async _onTransferItemDrop(event) {
|
|
// Try to extract the data
|
|
let data = null;
|
|
try {
|
|
data = JSON.parse(event.dataTransfer.getData("text/plain"));
|
|
if (data.type !== "Transfer") return false;
|
|
} catch (err) {
|
|
return false;
|
|
}
|
|
|
|
if (!data.data) return false;
|
|
|
|
if (data.actorId === this.actor.id) {
|
|
return this._onSortItem(event, data.data);
|
|
}
|
|
|
|
// limit transfer on personal weapons/armour/gear
|
|
if (["item", "weapon", "defensiveWeapon", "armor", "container"].includes(data.data.type)) {
|
|
try {
|
|
this.actor.createEmbeddedDocuments("Item", [duplicate(data.data)]); // Create a new Item
|
|
const actor = (game as any).actors.get(data.actorId);
|
|
await actor.items.get(data.data._id)?.delete(); // Delete originating item from other actor
|
|
} catch (e) {
|
|
console.error("Error transfering item between actors", e);
|
|
return false;
|
|
}
|
|
} else {
|
|
ui.notifications.warn("Nur Gegenstände können übertragen werden.");
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|