This repository has been archived on 2024-10-22. You can view files and clone it, but cannot push or open issues or pull requests.
foundryvtt-beam-saber/module/blades-actor.js

222 lines
6.2 KiB
JavaScript
Raw Normal View History

import { bladesRoll } from "./blades-roll.js";
2020-08-05 13:20:52 +00:00
import { BladesHelpers } from "./blades-helpers.js";
/**
* Extend the basic Actor
* @extends {Actor}
*/
export class BladesActor extends Actor {
/** @override */
static async create(data, options={}) {
2022-09-03 15:44:26 +00:00
data.prototypeToken = data.prototypeToken || {};
// For Crew and Character set the Token to sync with charsheet.
switch (data.type) {
case 'character':
case 'crew':
case '\uD83D\uDD5B clock':
2022-09-03 15:44:26 +00:00
data.prototypeToken.actorLink = true;
break;
}
return super.create(data, options);
}
/** @override */
getRollData() {
2022-09-03 15:44:26 +00:00
const rollData = super.getRollData();
2022-09-03 15:44:26 +00:00
rollData.dice_amount = this.getAttributeDiceToThrow();
2022-09-03 15:44:26 +00:00
return rollData;
}
/* -------------------------------------------- */
/**
* Calculate Attribute Dice to throw.
*/
getAttributeDiceToThrow() {
// Calculate Dice to throw.
let dice_amount = {};
dice_amount['BITD.Vice'] = 4;
for (var attribute_name in this.system.attributes) {
dice_amount[attribute_name] = 0;
for (var skill_name in this.system.attributes[attribute_name].skills) {
2022-09-03 15:44:26 +00:00
dice_amount[skill_name] = parseInt(this.system.attributes[attribute_name].skills[skill_name]['value'][0])
// We add a +1d for every skill higher than 0.
if (dice_amount[skill_name] > 0) {
dice_amount[attribute_name]++;
}
}
// Vice dice roll uses lowest attribute dice amount
if (dice_amount[attribute_name] < dice_amount['BITD.Vice'] ) {
dice_amount['BITD.Vice'] = dice_amount[attribute_name];
}
}
return dice_amount;
}
/* -------------------------------------------- */
rollAttributePopup(attribute_name) {
2020-08-05 13:20:52 +00:00
// const roll = new Roll("1d20 + @abilities.wis.mod", actor.getRollData());
let attribute_label = BladesHelpers.getRollLabel(attribute_name);
2020-08-05 13:20:52 +00:00
2022-09-03 15:44:26 +00:00
let content = `
2020-08-05 13:20:52 +00:00
<h2>${game.i18n.localize('BITD.Roll')} ${game.i18n.localize(attribute_label)}</h2>
<form>
<div class="form-group">
<label>${game.i18n.localize('BITD.Modifier')}:</label>
<select id="mod" name="mod">
${this.createListOfDiceMods(-3,+3,0)}
</select>
</div>`;
if (BladesHelpers.isAttributeAction(attribute_name)) {
content += `
<div class="form-group">
<label>${game.i18n.localize('BITD.Position')}:</label>
<select id="pos" name="pos">
<option value="controlled">${game.i18n.localize('BITD.PositionControlled')}</option>
<option value="risky" selected>${game.i18n.localize('BITD.PositionRisky')}</option>
<option value="desperate">${game.i18n.localize('BITD.PositionDesperate')}</option>
</select>
</div>
<div class="form-group">
<label>${game.i18n.localize('BITD.Effect')}:</label>
<select id="fx" name="fx">
<option value="limited">${game.i18n.localize('BITD.EffectLimited')}</option>
<option value="standard" selected>${game.i18n.localize('BITD.EffectStandard')}</option>
<option value="great">${game.i18n.localize('BITD.EffectGreat')}</option>
</select>
</div>`;
2022-07-01 19:33:21 +00:00
} else {
content += `
<input id="pos" name="pos" type="hidden" value="">
<input id="fx" name="fx" type="hidden" value="">`;
2022-07-01 19:33:21 +00:00
}
content += `
<div className="form-group">
<label>${game.i18n.localize('BITD.Notes')}:</label>
<input id="note" name="note" type="text" value="">
</div><br/>
</form>
`;
2022-09-03 15:44:26 +00:00
new Dialog({
title: `${game.i18n.localize('BITD.Roll')} ${game.i18n.localize(attribute_label)}`,
content: content,
buttons: {
yes: {
icon: "<i class='fas fa-check'></i>",
2020-08-05 13:20:52 +00:00
label: game.i18n.localize('BITD.Roll'),
callback: async (html) => {
let modifier = parseInt(html.find('[name="mod"]')[0].value);
let position = html.find('[name="pos"]')[0].value;
let effect = html.find('[name="fx"]')[0].value;
let note = html.find('[name="note"]')[0].value;
await this.rollAttribute(attribute_name, modifier, position, effect, note);
}
},
no: {
icon: "<i class='fas fa-times'></i>",
2020-08-05 13:20:52 +00:00
label: game.i18n.localize('Close'),
},
},
default: "yes",
}).render(true);
}
/* -------------------------------------------- */
2022-09-03 15:44:26 +00:00
async rollAttribute(attribute_name = "", additional_dice_amount = 0, position, effect, note) {
let dice_amount = 0;
if (attribute_name !== "") {
let roll_data = this.getRollData();
dice_amount += roll_data.dice_amount[attribute_name];
}
else {
dice_amount = 1;
}
dice_amount += additional_dice_amount;
await bladesRoll(dice_amount, attribute_name, position, effect, note, this.system.stress.value);
}
/* -------------------------------------------- */
/**
* Create <options> for available actions
* which can be performed.
*/
createListOfActions() {
2022-09-03 15:44:26 +00:00
let text, attribute, skill;
2022-09-03 15:44:26 +00:00
let attributes = this.system.attributes;
for ( attribute in attributes ) {
2022-09-03 15:44:26 +00:00
const skills = attributes[attribute].skills;
text += `<optgroup label="${attribute} Actions">`;
text += `<option value="${attribute}">${attribute} (Resist)</option>`;
2022-09-03 15:44:26 +00:00
for ( skill in skills ) {
text += `<option value="${skill}">${skill}</option>`;
}
2022-09-03 15:44:26 +00:00
text += `</optgroup>`;
2022-09-03 15:44:26 +00:00
}
2022-09-03 15:44:26 +00:00
return text;
2022-09-03 15:44:26 +00:00
}
/* -------------------------------------------- */
/**
* Creates <options> modifiers for dice roll.
*
* @param {int} rs
* Min die modifier
2022-09-03 15:44:26 +00:00
* @param {int} re
* Max die modifier
* @param {int} s
* Selected die
*/
createListOfDiceMods(rs, re, s) {
2022-09-03 15:44:26 +00:00
var text = ``;
var i = 0;
2022-09-03 15:44:26 +00:00
if ( s == "" ) {
s = 0;
}
2022-09-03 15:44:26 +00:00
for ( i = rs; i <= re; i++ ) {
var plus = "";
if ( i >= 0 ) { plus = "+" };
text += `<option value="${i}"`;
if ( i == s ) {
text += ` selected`;
}
2022-09-03 15:44:26 +00:00
text += `>${plus}${i}d</option>`;
}
2022-09-03 15:44:26 +00:00
return text;
2022-09-03 15:44:26 +00:00
}
/* -------------------------------------------- */
}