Skip to content
Make the player visible with a procedural 3D humanoid132be7c
JavaScript186 lines8.2 KB
import { test } from 'node:test';import assert from 'node:assert/strict';import { createMatch, step, simulate, CONFIG, DT, getHitbox, getHurtbox, rectsOverlap, distance, EMPTY_INPUT } from './engine.mjs';import { LEVELS } from './levels.mjs'; const P1 = (i) => [i, EMPTY_INPUT];const P2 = (i) => [EMPTY_INPUT, i]; function closeUp(state) {  // Walk P1 toward P2 until within punch reach.  let s = state;  for (let i = 0; i < 600 && distance(s) > CONFIG.fighterWidth + CONFIG.attacks.punch.reach * 0.6; i++) s = step(s, P1({ right: true }));  return simulate(s, [], 5); // settle, face each other} test('createMatch sets up two fighters facing each other inside the arena', () => {  const s = createMatch('dojo');  assert.equal(s.fighters.length, 2);  assert.equal(s.fighters[0].facing, 1);  assert.equal(s.fighters[1].facing, -1);  assert.ok(s.fighters[0].x < s.fighters[1].x);  assert.equal(s.phase, 'fight');  assert.equal(s.level.id, 'dojo');  assert.equal(createMatch(99).level, LEVELS[0]);}); test('step is pure and deterministic', () => {  const s0 = createMatch(0);  const json = JSON.stringify(s0);  const a = simulate(s0, P1({ right: true, jump: true }), 30);  const b = simulate(s0, P1({ right: true, jump: true }), 30);  assert.equal(JSON.stringify(s0), json, 'input state not mutated');  assert.deepEqual(a, b);  assert.equal(a.tick, 30);}); test('movement left/right, facing, and arena clamping', () => {  const s0 = createMatch(0);  const r = step(s0, P1({ right: true }));  assert.ok(r.fighters[0].x > s0.fighters[0].x);  assert.equal(r.fighters[0].stance, 'walk');  const l = step(s0, P1({ left: true }));  assert.ok(l.fighters[0].x < s0.fighters[0].x);  assert.equal(l.fighters[0].facing, -1);  const far = simulate(s0, P1({ left: true }), 600);  assert.equal(far.fighters[0].x, s0.level.arena.padding + CONFIG.fighterWidth / 2);  const idle = step(far, []);  assert.equal(idle.fighters[0].facing, 1, 'idle fighter turns to face opponent');}); test('jump rises then lands; stronger gravity lands sooner', () => {  const land = (levelId) => {    let s = step(createMatch(levelId), P1({ jump: true }));    assert.equal(s.fighters[0].onGround, false);    assert.equal(s.fighters[0].stance, 'jump');    let peak = 0, n = 0;    while (!s.fighters[0].onGround && n < 600) { s = step(s, []); peak = Math.max(peak, s.fighters[0].y); n++; }    assert.ok(peak > 50);    assert.equal(s.fighters[0].y, 0);    return n;  };  const normal = land('dojo');  const heavy = land('rooftop');  const light = land('void');  assert.ok(heavy < normal && normal < light);}); test('punch out of range whiffs; punch in range lands with damage, knockback and hitstun', () => {  const s0 = createMatch(0);  const whiff = simulate(s0, P1({ punch: true }), 20);  assert.equal(whiff.fighters[1].health, CONFIG.maxHealth);  const near = closeUp(s0);  let s = step(near, P1({ punch: true }));  assert.equal(s.fighters[0].stance, 'punch');  assert.ok(s.fighters[0].stamina < near.fighters[0].stamina);  let hitEvent = null;  for (let i = 0; i < 20 && !hitEvent; i++) { s = step(s, []); hitEvent = s.events.find((e) => e.type === 'hit'); }  assert.ok(hitEvent, 'hit event emitted');  assert.equal(hitEvent.damage, CONFIG.attacks.punch.damage);  assert.equal(s.fighters[1].health, CONFIG.maxHealth - CONFIG.attacks.punch.damage);  assert.ok(s.fighters[1].vx > 0, 'knocked back away from attacker');  assert.ok(s.fighters[1].hitstun > 0);  assert.equal(s.fighters[1].stance, 'hit');  const after = simulate(s, [], 30);  assert.equal(after.fighters[1].health, CONFIG.maxHealth - CONFIG.attacks.punch.damage, 'one attack hits once');}); test('kick does more damage than punch and only one attack starts at a time', () => {  const near = closeUp(createMatch(0));  let s = step(near, P1({ kick: true, punch: true }));  assert.equal(s.fighters[0].attack.type, 'punch', 'punch has priority');  s = step(near, P1({ kick: true }));  assert.equal(s.fighters[0].attack.type, 'kick');  const s2 = step(s, P1({ punch: true }));  assert.equal(s2.fighters[0].attack.type, 'kick', 'cannot cancel into another attack');  let dealt = 0;  for (let i = 0; i < 40; i++) { s = step(s, []); for (const e of s.events) if (e.type === 'hit') dealt += e.damage; }  assert.equal(dealt, CONFIG.attacks.kick.damage);  assert.ok(CONFIG.attacks.kick.damage > CONFIG.attacks.punch.damage);}); test('blocking reduces damage, removes hitstun, and drains stamina', () => {  const near = closeUp(createMatch(0));  let s = step(near, [{ kick: true }, { block: true }]);  assert.equal(s.fighters[1].stance, 'block');  let ev = null;  for (let i = 0; i < 40 && !ev; i++) { s = step(s, P2({ block: true })); ev = s.events.find((e) => e.type === 'block' || e.type === 'hit'); }  assert.ok(ev && ev.type === 'block');  assert.equal(s.fighters[1].health, CONFIG.maxHealth - Math.round(CONFIG.attacks.kick.damage * CONFIG.blockDamageFactor));  assert.equal(s.fighters[1].hitstun, 0);  assert.ok(s.fighters[1].stamina < CONFIG.maxStamina);}); test('stunned fighter cannot act until hitstun ends', () => {  const near = closeUp(createMatch(0));  let s = step(near, P1({ punch: true }));  while (!s.events.some((e) => e.type === 'hit')) s = step(s, []);  const stunned = step(s, P2({ punch: true }));  assert.equal(stunned.fighters[1].attack, null);  const recovered = simulate(s, [], Math.ceil(CONFIG.attacks.punch.hitstun / DT) + 2);  assert.equal(recovered.fighters[1].hitstun, 0);  assert.ok(step(recovered, P2({ punch: true })).fighters[1].attack);}); test('hitbox geometry respects facing and active frames', () => {  const f = { ...createMatch(0).fighters[0], attack: { type: 'punch', t: 0, hasHit: false } };  assert.equal(getHitbox(f), null, 'no hitbox during startup');  f.attack.t = CONFIG.attacks.punch.startup;  const hb = getHitbox(f);  assert.ok(hb.x >= f.x, 'hitbox in front when facing right');  assert.ok(!rectsOverlap(hb, getHurtbox(f)), 'cannot hit self');  const lb = getHitbox({ ...f, facing: -1 });  assert.ok(lb.x + lb.w <= f.x);  assert.equal(getHitbox({ ...f, attack: { type: 'punch', t: 1, hasHit: false } }), null);}); test('KO ends the round, rounds accumulate, and the match ends', () => {  const lvl = LEVELS[0];  let s = createMatch(lvl);  let lastEvents = [];  for (let round = 1; round <= lvl.roundsToWin; round++) {    assert.equal(s.round, round);    s = closeUp(s);    let guard = 0;    while (s.phase === 'fight' && guard++ < 5000) s = step(s, P1(distance(s) > CONFIG.fighterWidth + 40 ? { right: true } : { punch: true }));    assert.equal(s.fighters[1].health, 0);    assert.equal(s.fighters[1].stance, 'ko');    assert.equal(s.roundWinner, 0);    assert.equal(s.fighters[0].roundsWon, round);    lastEvents = s.events.map((e) => e.type);    if (round < lvl.roundsToWin) {      assert.equal(s.phase, 'roundover');      const frozen = step(s, P1({ punch: true }));      assert.deepEqual(frozen.fighters, s.fighters, 'fighters frozen during roundover');      s = simulate(s, [], Math.ceil(CONFIG.roundEndDelay / DT) + 1);      assert.equal(s.phase, 'fight');      assert.equal(s.fighters[1].health, CONFIG.maxHealth, 'health reset for new round');      assert.equal(s.fighters[0].roundsWon, round, 'score persists');    }  }  assert.equal(s.phase, 'matchover');  assert.equal(s.winner, 0);  assert.ok(lastEvents.includes('roundover') && lastEvents.includes('matchover'));  const after = step(s, P1({ punch: true }));  assert.equal(after.phase, 'matchover');  assert.deepEqual(after.fighters, s.fighters);}); test('round timer expiring awards the round to the healthier fighter', () => {  let s = createMatch(0);  s = { ...s, roundTime: 0.05, fighters: [s.fighters[0], { ...s.fighters[1], health: 40 }] };  s = simulate(s, [], 5);  assert.equal(s.phase, 'roundover');  assert.equal(s.roundWinner, 0);  let d = createMatch(0);  d = simulate({ ...d, roundTime: 0.05 }, [], 5);  assert.equal(d.roundWinner, -1, 'equal health is a draw');  assert.equal(d.fighters[0].roundsWon + d.fighters[1].roundsWon, 0);}); test('fighters cannot pass through each other', () => {  const s = simulate(createMatch(0), [{ right: true }, { left: true }], 400);  assert.ok(s.fighters[0].x < s.fighters[1].x);  assert.ok(distance(s) >= CONFIG.fighterWidth * 0.9 - 1e-6);});