Skip to content

Commit

Permalink
#206 refactor blocking
Browse files Browse the repository at this point in the history
  • Loading branch information
Annoraaq committed Oct 17, 2021
1 parent 8616e8f commit 0d55ddc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 63 deletions.
51 changes: 9 additions & 42 deletions src/GridCharacter/GridCharacter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,11 @@ describe("GridCharacter", () => {
const DEPTH_OF_CHAR_LAYER = 10;

function mockNonBlockingTile() {
gridTilemapMock.hasBlockingTile.mockReturnValue(false);
gridTilemapMock.hasNoTile.mockReturnValue(false);
gridTilemapMock.isBlocking.mockReturnValue(false);
}

function mockBlockingTile() {
gridTilemapMock.hasBlockingTile.mockReturnValue(true);
gridTilemapMock.hasNoTile.mockReturnValue(false);
gridTilemapMock.isBlocking.mockReturnValue(true);
}

afterEach(() => {
Expand All @@ -60,9 +58,7 @@ describe("GridCharacter", () => {

beforeEach(() => {
gridTilemapMock = {
hasBlockingTile: jest.fn(),
hasNoTile: jest.fn(),
hasBlockingChar: jest.fn().mockReturnValue(false),
isBlocking: jest.fn(),
getDepthOfCharLayer: jest.fn().mockReturnValue(DEPTH_OF_CHAR_LAYER),
getTransition: jest.fn(),
};
Expand Down Expand Up @@ -722,8 +718,7 @@ describe("GridCharacter", () => {
describe("isBlockingDirection", () => {
it("direction NONE never blocks", () => {
const direction = Direction.NONE;
gridTilemapMock.hasBlockingTile.mockReturnValue(true);
gridTilemapMock.hasBlockingChar.mockReturnValue(true);
gridTilemapMock.isBlocking.mockReturnValue(true);

const result = gridCharacter.isBlockingDirection(direction);
expect(result).toBe(false);
Expand All @@ -732,8 +727,7 @@ describe("GridCharacter", () => {
it("should detect non-blocking direction", () => {
const direction = Direction.RIGHT;
const oppositeDirection = Direction.LEFT;
gridTilemapMock.hasBlockingTile.mockReturnValue(false);
gridTilemapMock.hasBlockingChar.mockReturnValue(false);
gridTilemapMock.isBlocking.mockReturnValue(false);

gridCharacter.move(Direction.RIGHT);
gridCharacter.update(10);
Expand All @@ -745,33 +739,17 @@ describe("GridCharacter", () => {
{ x: 1, y: 0 },
undefined
);
expect(gridTilemapMock.hasBlockingTile).toHaveBeenCalledWith(
expect(gridTilemapMock.isBlocking).toHaveBeenCalledWith(
"layerInDir",
{
x: 2,
y: 0,
},
oppositeDirection
);
expect(gridTilemapMock.hasBlockingChar).toHaveBeenCalledWith(
{
x: 2,
y: 0,
},
"layerInDir"
);
expect(result).toBe(false);
});

it("should detect blocking direction if map blocks", () => {
const direction = Direction.RIGHT;
gridTilemapMock.hasBlockingTile.mockReturnValue(true);
gridTilemapMock.hasBlockingChar.mockReturnValue(false);

const result = gridCharacter.isBlockingDirection(direction);
expect(result).toBe(true);
});

it("should not detect blocking direction if char does not collide", () => {
gridCharacter = new GridCharacter("player", {
sprite: gridSpriteMock,
Expand All @@ -782,26 +760,15 @@ describe("GridCharacter", () => {
walkingAnimationMapping: 3,
});
const direction = Direction.RIGHT;
gridTilemapMock.hasBlockingTile.mockReturnValue(true);
gridTilemapMock.hasBlockingChar.mockReturnValue(false);
gridTilemapMock.isBlocking.mockReturnValue(true);

const result = gridCharacter.isBlockingDirection(direction);
expect(result).toBe(false);
});

it("should detect blocking direction if char blocks", () => {
const direction = Direction.RIGHT;
gridTilemapMock.hasBlockingTile.mockReturnValue(false);
gridTilemapMock.hasBlockingChar.mockReturnValue(true);

const result = gridCharacter.isBlockingDirection(direction);
expect(result).toBe(true);
});

it("should detect blocking direction if char and tile block", () => {
it("should detect blocking direction if tilemap blocks", () => {
const direction = Direction.RIGHT;
gridTilemapMock.hasBlockingTile.mockReturnValue(true);
gridTilemapMock.hasBlockingChar.mockReturnValue(true);
gridTilemapMock.isBlocking.mockReturnValue(true);

const result = gridCharacter.isBlockingDirection(direction);
expect(result).toBe(true);
Expand Down
7 changes: 1 addition & 6 deletions src/GridCharacter/GridCharacter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,16 +227,11 @@ export class GridCharacter {
const layerInDirection =
this.tilemap.getTransition(tilePosInDir, this.nextTilePos.layer) ||
this.nextTilePos.layer;
const hasBlockingTile = this.tilemap.hasBlockingTile(
return this.tilemap.isBlocking(
layerInDirection,
tilePosInDir,
oppositeDirection(this.toMapDirection(direction))
);
const hasBlockingChar = this.tilemap.hasBlockingChar(
tilePosInDir,
layerInDirection
);
return hasBlockingTile || hasBlockingChar;
}

isMoving(): boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ describe("IsometricGridCharacter", () => {
const DEPTH_OF_CHAR_LAYER = 10;

function mockNonBlockingTile() {
gridTilemapMock.hasBlockingTile.mockReturnValue(false);
gridTilemapMock.hasNoTile.mockReturnValue(false);
gridTilemapMock.isBlocking.mockReturnValue(false);
}

afterEach(() => {
Expand All @@ -50,9 +49,7 @@ describe("IsometricGridCharacter", () => {

beforeEach(() => {
gridTilemapMock = {
hasBlockingTile: jest.fn(),
hasNoTile: jest.fn(),
hasBlockingChar: jest.fn().mockReturnValue(false),
isBlocking: jest.fn().mockReturnValue(false),
getDepthOfCharLayer: jest.fn().mockReturnValue(DEPTH_OF_CHAR_LAYER),
getTransition: jest.fn(),
};
Expand Down Expand Up @@ -177,8 +174,7 @@ describe("IsometricGridCharacter", () => {

it("should detect non-blocking direction", () => {
const oppositeMapDirection = Direction.DOWN;
gridTilemapMock.hasBlockingTile.mockReturnValue(false);
gridTilemapMock.hasBlockingChar.mockReturnValue(false);
gridTilemapMock.isBlocking.mockReturnValue(false);

gridCharacter.setTilePosition({
position: new Vector2(3, 3),
Expand All @@ -189,21 +185,14 @@ describe("IsometricGridCharacter", () => {
gridCharacter.update(10);

const result = gridCharacter.isBlockingDirection(Direction.UP_RIGHT);
expect(gridTilemapMock.hasBlockingTile).toHaveBeenCalledWith(
expect(gridTilemapMock.isBlocking).toHaveBeenCalledWith(
"someLayer",
{
x: 3,
y: 1,
},
oppositeMapDirection
);
expect(gridTilemapMock.hasBlockingChar).toHaveBeenCalledWith(
{
x: 3,
y: 1,
},
"someLayer"
);
expect(result).toBe(false);
});

Expand Down

0 comments on commit 0d55ddc

Please sign in to comment.