fvtt-imperium5/modules/imperium5-actor.js

267 lines
7.9 KiB
JavaScript

/* -------------------------------------------- */
import { Imperium5Utility } from "./imperium5-utility.js";
import { Imperium5RollDialog } from "./imperium5-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 Imperium5Actor 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 (items) {
let actor = super.create(data, options);
return actor;
}
if (data.type == 'character') {
}
return super.create(data, options);
}
/* -------------------------------------------- */
prepareBaseData() {
}
/* -------------------------------------------- */
async prepareData() {
super.prepareData();
}
/* -------------------------------------------- */
prepareDerivedData() {
if (this.type == 'character') {
//this.system.encCapacity = this.getEncumbranceCapacity()
//this.buildContainerTree()
}
console.log("Acteur : ", this)
super.prepareDerivedData();
}
/* -------------------------------------------- */
_preUpdate(changed, options, user) {
super._preUpdate(changed, options, user);
}
/* -------------------------------------------- */
getArchetype() {
let item = duplicate( this.items.find( it => it.type == "archetype") || [])
return item
}
getSpecialites() {
let item = duplicate(this.items.filter( it => it.type == "specialite") || [] )
return item
}
getFamiliarites() {
let item = duplicate(this.items.filter( it => it.type == "familiarite") || [] )
return item
}
getNatureProfonde() {
let item = duplicate( this.items.find( it => it.type == "nature") || [])
return item
}
getTraits() {
let item = duplicate(this.items.filter( it => it.type == "trait") || [] )
return item
}
getSymbioses() {
let item = duplicate(this.items.filter( it => it.type == "symbiose") || [] )
return item
}
getEquipements() {
let item = duplicate(this.items.filter( it => it.type == "equipement") || [] )
return item
}
getCapacites() {
let item = duplicate(this.items.filter( it => it.type == "capacite") || [] )
return item
}
getUnusedCapacites(){
let item = this.items.filter( it => it.type == "capacite") || []
return item
}
getSingularites(){
let item = duplicate(this.items.filter( it => it.type == "singularite") || [] )
return item
}
getContacts(){
let item = duplicate(this.items.filter( it => it.type == "contact") || [] )
return item
}
/* -------------------------------------------- */
incDecKarma( value ) {
let karma = duplicate(this.system.karma)
karma.value += value
karma.value = Math.max(karma.value, 0)
this.update( { 'system.karma': karma})
}
/* -------------------------------------------- */
getItemById(id) {
let item = this.items.find(item => item.id == id)
if (item) {
item = duplicate(item)
}
return item;
}
/* -------------------------------------------- */
compareName(a, b) {
if (a.name < b.name) {
return -1;
}
if (a.name > b.name) {
return 1;
}
return 0;
}
/* -------------------------------------------- */
getActiveEffects(matching = it => true) {
let array = Array.from(this.getEmbeddedCollection("ActiveEffect").values());
return Array.from(this.getEmbeddedCollection("ActiveEffect").values()).filter(it => matching(it));
}
/* -------------------------------------------- */
getEffectByLabel(label) {
return this.getActiveEffects().find(it => it.label == label);
}
/* -------------------------------------------- */
getEffectById(id) {
return this.getActiveEffects().find(it => it.id == id);
}
/* -------------------------------------------- */
getInitiativeScore(combatId, combatantId) {
if (this.type == 'character') {
this.rollMR(true, combatId, combatantId)
}
console.log("Init required !!!!")
return -1;
}
/* -------------------------------------------- */
getSubActors() {
let subActors = [];
for (let id of this.system.subactors) {
subActors.push(duplicate(game.actors.get(id)))
}
return subActors;
}
/* -------------------------------------------- */
async addSubActor(subActorId) {
let subActors = duplicate(this.system.subactors);
subActors.push(subActorId);
await this.update({ 'data.subactors': subActors });
}
/* -------------------------------------------- */
async delSubActor(subActorId) {
let newArray = [];
for (let id of this.system.subactors) {
if (id != subActorId) {
newArray.push(id);
}
}
await this.update({ 'data.subactors': newArray });
}
/* -------------------------------------------- */
async deleteAllItemsByType(itemType) {
let items = this.items.filter(item => item.type == itemType);
await this.deleteEmbeddedDocuments('Item', items);
}
/* -------------------------------------------- */
async addItemWithoutDuplicate(newItem) {
let item = this.items.find(item => item.type == newItem.type && item.name.toLowerCase() == newItem.name.toLowerCase())
if (!item) {
await this.createEmbeddedDocuments('Item', [newItem]);
}
}
/* -------------------------------------------- */
async incDecQuantity(objetId, incDec = 0) {
let objetQ = this.items.get(objetId)
if (objetQ) {
let newQ = objetQ.system.quantity + incDec
if (newQ >= 0) {
const updated = await this.updateEmbeddedDocuments('Item', [{ _id: objetQ.id, 'data.quantity': newQ }]) // pdates one EmbeddedEntity
}
}
}
/* -------------------------------------------- */
/* ROLL SECTION
/* -------------------------------------------- */
/* -------------------------------------------- */
addEffects(rollData) {
let effects = this.items.filter(item => item.type == 'effect')
for (let effect of effects) {
effect = duplicate(effect)
if (!effect.system.hindrance
&& (effect.system.stataffected != "notapplicable" || effect.system.specaffected.length > 0)
&& effect.system.stataffected != "special") {
if (effect.system.effectstatlevel) {
effect.system.effectlevel = this.system.statistics[effect.system.effectstat].value
}
rollData.effectsList.push({ label: effect.name, type: "effect", applied: false, effect: effect, value: effect.system.effectlevel })
}
}
}
/* -------------------------------------------- */
rollAme( ameKey) {
let rollData = this.getCommonRollData()
rollData.ame = duplicate(this.system.ames[ameKey])
rollData.ameMalus = this.system.amestype[rollData.ame.type].malus
this.startRoll(rollData)
}
/* -------------------------------------------- */
getCommonRollData() {
let rollData = Imperium5Utility.getBasicRollData()
rollData.alias = this.name
rollData.actorImg = this.img
rollData.actorId = this.id
rollData.img = this.img
rollData.capacites = this.getUnusedCapacites()
rollData.karma = this.system.karma.value
return rollData
}
/* -------------------------------------------- */
async startRoll(rollData) {
let rollDialog = await Imperium5RollDialog.create(this, rollData)
console.log(rollDialog)
rollDialog.render(true)
}
}