Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/__tests__/utility/hexgrid-xray.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { describe, expect, jest, test } from '@jest/globals';

jest.mock('phaser-ce', () => ({}));
jest.mock('../../creature', () => ({
Creature: class Creature {},
}));

import { Creature } from '../../creature';
import { HexGrid } from '../../utility/hexgrid';

/* eslint-disable @typescript-eslint/no-explicit-any */

function makeCreature(hexagons: Array<{ ghostOverlap: jest.Mock }>) {
const CreatureCtor = Creature as unknown as { new (): Creature };
const creature = new CreatureCtor() as any;
creature.hexagons = hexagons;
creature.xray = jest.fn();
return creature;
}

describe('HexGrid.xray', () => {
test('checks every preview footprint hex for obstructing creatures', () => {
const activeHex = { ghostOverlap: jest.fn() };
const activeCreature = makeCreature([activeHex]);
const otherCreature = makeCreature([]);
const frontPreviewHex = { creature: undefined, ghostOverlap: jest.fn() };
const backPreviewHex = { creature: undefined, ghostOverlap: jest.fn() };

const grid = Object.create(HexGrid.prototype) as any;
grid.game = {
creatures: [activeCreature, otherCreature],
activeCreature,
};

grid.xray(frontPreviewHex as never, [frontPreviewHex as never, backPreviewHex as never]);

expect(frontPreviewHex.ghostOverlap).toHaveBeenCalledTimes(1);
expect(backPreviewHex.ghostOverlap).toHaveBeenCalledTimes(1);
expect(activeHex.ghostOverlap).toHaveBeenCalledWith(activeCreature);
expect(activeCreature.xray).toHaveBeenLastCalledWith(false);
});
});
58 changes: 58 additions & 0 deletions src/__tests__/utility/query_footprint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { describe, expect, jest, test } from '@jest/globals';
import { getQueryFootprintHexes } from '../../utility/query_footprint';

type TestHex = {
x: number;
y: number;
isWalkable: jest.Mock;
};

function buildGrid(width: number, height = 1) {
const hexes: TestHex[][] = [];

for (let y = 0; y < height; y++) {
const row: TestHex[] = [];
for (let x = 0; x < width; x++) {
row.push({
x,
y,
isWalkable: jest.fn((size: number) => x - size + 1 >= 0),
});
}
hexes.push(row);
}

return { hexes };
}

describe('getQueryFootprintHexes', () => {
test('returns every occupied hex for a non-flipped multi-hex preview', () => {
const grid = buildGrid(10);
const footprint = getQueryFootprintHexes(
grid as never,
grid.hexes[0][6] as never,
3,
false,
12,
);

expect(footprint.map(({ x, y }) => [x, y])).toEqual([
[6, 0],
[5, 0],
[4, 0],
]);
expect(grid.hexes[0][6].isWalkable).toHaveBeenCalledWith(3, 12);
});

test('offsets the footprint when the active player is flipped', () => {
const grid = buildGrid(10);
const footprint = getQueryFootprintHexes(grid as never, grid.hexes[0][6] as never, 3, true, 12);

expect(footprint.map(({ x, y }) => [x, y])).toEqual([
[8, 0],
[7, 0],
[6, 0],
]);
expect(grid.hexes[0][8].isWalkable).toHaveBeenCalledWith(3, 12);
});
});
Loading
Loading