From e93c7fe6f1c6d89613f835dc89ef1642a39a6f83 Mon Sep 17 00:00:00 2001 From: Job Vonk Date: Thu, 23 Apr 2020 14:54:46 +0200 Subject: [PATCH 1/2] Added support for tileSprites in entity.center() --- lib/index.d.ts | 8 ++++++-- lib/utilities/entity.js | 10 +++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/index.d.ts b/lib/index.d.ts index 17c1ae5..ca60682 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -600,9 +600,13 @@ export class Sound { stop(): void; } -interface NumericalEntity {pos: Coordinates, w: number, h: number} +interface NumericalEntityBase {pos: Coordinates} +interface NumericalSprite {w: number, h: number} +interface NumericalTileSprite {tileW: number, tileH: number} -interface NumericalEntityWithHitbox extends NumericalEntity {hitBox: NumericalEntity} +type NumericalEntity = NumericalSprite | NumericalTileSprite + +type NumericalEntityWithHitbox = {hitBox: NumericalEntity} & NumericalEntity export namespace deadInTracks { /** diff --git a/lib/utilities/entity.js b/lib/utilities/entity.js index 67c2aaa..414a7e6 100644 --- a/lib/utilities/entity.js +++ b/lib/utilities/entity.js @@ -31,7 +31,15 @@ function bounds(entity) { } function center(entity) { - const { pos, w, h } = entity; + let pos, w, h; + + // Object.prototype.hasOwnProperty.call is needed because of eslint + if (Object.prototype.hasOwnProperty.call(entity, "tileW") && Object.prototype.hasOwnProperty.call(entity, "tileH")) { + ({pos, w, h} = {pos: entity.pos, w: entity.tileW, h: entity.tileH}); + } else { + ({pos, w, h} = entity); + } + return { x: pos.x + w / 2, y: pos.y + h / 2 -- 2.45.2 From 0c042160ab5a89e0d63f7397d3376848271a0e21 Mon Sep 17 00:00:00 2001 From: Job Vonk Date: Fri, 24 Apr 2020 09:12:47 +0200 Subject: [PATCH 2/2] Added support for tileSprites in entity.bounds() --- lib/index.d.ts | 2 +- lib/utilities/entity.js | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/index.d.ts b/lib/index.d.ts index ca60682..b78fa76 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -650,7 +650,7 @@ export namespace entity { * @param container The container. * @param hitCallback The callback that is executed when an entity hits something in the container. */ - export function hits(entity: NumericalEntityWithHitbox | NumericalEntity, container: Container, hitCallback: (e2: NumericalEntityWithHitbox | NumericalEntity) => any): void; + export function hits(entity: NumericalEntityWithHitbox | NumericalEntity, container: Container, hitCallback: (e2: T) => any): void; /** * This functions calculates whether two entities hit each other. diff --git a/lib/utilities/entity.js b/lib/utilities/entity.js index 414a7e6..736f758 100644 --- a/lib/utilities/entity.js +++ b/lib/utilities/entity.js @@ -20,7 +20,16 @@ function angle(a, b) { } function bounds(entity) { - const { w, h, pos, hitBox } = entity; + let pos, w, h, hitBox; + // Object.prototype.hasOwnProperty.call is needed because of eslint + if (Object.prototype.hasOwnProperty.call(entity, "tileW") && Object.prototype.hasOwnProperty.call(entity, "tileH")) { + ({pos, w, h} = {pos: entity.pos, w: entity.tileW, h: entity.tileH}); + } else { + ({pos, w, h} = entity); + } + + if (entity.hitBox) hitBox = entity.hitBox; + const hit = hitBox || { x: 0, y: 0, w, h }; return { x: hit.x + pos.x, -- 2.45.2