actor class cleaning

This commit is contained in:
François-Xavier Guillois
2023-09-06 09:17:11 +02:00
parent 25b4179cd3
commit 980ab943c4
5 changed files with 28 additions and 484 deletions
-86
View File
@@ -41,26 +41,6 @@ export class VermineActorSheet extends ActorSheet {
context.flags = actorData.flags;
context.config = CONFIG.VERMINE;
// Prepare character data and items.
if (actorData.type == 'character') {
this._prepareCharacterItems(context);
}
// Prepare NPC data and items.
if (actorData.type == 'npc') {
this._prepareNpcItems(context);
}
// Prepare Group data and items.
if (actorData.type == 'group') {
this._prepareGroupItems(context);
}
// Prepare Creature data and items.
if (actorData.type == 'npc') {
this._prepareCreatureItems(context);
}
// Add roll data for TinyMCE editors.
context.rollData = context.actor.getRollData();
@@ -112,72 +92,6 @@ export class VermineActorSheet extends ActorSheet {
}
/**
* Organize and classify Items for Character sheets.
*
* @param {Object} actorData The actor to prepare.
*
* @return {undefined}
*/
_prepareCharacterItems(context) {
context.gear = this.actor.itemTypes['item'];
context.weapons = this.actor.itemTypes['weapon'];
context.defenses = this.actor.itemTypes['defense'];
context.traits = this.actor.itemTypes['trait'];
context.specialties = this.actor.itemTypes['specialty'];
context.abilities = this.actor.itemTypes['ability'];
context.evolutions = this.actor.itemTypes['evolution'];
context.traumas = this.actor.itemTypes['trauma'];
context.backgrounds = this.actor.itemTypes['background'];
context.rumors = this.actor.itemTypes['rumor'];
}
/**
* Organize and classify Items for Npc sheets.
*
* @param {Object} actorData The actor to prepare.
*
* @return {undefined}
*/
_prepareNpcItems(context) {
context.gear = this.actor.itemTypes['item'];
context.traits = this.actor.itemTypes['trait'];
}
/**
* Organize and classify Items for Group sheets.
*
* @param {Object} actorData The actor to prepare.
*
* @return {undefined}
*/
_prepareGroupItems(context) {
context.gear = this.actor.itemTypes['item'];
context.weapons = this.actor.itemTypes['weapon'];
context.defenses = this.actor.itemTypes['defense'];
context.vehicles = this.actor.itemTypes['vehicle'];
context.totem_abilities = this.actor.itemTypes['ability'].filter(i=>i.type !== 'totem');
context.abilities = this.actor.itemTypes['ability'].filter(i=>i.type === 'totem');
}
/**
* Organize and classify Items for Creature sheets.
*
* @param {Object} actorData The actor to prepare.
*
* @return {undefined}
*/
_prepareCreatureItems(context) {
context.gear = this.actor.itemTypes['item'];
context.traits = this.actor.itemTypes['trait'];
}
async _onItemCreate(event) {
event.preventDefault();
const header = event.currentTarget;
+10 -66
View File
@@ -85,45 +85,16 @@ export class VermineCharacterSheet extends VermineActorSheet {
* @return {undefined}
*/
_prepareItems(context) {
// Initialize containers.
const gear = [];
const features = [];
const spells = {
0: [],
1: [],
2: [],
3: [],
4: [],
5: [],
6: [],
7: [],
8: [],
9: []
};
// Iterate through items, allocating to containers
for (let i of context.items) {
i.img = i.img || DEFAULT_TOKEN;
// Append to gear.
if (i.type === 'item') {
gear.push(i);
}
// Append to features.
else if (i.type === 'feature') {
features.push(i);
}
// Append to spells.
else if (i.type === 'spell') {
if (i.system.spellLevel != undefined) {
spells[i.system.spellLevel].push(i);
}
}
}
// Assign and return
context.gear = gear;
context.features = features;
context.spells = spells;
context.gear = this.actor.itemTypes['item'];
context.weapons = this.actor.itemTypes['weapon'];
context.defenses = this.actor.itemTypes['defense'];
context.traits = this.actor.itemTypes['trait'];
context.specialties = this.actor.itemTypes['specialty'];
context.abilities = this.actor.itemTypes['ability'];
context.evolutions = this.actor.itemTypes['evolution'];
context.traumas = this.actor.itemTypes['trauma'];
context.backgrounds = this.actor.itemTypes['background'];
context.rumors = this.actor.itemTypes['rumor'];
}
/* -------------------------------------------- */
@@ -131,36 +102,9 @@ export class VermineCharacterSheet extends VermineActorSheet {
/** @override */
activateListeners(html) {
super.activateListeners(html);
// Render the item sheet for viewing/editing prior to the editable check.
html.find('.item-edit').click(ev => {
const li = $(ev.currentTarget).parents(".item");
const item = this.actor.items.get(li.data("itemId"));
item.sheet.render(true);
});
// -------------------------------------------------------------
// Everything below here is only needed if the sheet is editable
if (!this.isEditable) return;
// Add and delete Inventory Item
// already configured in parents listeners
// Rollable abilities.
html.find('.rollable').click(this._onRoll.bind(this));
// Choose Totem
html.find('.chooseTotem').click(this._onTotemButton.bind(this));
// Drag events for macros.
if (this.actor.isOwner) {
let handler = ev => this._onDragStart(ev);
html.find('li.item').each((i, li) => {
if (li.classList.contains("inventory-header")) return;
li.setAttribute("draggable", true);
li.addEventListener("dragstart", handler, false);
});
}
}
+2 -123
View File
@@ -83,45 +83,8 @@ export class VermineCreatureSheet extends ActorSheet {
* @return {undefined}
*/
_prepareItems(context) {
// Initialize containers.
const gear = [];
const features = [];
const spells = {
0: [],
1: [],
2: [],
3: [],
4: [],
5: [],
6: [],
7: [],
8: [],
9: []
};
// Iterate through items, allocating to containers
for (let i of context.items) {
i.img = i.img || DEFAULT_TOKEN;
// Append to gear.
if (i.type === 'item') {
gear.push(i);
}
// Append to features.
else if (i.type === 'feature') {
features.push(i);
}
// Append to spells.
else if (i.type === 'spell') {
if (i.system.spellLevel != undefined) {
spells[i.system.spellLevel].push(i);
}
}
}
// Assign and return
context.gear = gear;
context.features = features;
context.spells = spells;
context.gear = this.actor.itemTypes['item'];
context.traits = this.actor.itemTypes['trait'];
}
/* -------------------------------------------- */
@@ -129,90 +92,6 @@ export class VermineCreatureSheet extends ActorSheet {
/** @override */
activateListeners(html) {
super.activateListeners(html);
// Render the item sheet for viewing/editing prior to the editable check.
html.find('.item-edit').click(ev => {
const li = $(ev.currentTarget).parents(".item");
const item = this.actor.items.get(li.data("itemId"));
item.sheet.render(true);
});
// -------------------------------------------------------------
// Everything below here is only needed if the sheet is editable
if (!this.isEditable) return;
// Rollable abilities.
html.find('.rollable').click(this._onRoll.bind(this));
// Drag events for macros.
if (this.actor.isOwner) {
let handler = ev => this._onDragStart(ev);
html.find('li.item').each((i, li) => {
if (li.classList.contains("inventory-header")) return;
li.setAttribute("draggable", true);
li.addEventListener("dragstart", handler, false);
});
}
}
/**
* Handle creating a new Owned Item for the actor using initial data defined in the HTML dataset
* @param {Event} event The originating click event
* @private
*/
async _onItemCreate(event) {
event.preventDefault();
const header = event.currentTarget;
// Get the type of item to create.
const type = header.dataset.type;
// Grab any data associated with this control.
const data = duplicate(header.dataset);
// Initialize a default name.
const name = `New ${type.capitalize()}`;
// Prepare the item object.
const itemData = {
name: name,
type: type,
system: data
};
// Remove the type from the dataset since it's in the itemData.type prop.
delete itemData.system["type"];
// Finally, create the item!
return await Item.create(itemData, {parent: this.actor});
}
/**
* Handle clickable rolls.
* @param {Event} event The originating click event
* @private
*/
_onRoll(event) {
event.preventDefault();
const element = event.currentTarget;
const dataset = element.dataset;
// Handle item rolls.
if (dataset.rollType) {
if (dataset.rollType == 'item') {
const itemId = element.closest('.item').dataset.itemId;
const item = this.actor.items.get(itemId);
if (item) return item.roll();
}
}
// Handle rolls that supply the formula directly.
if (dataset.roll) {
let label = dataset.label ? `[ability] ${dataset.label}` : '';
let roll = new Roll(dataset.roll, this.actor.getRollData());
roll.toMessage({
speaker: ChatMessage.getSpeaker({ actor: this.actor }),
flavor: label,
rollMode: game.settings.get('core', 'rollMode'),
});
return roll;
}
}
}
+8 -79
View File
@@ -134,6 +134,14 @@ export class VermineGroupSheet extends VermineActorSheet {
context.encounters.push(game.actors.get(encounterId));
}
context.gear = this.actor.itemTypes['item'];
context.weapons = this.actor.itemTypes['weapon'];
context.defenses = this.actor.itemTypes['defense'];
context.vehicles = this.actor.itemTypes['vehicle'];
context.totem_abilities = this.actor.itemTypes['ability'].filter(i=>i.type !== 'totem');
context.abilities = this.actor.itemTypes['ability'].filter(i=>i.type === 'totem');
}
/* -------------------------------------------- */
@@ -141,19 +149,6 @@ export class VermineGroupSheet extends VermineActorSheet {
/** @override */
activateListeners(html) {
super.activateListeners(html);
// Render the item sheet for viewing/editing prior to the editable check.
html.find('.item-edit').click(ev => {
const li = $(ev.currentTarget).parents(".item");
const item = this.actor.items.get(li.data("itemId"));
item.sheet.render(true);
});
// -------------------------------------------------------------
// Everything below here is only needed if the sheet is editable
if (!this.isEditable) return;
// Add and delete Inventory Item
// already configured in parents listeners
// Choose Totem
html.find('.chooseTotem').click(this._onTotemButton.bind(this));
@@ -182,74 +177,8 @@ export class VermineGroupSheet extends VermineActorSheet {
this.render(true);
});
if (this.actor.isOwner) {
let handler = ev => this._onDragStart(ev);
html.find('li.item').each((i, li) => {
if (li.classList.contains("inventory-header")) return;
li.setAttribute("draggable", true);
li.addEventListener("dragstart", handler, false);
});
}
}
/**
* Handle creating a new Owned Item for the actor using initial data defined in the HTML dataset
* @param {Event} event The originating click event
* @private
*/
async _onItemCreate(event) {
event.preventDefault();
const header = event.currentTarget;
// Get the type of item to create.
const type = header.dataset.type;
// Grab any data associated with this control.
const data = duplicate(header.dataset);
// Initialize a default name.
const name = `New ${type.capitalize()}`;
// Prepare the item object.
const itemData = {
name: name,
type: type,
system: data
};
// Remove the type from the dataset since it's in the itemData.type prop.
delete itemData.system["type"];
// Finally, create the item!
return await Item.create(itemData, {parent: this.actor});
}
/**
* Handle clickable rolls.
* @param {Event} event The originating click event
* @private
*/
_onRoll(event) {
event.preventDefault();
const element = event.currentTarget;
const dataset = element.dataset;
// Handle item rolls.
if (dataset.rollType) {
if (dataset.rollType == 'item') {
const itemId = element.closest('.item').dataset.itemId;
const item = this.actor.items.get(itemId);
if (item) return item.roll();
}
}
// Handle rolls that supply the formula directly.
if (dataset.roll) {
let label = dataset.label ? `[ability] ${dataset.label}` : '';
let roll = new Roll(dataset.roll, this.actor.getRollData());
roll.toMessage({
speaker: ChatMessage.getSpeaker({ actor: this.actor }),
flavor: label,
rollMode: game.settings.get('core', 'rollMode'),
});
return roll;
}
}
/**
* Handle totem pick
+3 -125
View File
@@ -69,10 +69,7 @@ export class VermineNpcSheet extends VermineActorSheet {
* @return {undefined}
*/
_prepareCharacterData(context) {
// Handle ability scores.
for (let [k, v] of Object.entries(context.system.abilities)) {
v.label = game.i18n.localize(context.system.abilities[k].label) ?? k;
}
}
/**
@@ -83,45 +80,8 @@ export class VermineNpcSheet extends VermineActorSheet {
* @return {undefined}
*/
_prepareItems(context) {
// Initialize containers.
const gear = [];
const features = [];
const spells = {
0: [],
1: [],
2: [],
3: [],
4: [],
5: [],
6: [],
7: [],
8: [],
9: []
};
// Iterate through items, allocating to containers
for (let i of context.items) {
i.img = i.img || DEFAULT_TOKEN;
// Append to gear.
if (i.type === 'item') {
gear.push(i);
}
// Append to features.
else if (i.type === 'feature') {
features.push(i);
}
// Append to spells.
else if (i.type === 'spell') {
if (i.system.spellLevel != undefined) {
spells[i.system.spellLevel].push(i);
}
}
}
// Assign and return
context.gear = gear;
context.features = features;
context.spells = spells;
context.gear = this.actor.itemTypes['item'];
context.traits = this.actor.itemTypes['trait'];
}
/* -------------------------------------------- */
@@ -130,88 +90,6 @@ export class VermineNpcSheet extends VermineActorSheet {
activateListeners(html) {
super.activateListeners(html);
// Render the item sheet for viewing/editing prior to the editable check.
html.find('.item-edit').click(ev => {
const li = $(ev.currentTarget).parents(".item");
const item = this.actor.items.get(li.data("itemId"));
item.sheet.render(true);
});
// -------------------------------------------------------------
// Everything below here is only needed if the sheet is editable
if (!this.isEditable) return;
// Rollable abilities.
html.find('.rollable').click(this._onRoll.bind(this));
// Drag events for macros.
if (this.actor.isOwner) {
let handler = ev => this._onDragStart(ev);
html.find('li.item').each((i, li) => {
if (li.classList.contains("inventory-header")) return;
li.setAttribute("draggable", true);
li.addEventListener("dragstart", handler, false);
});
}
}
/**
* Handle creating a new Owned Item for the actor using initial data defined in the HTML dataset
* @param {Event} event The originating click event
* @private
*/
async _onItemCreate(event) {
event.preventDefault();
const header = event.currentTarget;
// Get the type of item to create.
const type = header.dataset.type;
// Grab any data associated with this control.
const data = duplicate(header.dataset);
// Initialize a default name.
const name = `New ${type.capitalize()}`;
// Prepare the item object.
const itemData = {
name: name,
type: type,
system: data
};
// Remove the type from the dataset since it's in the itemData.type prop.
delete itemData.system["type"];
// Finally, create the item!
return await Item.create(itemData, {parent: this.actor});
}
/**
* Handle clickable rolls.
* @param {Event} event The originating click event
* @private
*/
_onRoll(event) {
event.preventDefault();
const element = event.currentTarget;
const dataset = element.dataset;
// Handle item rolls.
if (dataset.rollType) {
if (dataset.rollType == 'item') {
const itemId = element.closest('.item').dataset.itemId;
const item = this.actor.items.get(itemId);
if (item) return item.roll();
}
}
// Handle rolls that supply the formula directly.
if (dataset.roll) {
let label = dataset.label ? `[ability] ${dataset.label}` : '';
let roll = new Roll(dataset.roll, this.actor.getRollData());
roll.toMessage({
speaker: ChatMessage.getSpeaker({ actor: this.actor }),
flavor: label,
rollMode: game.settings.get('core', 'rollMode'),
});
return roll;
}
}
}