fvtt-mournblade/modules/mournblade-actor.js

203 lines
6.0 KiB
JavaScript

/* -------------------------------------------- */
import { MournbladeUtility } from "./mournblade-utility.js";
import { MournbladeRollDialog } from "./mournblade-roll-dialog.js";
/* -------------------------------------------- */
/* -------------------------------------------- */
/**
* Extend the base Actor entity by defining a custom roll data structure which is ideal for the Simple system.
* @extends {Actor}
*/
export class MournbladeActor extends Actor {
/* -------------------------------------------- */
/**
* Override the create() function to provide additional SoS functionality.
*
* This overrided create() function adds initial items
* Namely: Basic skills, money,
*
* @param {Object} data Barebones actor data which this function adds onto.
* @param {Object} options (Unused) Additional options which customize the creation workflow.
*
*/
static async create(data, options) {
// Case of compendium global import
if (data instanceof Array) {
return super.create(data, options);
}
// If the created actor has items (only applicable to duplicated actors) bypass the new actor creation logic
if (data.items) {
let actor = super.create(data, options);
return actor;
}
if (data.type == 'personnage') {
const skills = await MournbladeUtility.loadCompendium("fvtt-mournblade.skills")
data.items = skills.map(i => i.toObject());
}
if (data.type == 'pnj') {
}
return super.create(data, options);
}
/* -------------------------------------------- */
getWeapons() {
return this.data.items.filter(item => item.type == "arme" )
}
/* -------------------------------------------- */
getArmors() {
return this.data.items.filter(item => item.type == "protection" )
}
/* -------------------------------------------- */
getSkills() {
return this.data.items.filter(item => item.type == "competence" )
}
/* -------------------------------------------- */
prepareBaseData() {
}
/* -------------------------------------------- */
async prepareData() {
super.prepareData();
}
/* -------------------------------------------- */
prepareDerivedData() {
if (this.type == 'character') {
}
super.prepareDerivedData();
}
/* -------------------------------------------- */
_preUpdate(changed, options, user) {
super._preUpdate(changed, options, user);
}
/* -------------------------------------------- */
getActivePerks() {
let perks = this.data.items.filter(item => item.type == 'perk' && item.data.data.active);
return perks;
}
/* -------------------------------------------- */
getItemById(id) {
let item = this.data.items.find(item => item.id == id);
if (item) {
item = duplicate(item)
}
return item;
}
/* -------------------------------------------- */
async equipItem(itemId) {
let item = this.data.items.find(item => item.id == itemId);
if (item && item.data.data) {
let update = { _id: item.id, "data.equipped": !item.data.data.equipped };
await this.updateEmbeddedDocuments('Item', [update]); // Updates one EmbeddedEntity
}
}
/* -------------------------------------------- */
compareName(a, b) {
if (a.name < b.name) {
return -1;
}
if (a.name > b.name) {
return 1;
}
return 0;
}
/* -------------------------------------------- */
getAttribute(attrKey) {
return this.data.data.attributes[attrKey];
}
/* -------------------------------------------- */
async equipGear(equipmentId) {
let item = this.data.items.find(item => item.id == equipmentId);
if (item && item.data.data) {
let update = { _id: item.id, "data.equipped": !item.data.data.equipped };
await this.updateEmbeddedDocuments('Item', [update]); // Updates one EmbeddedEntity
}
}
/* -------------------------------------------- */
getSubActors() {
let subActors = [];
for (let id of this.data.data.subactors) {
subActors.push(duplicate(game.actors.get(id)));
}
return subActors;
}
/* -------------------------------------------- */
async addSubActor(subActorId) {
let subActors = duplicate(this.data.data.subactors);
subActors.push(subActorId);
await this.update({ 'data.subactors': subActors });
}
/* -------------------------------------------- */
async delSubActor(subActorId) {
let newArray = [];
for (let id of this.data.data.subactors) {
if (id != subActorId) {
newArray.push(id);
}
}
await this.update({ 'data.subactors': newArray });
}
/* -------------------------------------------- */
async incDecQuantity(objetId, incDec = 0) {
let objetQ = this.data.items.get(objetId)
if (objetQ) {
let newQ = objetQ.data.data.quantity + incDec;
const updated = await this.updateEmbeddedDocuments('Item', [{ _id: objetQ.id, 'data.quantity': newQ }]); // pdates one EmbeddedEntity
}
}
/* -------------------------------------------- */
getCommonRollData(statKey = undefined, useShield = false) {
let rollData = MournbladeUtility.getBasicRollData()
rollData.alias = this.name
rollData.actorImg = this.img
rollData.actorId = this.id
rollData.img = this.img
rollData.activePerks = duplicate(this.getActivePerks())
if (statKey) {
rollData.statKey = statKey
rollData.stat = this.getStat(statKey)
rollData.statDicesLevel = rollData.stat.value
rollData.statMod = rollData.stat.mod
rollData.specList = this.getRelevantSpec(statKey)
rollData.selectedSpec = "0"
}
this.addEffects(rollData)
this.addArmorsShields(rollData, statKey, useShield)
this.addWeapons(rollData, statKey, useShield)
this.addEquipments(rollData, statKey)
return rollData
}
/* -------------------------------------------- */
async startRoll(rollData) {
this.syncRoll(rollData);
//console.log("ROLL DATA", rollData)
let rollDialog = await MournbladeRollDialog.create(this, rollData);
console.log(rollDialog);
rollDialog.render(true);
}
}