| 1 | "use strict"; |
| 2 | const { describe, it } = require("node:test"); |
| 3 | const assert = require("node:assert/strict"); |
| 4 | const fs = require("fs"); |
| 5 | const path = require("path"); |
| 6 | const vm = require("vm"); |
| 7 | |
| 8 | function 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/textureColumn.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 | return context; |
| 20 | } |
| 21 | |
| 22 | describe("World.rayCast", () => |
| 23 | { |
| 24 | it("hits a solid wall ahead of spawn looking slightly south of east", () => |
| 25 | { |
| 26 | const g = loadLegacyGlobals(); |
| 27 | g.World.loadLevel(0); |
| 28 | const spawn = g.World.spawnPoint(); |
| 29 | // Looking roughly south into the map (legacy +Y) |
| 30 | const hit = g.World.rayCast(spawn.x, spawn.y, 0.1); |
| 31 | assert.ok(hit); |
| 32 | assert.ok(hit.tile); |
| 33 | assert.equal(hit.tile.Solid, true); |
| 34 | assert.ok(hit.distance > 0); |
| 35 | assert.ok(hit.tileX >= 0 && hit.tileX <= 1); |
| 36 | }); |
| 37 | |
| 38 | it("tileX stays in [0,1] for many angles", () => |
| 39 | { |
| 40 | const g = loadLegacyGlobals(); |
| 41 | g.World.loadLevel(0); |
| 42 | const spawn = g.World.spawnPoint(); |
| 43 | for (let i = 0; i < 64; i++) |
| 44 | { |
| 45 | const ang = (i / 64) * Math.PI * 2; |
| 46 | const hit = g.World.rayCast(spawn.x, spawn.y, ang); |
| 47 | assert.ok(hit.tileX >= 0 && hit.tileX <= 1, "tileX out of range at " + ang); |
| 48 | } |
| 49 | }); |
| 50 | }); |