const { describe, it } = require('node:test'); const assert = require('node:assert/strict'); const FloorCast = require('../src/floorCast.js'); describe('FloorCast math', () => { it('floorHit below horizon returns positive wy path and isCeiling false', () => { const hit = FloorCast.floorHit(160, 300, 320, 240, 48, 48, 0, 0, 120); assert.ok(hit); assert.equal(hit.isCeiling, false); assert.ok(hit.rowDist > 0); // looking east (rot 0): wx increases with distance assert.ok(hit.wx > 48); }); it('floorHit above horizon is ceiling', () => { const hit = FloorCast.floorHit(160, 40, 320, 240, 48, 48, 0, 0, 120); assert.ok(hit); assert.equal(hit.isCeiling, true); }); it('sampleNearest wraps and picks texel', () => { const rgba = new Uint8ClampedArray([ 10, 0, 0, 255, 20, 0, 0, 255, 30, 0, 0, 255, 40, 0, 0, 255 ]); assert.deepEqual(FloorCast.sampleNearest(rgba, 2, 2, 0.1, 0.1), [10, 0, 0, 255]); assert.deepEqual(FloorCast.sampleNearest(rgba, 2, 2, 0.6, 0.1), [20, 0, 0, 255]); assert.deepEqual(FloorCast.sampleNearest(rgba, 2, 2, 1.1, 0.1), [10, 0, 0, 255]); assert.deepEqual(FloorCast.sampleNearest(rgba, 2, 2, -0.1, 0.6), [40, 0, 0, 255]); }); it('render fills floor pixels from texMap and leaves black where null', () => { const w = 64, h = 48; const img = { data: new Uint8ClampedArray(w * h * 4) }; const level = { width: 3, height: 3, floors: [null, null, null, null, 'stone', null, null, null, null], ceilings: [null, null, null, null, 'stone', null, null, null, null] }; const stone = { w: 1, h: 1, rgba: new Uint8ClampedArray([200, 100, 50, 255]) }; const player = { x: 48, y: 48, rotation: 0, pitch: 0 }; FloorCast.render(img, w, h, player, level, { stone: stone }, 32); let nonzero = 0; for (let i = 0; i < img.data.length; i += 4) { if (img.data[i] || img.data[i + 1] || img.data[i + 2]) nonzero++; } assert.ok(nonzero > 10, 'expected some textured floor/ceiling pixels'); // alpha always 255 assert.equal(img.data[3], 255); }); it('perspective: farther rows have larger world step (rowDist grows toward horizon)', () => { const near = FloorCast.floorHit(160, 220, 320, 240, 48, 48, 0, 0, 120); const far = FloorCast.floorHit(160, 130, 320, 240, 48, 48, 0, 0, 120); assert.ok(far.rowDist > near.rowDist); }); });