Added support for tileSprites in entity.bounds()

This commit is contained in:
corner 2020-04-24 09:12:47 +02:00
parent e93c7fe6f1
commit 0c042160ab
2 changed files with 11 additions and 2 deletions

2
lib/index.d.ts vendored
View File

@ -650,7 +650,7 @@ export namespace entity {
* @param container The container. * @param container The container.
* @param hitCallback The callback that is executed when an entity hits something in 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<NumericalEntityWithHitbox | NumericalEntity>, hitCallback: (e2: NumericalEntityWithHitbox | NumericalEntity) => any): void; export function hits<T extends NumericalEntityWithHitbox | NumericalEntity>(entity: NumericalEntityWithHitbox | NumericalEntity, container: Container<T>, hitCallback: (e2: T) => any): void;
/** /**
* This functions calculates whether two entities hit each other. * This functions calculates whether two entities hit each other.

View File

@ -20,7 +20,16 @@ function angle(a, b) {
} }
function bounds(entity) { 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 }; const hit = hitBox || { x: 0, y: 0, w, h };
return { return {
x: hit.x + pos.x, x: hit.x + pos.x,