UNPKG

hexagrid

Version:

Hexagonal grid with fast lookup

85 lines 4.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = require("chai"); const grid_1 = require("./grid"); const hex_1 = require("./hex"); describe("Grid", () => { it("should be able to get individual hexes", () => { var _a, _b; const grid = new grid_1.default(new hex_1.default(2, -1, "Hello"), new hex_1.default(2, 0, "World")); chai_1.expect(grid.get({ q: 2, r: 1 })).to.be.undefined; chai_1.expect((_a = grid.get({ q: 2, r: 0 })) === null || _a === void 0 ? void 0 : _a.data).to.equal("World"); chai_1.expect((_b = grid.get({ q: 2, r: -1 })) === null || _b === void 0 ? void 0 : _b.data).to.equal("Hello"); }); it("should overwrite hex at same position and have consistent size", () => { var _a; const grid = new grid_1.default(); chai_1.expect(grid.size).to.equal(0); grid.push(new hex_1.default(0, 2), new hex_1.default(4, -2, "Hi")); chai_1.expect(grid.size).to.equal(2); grid.push(new hex_1.default(1, 2), new hex_1.default(4, -2, "Boy")); chai_1.expect(grid.size).to.equal(3); chai_1.expect((_a = grid.get({ q: 4, r: -2 })) === null || _a === void 0 ? void 0 : _a.data).to.equal("Boy"); }); it("should be able to remove hexes", () => { const grid = new grid_1.default(...hex_1.default.ring(2)); chai_1.expect(grid.size).to.equal(12); grid.remove({ q: -2, r: 0 }); chai_1.expect(grid.size).to.equal(11); grid.push(new hex_1.default(-2, 0)); chai_1.expect(grid.size).to.equal(12); }); it("should be able to rotate fully", () => { var _a, _b; const grid = new grid_1.default(...hex_1.default.hexagon(1)); chai_1.expect(grid.size).to.equal(7); grid.remove({ q: 1, r: 0 }); grid.get({ q: 0, r: 1 }).data = "Single data"; chai_1.expect(grid.size).to.equal(6); grid.rotateLeft(3); chai_1.expect(grid.get({ q: -1, r: 0 })).to.be.undefined; chai_1.expect((_a = grid.get({ q: 0, r: -1 })) === null || _a === void 0 ? void 0 : _a.data).to.equal("Single data"); chai_1.expect(grid.size).to.equal(6); grid.push(new hex_1.default(0, -1, "newer")); chai_1.expect(grid.size).to.equal(6); chai_1.expect((_b = grid.get({ q: 0, r: -1 })) === null || _b === void 0 ? void 0 : _b.data).to.equal("newer"); }); it("should be able to recalibrate", () => { var _a; const grid = new grid_1.default(new hex_1.default(-2, 0), new hex_1.default(1, 1, "ola")); grid.get({ q: 1, r: 1 }).r = 2; grid.recalibrate(); chai_1.expect(grid.get({ q: 1, r: 1 })).to.be.undefined; chai_1.expect((_a = grid.get({ q: 1, r: 2 })) === null || _a === void 0 ? void 0 : _a.data).to.equal("ola"); }); it("should be able to find neighbours", () => { const grid = new grid_1.default(...hex_1.default.ring(1)); chai_1.expect(grid.neighbours({ q: 0, r: 0 }).length).to.equal(6); chai_1.expect(grid.neighbours({ q: 1, r: 0 }).length).to.equal(2); }); describe("distance", () => { it("should return 0 if the starting point and end are the same", () => { const grid = new grid_1.default(...hex_1.default.hexagon(1)); chai_1.expect(grid.distance({ q: 1, r: 0 }, { q: 1, r: 0 })).to.equal(0); }); it("should return the distance between two hexagons", () => { const grid = new grid_1.default(...hex_1.default.hexagon(1), ...hex_1.default.ring(1, { center: { q: 10, r: 0, s: -10 } })); chai_1.expect(grid.distance({ q: 1, r: 0 }, { q: 0, r: 0 })).to.equal(1); chai_1.expect(grid.distance({ q: 1, r: 0 }, { q: -1, r: 0 })).to.equal(2); chai_1.expect(grid.distance({ q: 11, r: 0 }, { q: 9, r: 0 })).to.equal(3); }); it("should return -1 if there's no path", () => { const grid = new grid_1.default(...hex_1.default.hexagon(1), ...hex_1.default.hexagon(1, { center: { q: 10, r: 0, s: -10 } })); chai_1.expect(grid.distance({ q: 0, r: 0 }, { q: 10, r: 0 })).to.equal(-1); chai_1.expect(grid.distance({ q: -5, r: 0 }, { q: -5, r: 0 })).to.equal(-1); chai_1.expect(grid.distance({ q: 10, r: 1 }, { q: 10, r: 0 })).to.equal(1); }); }); describe("groups", () => { it("should divide hexes into groups", () => { const grid = new grid_1.default(...hex_1.default.hexagon(1), ...hex_1.default.ring(1, { center: { q: 10, r: 0, s: -10 } })); chai_1.expect(grid.groups([...grid.values()])).to.have.lengthOf(2); }); }); }); //# sourceMappingURL=grid.spec.js.map