Skip to content

Commit

Permalink
#199: fix pathfinding
Browse files Browse the repository at this point in the history
  • Loading branch information
Annoraaq committed Oct 6, 2021
1 parent 5973574 commit 4a449fa
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/GridCharacter/GridCharacter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ export class GridCharacter {
return this.movement;
}

getCollides(): boolean {
return this.collides;
}

setWalkingAnimationMapping(
walkingAnimationMapping: WalkingAnimationMapping | number
): void {
Expand Down
53 changes: 52 additions & 1 deletion src/Movement/TargetMovement/TargetMovement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ describe("TargetMovement", () => {
isMoving: () => false,
turnTowards: jest.fn(),
autoMovementSet: jest.fn().mockReturnValue(of()),
getCollides: jest.fn().mockReturnValue(true),
};
}
function layerPos(vec: Vector2): LayerPosition {
Expand Down Expand Up @@ -657,11 +658,54 @@ describe("TargetMovement", () => {
});
});

it("should delegate getNeighbours to gridTilemap", () => {
it("should ignore blocked tiles with non-colliding char", () => {
const charPos = layerPos(new Vector2(3, 1));
const targetPos = layerPos(new Vector2(1, 1));
targetMovement = new TargetMovement(gridTilemapMock, targetPos);
const mockChar = createMockChar("char1", charPos.position);
mockChar.getCollides.mockReturnValue(false);

mockBfs.getShortestPath = jest.fn().mockReturnValue({
path: [],
closestToTarget: undefined,
});

targetMovement.setCharacter(mockChar);
gridTilemapMock.isBlocking.mockReturnValue(true);
const getNeighbours = targetMovement.getNeighbours(charPos);

expect(getNeighbours).toEqual([
{
position: new Vector2(charPos.position.x, charPos.position.y + 1),
layer: "layer1",
},
{
position: new Vector2(charPos.position.x + 1, charPos.position.y),
layer: "layer1",
},
{
position: new Vector2(charPos.position.x - 1, charPos.position.y),
layer: "layer1",
},
{
position: new Vector2(charPos.position.x, charPos.position.y - 1),
layer: "layer1",
},
]);
});

it("should delegate getNeighbours to gridTilemap", () => {
const charPos = layerPos(new Vector2(3, 1));
const targetPos = layerPos(new Vector2(1, 1));
const mockChar = createMockChar("char1", charPos.position);
mockBfs.getShortestPath = jest.fn().mockReturnValue({
path: [],
closestToTarget: undefined,
});
gridTilemapMock.isBlocking.mockReturnValue(true);

targetMovement = new TargetMovement(gridTilemapMock, targetPos);
targetMovement.setCharacter(mockChar);
let getNeighbours = targetMovement.getNeighbours(charPos);
expect(getNeighbours).toEqual([]);

Expand Down Expand Up @@ -1071,7 +1115,14 @@ describe("TargetMovement", () => {
it("should get 8 neighbours", () => {
const charPos = layerPos(new Vector2(3, 1));
const targetPos = layerPos(new Vector2(1, 1));
const mockChar = createMockChar("char1", charPos.position);
mockBfs.getShortestPath = jest.fn().mockReturnValue({
path: [],
closestToTarget: undefined,
});
gridTilemapMock.isBlocking.mockReturnValue(true);
targetMovement = new TargetMovement(gridTilemapMock, targetPos);
targetMovement.setCharacter(mockChar);
targetMovement.setNumberOfDirections(NumberOfDirections.EIGHT);
gridTilemapMock.isBlocking.mockReturnValue(true);
let getNeighbours = targetMovement.getNeighbours(charPos);
Expand Down
1 change: 1 addition & 0 deletions src/Movement/TargetMovement/TargetMovement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ export class TargetMovement implements Movement {
}

private isBlocking = (pos?: Vector2, charLayer?: string): boolean => {
if (!this.character?.getCollides()) return false;
return !pos || this.tilemap.isBlocking(charLayer, pos);
};

Expand Down

0 comments on commit 4a449fa

Please sign in to comment.