1
0
Fork 0
spacetac/src/core/ArenaGrid.spec.ts
2018-07-10 16:23:32 +02:00

62 lines
3.2 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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("ArenaGrid", test =>  {
test.case("checks if a location is in range of another", check => {
let grid = new ArenaGrid();
check.in("first order", check => {
check.equals(grid.inRange({ x: 0, y: 0 }, { x: 4, y: 4 }, 5), false, "<5");
check.equals(grid.inRange({ x: 0, y: 0 }, { x: 4, y: 4 }, 8), true, "<8");
});
check.in("second order", check => {
check.equals(grid.inRange({ x: 4, y: 4 }, { x: 0, y: 0 }, 5), false, "<5");
check.equals(grid.inRange({ x: 4, y: 4 }, { x: 0, y: 0 }, 8), true, "<8");
});
check.equals(grid.inRange({ x: 0, y: 0 }, { x: 0.99999999999999, y: 0 }, 1), true, "0.99999999999999");
check.equals(grid.inRange({ x: 0, y: 0 }, { x: 1.00000000000001, y: 0 }, 1), true, "1.00000000000001");
check.equals(grid.inRange({ x: 0, y: 0 }, { x: 1.000001, y: 0 }, 1), false, "1.000001");
})
});
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));
});
});
}