Skip to content

Commit

Permalink
Handle taking the turn out of order correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
Azurelol committed Mar 8, 2025
1 parent eb6ecd9 commit a9fea4c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 19 deletions.
2 changes: 2 additions & 0 deletions module/ui/combat-hud.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ export class CombatHUD extends Application {
isOwner: combatant.isOwner,
totalTurns: combatant.totalTurns,
token: combatant.token,
faction: combatant.faction,
effects: activeEffects,
img:
game.settings.get(SYSTEM, SETTINGS.optionCombatHudPortrait) === 'token'
Expand Down Expand Up @@ -337,6 +338,7 @@ export class CombatHUD extends Application {

html.find('a[data-action=start-turn]').click((event) => ui.combat.handleStartTurn(event));
html.find('a[data-action=end-turn]').click((event) => ui.combat.handleEndTurn(event));
html.find('a[data-action=take-turn-out-of-turn]').click((event) => ui.combat.handleTakeTurnOutOfTurn(event));
}

_doHudDragStart(event) {
Expand Down
3 changes: 1 addition & 2 deletions module/ui/combat-tracker.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export class FUCombatTracker extends CombatTracker {
if (data.turns.size === 0) {
console.error(`Found no available turns on combat ${combat}`);
}
data.factions = await this.getFactions(data.turns, combat);
}
return data;
}
Expand Down Expand Up @@ -145,7 +144,7 @@ export class FUCombatTracker extends CombatTracker {

async handleTakeTurnOutOfTurn(event) {
if (event.shiftKey) {
await this.handleEndTurn(event);
await this.handleStartTurn(event);
} else {
ui.notifications.info('FU.CombatTakeTurnOutOfTurn', { localize: true });
}
Expand Down
26 changes: 26 additions & 0 deletions module/ui/combat.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,37 @@ export class FUCombat extends Combat {
data.combatant = this.combatant;
// ID : Turns Left
data.turnsLeft = this.countTurnsLeft();
data.factions = this.getFactions();
// What faction's turn it is
data.currentTurn = this.getCurrentTurn();
data.outOfTurn = false;
data.round = this.round;
data.isGM = game.user.isGM;

console.debug(`Combat round ${this.round}, current faction ${data.currentTurn}`);
for (const combatant of this.combatants) {
console.debug(`- Combatant: ${combatant.name}, faction: ${combatant.faction}, isOwner: ${combatant.isOwner}`);
}
}

/**
* @param turns
* @param {FUCombat} combat
* @return {Object.<"friendly"|"neutral"|"hostile", {}[]>}
*/
getFactions() {
return this.combatants.reduce(
(agg, combatant) => {
//const combatant = combat.combatants.get(combatantData.id);
if (combatant.token.disposition === foundry.CONST.TOKEN_DISPOSITIONS.FRIENDLY) {
agg.friendly.push(combatant);
} else {
agg.hostile.push(combatant);
}
return agg;
},
{ friendly: [], hostile: [] },
);
}

/**
Expand Down
35 changes: 18 additions & 17 deletions templates/ui/partials/combat-hud-turn.hbs
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
{{#if @root.hasCombatStarted}}
{{! If the combatant HAS turns left }}
{{#if (and @root.round (lookup @root.turnsLeft this.id))}}
{{! If a combatant has already started a turn }}
{{#if @root.turnStarted}}
{{#if (eq @root.combatant.id this.id)}}
<a class="combatant-control take-turn" role="button" data-action="end-turn">
<i class="mats-o mats-fill">check_circle</i>
</a>
{{/if}}
{{! If a combatant can start a turn }}
{{else}}
<a class="combatant-control take-turn" role="button" data-action="start-turn">
<i class="mats-o mats-fill">counter_{{lookup @root.turnsLeft this.id}}</i>
</a>
{{/if}}
{{else}}
{{#if @root.outOfTurn}}
{{#if this.owner}}
<a class="combatant-control take-turn out-of-turn"
data-action="take-turn-out-of-turn">
{{# if (gt (lookup @root.totalTurns this.id) 1)}}
<i class="mats-o">counter_{{lookup @root.turnsLeft this.id}}</i>
{{else}}
<i class="mats-o">check_circle</i>
{{/if}}
{{! If the combatant's faction CAN take a turn}}
{{#if (eq @root.currentTurn this.faction)}}
<a class="combatant-control take-turn" role="button" data-action="start-turn">
<i class="mats-o mats-fill">counter_{{lookup @root.turnsLeft this.id}}</i>
</a>
{{! If the turn is taken out of order due to some mechanic }}
{{else}}
<span class="combatant-control take-turn">
<i class="mats-o">check_circle</i>
</span>
{{#if this.isOwner}}
<a class="combatant-control take-turn out-of-turn" data-action="take-turn-out-of-turn">
<i class="mats-o">counter_{{lookup @root.turnsLeft this.id}}</i>
</a>
{{/if}}
{{/if}}
{{/if}}
{{! If the combatant has NO turns left }}
{{else}}
<a class="combatant-control" style="opacity: 0.5">
<i class="mats-o mats-fill">check_circle</i>
</a>
{{/if}}
{{/if}}

0 comments on commit a9fea4c

Please sign in to comment.