Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions docs/product/features/combat-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -399,3 +399,66 @@ broad-phase / CCD は後続 Issue(#483 またはその sub-issue)で対応
| AC12 | 同一 tick 処理順序(projectile-enemy 先処理、次に player-enemy;同種内は id 昇順)が明記されている |
| AC13 | non-goals に campaign / upgrade / persistence / audio / network / VFX / broad-phase / CCD が含まれる |
| AC14 | `related_tests` に `tests/collision-system.test.ts` / `tests/combat-system.test.ts` が列挙されている |


---

## Combat Numeric Display Policy (Issue #581)

SSOT identifiers: `number_display_policy`, `formatCombatNumber` (see `src/render/renderUtils.ts`).

### `number_display_policy`

```yaml
number_display_policy:
hp:
input_domain: integer >= 0
invalid_input: "0 扱い(NaN / Infinity / 負数 / 非整数は 0 として表示)— レンダリング層のため throw より safe fallback を選択"
exact_display_until: 9999
compact_from: 10000
rounding: floor
unit_case: lower
examples:
0: "0"
999: "999"
9999: "9999"
10000: "10k"
999999: "999k"
1000000: "1M"
enemy_hp_label:
anchor: "enemy center x, enemy top y - 8px"
text_align: center
text_baseline: middle
x_clamp: "[padding + textWidth/2, arenaWidth - padding - textWidth/2]"
y_clamp: "[fontSize/2 + padding, arenaHeight - fontSize/2 - padding]"
font: "10px sans-serif"
color: "#ffffff"
padding: 2
```

### Abbreviation Rules

| Input Range | Format | Example |
|---|---|---|
| 0 to 9999 | exact | "0", "999", "9999" |
| 10000 to 999999 | floor(value / 1000) + "k" | "10k", "999k" |
| 1000000+ | floor(value / 1000000) + "M" | "1M" |

Floor rounding is used to avoid displaying HP as larger than actual.

### Canvas Bounds Clamp Policy

The HP label bounding box must remain within arena bounds regardless of enemy position.

Render pipeline: format -> measureText -> bounds clamp -> fillText.
`maxWidth` is NOT used (browser-dependent compression; no readability guarantee).

### Canvas State Protection

`drawEnemyHpLabel` uses `ctx.save()` / `ctx.restore()` to isolate font, fillStyle, textAlign, textBaseline.
No canvas state leaks to the caller.

### Future Numeric Labels

Score, timer, and other future numeric labels should also use `formatCombatNumber`.
Bulk unification is out of scope for this Issue (policy definition only).
15 changes: 15 additions & 0 deletions src/render/CanvasRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { GameState } from '../state'
import { drawEnemyHpLabel } from './renderUtils'

export interface CanvasRenderer {
render(state: GameState): void
Expand Down Expand Up @@ -140,6 +141,20 @@ export function createCanvasRenderer(canvas: HTMLCanvasElement): CanvasRenderer
context.fill()
}

// --- Layer 5b: enemy HP labels (above enemy circles, below projectiles) ---
for (const enemy of state.enemies) {
if (enemy.defeated) continue
drawEnemyHpLabel({
ctx: context,
enemyX: enemy.x,
enemyY: enemy.y,
enemyRadius: enemy.radius,
enemyHp: enemy.hp,
arenaWidth: arenaW,
arenaHeight: arenaH,
})
}

// --- Layer 6: projectiles ---
context.fillStyle = '#f4c25b'
for (const projectile of state.projectiles) {
Expand Down
187 changes: 187 additions & 0 deletions src/render/renderUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
/**
* renderUtils.ts — combat numeric display helpers (Issue #581)
*
* Exports:
* - formatCombatNumber: deterministic compact notation (number_display_policy SSOT)
* - drawEnemyHpLabel: measure -> clamp -> draw with save()/restore() isolation
*
* Design: format -> measureText -> bounds clamp -> draw
* maxWidth is NOT used (browser-dependent compression behaviour; no readability guarantee).
*/

// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------

/** Font used for enemy HP labels. */
export const HP_LABEL_FONT = '10px sans-serif'

/** Fill colour for enemy HP labels. */
export const HP_LABEL_COLOR = '#ffffff'

/** Minimum padding (logical px) between the label bounding box and arena edge. */
export const HP_LABEL_PADDING = 2

// ---------------------------------------------------------------------------
// formatCombatNumber (number_display_policy SSOT)
// ---------------------------------------------------------------------------

/**
* Format an integer HP value for canvas display.
*
* Policy (SSOT: docs/product/features/combat-core.md#combat-numeric-display-policy):
* 0 - 9999 -> exact string ("0", "999", "9999")
* 10000+ -> compact with floor rounding, lowercase unit
* 10000 -> "10k", 999999 -> "999k", 1000000 -> "1M"
*
* Input domain: integer >= 0.
* Invalid inputs (NaN, Infinity, negative, non-integer) are treated as 0.
* Safe fallback is used rather than throwing — this is a rendering-layer function
* (docs/product/features/combat-core.md number_display_policy: invalid input → 0).
* Output is locale-independent.
*/
export function formatCombatNumber(value: number): string {
if (!Number.isFinite(value) || !Number.isInteger(value) || value < 0) {
return '0'
}
if (value < 10_000) {
return String(Math.floor(value))
}
if (value < 1_000_000) {
return String(Math.floor(value / 1_000)) + 'k'
}
return String(Math.floor(value / 1_000_000)) + 'M'
}

// ---------------------------------------------------------------------------
// HP label layout helpers
// ---------------------------------------------------------------------------

/**
* Parameters for computing a clamped HP label position.
*/
export interface HpLabelParams {
/** Enemy center X in logical (CSS) pixels. */
enemyX: number
/** Enemy top Y in logical (CSS) pixels (= enemy.y - enemy.radius). */
enemyTopY: number
/** Measured text width from ctx.measureText (logical pixels). */
textWidth: number
/** Arena width in logical pixels. */
arenaWidth: number
/** Arena height in logical pixels. */
arenaHeight: number
/**
* Font size in logical pixels (used for vertical half-extent of bounding box).
* Default: 10 (matches HP_LABEL_FONT "10px sans-serif").
*/
fontSize?: number
/**
* Padding between label bounding box and arena edge (logical pixels).
* Default: HP_LABEL_PADDING (2).
*/
padding?: number
}

/**
* Compute the clamped (x, y) draw position so the label bounding box stays
* entirely within arena bounds.
*
* The (x, y) is the canvas fillText anchor when:
* textAlign = "center", textBaseline = "middle"
*
* Clamp logic (logical px):
* x: clamp to [padding + textWidth/2, arenaWidth - padding - textWidth/2]
* y: clamp to [fontSize/2 + padding, arenaHeight - fontSize/2 - padding]
*/
export function computeHpLabelPosition(params: HpLabelParams): { x: number; y: number } {
const {
enemyX,
enemyTopY,
textWidth,
arenaWidth,
arenaHeight,
fontSize = 10,
padding = HP_LABEL_PADDING,
} = params

// Anchor: enemy center x, enemy top y - 8px (above the enemy circle)
const anchorX = enemyX
const anchorY = enemyTopY - 8

const halfW = textWidth / 2
const halfH = fontSize / 2

const xMin = padding + halfW
const xMax = arenaWidth - padding - halfW
const yMin = halfH + padding
const yMax = arenaHeight - halfH - padding

const x = Math.max(xMin, Math.min(xMax, anchorX))
const y = Math.max(yMin, Math.min(yMax, anchorY))

return { x, y }
}

// ---------------------------------------------------------------------------
// drawEnemyHpLabel
// ---------------------------------------------------------------------------

/**
* Parameters for drawEnemyHpLabel.
*/
export interface DrawEnemyHpLabelParams {
/** 2D rendering context. */
ctx: CanvasRenderingContext2D
/** Enemy center X (logical px). */
enemyX: number
/** Enemy center Y (logical px). */
enemyY: number
/** Enemy radius (logical px) -- used to compute top Y. */
enemyRadius: number
/** Enemy current HP (integer >= 0). */
enemyHp: number
/** Arena width (logical px). */
arenaWidth: number
/** Arena height (logical px). */
arenaHeight: number
}

/**
* Draw the HP label for one enemy.
*
* Uses save()/restore() to isolate all canvas state changes:
* font, fillStyle, textAlign, textBaseline.
*
* Render pipeline: format -> measureText -> bounds clamp -> fillText
*/
export function drawEnemyHpLabel(params: DrawEnemyHpLabelParams): void {
const { ctx, enemyX, enemyY, enemyRadius, enemyHp, arenaWidth, arenaHeight } = params

const label = formatCombatNumber(enemyHp)

ctx.save()
try {
ctx.font = HP_LABEL_FONT
ctx.fillStyle = HP_LABEL_COLOR
ctx.textAlign = 'center'
ctx.textBaseline = 'middle'

const measured = ctx.measureText(label)
const textWidth = measured.width

const enemyTopY = enemyY - enemyRadius

const { x, y } = computeHpLabelPosition({
enemyX,
enemyTopY,
textWidth,
arenaWidth,
arenaHeight,
})

ctx.fillText(label, x, y)
} finally {
ctx.restore()
}
}
Loading
Loading