Skip to content

Commit

Permalink
Merge pull request #397 from Annoraaq/feature/#394-offset-setter
Browse files Browse the repository at this point in the history
#394 add setter for offset
  • Loading branch information
Annoraaq committed Jun 18, 2023
2 parents 3712ae8 + 42e3247 commit 6960a44
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/GridEngine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1908,4 +1908,22 @@ describe("GridEngine", () => {
"Using GridEngine vGRID.ENGINE.VERSION"
);
});

it("should set/get offset", () => {
const offsetX = 3;
const offsetY = 4;
gridEngine.create(createDefaultMockWithLayer(undefined), {
characters: [
{
id: "player",
sprite: playerSpriteMock,
walkingAnimationMapping: 3,
},
],
});
gridEngine.setOffsetX("player", offsetX);
gridEngine.setOffsetY("player", offsetY);
expect(gridEngine.getOffsetX("player")).toEqual(offsetX);
expect(gridEngine.getOffsetY("player")).toEqual(offsetY);
});
});
20 changes: 20 additions & 0 deletions src/GridEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,16 @@ export class GridEngine implements IGridEngine {
return gridChar.getOffsetX();
}

/**
* Set custom x-offset for the sprite/container.
*/
setOffsetX(charId: string, offsetX: number): void {
this.initGuard();
const gridChar = this.gridCharacters?.get(charId);
if (!gridChar) throw this.createCharUnknownErr(charId);
gridChar.setOffsetX(offsetX);
}

/** @returns Y-offset for a character. */
getOffsetY(charId: string): number {
this.initGuard();
Expand All @@ -302,6 +312,16 @@ export class GridEngine implements IGridEngine {
return gridChar.getOffsetY();
}

/**
* Set custom y-offset for the sprite/container.
*/
setOffsetY(charId: string, offsetY: number): void {
this.initGuard();
const gridChar = this.gridCharacters?.get(charId);
if (!gridChar) throw this.createCharUnknownErr(charId);
gridChar.setOffsetY(offsetY);
}

/** {@inheritDoc IGridEngine.collidesWithTiles} */
collidesWithTiles(charId: string): boolean {
return this.geHeadless.collidesWithTiles(charId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ describe("GridCharacterPhaser", () => {
};
}

it("should set/get offset", () => {
const { gridCharPhaser } = createChar({}, true);
const offsetX = 3;
const offsetY = 4;
gridCharPhaser.setOffsetX(offsetX);
gridCharPhaser.setOffsetY(offsetY);
expect(gridCharPhaser.getOffsetX()).toEqual(offsetX);
expect(gridCharPhaser.getOffsetY()).toEqual(offsetY);
});

describe("On creation", () => {
it("should create a grid character", () => {
const walkingAnimationMock = {} as any;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,18 @@ export class GridCharacterPhaser {
return this.customOffset.x;
}

setOffsetX(offsetX: number): void {
this.customOffset.x = offsetX;
}

getOffsetY(): number {
return this.customOffset.y;
}

setOffsetY(offsetY: number): void {
this.customOffset.y = offsetY;
}

getWalkingAnimationMapping(): WalkingAnimationMapping | number | undefined {
return this.walkingAnimationMapping;
}
Expand Down

0 comments on commit 6960a44

Please sign in to comment.