175 lines
6.3 KiB
JavaScript
175 lines
6.3 KiB
JavaScript
import { PegasusUtility } from "./pegasus-utility.js";
|
|
import { PegasusActor } from "./pegasus-actor.js";
|
|
import { PegasusActorSheet } from "./pegasus-actor-sheet.js";
|
|
|
|
export class PegasusActorCreate {
|
|
|
|
/* -------------------------------------------- */
|
|
async start() {
|
|
this.actor = await Actor.create({
|
|
name: "New Actor",
|
|
type: "character"
|
|
});
|
|
this.actor.sheet.render(true);
|
|
|
|
const racesPack = await PegasusUtility.loadCompendium("fvtt-pegasus-rpg.race");
|
|
this.races = racesPack.map(i => i.toObject());
|
|
const rolesPack = await PegasusUtility.loadCompendium("fvtt-pegasus-rpg.role");
|
|
this.roles = rolesPack.map(i => i.toObject());
|
|
|
|
this.showRaces()
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
createFormData(step) {
|
|
let formData = {
|
|
name: this.actor.name,
|
|
img: this.actor.img,
|
|
step: step,
|
|
races: this.races,
|
|
roles: this.roles,
|
|
nboptionnal: this.raceOptionnalAbilities?.nboptionnal?? 0,
|
|
optionnalabilities: this.raceOptionnalAbilities?.optionnalabilities?? [],
|
|
}
|
|
if ( this.raceSelectableStats ) {
|
|
formData.numberstats = this.raceSelectableStats.numberstats;
|
|
formData.statsonlyonce = this.raceSelectableStats.statsonlyonce;
|
|
formData.stats = this.raceSelectableStats.stats;
|
|
}
|
|
return formData;
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
processChatEvent( event ) {
|
|
const step = $(event.currentTarget).data("step-name");
|
|
const itemId = $(event.currentTarget).data("item-id");
|
|
console.log("Create chat evet", event, itemId, step)
|
|
if ( step == "select-race") {
|
|
let race = this.races.find( item => item._id == itemId);
|
|
this.actor.applyRace( race);
|
|
PegasusUtility.removeChatMessageId(PegasusUtility.findChatMessageId(event.currentTarget));
|
|
if ( race.data.nboptionnal > 0 && race.data.optionnalabilities.length > 0) {
|
|
this.manageOptionnalAbilities(race);
|
|
} else {
|
|
if ( race.data.selectablestats ) {
|
|
this.manageSelectableStats(race);
|
|
} else {
|
|
this.showRoles()
|
|
}
|
|
}
|
|
}
|
|
|
|
if ( step == 'select-race-optionnal') {
|
|
let ability = this.raceOptionnalAbilities.optionnalabilities.find( item => item._id == itemId);
|
|
this.actor.createEmbeddedDocuments( 'Item', [ability]);
|
|
PegasusUtility.removeChatMessageId(PegasusUtility.findChatMessageId(event.currentTarget));
|
|
this.raceOptionnalAbilities.optionnalabilities = this.raceOptionnalAbilities.optionnalabilities.filter( item => item._id != itemId);
|
|
this.raceOptionnalAbilities.nboptionnal -= 1;
|
|
this.processOptionnalAbilitiesStep();
|
|
}
|
|
|
|
if (step == 'select-race-stats') {
|
|
let statKey = $(event.currentTarget).data("stat-key");
|
|
this.actor.modStat( statKey, 1);
|
|
this.raceSelectableStats.stats[statKey].used = true;
|
|
this.raceSelectableStats.numberstats -=1;
|
|
PegasusUtility.removeChatMessageId(PegasusUtility.findChatMessageId(event.currentTarget));
|
|
this.processSelectableStats();
|
|
}
|
|
|
|
if ( step == 'select-role') {
|
|
let role = this.roles.find( item => item._id == itemId);
|
|
this.actor.applyRole( role );
|
|
}
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
manageSelectableStats( race ) {
|
|
this.raceSelectableStats = {
|
|
"race": race,
|
|
"statsonlyonce": race.data.statsonlyonce,
|
|
"numberstats": race.data.numberstats,
|
|
"stats": duplicate(this.actor.data.data.statistics)
|
|
}
|
|
this.processSelectableStats()
|
|
}
|
|
|
|
/* --------------- ----------------------------- */
|
|
async processSelectableStats() {
|
|
// End of race options choice
|
|
if ( this.raceSelectableStats.numberstats == 0) {
|
|
this.showRoles()
|
|
return;
|
|
}
|
|
let formData = this.createFormData("select-race-stats")
|
|
let chatData = {
|
|
user: game.user.id,
|
|
alias : this.actor.name,
|
|
rollMode: game.settings.get("core", "rollMode"),
|
|
whisper: [game.user.id].concat( ChatMessage.getWhisperRecipients('GM') ),
|
|
content: await renderTemplate('systems/fvtt-pegasus-rpg/templates/chat-create-actor.html', formData)
|
|
};
|
|
//console.log("Apply damage chat", chatData );
|
|
await ChatMessage.create( chatData );
|
|
}
|
|
|
|
/* --------------- ----------------------------- */
|
|
manageOptionnalAbilities( race) {
|
|
this.raceOptionnalAbilities = {
|
|
"nboptionnal": race.data.nboptionnal,
|
|
"optionnalabilities": duplicate(race.data.optionnalabilities),
|
|
}
|
|
this.processOptionnalAbilitiesStep()
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
async processOptionnalAbilitiesStep() {
|
|
// End of race options choice
|
|
if ( this.raceOptionnalAbilities.nboptionnal == 0) {
|
|
if ( this.raceSelectableStats ) {
|
|
this.manageSelectableStats(this.raceSelectableStats.race);
|
|
} else {
|
|
this.showRoles()
|
|
}
|
|
}
|
|
let formData = this.createFormData("select-race-optionnal")
|
|
let chatData = {
|
|
user: game.user.id,
|
|
alias : this.actor.name,
|
|
rollMode: game.settings.get("core", "rollMode"),
|
|
whisper: [game.user.id].concat( ChatMessage.getWhisperRecipients('GM') ),
|
|
content: await renderTemplate('systems/fvtt-pegasus-rpg/templates/chat-create-actor.html', formData)
|
|
};
|
|
//console.log("Apply damage chat", chatData );
|
|
await ChatMessage.create( chatData );
|
|
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
async showRaces() {
|
|
let formData = this.createFormData("select-race")
|
|
let chatData = {
|
|
user: game.user.id,
|
|
alias : this.actor.name,
|
|
rollMode: game.settings.get("core", "rollMode"),
|
|
whisper: [game.user.id].concat( ChatMessage.getWhisperRecipients('GM') ),
|
|
content: await renderTemplate('systems/fvtt-pegasus-rpg/templates/chat-create-actor.html', formData)
|
|
};
|
|
//console.log("Apply damage chat", chatData );
|
|
await ChatMessage.create( chatData );
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
async showRoles() {
|
|
let formData = this.createFormData("select-role")
|
|
let chatData = {
|
|
user: game.user.id,
|
|
alias : this.actor.name,
|
|
rollMode: game.settings.get("core", "rollMode"),
|
|
whisper: [game.user.id].concat( ChatMessage.getWhisperRecipients('GM') ),
|
|
content: await renderTemplate('systems/fvtt-pegasus-rpg/templates/chat-create-actor.html', formData)
|
|
};
|
|
await ChatMessage.create( chatData );
|
|
}
|
|
}
|