forked from arne/asdf-games
Merge pull request 'Merge branch entity-center-fix' (#4) from entity-center-fix into master
This commit is contained in:
commit
2c28b9205c
10
lib/index.d.ts
vendored
10
lib/index.d.ts
vendored
@ -602,9 +602,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 {
|
||||
/**
|
||||
@ -648,7 +652,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<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.
|
||||
|
@ -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,
|
||||
@ -31,7 +40,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
|
||||
|
Loading…
Reference in New Issue
Block a user