1
0
Fork 0
spacetac/src/core/ArenaGrid.spec.ts
2018-07-09 16:59:17 +02:00

42 lines
2.2 KiB
TypeScript

module TK.SpaceTac.Specs {
function checkLocation(check: TestContext, got: IArenaLocation, expected_x: number, expected_y: number) {
check.equals(got.x, expected_x, `x differs (${got.x},${got.y}) (${expected_x},${expected_y})`);
check.equals(got.y, expected_y, `y differs (${got.x},${got.y}) (${expected_x},${expected_y})`);
}
testing("HexagonalArenaGrid", test => {
test.case("checks coordinates", check => {
let grid = new HexagonalArenaGrid(5, 1);
check.equals(grid.check({ x: 0, y: 0 }), true, "0,0");
check.equals(grid.check({ x: 1, y: 0 }), false, "1,0");
check.equals(grid.check({ x: 5, y: 0 }), true, "5,0");
check.equals(grid.check({ x: 6, y: 0 }), false, "6,0");
check.equals(grid.check({ x: 0, y: 5 }), false, "0,5");
check.equals(grid.check({ x: 2.5, y: 5 }), true, "2.5,5");
check.equals(grid.check({ x: 5, y: 5 }), false, "5,5");
check.equals(grid.check({ x: 7.5, y: 5 }), true, "7.5,5");
});
test.case("snaps coordinates to the nearest grid point, on a biased grid", check => {
let grid = new HexagonalArenaGrid(4, 0.75);
checkLocation(check, grid.snap({ x: 0, y: 0 }), 0, 0);
checkLocation(check, grid.snap({ x: 1, y: 0 }), 0, 0);
checkLocation(check, grid.snap({ x: 1.9, y: 0 }), 0, 0);
checkLocation(check, grid.snap({ x: 2.1, y: 0 }), 4, 0);
checkLocation(check, grid.snap({ x: 1, y: 1 }), 0, 0);
checkLocation(check, grid.snap({ x: 1, y: 2 }), 2, 3);
checkLocation(check, grid.snap({ x: -1, y: -1 }), 0, 0);
checkLocation(check, grid.snap({ x: -2, y: -2 }), -2, -3);
checkLocation(check, grid.snap({ x: -3, y: -1 }), -4, 0);
checkLocation(check, grid.snap({ x: 6, y: -5 }), 8, -6);
});
test.case("snaps coordinates to the nearest grid point, on a regular grid", check => {
let grid = new HexagonalArenaGrid(10);
checkLocation(check, grid.snap({ x: 0, y: 0 }), 0, 0);
checkLocation(check, grid.snap({ x: 8, y: 0 }), 10, 0);
checkLocation(check, grid.snap({ x: 1, y: 6 }), 5, 10 * Math.sqrt(0.75));
});
});
}