V1.6.0: targets and an attack bar in encounters

- One combatant at a time can be marked as the target with the Target
  button on its card. The DM can mark anyone at any time; a player can
  while their own character is on turn in a battle.
- The target's card is outlined for everyone and names who is going for it.
- While a target is marked the DM gets an attack bar above the list: who is
  attacking whom, the target's AC and HP, the attacker's weapons, spells or
  monster attack, and a box to apply damage or healing straight away.
- The target is cleared when the turn or round moves on, when the encounter
  begins, ends or is reopened, or when the target is removed.
- All packages at version 1.6.0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-06 12:53:18 +02:00
co-authored by Claude Opus 5.5
parent ec48b65481
commit e4456b0df2
13 changed files with 316 additions and 11 deletions
+10
View File
@@ -750,6 +750,16 @@ export function usePatchEncounter(encounterId: string) {
}
type EncounterStatusInput = 'PLANNING' | 'ACTIVE' | 'COMPLETE';
// Marks a combatant as the target (null clears it). The DM at any time; a
// player while their own character is on turn.
export function useSetTarget(encounterId: string) {
const qc = useQueryClient();
return useMutation({
mutationFn: async (participantId: string | null) => (await apiClient.put(`/encounters/${encounterId}/target`, { participantId })).data,
onSuccess: () => qc.invalidateQueries({ queryKey: ['encounter', encounterId] }),
});
}
export function useNextTurn(encounterId: string) {
const qc = useQueryClient();
return useMutation({
+2
View File
@@ -447,6 +447,8 @@ export interface Encounter {
status: EncounterStatus;
currentRound: number;
activeParticipantId: string | null;
// The combatant marked as the target right now, for everyone to see.
targetParticipantId: string | null;
description: string | null;
dmNotes?: string | null; // DM only
// Where the scene is. A player is only told once their character has been there.
+35
View File
@@ -805,6 +805,41 @@ table.data-table tbody tr:hover {
.combatant.is-down {
opacity: 0.6;
}
/* The marked target stands out even when it is down or not on turn. */
.combatant.is-targeted {
opacity: 1;
border-color: var(--danger-strong);
box-shadow: 0 0 0 1px var(--danger-strong), 0 0 22px rgba(216, 84, 84, 0.3), var(--shadow-deep);
}
.target-marker {
color: var(--danger-strong);
font-family: var(--font-display);
font-size: 0.75rem;
text-transform: uppercase;
letter-spacing: 0.08em;
}
.btn-target {
padding: 0.15rem 0.6rem;
font-size: 0.8rem;
}
.btn-target.is-on {
border-color: var(--danger-strong);
color: var(--danger-strong);
background: var(--danger-soft);
}
/* The DM's attack bar stays in reach while scrolling the initiative order. */
.attack-bar {
position: sticky;
top: 4.5rem;
z-index: 20;
border-color: var(--danger-strong);
}
.attack-bar-title {
font-family: var(--font-display);
font-size: 1.1rem;
color: var(--text);
}
.combatant-name {
font-family: var(--font-display);
font-size: 1.1rem;
+137 -1
View File
@@ -14,6 +14,7 @@ import {
useRemoveParticipant,
useAdvanceRound,
useNextTurn,
useSetTarget,
usePatchEncounter,
useSetEncounterArchived,
useDeleteEncounter,
@@ -99,6 +100,8 @@ function CombatantCard({
isActive,
onPatch,
canRollInitiative,
targetedBy,
onTarget,
onRemove,
onError,
}: {
@@ -109,6 +112,12 @@ function CombatantCard({
// The DM for anyone; a player for their own character. The same goes for
// that character's death saves.
canRollInitiative: boolean;
// Set while this combatant is the target: who is going for them, or '' when
// nobody is on turn.
targetedBy?: string;
// Marks this combatant as the target, or clears it. Absent for someone who
// may not pick one right now.
onTarget?: () => void;
onRemove: () => void;
onError: (err: unknown) => void;
}) {
@@ -131,6 +140,7 @@ function CombatantCard({
const isDown = tracksHp && pct <= 0;
// A dying character still has something to do each turn, so its card stays lit.
const isDying = info?.deathSaves.state === 'DYING';
const isTargeted = targetedBy !== undefined;
// Players get a monster's name and health bar only; the stat block is the DM's.
const monsterStats = monster && monster.ac !== undefined ? monster : null;
const hasDetails = Boolean(info || monsterStats || npc?.dmNotes);
@@ -149,10 +159,11 @@ function CombatantCard({
}
return (
<div className={`card combatant combatant-${p.kind.toLowerCase()}${isActive ? ' is-active' : ''}${isDown && !isDying ? ' is-down' : ''}`}>
<div className={`card combatant combatant-${p.kind.toLowerCase()}${isActive ? ' is-active' : ''}${isDown && !isDying ? ' is-down' : ''}${isTargeted ? ' is-targeted' : ''}`}>
<div className="flex-between flex-wrap">
<div>
{isActive && <div className="turn-marker">▶ On turn</div>}
{isTargeted && <div className="target-marker">🎯 Targeted{targetedBy && ` by ${targetedBy}`}</div>}
<div className="flex-row flex-wrap">
<span className={`badge ${KIND_BADGES[p.kind]}`}>{KIND_LABELS[p.kind]}</span>
{p.character ? (
@@ -164,6 +175,16 @@ function CombatantCard({
)}
{npc && <span className={`badge badge-${npc.disposition.toLowerCase()}`}>{npc.disposition}</span>}
{isDown && !info && <span className="badge badge-hostile">Down</span>}
{onTarget && (
<button
className={`btn btn-target${isTargeted ? ' is-on' : ''}`}
aria-pressed={isTargeted}
title={isTargeted ? 'Stop targeting' : `Mark ${p.displayName} as the target`}
onClick={onTarget}
>
🎯 {isTargeted ? 'Targeted' : 'Target'}
</button>
)}
</div>
<div className="combatant-sub">{subline(p)}</div>
</div>
@@ -395,6 +416,107 @@ function CombatantCard({
);
}
// What a combatant hits with, one line per attack: a character's weapons
// (the equipped ones, when any are) and damaging spells, or the single attack
// of a monster or NPC.
function attackLines(p: EncounterParticipant): string[] {
const info = p.characterCombatInfo;
if (info) {
const equipped = info.weapons.filter((w) => w.equipped);
const weapons = (equipped.length ? equipped : info.weapons).map(
(w) => `${w.name}: ${fmtMod(w.attackBonus)} to hit · ${w.damageDice} ${fmtMod(w.damageBonus)} ${w.damageType}`
);
const spells = info.spells
.filter((s) => s.damageDice)
.map((s) => `${s.name}: ${s.damageDice}${info.spellcasting ? ` · ${fmtMod(info.spellcasting.attackBonus)} to hit / DC ${info.spellcasting.saveDC}` : ''}`);
return [...weapons, ...spells];
}
const stats = p.monster ?? p.npc;
if (!stats || (stats.attackBonus == null && !stats.damageDice)) return [];
return [[stats.attackBonus != null && `${fmtMod(stats.attackBonus)} to hit`, stats.damageDice].filter(Boolean).join(' · ')];
}
// The DM's shortcut once a target is marked: who is attacking whom, the
// numbers needed to call the hit, and a box to apply the damage without
// hunting for the target's card.
function AttackBar({
attacker,
target,
onApply,
onClear,
}: {
attacker: EncounterParticipant | undefined;
target: EncounterParticipant;
onApply: (currentHp: number) => void;
onClear: () => void;
}) {
const [amount, setAmount] = useState('');
const info = target.characterCombatInfo;
const ac = info?.armorClass ?? target.monster?.ac ?? target.npc?.ac;
const currentHp = target.currentHp ?? 0;
const tracksHp = (target.maxHp ?? 0) > 0;
const attacks = attacker && attacker.id !== target.id ? attackLines(attacker) : [];
function apply(sign: 1 | -1) {
const value = Number(amount);
if (!value) return;
onApply(currentHp + sign * value);
setAmount('');
}
return (
<div className="card attack-bar">
<div className="flex-between flex-wrap">
<div className="attack-bar-title">
{attacker && attacker.id !== target.id && <>{attacker.displayName} → </>}🎯 {target.displayName}
</div>
<button className="btn btn-ghost" onClick={onClear}>
✕ Clear target
</button>
</div>
<div className="stat-chips mt-sm">
{ac != null && <StatChip label="Target AC">{ac}</StatChip>}
{tracksHp && (
<StatChip label="Target HP">
{currentHp}/{target.maxHp}
{info && info.tempHp > 0 && ` +${info.tempHp}`}
</StatChip>
)}
</div>
{attacks.map((line) => (
<div key={line} className="combatant-sub mt-sm">
⚔ {line}
</div>
))}
{tracksHp ? (
<div className="flex-row flex-wrap gap-sm mt-sm">
{/* Remounted per target, so the cursor lands here as soon as one is marked. */}
<NumberInput
key={target.id}
autoFocus
min={1}
max={99999}
placeholder="Amount"
aria-label={`HP change for ${target.displayName}`}
value={amount}
onChange={(e) => setAmount(e.target.value)}
onKeyDown={(e) => e.key === 'Enter' && apply(-1)}
style={{ width: 90 }}
/>
<button className="btn btn-danger" onClick={() => apply(-1)} disabled={!amount}>
− Damage
</button>
<button className="btn" onClick={() => apply(1)} disabled={!amount}>
+ Heal
</button>
</div>
) : (
<p className="combatant-sub">{target.displayName} has no hit points to track.</p>
)}
</div>
);
}
function rewardLabel(r: EncounterReward): string {
if (r.type === 'GOLD') return `${r.gold} ${COIN_LABELS[r.currency ?? 'GOLD'].toLowerCase()}`;
const entry = r.type === 'WEAPON' ? r.weapon : r.item;
@@ -639,6 +761,7 @@ export function EncounterPage() {
const removeParticipant = useRemoveParticipant(encounterId);
const advanceRound = useAdvanceRound(encounterId);
const nextTurn = useNextTurn(encounterId);
const setTarget = useSetTarget(encounterId);
const patchEncounter = usePatchEncounter(encounterId);
const setArchived = useSetEncounterArchived(encounter?.campaignId ?? '');
const deleteEncounter = useDeleteEncounter(encounter?.campaignId ?? '');
@@ -655,6 +778,9 @@ export function EncounterPage() {
const isBattle = encounter.mode === 'BATTLE';
const participants = encounter.participants ?? []; // already in initiative order
const active = isBattle && encounter.status === 'ACTIVE' ? participants.find((p) => p.id === encounter.activeParticipantId) : undefined;
const target = participants.find((p) => p.id === encounter.targetParticipantId);
// The DM marks a target whenever they like; a player while their own character is on turn.
const canTarget = isDM || (active?.character != null && active.character.userId === user?.id);
const builtCharacters = characters?.filter((c) => c.buildStage === 'COMPLETE') ?? [];
const onError = (err: unknown) => setFormError(extractErrorMessage(err));
const fail = { onError };
@@ -683,6 +809,8 @@ export function EncounterPage() {
isActive={p.id === active?.id}
onPatch={(data) => patchParticipant.mutate({ participantId: p.id, ...data }, fail)}
canRollInitiative={isDM || (p.character != null && p.character.userId === user?.id)}
targetedBy={p.id === target?.id ? (active && active.id !== p.id ? active.displayName : '') : undefined}
onTarget={canTarget ? () => setTarget.mutate(p.id === target?.id ? null : p.id, fail) : undefined}
onRemove={() => removeParticipant.mutate(p.id, fail)}
onError={onError}
/>
@@ -819,6 +947,14 @@ export function EncounterPage() {
<div className="encounter-layout mt-md">
<div>
{isDM && target && (
<AttackBar
attacker={active}
target={target}
onApply={(currentHp) => patchParticipant.mutate({ participantId: target.id, currentHp }, fail)}
onClear={() => setTarget.mutate(null, fail)}
/>
)}
{participants.length === 0 && (
<div className="empty-state card">
{isDM