foundryvtt-shadows-over-sol/module/sos-dialog-combat-actions.js

151 lines
4.6 KiB
JavaScript
Raw Permalink Normal View History

2021-02-03 15:33:07 +01:00
import { SoSUtility } from "./sos-utility.js";
/* -------------------------------------------- */
export class SoSDialogCombatActions extends Dialog {
/* -------------------------------------------- */
2021-02-04 08:38:59 +01:00
static async create( combatId, combatantId, round, uniqId ) {
2021-02-09 23:32:55 +01:00
let combat = game.combats.get( combatId);
2021-02-03 15:33:07 +01:00
let combatActions = {
actionsList: await SoSUtility.loadCompendium( 'foundryvtt-shadows-over-sol.combat-actions' ),
actionPoints: SoSUtility.fillRange(0, 6),
2021-02-09 23:32:55 +01:00
combatId: combatId,
2021-02-04 08:38:59 +01:00
combatantId: combatantId,
2022-07-17 20:53:25 +02:00
combatantsList: combat.combatants,
2021-02-04 08:38:59 +01:00
uniqId: uniqId,
2021-02-03 15:33:07 +01:00
round: round
}
for ( let i=0; i<combatActions.actionsList.length; i++) {
if ( combatActions.actionsList[i].name == 'No Action') {
combatActions.noActionId = i;
}
}
//console.log("ACTIONS", combatActions.actionsList );
let html = await renderTemplate('systems/foundryvtt-shadows-over-sol/templates/dialog-combat-actions.html', combatActions);
2021-02-09 23:32:55 +01:00
return new SoSDialogCombatActions(combatActions, html , { width: 640, height: 320} );
2021-02-03 15:33:07 +01:00
}
/* -------------------------------------------- */
constructor(combatActions, html, options = {} ) {
let dialogConf = {
title: "Combat Actions",
content: html,
buttons: {
validate: {
icon: '<i class="fas fa-check"></i>',
label: "Validate",
callback: (html) => { this.validateActions(html) }
}
},
default: "validate"
};
super(dialogConf, options);
this.combatActions = combatActions;
}
/* -------------------------------------------- */
async close() {
let ap = Number($('#remain-ap').text());
if ( ap >= 0 ) {
super.close();
let action3Index = $('#action3').val();
let action3 = duplicate(this.combatActions.actionsList[action3Index]);
let action2Index = $('#action2').val();
2021-02-04 22:55:57 +01:00
let action2 = duplicate(this.combatActions.actionsList[action2Index]);
2021-02-03 15:33:07 +01:00
let action1Index = $('#action1').val();
2021-02-04 22:55:57 +01:00
let action1 = duplicate(this.combatActions.actionsList[action1Index]);
2021-02-09 23:32:55 +01:00
let combatant3Id = $('#combatant3').val();
let combatant2Id = $('#combatant2').val();
let combatant1Id = $('#combatant1').val();
2021-02-04 08:38:59 +01:00
let msgdata = {
combatId: this.combatActions.combatId,
combatantId: this.combatActions.combatantId,
uniqId: this.combatActions.uniqId,
userId: game.userId,
phaseArray: [ action1, action2, action3],
2021-02-09 23:32:55 +01:00
targetArray: [ combatant1Id, combatant2Id, combatant3Id],
2021-02-04 08:38:59 +01:00
remainingAP: ap
}
if ( game.user.isGM ) {
let combat = game.combats.get( this.combatActions.combatId ); // Get the associated combat
combat.setupActorActions( msgdata );
} else {
2021-02-17 22:38:12 +01:00
game.socket.emit("system.foundryvtt-shadows-over-sol", {
2021-02-04 08:38:59 +01:00
name: "msg_declare_actions", data: msgdata } );
}
2021-02-03 15:33:07 +01:00
} else {
ui.notifications.warn("Action Points are below 0 ! Please check your phases !");
}
}
/* -------------------------------------------- */
validateActions( html ) {
2021-02-04 08:38:59 +01:00
2021-02-03 15:33:07 +01:00
}
/* -------------------------------------------- */
resetAP() {
let maxAP = $('#actionMax').val();
$('#remain-ap').text( maxAP);
$('#action3').val( this.combatActions.noActionId );
$('#action2').val( this.combatActions.noActionId );
$('#action1').val( this.combatActions.noActionId );
$('#action3').prop('disabled', false);
$('#action2').prop('disabled', false);
$('#action1').prop('disabled', false);
}
/* -------------------------------------------- */
updateAP( ) {
let remainAP = $('#actionMax').val();
let action3Index = $('#action3').val();
if ( this.combatActions.actionsList[action3Index].name != 'No Action') {
remainAP -= 3;
}
let action2Index = $('#action2').val();
if ( this.combatActions.actionsList[action2Index].name != 'No Action') {
remainAP -= 2;
}
let action1Index = $('#action1').val();
if ( this.combatActions.actionsList[action1Index].name != 'No Action') {
remainAP -= 1;
}
$('#remain-ap').text( remainAP);
if ( remainAP < 0 ) {
$("#remain-ap").addClass("text-red");
ui.notifications.warn("No more Action Points ! Please check your phases !");
} else {
$("#remain-ap").removeClass("text-red");
}
}
/* -------------------------------------------- */
/** @override */
activateListeners(html) {
super.activateListeners(html);
html.find('#actionMax').change((event) => {
this.resetAP( );
});
html.find('.action-select').change((event) => {
this.updateAP( );
});
}
}