forked from public/foundryvtt-reve-de-dragon
Push initial structure
This commit is contained in:
130
module/actor-sheet.js
Normal file
130
module/actor-sheet.js
Normal file
@ -0,0 +1,130 @@
|
||||
/**
|
||||
* Extend the basic ActorSheet with some very simple modifications
|
||||
* @extends {ActorSheet}
|
||||
*/
|
||||
export class SimpleActorSheet extends ActorSheet {
|
||||
|
||||
/** @override */
|
||||
static get defaultOptions() {
|
||||
return mergeObject(super.defaultOptions, {
|
||||
classes: ["worldbuilding", "sheet", "actor"],
|
||||
template: "systems/worldbuilding/templates/actor-sheet.html",
|
||||
width: 600,
|
||||
height: 600,
|
||||
tabs: [{navSelector: ".sheet-tabs", contentSelector: ".sheet-body", initial: "description"}],
|
||||
dragDrop: [{dragSelector: ".item-list .item", dropSelector: null}]
|
||||
});
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
|
||||
/** @override */
|
||||
getData() {
|
||||
const data = super.getData();
|
||||
data.dtypes = ["String", "Number", "Boolean"];
|
||||
for ( let attr of Object.values(data.data.attributes) ) {
|
||||
attr.isCheckbox = attr.dtype === "Boolean";
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
|
||||
/** @override */
|
||||
activateListeners(html) {
|
||||
super.activateListeners(html);
|
||||
|
||||
// Everything below here is only needed if the sheet is editable
|
||||
if (!this.options.editable) return;
|
||||
|
||||
// Update Inventory Item
|
||||
html.find('.item-edit').click(ev => {
|
||||
const li = $(ev.currentTarget).parents(".item");
|
||||
const item = this.actor.getOwnedItem(li.data("itemId"));
|
||||
item.sheet.render(true);
|
||||
});
|
||||
|
||||
// Delete Inventory Item
|
||||
html.find('.item-delete').click(ev => {
|
||||
const li = $(ev.currentTarget).parents(".item");
|
||||
this.actor.deleteOwnedItem(li.data("itemId"));
|
||||
li.slideUp(200, () => this.render(false));
|
||||
});
|
||||
|
||||
// Add or Remove Attribute
|
||||
html.find(".attributes").on("click", ".attribute-control", this._onClickAttributeControl.bind(this));
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
|
||||
/** @override */
|
||||
setPosition(options={}) {
|
||||
const position = super.setPosition(options);
|
||||
const sheetBody = this.element.find(".sheet-body");
|
||||
const bodyHeight = position.height - 192;
|
||||
sheetBody.css("height", bodyHeight);
|
||||
return position;
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Listen for click events on an attribute control to modify the composition of attributes in the sheet
|
||||
* @param {MouseEvent} event The originating left click event
|
||||
* @private
|
||||
*/
|
||||
async _onClickAttributeControl(event) {
|
||||
event.preventDefault();
|
||||
const a = event.currentTarget;
|
||||
const action = a.dataset.action;
|
||||
const attrs = this.object.data.data.attributes;
|
||||
const form = this.form;
|
||||
|
||||
// Add new attribute
|
||||
if ( action === "create" ) {
|
||||
const nk = Object.keys(attrs).length + 1;
|
||||
let newKey = document.createElement("div");
|
||||
newKey.innerHTML = `<input type="text" name="data.attributes.attr${nk}.key" value="attr${nk}"/>`;
|
||||
newKey = newKey.children[0];
|
||||
form.appendChild(newKey);
|
||||
await this._onSubmit(event);
|
||||
}
|
||||
|
||||
// Remove existing attribute
|
||||
else if ( action === "delete" ) {
|
||||
const li = a.closest(".attribute");
|
||||
li.parentElement.removeChild(li);
|
||||
await this._onSubmit(event);
|
||||
}
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
|
||||
/** @override */
|
||||
_updateObject(event, formData) {
|
||||
|
||||
// Handle the free-form attributes list
|
||||
const formAttrs = expandObject(formData).data.attributes || {};
|
||||
const attributes = Object.values(formAttrs).reduce((obj, v) => {
|
||||
let k = v["key"].trim();
|
||||
if ( /[\s\.]/.test(k) ) return ui.notifications.error("Attribute keys may not contain spaces or periods");
|
||||
delete v["key"];
|
||||
obj[k] = v;
|
||||
return obj;
|
||||
}, {});
|
||||
|
||||
// Remove attributes which are no longer used
|
||||
for ( let k of Object.keys(this.object.data.data.attributes) ) {
|
||||
if ( !attributes.hasOwnProperty(k) ) attributes[`-=${k}`] = null;
|
||||
}
|
||||
|
||||
// Re-combine formData
|
||||
formData = Object.entries(formData).filter(e => !e[0].startsWith("data.attributes")).reduce((obj, e) => {
|
||||
obj[e[0]] = e[1];
|
||||
return obj;
|
||||
}, {_id: this.object._id, "data.attributes": attributes});
|
||||
|
||||
// Update the Actor
|
||||
return this.object.update(formData);
|
||||
}
|
||||
}
|
35
module/actor.js
Normal file
35
module/actor.js
Normal file
@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Extend the base Actor entity by defining a custom roll data structure which is ideal for the Simple system.
|
||||
* @extends {Actor}
|
||||
*/
|
||||
export class SimpleActor extends Actor {
|
||||
|
||||
/** @override */
|
||||
getRollData() {
|
||||
const data = super.getRollData();
|
||||
const shorthand = game.settings.get("worldbuilding", "macroShorthand");
|
||||
|
||||
// Re-map all attributes onto the base roll data
|
||||
if ( !!shorthand ) {
|
||||
for ( let [k, v] of Object.entries(data.attributes) ) {
|
||||
if ( !(k in data) ) data[k] = v.value;
|
||||
}
|
||||
delete data.attributes;
|
||||
}
|
||||
|
||||
// Map all items data using their slugified names
|
||||
data.items = this.data.items.reduce((obj, i) => {
|
||||
let key = i.name.slugify({strict: true});
|
||||
let itemData = duplicate(i.data);
|
||||
if ( !!shorthand ) {
|
||||
for ( let [k, v] of Object.entries(itemData.attributes) ) {
|
||||
if ( !(k in itemData) ) itemData[k] = v.value;
|
||||
}
|
||||
delete itemData["attributes"];
|
||||
}
|
||||
obj[key] = itemData;
|
||||
return obj;
|
||||
}, {});
|
||||
return data;
|
||||
}
|
||||
}
|
115
module/item-sheet.js
Normal file
115
module/item-sheet.js
Normal file
@ -0,0 +1,115 @@
|
||||
/**
|
||||
* Extend the basic ItemSheet with some very simple modifications
|
||||
* @extends {ItemSheet}
|
||||
*/
|
||||
export class SimpleItemSheet extends ItemSheet {
|
||||
|
||||
/** @override */
|
||||
static get defaultOptions() {
|
||||
return mergeObject(super.defaultOptions, {
|
||||
classes: ["worldbuilding", "sheet", "item"],
|
||||
template: "systems/worldbuilding/templates/item-sheet.html",
|
||||
width: 520,
|
||||
height: 480,
|
||||
tabs: [{navSelector: ".sheet-tabs", contentSelector: ".sheet-body", initial: "description"}]
|
||||
});
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
|
||||
/** @override */
|
||||
getData() {
|
||||
const data = super.getData();
|
||||
data.dtypes = ["String", "Number", "Boolean"];
|
||||
for ( let attr of Object.values(data.data.attributes) ) {
|
||||
attr.isCheckbox = attr.dtype === "Boolean";
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
|
||||
/** @override */
|
||||
setPosition(options={}) {
|
||||
const position = super.setPosition(options);
|
||||
const sheetBody = this.element.find(".sheet-body");
|
||||
const bodyHeight = position.height - 192;
|
||||
sheetBody.css("height", bodyHeight);
|
||||
return position;
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
|
||||
/** @override */
|
||||
activateListeners(html) {
|
||||
super.activateListeners(html);
|
||||
|
||||
// Everything below here is only needed if the sheet is editable
|
||||
if (!this.options.editable) return;
|
||||
|
||||
// Add or Remove Attribute
|
||||
html.find(".attributes").on("click", ".attribute-control", this._onClickAttributeControl.bind(this));
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Listen for click events on an attribute control to modify the composition of attributes in the sheet
|
||||
* @param {MouseEvent} event The originating left click event
|
||||
* @private
|
||||
*/
|
||||
async _onClickAttributeControl(event) {
|
||||
event.preventDefault();
|
||||
const a = event.currentTarget;
|
||||
const action = a.dataset.action;
|
||||
const attrs = this.object.data.data.attributes;
|
||||
const form = this.form;
|
||||
|
||||
// Add new attribute
|
||||
if ( action === "create" ) {
|
||||
const nk = Object.keys(attrs).length + 1;
|
||||
let newKey = document.createElement("div");
|
||||
newKey.innerHTML = `<input type="text" name="data.attributes.attr${nk}.key" value="attr${nk}"/>`;
|
||||
newKey = newKey.children[0];
|
||||
form.appendChild(newKey);
|
||||
await this._onSubmit(event);
|
||||
}
|
||||
|
||||
// Remove existing attribute
|
||||
else if ( action === "delete" ) {
|
||||
const li = a.closest(".attribute");
|
||||
li.parentElement.removeChild(li);
|
||||
await this._onSubmit(event);
|
||||
}
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
|
||||
/** @override */
|
||||
_updateObject(event, formData) {
|
||||
|
||||
// Handle the free-form attributes list
|
||||
const formAttrs = expandObject(formData).data.attributes || {};
|
||||
const attributes = Object.values(formAttrs).reduce((obj, v) => {
|
||||
let k = v["key"].trim();
|
||||
if ( /[\s\.]/.test(k) ) return ui.notifications.error("Attribute keys may not contain spaces or periods");
|
||||
delete v["key"];
|
||||
obj[k] = v;
|
||||
return obj;
|
||||
}, {});
|
||||
|
||||
// Remove attributes which are no longer used
|
||||
for ( let k of Object.keys(this.object.data.data.attributes) ) {
|
||||
if ( !attributes.hasOwnProperty(k) ) attributes[`-=${k}`] = null;
|
||||
}
|
||||
|
||||
// Re-combine formData
|
||||
formData = Object.entries(formData).filter(e => !e[0].startsWith("data.attributes")).reduce((obj, e) => {
|
||||
obj[e[0]] = e[1];
|
||||
return obj;
|
||||
}, {_id: this.object._id, "data.attributes": attributes});
|
||||
|
||||
// Update the Item
|
||||
return this.object.update(formData);
|
||||
}
|
||||
}
|
46
module/simple.js
Normal file
46
module/simple.js
Normal file
@ -0,0 +1,46 @@
|
||||
/**
|
||||
* A simple and flexible system for world-building using an arbitrary collection of character and item attributes
|
||||
* Author: Atropos
|
||||
* Software License: GNU GPLv3
|
||||
*/
|
||||
|
||||
// Import Modules
|
||||
import { SimpleActor } from "./actor.js";
|
||||
import { SimpleItemSheet } from "./item-sheet.js";
|
||||
import { SimpleActorSheet } from "./actor-sheet.js";
|
||||
|
||||
/* -------------------------------------------- */
|
||||
/* Foundry VTT Initialization */
|
||||
/* -------------------------------------------- */
|
||||
|
||||
Hooks.once("init", async function() {
|
||||
console.log(`Initializing Simple Worldbuilding System`);
|
||||
|
||||
/**
|
||||
* Set an initiative formula for the system
|
||||
* @type {String}
|
||||
*/
|
||||
CONFIG.Combat.initiative = {
|
||||
formula: "1d20",
|
||||
decimals: 2
|
||||
};
|
||||
|
||||
// Define custom Entity classes
|
||||
CONFIG.Actor.entityClass = SimpleActor;
|
||||
|
||||
// Register sheet application classes
|
||||
Actors.unregisterSheet("core", ActorSheet);
|
||||
Actors.registerSheet("dnd5e", SimpleActorSheet, { makeDefault: true });
|
||||
Items.unregisterSheet("core", ItemSheet);
|
||||
Items.registerSheet("dnd5e", SimpleItemSheet, {makeDefault: true});
|
||||
|
||||
// Register system settings
|
||||
game.settings.register("worldbuilding", "macroShorthand", {
|
||||
name: "Shortened Macro Syntax",
|
||||
hint: "Enable a shortened macro syntax which allows referencing attributes directly, for example @str instead of @attributes.str.value. Disable this setting if you need the ability to reference the full attribute model, for example @attributes.str.label.",
|
||||
scope: "world",
|
||||
type: Boolean,
|
||||
default: true,
|
||||
config: true
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user