fvtt-imperium5/modules/imperium5-actor.js

267 lines
7.9 KiB
JavaScript
Raw Normal View History

2022-03-13 16:17:04 +01:00
/* -------------------------------------------- */
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}
*/
2022-03-19 09:30:00 +01:00
export class Imperium5Actor extends Actor {
2022-03-13 16:17:04 +01:00
/* -------------------------------------------- */
/**
* 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
2022-10-19 17:30:47 +02:00
if (items) {
2022-03-13 16:17:04 +01:00
let actor = super.create(data, options);
return actor;
}
if (data.type == 'character') {
2022-03-19 09:30:00 +01:00
2022-03-13 16:17:04 +01:00
}
return super.create(data, options);
}
/* -------------------------------------------- */
prepareBaseData() {
}
/* -------------------------------------------- */
async prepareData() {
super.prepareData();
}
/* -------------------------------------------- */
prepareDerivedData() {
if (this.type == 'character') {
2022-10-19 17:30:47 +02:00
//this.system.encCapacity = this.getEncumbranceCapacity()
2022-03-19 09:30:00 +01:00
//this.buildContainerTree()
2022-03-13 16:17:04 +01:00
}
2022-03-19 09:30:00 +01:00
console.log("Acteur : ", this)
2022-03-13 16:17:04 +01:00
super.prepareDerivedData();
}
/* -------------------------------------------- */
_preUpdate(changed, options, user) {
super._preUpdate(changed, options, user);
}
2022-03-19 09:30:00 +01:00
2022-03-13 16:17:04 +01:00
/* -------------------------------------------- */
2022-03-19 09:30:00 +01:00
getArchetype() {
2022-10-19 17:30:47 +02:00
let item = duplicate( this.items.find( it => it.type == "archetype") || [])
2022-03-19 09:30:00 +01:00
return item
2022-03-13 16:17:04 +01:00
}
2022-03-19 09:30:00 +01:00
getSpecialites() {
2022-10-19 17:30:47 +02:00
let item = duplicate(this.items.filter( it => it.type == "specialite") || [] )
2022-03-19 09:30:00 +01:00
return item
2022-03-13 16:17:04 +01:00
}
2022-03-19 09:30:00 +01:00
getFamiliarites() {
2022-10-19 17:30:47 +02:00
let item = duplicate(this.items.filter( it => it.type == "familiarite") || [] )
2022-03-19 09:30:00 +01:00
return item
2022-03-13 16:17:04 +01:00
}
2022-03-22 09:01:20 +01:00
getNatureProfonde() {
2022-10-19 17:30:47 +02:00
let item = duplicate( this.items.find( it => it.type == "nature") || [])
2022-03-22 09:01:20 +01:00
return item
}
getTraits() {
2022-10-19 17:30:47 +02:00
let item = duplicate(this.items.filter( it => it.type == "trait") || [] )
2022-03-22 09:01:20 +01:00
return item
}
getSymbioses() {
2022-10-19 17:30:47 +02:00
let item = duplicate(this.items.filter( it => it.type == "symbiose") || [] )
2022-03-22 09:01:20 +01:00
return item
}
getEquipements() {
2022-10-19 17:30:47 +02:00
let item = duplicate(this.items.filter( it => it.type == "equipement") || [] )
2022-03-22 09:01:20 +01:00
return item
}
getCapacites() {
2022-10-19 17:30:47 +02:00
let item = duplicate(this.items.filter( it => it.type == "capacite") || [] )
return item
}
getUnusedCapacites(){
let item = this.items.filter( it => it.type == "capacite") || []
2022-03-22 09:01:20 +01:00
return item
}
getSingularites(){
2022-10-19 17:30:47 +02:00
let item = duplicate(this.items.filter( it => it.type == "singularite") || [] )
2022-03-22 09:01:20 +01:00
return item
}
getContacts(){
2022-10-19 17:30:47 +02:00
let item = duplicate(this.items.filter( it => it.type == "contact") || [] )
2022-03-22 09:01:20 +01:00
return item
}
2022-03-13 16:17:04 +01:00
2022-10-19 17:30:47 +02:00
/* -------------------------------------------- */
incDecKarma( value ) {
let karma = duplicate(this.system.karma)
karma.value += value
karma.value = Math.max(karma.value, 0)
this.update( { 'system.karma': karma})
}
2022-03-13 16:17:04 +01:00
/* -------------------------------------------- */
getItemById(id) {
2022-10-19 17:30:47 +02:00
let item = this.items.find(item => item.id == id)
2022-03-13 16:17:04 +01:00
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) {
2022-10-19 17:30:47 +02:00
return this.getActiveEffects().find(it => it.label == label);
2022-03-13 16:17:04 +01:00
}
/* -------------------------------------------- */
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 = [];
2022-10-19 17:30:47 +02:00
for (let id of this.system.subactors) {
2022-03-13 16:17:04 +01:00
subActors.push(duplicate(game.actors.get(id)))
}
return subActors;
}
/* -------------------------------------------- */
async addSubActor(subActorId) {
2022-10-19 17:30:47 +02:00
let subActors = duplicate(this.system.subactors);
2022-03-13 16:17:04 +01:00
subActors.push(subActorId);
await this.update({ 'data.subactors': subActors });
}
/* -------------------------------------------- */
async delSubActor(subActorId) {
let newArray = [];
2022-10-19 17:30:47 +02:00
for (let id of this.system.subactors) {
2022-03-13 16:17:04 +01:00
if (id != subActorId) {
newArray.push(id);
}
}
await this.update({ 'data.subactors': newArray });
}
/* -------------------------------------------- */
async deleteAllItemsByType(itemType) {
2022-10-19 17:30:47 +02:00
let items = this.items.filter(item => item.type == itemType);
2022-03-13 16:17:04 +01:00
await this.deleteEmbeddedDocuments('Item', items);
}
/* -------------------------------------------- */
async addItemWithoutDuplicate(newItem) {
2022-10-19 17:30:47 +02:00
let item = this.items.find(item => item.type == newItem.type && item.name.toLowerCase() == newItem.name.toLowerCase())
2022-03-13 16:17:04 +01:00
if (!item) {
await this.createEmbeddedDocuments('Item', [newItem]);
}
}
/* -------------------------------------------- */
async incDecQuantity(objetId, incDec = 0) {
2022-10-19 17:30:47 +02:00
let objetQ = this.items.get(objetId)
2022-03-13 16:17:04 +01:00
if (objetQ) {
2022-10-19 17:30:47 +02:00
let newQ = objetQ.system.quantity + incDec
2022-03-13 16:17:04 +01:00
if (newQ >= 0) {
const updated = await this.updateEmbeddedDocuments('Item', [{ _id: objetQ.id, 'data.quantity': newQ }]) // pdates one EmbeddedEntity
}
}
}
/* -------------------------------------------- */
/* ROLL SECTION
/* -------------------------------------------- */
/* -------------------------------------------- */
addEffects(rollData) {
2022-10-19 17:30:47 +02:00
let effects = this.items.filter(item => item.type == 'effect')
2022-03-13 16:17:04 +01:00
for (let effect of effects) {
effect = duplicate(effect)
2022-10-19 17:30:47 +02:00
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
2022-03-13 16:17:04 +01:00
}
2022-10-19 17:30:47 +02:00
rollData.effectsList.push({ label: effect.name, type: "effect", applied: false, effect: effect, value: effect.system.effectlevel })
2022-03-13 16:17:04 +01:00
}
}
}
/* -------------------------------------------- */
2022-10-19 17:30:47 +02:00
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() {
2022-03-19 09:30:00 +01:00
let rollData = Imperium5Utility.getBasicRollData()
2022-03-13 16:17:04 +01:00
rollData.alias = this.name
rollData.actorImg = this.img
rollData.actorId = this.id
rollData.img = this.img
2022-10-19 17:30:47 +02:00
rollData.capacites = this.getUnusedCapacites()
rollData.karma = this.system.karma.value
2022-03-13 16:17:04 +01:00
return rollData
}
/* -------------------------------------------- */
async startRoll(rollData) {
2022-03-19 09:30:00 +01:00
let rollDialog = await Imperium5RollDialog.create(this, rollData)
console.log(rollDialog)
rollDialog.render(true)
2022-03-13 16:17:04 +01:00
}
}