Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 10 additions & 17 deletions src/entity_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,7 @@ class EntityManager {
this.#entities = this.#entitiesUpdated;
this.#entitiesUpdated = [];
});
}

get entities(): Entity[] {
return this.#entities;
}

/**
* Calling this method is mandatory to start recording entities.
*/
initialize(): void {
this.#triangleHook();

this.#squareHook();
Expand All @@ -40,6 +31,10 @@ class EntityManager {
this.#playerHook();
}

get entities(): Entity[] {
return this.#entities;
}

/**
*
* @returns The own player entity
Expand All @@ -66,7 +61,7 @@ class EntityManager {
let parent = null;
if (type == EntityType.Bullet) {
// TODO: we want to change this to EntityType.Barrel in the future?
const parentIndex = this.#findEntity(EntityType.Player, position, 1000);
const parentIndex = this.#findEntity(EntityType.Player, position, 300);
if (parentIndex >= 0) {
parent = this.entities[parentIndex];
}
Expand All @@ -89,12 +84,14 @@ class EntityManager {
* Searches `#entities` for the entity that is closest to `position` and
* returns the __index__ of that entity or __-1__ if there is no match.
*/
#findEntity(type: EntityType, position: Vector, tolerance: number = 28): number {
#findEntity(type: EntityType, position: Vector, tolerance: number = 42): number {
let result = -1;
let shortestDistance = Number.MAX_SAFE_INTEGER;
let shortestDistance = Infinity;

this.#entities.forEach((x, i) => {
const distance = Vector.distance(x.predictPos(0), position);
if (x.type !== type) return;

const distance = Vector.distance(x.position, position);

if (distance < shortestDistance) {
shortestDistance = distance;
Expand All @@ -106,10 +103,6 @@ class EntityManager {
return -1;
}

if (this.#entities[result].type !== type) {
return -1;
}

return result;
}

Expand Down