Functoning weapons

This commit is contained in:
Byroks committed 2026-10-20 18:38:29 +02:00
1 parent 747bd3eb8e
commit 2291c5d1e6
24 files changed
+1091 -167

No files matched your search

+27
View File
@@ -0,0 +1,27 @@
import { prepareActiveEffectCategories } from "../helpers/utils.mjs";
import { systemPath } from "../constants.mjs";
import { M6ItemSheet } from "./item-sheet.mjs";
const { api, sheets } = foundry.applications;
/**
* Extend the basic ItemSheet with some very simple modifications.
*/
export class M6ArmorSheet extends M6ItemSheet {
static get DEFAULT_OPTIONS() {
return {
...super.DEFAULT_OPTIONS, // Kopiert alle bestehenden Actions aus M6ItemSheet
actions: {
...super.DEFAULT_OPTIONS.actions,
cycleEquip: this.#cycleEquip // Fügt NUR für dieses Sheet die neue Action hinzu
}
};
}
static #cycleEquip() {
this.item.update({
system: {
equipped: !this.item.system.equipped
}
});
}
}
+405 -13
View File
@@ -24,12 +24,15 @@ export class M6ActorSheet extends api.HandlebarsApplicationMixin(sheets.ActorShe
deleteDoc: this.#deleteDoc,
toggleEffect: this.#toggleEffect,
rollSkill: this.#rollSkill,
rollWeapon: this.#rollWeapon,
cycleEquip: this.#cycleEquip,
addSpezialisierung: addSpezialisierung,
editSepzialisierung: editSepzialisierung
},
form: {
submitOnChange: true
}
},
dragDrop: [{ dragSelector: ".draggable", dropSelector: null }]
};
/* -------------------------------------------------- */
@@ -39,12 +42,16 @@ export class M6ActorSheet extends api.HandlebarsApplicationMixin(sheets.ActorShe
tabs: [
{
id: "properties",
icon: "fa-feather-pointed"
icon: "fa-shield-halved"
},
{
id: "items",
icon: "fa-suitcase"
},
{
id: "biographie",
icon: "fa-feather"
},
{
id: "effects",
icon: "fa-bolt"
@@ -73,6 +80,10 @@ export class M6ActorSheet extends api.HandlebarsApplicationMixin(sheets.ActorShe
template: systemPath("templates/actor/items.hbs"),
scrollable: [""]
},
// biographie: {
// template: systemPath("templates/items/description.hbs"),
// scrollable: [""]
// },
effects: {
template: systemPath("templates/shared/effects.hbs"),
scrollable: [""]
@@ -125,10 +136,11 @@ export class M6ActorSheet extends api.HandlebarsApplicationMixin(sheets.ActorShe
case "properties":
context.skills = await this._getSkills();
context.resistenzen = await this._getResistenzen();
context.equippedWeapons = await this._prepWeapons();
context.tab = context.tabs[partId];
break;
case "items":
context.itemTypes = this._getItems();
context.itemTypes = await this._getItems();
context.gewicht = this._clacWeight();
context.tab = context.tabs[partId];
break;
@@ -245,6 +257,35 @@ export class M6ActorSheet extends api.HandlebarsApplicationMixin(sheets.ActorShe
return fieldSets;
}
async _prepWeapons() {
const weaponItems = this.actor.items.filter((x) => x.type === "weapon" && x.system.equipped);
const weapons = [];
weaponItems.forEach((weapon) => {
const skill = foundry.utils.getProperty(this.actor, weapon.system.fertigkeit);
const w = {
name: weapon.name,
skillValue: skill.value,
bonusValue:
skill.bonuses.find((x) => x.name === weapon.system.spezialisierung)?.level + 1 || "",
img: weapon.img,
skillPath: weapon.system.fertigkeit,
path: weapon.system.fertigkeit,
id: weapon.id
};
weapons.push(w);
});
return weapons;
}
static async #cycleEquip(event, target) {
const item = this.actor.items.get(target.dataset.itemId);
item.update({
system: {
equipped: !item.system.equipped
}
});
}
/* -------------------------------------------------- */
/**
@@ -279,17 +320,18 @@ export class M6ActorSheet extends api.HandlebarsApplicationMixin(sheets.ActorShe
for (const item of this.actor.items) {
types[item.type].items.push(item);
}
for (const item of this.actor.items) {
types[item.type].items.sort((a, b) => a.sort - b.sort);
}
// Only show Base if it's actually being used
if (types.base.items.length === 0) delete types.base;
return types;
}
_clacWeight() {
const items = this._getItems();
let gewicht = items.item.items.reduce(
(accumulator, item) => accumulator + item.system.gewicht,
0
);
const items = this.actor.items.filter((x) => x.system.gewicht);
let gewicht = items.reduce((accumulator, item) => accumulator + item.system.gewicht, 0);
let traglast = Math.max(this.actor.system.skills.körper.athletik.value, 10);
return {
@@ -394,8 +436,8 @@ export class M6ActorSheet extends api.HandlebarsApplicationMixin(sheets.ActorShe
skill,
actor
});
console.warn(dialogResult);
if (dialogResult.noRoll) {
console.log("Dialog Result", dialogResult);
if (!dialogResult) {
return;
}
// Construct the Roll instance
@@ -441,11 +483,83 @@ export class M6ActorSheet extends api.HandlebarsApplicationMixin(sheets.ActorShe
roll.toMessage();
}
static async #rollWeapon(event, target) {
const dataset = target.dataset;
const weapon = this.actor.items.get(dataset.itemId);
const skill = foundry.utils.getProperty(this.actor, weapon.system.fertigkeit);
const actor = this.actor;
const dialogResult = await this.rollDialog({
name: weapon.name,
skill,
damageRoll: weapon.system.schaden,
preSelectedSpez: weapon.system.spezialisierung,
actor
});
console.log("Dialog Result", dialogResult);
if (!dialogResult) {
return;
}
// Construct the Roll instance
const skillValue = skill.value;
let die = "";
switch (dialogResult.vorteil) {
case -1:
die = "2d20kl";
break;
case 1:
die = "2d20kh";
break;
default:
die = "1d20";
break;
}
let rollTerm = "@die + @skill";
let rollFlavor = game.i18n.localize("Midgard6.Angriff");
let attackRoll;
let damageRoll;
if (dialogResult.spezLevel) {
rollTerm += " + @spezBonus";
rollFlavor += " (" + dialogResult.spezName + ")";
}
rollFlavor += ": " + weapon.name;
if (dialogResult.customRoll) {
attackRoll = await new Roll(dialogResult.customRoll).evaluate();
} else {
attackRoll = await new Roll(
rollTerm,
{ die: die, skill: skillValue, spezBonus: dialogResult.spezLevel + 1 },
{ flavor: rollFlavor }
).evaluate();
}
damageRoll = await new Roll(
"@die",
{ die: weapon.system.schaden },
{ flavor: game.i18n.localize("Midgard6.Schaden") }
).evaluate();
// The parsed terms of the roll formula
const chatData = {
user: game.user.id,
rolls: [attackRoll, damageRoll] // Übergibt beide Rolls an die Nachricht
};
console.log("Roll:", chatData.rolls);
await ChatMessage.create(chatData);
}
async rollDialog(rollDialogRequest) {
// Assamble dialog depending on roll type
let label = rollDialogRequest.name;
let skillValue = rollDialogRequest.skill.value || rollDialogRequest.skill;
let bonuses = rollDialogRequest.skill.bonuses || [];
let damageRoll = rollDialogRequest.damageRoll || "";
let preSelectedSpez = rollDialogRequest.preSelectedSpez || "";
let vorteil = 0;
let customRoll = "";
let spezLevel;
@@ -453,10 +567,11 @@ export class M6ActorSheet extends api.HandlebarsApplicationMixin(sheets.ActorShe
// 1. Define the path to .hbs template
const templatePath = systemPath("templates/dialog/roll-dialog.hbs");
const templateData = {
classes: ["M6", "dialog"],
skillValue,
bonuses,
vorteil,
damageRoll,
preSelectedSpez,
customRoll
};
// 2. Render the Handlebars template into an HTML string
@@ -471,6 +586,7 @@ export class M6ActorSheet extends api.HandlebarsApplicationMixin(sheets.ActorShe
position: {
width: 600
},
classes: ["M6", "dialog"],
content,
actions: {},
render: (_event, dialog) => {
@@ -499,9 +615,13 @@ export class M6ActorSheet extends api.HandlebarsApplicationMixin(sheets.ActorShe
document.querySelectorAll('input[name="spezialisierung-segment"]').forEach((radio) => {
radio.addEventListener("change", (e) => {
console.log(e.target.dataset.name);
spezLevel = parseInt(e.target.value);
spezName = e.target.dataset.name;
});
if (radio.dataset.name === preSelectedSpez) {
radio.click();
}
});
},
buttons: [
@@ -509,7 +629,7 @@ export class M6ActorSheet extends api.HandlebarsApplicationMixin(sheets.ActorShe
action: "abort",
label: "Midgard6.Dialog.abbrechen",
callback: (event, button, dialog) => {
return { noRoll: true };
return null;
}
},
{
@@ -519,6 +639,7 @@ export class M6ActorSheet extends api.HandlebarsApplicationMixin(sheets.ActorShe
callback: (event, button, dialog) => {
const rollData = {
customRoll: button.form.elements.customRoll.value,
customDamageRoll: button.form.elements.customDamageRoll?.value || "",
vorteil,
spezLevel,
spezName
@@ -577,7 +698,7 @@ export class M6ActorSheet extends api.HandlebarsApplicationMixin(sheets.ActorShe
/* -------------------------------------------------- */
/**
* Disables inpM6 subject to active effects.
* Disables inputs subject to active effects.
*/
#disableOverrides() {
const flatOverrides = foundry.utils.flattenObject(this.actor.overrides);
@@ -586,4 +707,275 @@ export class M6ActorSheet extends api.HandlebarsApplicationMixin(sheets.ActorShe
if (input) input.disabled = true;
}
}
/* -------------------------------------------------- */
/* Drag and drop */
/* -------------------------------------------------- */
/**
* Define whether a user is able to begin a dragstart workflow for a given drag selector.
* @param {string} selector The candidate HTML selector for dragging.
* @returns {boolean} Can the current user drag this selector?
* @protected
*/
_canDragStart(selector) {
return this.isEditable;
}
/* -------------------------------------------------- */
/**
* Define whether a user is able to conclude a drag-and-drop workflow for a given drop selector.
* @param {string} selector The candidate HTML selector for the drop target.
* @returns {boolean} Can the current user drop on this selector?
* @protected
*/
_canDragDrop(selector) {
return this.isEditable;
}
/* -------------------------------------------------- */
/**
* Callback actions which occur at the beginning of a drag start workflow.
* @param {DragEvent} event The originating DragEvent.
* @protected
*/
_onDragStart(event) {
const li = event.currentTarget;
const dataset = event.target.dataset;
if ("link" in event.target.dataset) return;
let dragData = null;
if (li.dataset.effectId) {
const effect = this.item.effects.get(li.dataset.effectId);
dragData = effect.toDragData();
}
dragData = {
type: dataset.type,
itemId: dataset.itemId
};
if (!dragData) return;
event.dataTransfer.setData("text/plain", JSON.stringify(dragData));
}
/* -------------------------------------------------- */
/**
* Callback actions which occur when a dragged element is over a drop target.
* @param {DragEvent} event The originating DragEvent.
* @protected
*/
_onDragOver(event) {}
/* -------------------------------------------------- */
/**
* Callback actions which occur when a dragged element is dropped on a target.
* @param {DragEvent} event The originating DragEvent.
* @protected
*/
async _onDrop(event) {
const data = foundry.applications.ux.TextEditor.implementation.getDragEventData(event);
const item = this.item;
const allowed = Hooks.call("dropItemSheetData", item, this, data);
if (allowed === false) return;
// Handle different data types
switch (data.type) {
case "ActiveEffect":
return this._onDropActiveEffect(event, data);
case "Actor":
return this._onDropActor(event, data);
case "Item":
return this._onDropItem(event, data);
case "Folder":
return this._onDropFolder(event, data);
}
}
/* -------------------------------------------------- */
/**
* Handle the dropping of ActiveEffect data onto an Actor Sheet.
* @param {DragEvent} event The concluding DragEvent which contains drop data.
* @param {object} data The data transfer extracted from the event.
* @returns {Promise<ActiveEffect|boolean>} The created ActiveEffect object or false if it couldn't be created.
* @protected
*/
async _onDropActiveEffect(event, data) {
const aeCls = getDocumentClass("ActiveEffect");
const effect = await aeCls.fromDropData(data);
if (!this.item.isOwner || !effect) return false;
if (this.item.uuid === effect.parent?.uuid) return this._onEffectSort(event, effect);
aeCls.create(effect, { parent: this.item });
}
/* -------------------------------------------------- */
/**
* Sorts an Active Effect based on its surrounding attributes.
*
* @param {DragEvent} event
* @param {ActiveEffect} effect
*/
_onEffectSort(event, effect) {
const effects = this.item.effects;
const dropTarget = event.target.closest("[data-effect-id]");
if (!dropTarget) return;
const target = effects.get(dropTarget.dataset.effectId);
// Don't sort on yourself
if (effect.id === target.id) return;
// Identify sibling items based on adjacent HTML elements
const siblings = [];
for (let el of dropTarget.parentElement.children) {
const siblingId = el.dataset.effectId;
if (siblingId && siblingId !== effect.id) siblings.push(effects.get(el.dataset.effectId));
}
// Perform the sort
const sortUpdates = foundry.utils.performIntegerSort(effect, {
target,
siblings
});
const updateData = sortUpdates.map((u) => {
const update = u.update;
update._id = u.target._id;
return update;
});
// Perform the update
this.item.updateEmbeddedDocuments("ActiveEffect", updateData);
}
/* -------------------------------------------------- */
/**
* Handle dropping of an Actor data onto another Actor sheet.
* @param {DragEvent} event The concluding DragEvent which contains drop data.
* @param {object} data The data transfer extracted from the event.
* @returns {Promise<object|boolean>} A data object which describes the result of the drop, or false if the drop was
* not permitted.
* @protected
*/
async _onDropActor(event, data) {
if (!this.item.isOwner) return false;
}
/* -------------------------------------------------- */
/**
* Handle dropping of an item reference or item data onto an Actor Sheet.
* @param {DragEvent} event The concluding DragEvent which contains drop data.
* @param {object} data The data transfer extracted from the event.
* @returns {Promise<Item[]|boolean>} The created or updated Item instances, or false if the drop was not permitted.
* @protected
*/
async _onDropItem(event, data) {
if (!this.actor.isOwner) return false;
// Prüfen, ob das Item von diesem Actor stammt und Shift NICHT gedrückt ist
// Sortieren, nicht kopieren/verschieben
return this._onSortItem(event, data);
}
/**
* Handle a drop event for an existing embedded Item to sort that Item relative to its siblings
* @param {Event} event
* @param {Item} item
* @private
*/
_onSortItem(event, item) {
// Get the drag source and drop target
const items = this.actor.items;
const dropTarget = event.target.closest("[data-item-id]");
const source = items.get(item.itemId);
if (!dropTarget) return;
const target = items.get(dropTarget.dataset.itemId);
// Don't sort on yourself
if (source.id === target.id) return;
// Identify sibling items based on adjacent HTML elements
const siblings = [];
for (let el of dropTarget.parentElement.children) {
const siblingId = el.dataset.itemId;
if (siblingId && siblingId !== source.id) siblings.push(items.get(siblingId));
}
// item = items.get(item.itemId);
// Perform the sort
const sortUpdates = foundry.utils.performIntegerSort(source, {
target,
siblings
});
const updateData = sortUpdates.map((u) => {
const update = u.update;
update._id = u.target.id;
return update;
});
// Perform the update
return this.actor.updateEmbeddedDocuments("Item", updateData);
}
/* -------------------------------------------------- */
/**
* Handle dropping of a Folder on an Actor Sheet.
* The core sheet currently supports dropping a Folder of Items to create all items as owned items.
* @param {DragEvent} event The concluding DragEvent which contains drop data.
* @param {object} data The data transfer extracted from the event.
* @returns {Promise<Item[]>}
* @protected
*/
async _onDropFolder(event, data) {
if (!this.item.isOwner) return [];
}
/* -------------------------------------------------- */
/* The following pieces set up drag */
/* handling and are unlikely to need modification */
/* -------------------------------------------------- */
// This is marked as private because there's no real need
// for subclasses or external hooks to mess with it directly
#dragDrop = this.#createDragDropHandlers();
/**
* Returns an array of DragDrop instances.
* @type {DragDrop[]}
*/
get dragDrop() {
return this.#dragDrop;
}
/* -------------------------------------------------- */
/**
* Create drag-and-drop workflow handlers for this Application.
* @returns {DragDrop[]} An array of DragDrop handlers.
* @private
*/
#createDragDropHandlers() {
return this.options.dragDrop.map((d) => {
d.permissions = {
dragstart: this._canDragStart.bind(this),
drop: this._canDragDrop.bind(this)
};
d.callbacks = {
dragstart: this._onDragStart.bind(this),
dragover: this._onDragOver.bind(this),
drop: this._onDrop.bind(this)
};
return new foundry.applications.ux.DragDrop(d);
});
}
}
+65 -6
View File
@@ -7,6 +7,7 @@ const { api, sheets } = foundry.applications;
*/
export class M6ItemSheet extends api.HandlebarsApplicationMixin(sheets.ItemSheet) {
/** @inheritdoc */
static DEFAULT_OPTIONS = {
window: {
resizable: true
@@ -35,7 +36,7 @@ export class M6ItemSheet extends api.HandlebarsApplicationMixin(sheets.ItemSheet
primary: {
tabs: [
{
id: "description",
id: "beschreibung",
icon: "fa-feather-pointed"
},
{
@@ -43,8 +44,8 @@ export class M6ItemSheet extends api.HandlebarsApplicationMixin(sheets.ItemSheet
icon: "fa-bolt"
}
],
initial: "description",
labelPrefix: "M6.Sheets.Tabs"
initial: "beschreibung",
labelPrefix: "Midgard6.Sheets.Tabs"
}
};
@@ -58,7 +59,7 @@ export class M6ItemSheet extends api.HandlebarsApplicationMixin(sheets.ItemSheet
tabs: {
template: systemPath("templates/actor/tabs.hbs")
},
description: {
beschreibung: {
template: systemPath("templates/item/description.hbs"),
scrollable: [""]
},
@@ -109,7 +110,7 @@ export class M6ItemSheet extends api.HandlebarsApplicationMixin(sheets.ItemSheet
context.effects = prepareActiveEffectCategories(this.item.effects);
context.tab = context.tabs[partId];
break;
case "description":
case "beschreibung":
context.fields = await this._getFields();
context.enrichedBeschreibung =
await foundry.applications.ux.TextEditor.implementation.enrichHTML(
@@ -125,6 +126,9 @@ export class M6ItemSheet extends api.HandlebarsApplicationMixin(sheets.ItemSheet
);
context.tab = context.tabs[partId];
break;
default:
await this._addSpecifics(context);
context.tab = context.tabs[partId];
}
return context;
}
@@ -338,8 +342,10 @@ export class M6ItemSheet extends api.HandlebarsApplicationMixin(sheets.ItemSheet
async _onDrop(event) {
const data = foundry.applications.ux.TextEditor.implementation.getDragEventData(event);
const item = this.item;
console.warn(data.type);
const allowed = Hooks.call("dropItemSheetData", item, this, data);
if (allowed === false) return;
// if (allowed === false) return;
// Handle different data types
switch (data.type) {
@@ -436,6 +442,55 @@ export class M6ItemSheet extends api.HandlebarsApplicationMixin(sheets.ItemSheet
*/
async _onDropItem(event, data) {
if (!this.item.isOwner) return false;
// Prüfen, ob das Item von diesem Actor stammt und Shift NICHT gedrückt ist
const isSameActor = this.actor.uuid === item.parent?.uuid;
const isShift = event.shiftKey;
console.warn(isSameActor);
if (isSameActor) {
console.warn("here");
// Sortieren, nicht kopieren/verschieben
return this._onSortItem(event, item);
}
}
/**
* Handle a drop event for an existing embedded Item to sort that Item relative to its siblings
* @param {Event} event
* @param {Item} item
* @private
*/
_onSortItem(event, item) {
// Get the drag source and drop target
const items = this.actor.items;
const dropTarget = event.target.closest("[data-item-id]");
if (!dropTarget) return;
const target = items.get(dropTarget.dataset.itemId);
// Don't sort on yourself
if (item.id === target.id) return;
// Identify sibling items based on adjacent HTML elements
const siblings = [];
for (let el of dropTarget.parentElement.children) {
const siblingId = el.dataset.itemId;
if (siblingId && siblingId !== item.id) siblings.push(items.get(el.dataset.itemId));
}
// Perform the sort
const sortUpdates = foundry.utils.SortingHelpers.performIntegerSort(item, {
target,
siblings
});
const updateData = sortUpdates.map((u) => {
const update = u.update;
update._id = u.target._id;
return update;
});
// Perform the update
return this.actor.updateEmbeddedDocuments("Item", updateData);
}
/* -------------------------------------------------- */
@@ -490,4 +545,8 @@ export class M6ItemSheet extends api.HandlebarsApplicationMixin(sheets.ItemSheet
return new foundry.applications.ux.DragDrop(d);
});
}
async _addSpecifics(context) {
return null;
}
}
+74
View File
@@ -0,0 +1,74 @@
import { prepareActiveEffectCategories } from "../helpers/utils.mjs";
import { systemPath } from "../constants.mjs";
import { M6ItemSheet } from "./item-sheet.mjs";
const { api, sheets } = foundry.applications;
/**
* Extend the basic ItemSheet with some very simple modifications.
*/
export class M6WeaponSheet extends M6ItemSheet {
static get DEFAULT_OPTIONS() {
return {
...super.DEFAULT_OPTIONS, // Kopiert alle bestehenden Actions aus M6ItemSheet
actions: {
...super.DEFAULT_OPTIONS.actions,
cycleEquip: this.#cycleEquip // Fügt NUR für dieses Sheet die neue Action hinzu
}
};
}
static TABS = {
primary: {
tabs: [
{
id: "eigenschaften",
icon: "fa-shield-halved"
},
{
id: "beschreibung",
icon: "fa-feather-pointed"
},
{
id: "effects",
icon: "fa-bolt"
}
],
initial: "eigenschaften",
labelPrefix: "Midgard6.Sheets.Tabs"
}
};
/** @inheritdoc */
static PARTS = {
header: {
template: systemPath("templates/item/header.hbs")
},
tabs: {
template: systemPath("templates/actor/tabs.hbs")
},
eigenschaften: {
template: systemPath("templates/item/eigenschaften.hbs"),
scrollable: [""]
},
beschreibung: {
template: systemPath("templates/item/description.hbs"),
scrollable: [""]
},
effects: {
template: systemPath("templates/shared/effects.hbs"),
scrollable: [""]
}
};
_addSpecifics(context) {
context.bonuses = foundry.utils.getProperty(this.actor, this.item.system.fertigkeit).bonuses;
}
static #cycleEquip() {
this.item.update({
system: {
equipped: !this.item.system.equipped
}
});
}
}