BlueGit

timfa raytracer

Files

raytracer / test / worldRaycast.test.js

worldRaycast.test.js

worldRaycast.test.js 1.5 KiB Download Raw
1"use strict";
2const { describe, it } = require("node:test");
3const assert = require("node:assert/strict");
4const fs = require("fs");
5const path = require("path");
6const vm = require("vm");
7
8function loadLegacyGlobals ()
9{
10 const root = path.join(__dirname, "..");
11 const context = { console, Math, module: {}, exports: {} };
12 context.global = context;
13 vm.createContext(context);
14 for (const rel of ["src/levelFormat.js", "src/levels.js", "src/tiles.js", "src/world.js"])
15 {
16 const code = fs.readFileSync(path.join(root, rel), "utf8");
17 vm.runInContext(code, context, { filename: rel });
18 }
19 const pack = context.LevelFormat.fromLegacyLevels(context.Levels);
20 context.World.loadPack(pack);
21 return context;
22}
23
24describe("World.rayCast", () =>
25{
26 it("hits a solid wall ahead of spawn looking slightly south of east", () =>
27 {
28 const g = loadLegacyGlobals();
29 g.World.loadLevel(0);
30 const spawn = g.World.spawnPoint();
31 const hit = g.World.rayCast(spawn.x, spawn.y, 0.1);
32 assert.ok(hit);
33 assert.ok(hit.tile);
34 assert.equal(hit.tile.Solid, true);
35 assert.ok(hit.distance > 0);
36 assert.ok(hit.tileX >= 0 && hit.tileX <= 1);
37 });
38
39 it("tileX stays in [0,1] for many angles", () =>
40 {
41 const g = loadLegacyGlobals();
42 g.World.loadLevel(0);
43 const spawn = g.World.spawnPoint();
44 for (let i = 0; i < 64; i++)
45 {
46 const ang = (i / 64) * Math.PI * 2;
47 const hit = g.World.rayCast(spawn.x, spawn.y, ang);
48 assert.ok(hit.tileX >= 0 && hit.tileX <= 1, "tileX out of range at " + ang);
49 }
50 });
51});