diff --git a/public/js/render/units.js b/public/js/render/units.js index ae4834b5..adf55a38 100644 --- a/public/js/render/units.js +++ b/public/js/render/units.js @@ -228,6 +228,38 @@ export const ALIAS_TINT = Object.freeze({ enemy_1305_mhslim: 0xffc48a, enemy_130 * between `pulse` and 1 at `pulseHz` (real time). */ export const EL_BAR = Object.freeze({ icon: 0.15, min: 8, max: 15, enemy: 0.8, gap: 1, pulse: 0.45, pulseHz: 1.5 }); + +/** The share of a model's height a bar is placed above (the head is not the very top of the silhouette). */ +export const HEAD_TOP_K = 0.92; +/** Enemies whose model is taller than this many tiles keep their bar at the cap — a bar must stay on screen. */ +export const HEAD_TILES_CAP = 2.2; +export const HEAD_TILES_CAP_BOSS = 3.2; + +/** + * Tiles above the feet where a unit's HP bar / name tag sits. + * + * Operators and tokens are uniform chibis (UNIT.headroom). Enemies vary: with a known model they use **how far the model + * reaches above its own origin** (spine.js `top` = `bounds.y + bounds.height`, not the box's raw height — a body whose + * origin sits mid-model, 逐腐兽, would otherwise push its bar ~a tile too high), scaled by the official model factor and + * the vertical-stretch quirk; without bounds they fall back to the chibi headroom × that factor. + * @param {{ isEnemy:boolean, isBoss:boolean, spineShown:boolean, bounds?:{y?:number,height?:number}|null, modelK:number, modelKY:number }} o + * @returns {number} tiles + */ +export function headTilesForUnit(o) { + const k = (Number.isFinite(o.modelK) ? o.modelK : 1) * (Number.isFinite(o.modelKY) ? o.modelKY : 1); + if (!o.isEnemy) return UNIT.headroom; + const cap = o.isBoss ? HEAD_TILES_CAP_BOSS : HEAD_TILES_CAP; + const b = o.spineShown ? o.bounds : null; + if (b && Number.isFinite(b.height) && b.height > 20) { + // how far the box reaches *above the origin*: 猎狗pro −3.7, 大鲍勃 −2.6 (feet-anchored, ≈ height), 逐腐兽 −404.7 of + // 722.6 and 锏 −798 of 1183 (origin mid-body). Using the raw height there put the bar about a tile too high. + const top = Number.isFinite(b.y) ? b.y + b.height : b.height; + return clamp(Math.min(Math.max(top, 20), 900) * UNIT.modelScale * k * HEAD_TOP_K, 0.55, cap); + } + if (o.isBoss) return HEAD_TILES_CAP; + if (o.spineShown) return clamp(UNIT.headroom * k, 0.55, HEAD_TILES_CAP); + return UNIT.headroom; +} /** * The HP-bar readouts under the bars (b.snap `ammo`, `wolves`, `neg`; render/interp.js header): * - AMMO_BAR — the ammo skill's segmented bar replaces the SP bar: a cell per round, `height` of the HP bar's thickness @@ -1297,13 +1329,14 @@ export class UnitView { this.aura.scale.set((s * 1.3) / 128, (s * 1.9) / 128); } - // head height: operators/tokens are uniform chibis; enemies vary (setup-pose bounds, when known; else the chibi - // headroom × their official model factor) - let headTiles = UNIT.headroom; - if (this.isEnemy && spineShown && this.actor.entry.bounds) headTiles = clamp(this.actor.height * UNIT.modelScale * this.modelK * this.modelKY * 0.92, 0.55, this.isBoss ? 3.2 : 2.2); - else if (this.isEnemy && this.isBoss) headTiles = 2.2; - else if (this.isEnemy && spineShown) headTiles = clamp(UNIT.headroom * this.modelK * this.modelKY, 0.55, 2.2); - this._headTiles = headTiles; + // keep the original short-circuit: many views never build an actor (prep chips, diamonds, tests), so the model + // only counts when a spine is actually shown + const actor = spineShown ? this.actor : null; + const headTiles = this._headTiles = headTilesForUnit({ + isEnemy: this.isEnemy, isBoss: this.isBoss, spineShown, + bounds: actor && actor.entry ? actor.entry.bounds : null, + modelK: this.modelK, modelKY: this.modelKY, + }); this.screen.top = by - headTiles * s; this._updateHud(dt, s, bx, by - headTiles * s, alpha, t); } diff --git a/test/render/bar-head.test.js b/test/render/bar-head.test.js new file mode 100644 index 00000000..3cf9b5ff --- /dev/null +++ b/test/render/bar-head.test.js @@ -0,0 +1,67 @@ +// 敌方血条/名字牌的头顶高度:定位要用"原点以上的高度"(bounds.y + bounds.height),不是包围盒高度。 +// 数值取自 data/assets.json 里真实的 setup-pose bounds(逐腐兽 / 猎狗pro / 大鲍勃 / 锏)。 +import { test, describe } from 'node:test'; +import assert from 'node:assert/strict'; +import { readFileSync } from 'node:fs'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..'); +// units.js 读的是浏览器全局的 PIXI(脚本标签加载),模块只在运行时碰它 +globalThis.PIXI = globalThis.PIXI || { Container: class {}, Graphics: class {}, Sprite: class {} }; +const { headTilesForUnit, HEAD_TILES_CAP, HEAD_TILES_CAP_BOSS } = await import('../../public/js/render/units.js'); +const { UNIT } = await import('../../public/js/render/style.js'); + +/** The real setup-pose bounds (data/assets.json enemies.*.spine.bounds), with the official model factor. */ +const CASES = { + 逐腐兽: { y: -404.687, height: 722.64, modelScale: 0.8889 }, + 疯狂的逐腐兽: { y: -404.687, height: 722.64, modelScale: 0.8889 }, + 猎狗pro: { y: -3.745, height: 257.006, modelScale: 0.963 }, + 大鲍勃: { y: -2.62, height: 408.999, modelScale: 1.1111, boss: true }, + 锏: { y: -798, height: 1183, modelScale: 1, boss: true }, +}; +const head = (name, extra = {}) => { + const c = CASES[name]; + return headTilesForUnit({ + isEnemy: true, isBoss: !!c.boss, spineShown: true, hasBounds: true, + bounds: { y: c.y, height: c.height }, modelK: c.modelScale, modelKY: 1, ...extra, + }); +}; + +describe('敌方血条的头顶高度(bounds.y 不能忽略)', () => { + test('逐腐兽:原点在身体中间,血条按"原点以上" 0.8 格,而不是包围盒高度的 1.85 格', () => { + const tiles = head('逐腐兽'); + assert.ok(Math.abs(tiles - 0.81) < 0.02, `逐腐兽 ≈ 0.81 格,实得 ${tiles.toFixed(3)}`); + assert.ok(tiles < head('逐腐兽', { bounds: { height: CASES.逐腐兽.height } }) - 0.9, '与忽略 bounds.y 的老算法差约一格'); + assert.equal(head('疯狂的逐腐兽').toFixed(3), tiles.toFixed(3), '同一个模型,同样的高度'); + }); + + test('脚锚定的敌人(y ≈ 0)几乎不变:猎狗pro / 大鲍勃', () => { + assert.ok(Math.abs(head('猎狗pro') - 0.71) < 0.01, `猎狗pro ≈ 0.71 格,实得 ${head('猎狗pro').toFixed(3)}`); + // y = −3.7(猎狗pro)时两种算法的差只有 0.01 格:脚锚定的模型几乎不受这次修正影响 + assert.ok(Math.abs(head('猎狗pro') - head('猎狗pro', { bounds: { height: CASES.猎狗pro.height } })) < 0.02, 'y≈0 时两种算法几乎一致'); + assert.ok(Math.abs(head('大鲍勃') - 1.31) < 0.02, `大鲍勃 ≈ 1.31 格,实得 ${head('大鲍勃').toFixed(3)}`); + }); + + test('上限仍然兜底:模型再高,血条也不会飞出屏幕', () => { + assert.ok(Math.abs(head('锏') - 1.11) < 0.02, `锏 ≈ 1.11 格(385/320×0.92),实得 ${head('锏').toFixed(3)}`); + assert.ok(head('锏') <= HEAD_TILES_CAP_BOSS); + assert.ok(head('逐腐兽') <= HEAD_TILES_CAP); + const absurd = headTilesForUnit({ isEnemy: true, isBoss: false, spineShown: true, bounds: { y: 0, height: 5000 }, modelK: 1, modelKY: 1 }); + assert.equal(absurd, HEAD_TILES_CAP, '超过上限就截到上限'); + }); + + test('回退分支:没有 bounds 用 chibi 头顶余量 × 模型系数;非敌方用固定余量;Boss 无模型用 Boss 高度', () => { + assert.equal(headTilesForUnit({ isEnemy: true, isBoss: false, spineShown: true, bounds: null, modelK: 1, modelKY: 1 }), UNIT.headroom); + assert.equal(headTilesForUnit({ isEnemy: true, isBoss: false, spineShown: true, bounds: null, modelK: 1.5, modelKY: 1 }), Math.min(HEAD_TILES_CAP, UNIT.headroom * 1.5)); + assert.equal(headTilesForUnit({ isEnemy: true, isBoss: true, spineShown: false, bounds: null, modelK: 1, modelKY: 1 }), HEAD_TILES_CAP, 'Boss 没有模型时按 Boss 高度'); + assert.equal(headTilesForUnit({ isEnemy: false, isBoss: false, spineShown: true, bounds: { y: 0, height: 500 }, modelK: 1, modelKY: 1 }), UNIT.headroom, '干员/召唤物一律用统一余量'); + }); + + test('实现只有一处:纯函数算 y + height,units.js 把 bounds 交给它(防止有人改回 height)', () => { + const units = readFileSync(path.join(ROOT, 'public/js/render/units.js'), 'utf8'); + assert.match(units, /const top = Number\.isFinite\(b\.y\) \? b\.y \+ b\.height : b\.height;/, 'top = y + height'); + assert.match(units, /bounds: actor && actor\.entry \? actor\.entry\.bounds : null/, 'units.js 把 bounds 交给纯函数'); + assert.match(units, /headTilesForUnit\(\{/, '而且用的就是它'); + }); +}); diff --git a/test/render/playtest6-enemies.test.js b/test/render/playtest6-enemies.test.js index 6c2aafdc..3dfffc23 100644 --- a/test/render/playtest6-enemies.test.js +++ b/test/render/playtest6-enemies.test.js @@ -41,6 +41,12 @@ function view(info, opts) { return new U.UnitView(ctx, { id: 1, side: 'enemy', kind: 'enemy', tier: 1, x: 6, y: 10, maxHp: 1000, motion: 'FLY', ...info }); } const width = (key) => A.enemies[key].spine.bounds.width; +/** + * How far a model's setup-pose box reaches above its own origin (render/units.js headTilesForUnit): the bar clears the + * visible top, and these fixtures' synthetic box is deliberately not feet-anchored (y −150 of height 390 → 240 units), + * so a bar placed on the raw `height` would float 390/240 ≈ 1.6× too high. + */ +const topUnits = (v) => { const b = v.actor.entry.bounds; return Number.isFinite(b.y) ? b.y + b.height : b.height; }; describe('#9 enemy models at their official prefab scale', () => { test('data: 威龙 0.16 / 0.27, 妖怪 0.20 / 0.27, 妖怪MKII 0.22 / 0.27; standard-size enemies carry none', () => { @@ -69,8 +75,10 @@ describe('#9 enemy models at their official prefab scale', () => { assert.equal(U.enemyModelScale(E.enemy_1005_yokai_3), 0.5926); assert.equal(U.enemyModelScale(null), 1); assert.equal(U.enemyModelScale({ modelScale: 'x' }), 1); - // the HP bar sits on its (smaller) model: 390 units × 1/320 × 0.5926 × 0.92, clamped at 0.55 - assert.ok(Math.abs(drone._headTiles - Math.max(0.55, 390 * UNIT.modelScale * 0.5926 * 0.92)) < 1e-9); + // the HP bar sits on its (smaller) model, on however far the setup-pose box reaches *above the origin* + // (bounds.y + bounds.height — feet-anchored models have y ≈ 0, 逐腐兽's origin is mid-body), × 1/320 × 0.5926 × 0.92, + // clamped at 0.55 + assert.ok(Math.abs(drone._headTiles - Math.max(0.55, topUnits(drone) * UNIT.modelScale * 0.5926 * 0.92)) < 1e-9, `bar ${drone._headTiles}`); assert.ok(bird._headTiles > drone._headTiles); }); @@ -118,8 +126,8 @@ describe('official prefab quirks: vertical stretch and mirrored models', () => { assert.equal(U.enemyModelScaleY(null), 1); assert.equal(U.enemyModelScaleY({ modelScaleY: 'x' }), 1); assert.equal(U.enemyModelScaleY({ modelScaleY: 99 }), 1, 'absurd values fall back to uniform'); - // the HP bar rides the stretched model: 390 units × 1/320 × 0.7037 × 1.263 × 0.92 - assert.ok(Math.abs(drone._headTiles - 390 * UNIT.modelScale * 0.7037 * 1.263 * 0.92) < 1e-9, `bar ${drone._headTiles}`); + // the HP bar rides the stretched model: (bounds.y + bounds.height) × 1/320 × 0.7037 × 1.263 × 0.92 + assert.ok(Math.abs(drone._headTiles - topUnits(drone) * UNIT.modelScale * 0.7037 * 1.263 * 0.92) < 1e-9, `bar ${drone._headTiles}`); assert.ok(drone._headTiles > plain._headTiles); });