fvtt-vadentis/modules/vadentis-actor.js

175 lines
6.8 KiB
JavaScript
Raw Normal View History

2021-04-01 21:18:36 +02:00
import { VadentisUtility } from "./vadentis-utility.js";
/* -------------------------------------------- */
/**
* Extend the base Actor entity by defining a custom roll data structure which is ideal for the Simple system.
* @extends {Actor}
*/
export class VadentisActor 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;
}
2021-04-01 22:35:03 +02:00
/*data.items = [];
2021-04-01 21:18:36 +02:00
let compendiumName = "foundryvtt-vadentis.competences";
if ( compendiumName ) {
let skills = await SoSUtility.loadCompendium(compendiumName);
data.items = data.items.concat( skills );
2021-04-01 22:35:03 +02:00
}*/
2021-04-01 21:18:36 +02:00
return super.create(data, options);
}
/* -------------------------------------------- */
async prepareData() {
super.prepareData();
2021-04-02 14:59:58 +02:00
}
/* -------------------------------------------- */
getCompetences() {
return this.data.items.filter( item => item.type == 'competence');
}
2021-04-01 21:18:36 +02:00
2021-04-02 14:59:58 +02:00
/* -------------------------------------------- */
getDonnees() {
return this.data.items.filter( item => item.type == 'donnee');
2021-04-01 21:18:36 +02:00
}
2021-04-02 16:47:38 +02:00
/* -------------------------------------------- */
getSorts() {
return this.data.items.filter( item => item.type == 'sort');
}
/* -------------------------------------------- */
getDevotions() {
return this.data.items.filter( item => item.type == 'devotion');
}
2021-04-02 14:59:58 +02:00
/* -------------------------------------------- */
async updateCompetence( name, field, value) {
let competence = this.data.items.find( item => item.type == 'competence' && item.name == name);
if (competence) {
let dataPath = 'data.'+field;
await this.updateOwnedItem( { _id: competence._id, [dataPath]:value });
}
}
2021-04-02 16:47:38 +02:00
/* -------------------------------------------- */
rollSort( sortId ) {
let sort = this.data.items.find( item => item.type == 'sort' && item._id == sortId );
if ( sort ) {
if ( sort.data.pe > this.data.data.stats.pointsenergie.value) { // Vérification du ~ de points d'énergie
ChatMessage.create({ content: `Vous n'avez pas assez de Points d'Energie pour lancer votre sort ${sort.name}` } );
return;
}
let magieElementaire = this.data.data.magie['matriseelementaire'];
let statValue = magieElementaire.base + magieElementaire.malus + magieElementaire.bonus;
let formulaFull = `1d20+${magieElementaire.base}+${magieElementaire.malus}+${magieElementaire.bonus}`;
let myRoll = new Roll("1d20+"+statValue);
myRoll.evaluate();
myRoll.toMessage( { flavor: `Lancer de Sort : ${sort.name} (${formulaFull})` } );
if (myRoll.total >= sort.data.difficulty) {
let content = `Votre sort ${sort.name} a réussi ! Vous perdez ${sort.data.pe} Points d'Energie et votre sort produit l'effet : <br>${sort.data.effect}`;
2021-04-02 17:31:18 +02:00
let newEnergie = this.data.data.stats.pointsenergie - sort.data.pe;
await this.update( {'data.stats.pointsenergie': newEnergie });
2021-04-02 16:47:38 +02:00
if ( sort.data.damage != "") {
if (myRoll.results[0] == 20 ) { // Critique ?
content += `<br>Et provoque les dégats critiques suivants : ${sort.data.damagecritical}`;
} else {
content += `<br>Et provoque les dégats suivants : ${sort.data.damage}`;
}
}
ChatMessage.create({ content:content} );
} else {
ChatMessage.create({ content: `Votre sort ${sort.name} a échoué !`});
}
}
}
/* -------------------------------------------- */
rollDevotion( devotionId ) {
let devotion = this.data.items.find( item => item.type == 'devotion' && item._id == devotionId );
if ( devotion ) {
if ( devotion.data.pe > this.data.data.stats.pointsenergie.value) { // Vérification du ~ de points d'énergie
ChatMessage.create({ content: `Vous n'avez pas assez de Points d'Energie pour lancer votre dévotion ${devotion.name}` } );
return;
}
let devotionComp = this.data.data.magie['devotion'];
let statValue = devotionComp.base + devotionComp.malus + devotionComp.bonus;
let formulaFull = `1d20+${devotionComp.base}+${devotionComp.malus}+${devotionComp.bonus}`;
let myRoll = new Roll("1d20+"+statValue);
myRoll.evaluate();
myRoll.toMessage( { flavor: `Lancer de Dévotion : ${devotion.name} (${formulaFull})` } );
if (myRoll.total >= devotion.data.difficulty) {
let content = `Votre dévotion ${devotion.name} a réussie ! Vous perdez ${devotion.data.pe} Points d'Energie et votre dévotion produit l'effet : <br>${devotion.data.effect}`;
2021-04-02 17:31:18 +02:00
let newEnergie = this.data.data.stats.pointsenergie - devotion.data.pe;
await this.update( {'data.stats.pointsenergie': newEnergie });
2021-04-02 16:47:38 +02:00
if ( devotion.data.damage != "") {
if (myRoll.results[0] == 20 ) { // Critique ?
content += `<br>Et provoque les dégats critiques suivants : ${devotion.data.damagecritical}`;
} else {
content += `<br>Et provoque les dégats suivants : ${devotion.data.damage}`;
}
}
ChatMessage.create({ content:content} );
} else {
ChatMessage.create({ content: `Votre dévotion ${devotion.name} a échoué !`});
}
}
}
2021-04-02 14:59:58 +02:00
/* -------------------------------------------- */
rollCompetence( competenceId ) {
let competence = this.data.items.find( item => item.type == 'competence' && item._id == competenceId);
if ( competence) {
let statValue = competence.data.base + competence.data.malus + competence.data.bonus;
let formulaFull = `1d20+${competence.data.base}+${competence.data.malus}+${competence.data.bonus}`;
let myRoll = new Roll("1d20+"+statValue);
myRoll.evaluate();
myRoll.toMessage( { flavor: `Jet de compétence : ${competence.name} (${formulaFull})` } );
}
}
/* -------------------------------------------- */
rollCombat( combatName ) {
let stat = this.data.data.combat[combatName];
let statValue = stat.base + stat.malus + stat.bonus;
let formulaFull = `1d20+${stat.base}+${stat.malus}+${stat.bonus}`;
let myRoll = new Roll("1d20+"+statValue);
myRoll.evaluate();
myRoll.toMessage( { flavor: `Jet de ${stat.label} (${formulaFull})` } );
}
2021-04-01 21:18:36 +02:00
2021-04-02 14:59:58 +02:00
2021-04-01 21:18:36 +02:00
}