Enhance combat
This commit is contained in:
parent
63e552cc6a
commit
92f8fe4ee8
@ -7,11 +7,13 @@ import { SoSDialogCombatActions } from "./sos-dialog-combat-actions.js";
|
||||
export class SoSCombat extends Combat {
|
||||
|
||||
/* -------------------------------------------- */
|
||||
async nextRound() {
|
||||
console.log("NEXT ROUND !!!!");
|
||||
|
||||
if ( game.user.isGM ) {
|
||||
requestActions() {
|
||||
if ( game.user.isGM && !this.actionsRequested) {
|
||||
console.log("REQUEST ACTIONS !!!");
|
||||
this.actionsRequested = true;
|
||||
this.phaseSetup = {}; // Reset each new round/update
|
||||
for( let combatant of this.combatants) {
|
||||
this.setInitiative(combatant._id, -1 ); // Reset init
|
||||
let uniq = randomID(16);
|
||||
if ( combatant.players[0]) {
|
||||
// A player controls this combatant -> message !
|
||||
@ -25,15 +27,89 @@ export class SoSCombat extends Combat {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
async nextRound() {
|
||||
this.actionsRequested = false;
|
||||
super.nextRound();
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
gotoNextTurn() {
|
||||
this.phaseNumber -= 1;
|
||||
if ( this.phaseNumber <= 0) {
|
||||
this.nextRound(); // Auto-switch to next round
|
||||
} else {
|
||||
this.nextTurn();
|
||||
}
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
async nextTurn() {
|
||||
console.log("Goingo to phase !", this.phaseNumber );
|
||||
// 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;
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
getPhaseRank( actionConf) {
|
||||
for (let i=2; i>=0; i--) {
|
||||
let action = actionConf.phaseArray[i];
|
||||
if (action.name != "No Action") {
|
||||
console.log("Init is : ", i+1);
|
||||
return i+1;
|
||||
}
|
||||
}
|
||||
@ -41,25 +117,27 @@ export class SoSCombat extends Combat {
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
setupActorActions(actionConf) {
|
||||
async setupActorActions(actionConf) {
|
||||
console.log("Setting combat for phase : ", actionConf);
|
||||
|
||||
if ( !this.phaseSetup) this.phaseSetup = []; // Opportunistic init
|
||||
if ( !this.phaseSetup[this.round] ) this.phaseSetup[this.round] = {}; // Bis
|
||||
if ( !this.phaseSetup) this.phaseSetup = {}; // Opportunistic init
|
||||
|
||||
// Keep track
|
||||
this.phaseSetup[this.round][actionConf.combatantId] = actionConf;
|
||||
this.phaseSetup[actionConf.combatantId] = actionConf;
|
||||
console.log( this.combatants);
|
||||
let combatant = this.combatants.find( comb => comb._id == actionConf.combatantId);
|
||||
this.setInitiative( actionConf.combatantId, this.getPhaseRank( actionConf ) );
|
||||
//let combatant = this.combatants.find( comb => comb._id == actionConf.combatantId);
|
||||
await this.setInitiative( actionConf.combatantId, this.getPhaseRank( actionConf ) );
|
||||
|
||||
let actionsDone = true
|
||||
for( let combatant of this.combatants) {
|
||||
if ( !combatant.initiative ) actionsDone = false;
|
||||
if ( combatant.initiative == -1 ) actionsDone = false;
|
||||
}
|
||||
if ( actionsDone ) {
|
||||
this.actionsRequested = false;
|
||||
ChatMessage.create( { content: `Action phase has been completed ! Now proceeding with actions.`,
|
||||
whisper: [ ChatMessage.getWhisperRecipients("GM") ] } );
|
||||
this.phaseNumber = 3;
|
||||
this.nextTurn();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -53,9 +53,9 @@ export class SoSDialogCombatActions extends Dialog {
|
||||
let action3Index = $('#action3').val();
|
||||
let action3 = duplicate(this.combatActions.actionsList[action3Index]);
|
||||
let action2Index = $('#action2').val();
|
||||
let action2 = duplicate(this.combatActions.actionsList[action2Index]._id);
|
||||
let action2 = duplicate(this.combatActions.actionsList[action2Index]);
|
||||
let action1Index = $('#action1').val();
|
||||
let action1 = duplicate(this.combatActions.actionsList[action1Index]._id);
|
||||
let action1 = duplicate(this.combatActions.actionsList[action1Index]);
|
||||
|
||||
let msgdata = {
|
||||
combatId: this.combatActions.combatId,
|
||||
|
@ -57,6 +57,10 @@ Hooks.once("init", async function () {
|
||||
Hooks.on('renderChatLog', (log, html, data) => {
|
||||
SoSUtility.registerChatCallbacks(html);
|
||||
});
|
||||
// Init/registers
|
||||
Hooks.on('updateCombat', (combat, round, diff, id) => {
|
||||
SoSUtility.updateCombat(combat, round, diff, id);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
@ -30,9 +30,15 @@ import { SoSDialogCombatActions } from "./sos-dialog-combat-actions.js";
|
||||
/* -------------------------------------------- */
|
||||
onSocketMesssage( msg ) {
|
||||
if (msg.name == 'msg_declare_actions' ) {
|
||||
if (game.user.isGM) {
|
||||
let combat = game.combats.get( msg.data.combatId); // Get the associated combat
|
||||
combat.setupActorActions( msg.data );
|
||||
}
|
||||
} else if (msg.name == 'msg_close_action') {
|
||||
if (game.user.isGM) {
|
||||
game.combat.closeAction( msg.data.uniqId );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
@ -60,6 +66,11 @@ import { SoSDialogCombatActions } from "./sos-dialog-combat-actions.js";
|
||||
return list;
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
static updateCombat(combat, round, diff, id) {
|
||||
combat.requestActions();
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
static async openDeclareActions( event) {
|
||||
event.preventDefault();
|
||||
@ -71,12 +82,29 @@ import { SoSDialogCombatActions } from "./sos-dialog-combat-actions.js";
|
||||
d.render(true);
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
static closeAction(event) {
|
||||
let uniqId = event.currentTarget.attributes['data-uniq-id'].value;
|
||||
// Delete message !
|
||||
const toDelete = game.messages.filter(it => it.data.content.includes( uniqId ));
|
||||
toDelete.forEach(it => it.delete());
|
||||
|
||||
if ( game.user.isGM ) {
|
||||
game.combat.closeAction( uniqId );
|
||||
} else {
|
||||
game.socket.emit("system.foundryvtt-reve-de-dragon", {
|
||||
name: "msg_close_action", data: { uniqId: uniqId} } );
|
||||
}
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
static async registerChatCallbacks(html) {
|
||||
html.on("click", '#button-declare-actions', event => {
|
||||
SoSUtility.openDeclareActions( event );
|
||||
});
|
||||
|
||||
html.on("click", '#button-end-action', event => {
|
||||
SoSUtility.closeAction( event );
|
||||
});
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user