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

180 lines
7.3 KiB
JavaScript
Raw Normal View History

2021-02-03 15:33:07 +01:00
import { SoSCardDeck } from "./sos-card-deck.js";
import { SoSUtility } from "./sos-utility.js";
import { SoSFlipDialog } from "./sos-flip-dialog.js";
import { SoSDialogCombatActions } from "./sos-dialog-combat-actions.js";
/* -------------------------------------------- */
export class SoSCombat extends Combat {
/* -------------------------------------------- */
2021-02-04 22:55:57 +01:00
requestActions() {
if ( game.user.isGM && !this.actionsRequested) {
console.log("REQUEST ACTIONS !!!");
this.actionsRequested = true;
this.phaseSetup = {}; // Reset each new round/update
2021-02-04 08:38:59 +01:00
for( let combatant of this.combatants) {
2021-02-04 22:55:57 +01:00
this.setInitiative(combatant._id, -1 ); // Reset init
2021-02-04 08:38:59 +01:00
let uniq = randomID(16);
if ( combatant.players[0]) {
// A player controls this combatant -> message !
ChatMessage.create( { content: `New round ! Click on the button below to declare the actions of ${combatant.actor.data.name} for round ${this.round} !<br>
<a class='chat-card-button' id='button-declare-actions' data-uniq-id='${uniq}' data-combatant-id='${combatant._id}' data-combat-id='${this._id}' data-round='${this.round}'>Declare actions</a>`,
whisper: [ combatant.players[0].data._id] } );
} else {
ChatMessage.create( { content: `New round ! Click on the button below to declare the actions of ${combatant.actor.data.name} for round ${this.round} !<br>
<a class='chat-card-button' id='button-declare-actions' data-uniq-id='${uniq}' data-combatant-id='${combatant._id}' data-combat-id='${this._id}' data-round='${this.round}'>Declare actions</a>`,
whisper: [ ChatMessage.getWhisperRecipients("GM") ] } );
}
}
2021-02-03 15:33:07 +01:00
}
2021-02-04 22:55:57 +01:00
}
/* -------------------------------------------- */
async nextRound() {
this.actionsRequested = false;
2021-02-03 15:33:07 +01:00
super.nextRound();
}
2021-02-04 22:55:57 +01:00
/* -------------------------------------------- */
gotoNextTurn() {
this.phaseNumber -= 1;
if ( this.phaseNumber <= 0) {
2021-02-15 23:21:53 +01:00
this.applyConsequences();
2021-02-04 22:55:57 +01:00
this.nextRound(); // Auto-switch to next round
} else {
this.nextTurn();
}
}
/* -------------------------------------------- */
async nextTurn() {
2021-02-09 23:32:55 +01:00
console.log("Going to phase !", this.phaseNumber );
2021-02-04 22:55:57 +01:00
// Get all actions for this phase
let phaseIndex = this.phaseNumber - 1;
let actionList = [];
let actionMsg = `<h4>Actions for phase ${this.phaseNumber}</h4>`;
for (let combatantId in this.phaseSetup ) {
let actionData = this.phaseSetup[combatantId];
if ( actionData.phaseArray[phaseIndex].name != 'No Action' ) {
let combatant = this.combatants.find( comb => comb._id == actionData.combatantId);
actionList.push( { combatant: combatant,
action: actionData.phaseArray[phaseIndex],
isDone: false
});
actionMsg += `<br>${combatant.actor.name} is going to : ${actionData.phaseArray[phaseIndex].name}`;
}
}
if ( actionList.length == 0) {
actionMsg += "<br>No actions for the phase !";
this.gotoNextTurn();
}
// Display a nice message
ChatMessage.create( { content: actionMsg });
// Now push specific messages
for ( let action of actionList) {
let uniq = randomID(16);
action.uniqId = uniq; // Easy tracking with chat messages
if ( action.combatant.players[0]) {
// A player controls this combatant -> message !
ChatMessage.create( { content: `Phase ${this.phaseNumber} ! ${action.combatant.actor.data.name} must perform a <strong>${action.action.name}</strong> action.
When done, click on the button below to close the action.
<a class='chat-card-button' id='button-end-action' data-uniq-id='${uniq}' data-combatant-id='${action.combatant._id}' data-combat-id='${this._id}' data-round='${this.round}'>Action is done !</a>`,
whisper: [ action.combatant.players[0].data._id] } );
} else {
ChatMessage.create( { content: `Phase ${this.phaseNumber} ! ${action.combatant.actor.data.name} must perform a <strong>${action.action.name}</strong> action.<br>
When done, click on the button below to close the action.
<a class='chat-card-button' id='button-end-action' data-uniq-id='${uniq}' data-combatant-id='${action.combatant._id}' data-combat-id='${this._id}' data-round='${this.round}'>Action is done !</a>`,
whisper: [ ChatMessage.getWhisperRecipients("GM") ] } );
}
}
// Save for easy access
this.currentActions = actionList;
}
2021-02-15 23:21:53 +01:00
/* -------------------------------------------- */
applyConsequences( ) {
if (game.user.isGM ) {
for( let combatant of this.combatants) {
let bleeding = combatant.actor.data.items.find( item => item.type == 'consequence' && item.name == 'Bleeding');
combatant.actor.applyConsequenceWound( bleeding.severity, "bleeding" );
}
}
}
2021-02-04 22:55:57 +01:00
/* -------------------------------------------- */
closeAction( uniqId) {
let action = this.currentActions.find( _action => _action.uniqId == uniqId );
if (action) {
action.isDone = true;
let filtered = this.currentActions.filter( _action => action.isDone );
if ( filtered.length == this.currentActions.length) { // All actions closed !
console.log("Going next turn !!!");
this.gotoNextTurn();
}
}
}
2021-02-04 08:38:59 +01:00
/* -------------------------------------------- */
getPhaseRank( actionConf) {
for (let i=2; i>=0; i--) {
let action = actionConf.phaseArray[i];
if (action.name != "No Action") {
return i+1;
}
}
return 0;
}
2021-02-11 00:07:13 +01:00
/* -------------------------------------------- */
getAPFromActor( actorId ) {
for( let combatant of this.combatants) {
//console.log(combatant);
if ( combatant.actor.data._id == actorId ) {
let phase = this.phaseSetup[combatant._id];
return phase.remainingAP;
}
}
return 0;
}
2021-02-16 22:04:59 +01:00
/* -------------------------------------------- */
decreaseAPFromActor( actorId ) {
for( let combatant of this.combatants) {
//console.log(combatant);
if ( combatant.actor.data._id == actorId ) {
let phase = this.phaseSetup[combatant._id];
phase.remainingAP -= 1;
if ( phase.remainingAP < 0 ) phase.remainingAP = 0;
}
}
}
2021-02-03 15:33:07 +01:00
/* -------------------------------------------- */
2021-02-04 22:55:57 +01:00
async setupActorActions(actionConf) {
2021-02-04 08:38:59 +01:00
console.log("Setting combat for phase : ", actionConf);
2021-02-04 22:55:57 +01:00
if ( !this.phaseSetup) this.phaseSetup = {}; // Opportunistic init
2021-02-03 15:33:07 +01:00
// Keep track
2021-02-04 22:55:57 +01:00
this.phaseSetup[actionConf.combatantId] = actionConf;
2021-02-11 00:07:13 +01:00
console.log( this.combatants );
2021-02-04 22:55:57 +01:00
//let combatant = this.combatants.find( comb => comb._id == actionConf.combatantId);
await this.setInitiative( actionConf.combatantId, this.getPhaseRank( actionConf ) );
2021-02-04 08:38:59 +01:00
let actionsDone = true
for( let combatant of this.combatants) {
2021-02-04 22:55:57 +01:00
if ( combatant.initiative == -1 ) actionsDone = false;
2021-02-04 08:38:59 +01:00
}
if ( actionsDone ) {
2021-02-04 22:55:57 +01:00
this.actionsRequested = false;
2021-02-16 21:14:13 +01:00
ChatMessage.create( { content: `Action declaration has been completed ! Now proceeding with actions.`,
2021-02-04 08:38:59 +01:00
whisper: [ ChatMessage.getWhisperRecipients("GM") ] } );
2021-02-04 22:55:57 +01:00
this.phaseNumber = 3;
this.nextTurn();
2021-02-04 08:38:59 +01:00
}
2021-02-03 15:33:07 +01:00
}
}