From 56d0e4214509b0357d95ef0e03eac009016ff40b Mon Sep 17 00:00:00 2001 From: Cazka Date: Thu, 17 Mar 2022 00:01:33 +0100 Subject: [PATCH 1/7] draw link between bullet and owner --- tools/entities_debug_tool.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tools/entities_debug_tool.ts b/tools/entities_debug_tool.ts index 41344ae..2d21286 100644 --- a/tools/entities_debug_tool.ts +++ b/tools/entities_debug_tool.ts @@ -1,5 +1,7 @@ import { entityManager, game, arenaScaling, Vector, CanvasKit } from 'index'; +entityManager.initialize(); + class EntityOverlay { #canvas: HTMLCanvasElement; #ctx: CanvasRenderingContext2D; @@ -45,6 +47,18 @@ class EntityOverlay { this.#ctx.lineTo(futurePos.x, futurePos.y); this.#ctx.stroke(); + // parent + if (entity.parent !== null) { + const parentPos = arenaScaling.toCanvasPos(entity.parent.position); + + this.#ctx.strokeStyle = '#8aff69'; + this.#ctx.lineWidth = 3; + this.#ctx.beginPath(); + this.#ctx.moveTo(position.x, position.y); + this.#ctx.lineTo(parentPos.x, parentPos.y); + this.#ctx.stroke(); + } + //Time alive + id const fontSize = arenaScaling.toCanvasUnits(new Vector(30, 30)); this.#ctx.font = fontSize.x + 'px Ubuntu'; From ef86029d2541f01559750b76fc5a25e442fb0e17 Mon Sep 17 00:00:00 2001 From: Cazka Date: Thu, 17 Mar 2022 00:01:44 +0100 Subject: [PATCH 2/7] export some more useful stuff --- src/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/index.ts b/src/index.ts index 90f5a37..8ab002b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ export { Vector } from './vector'; export { CanvasKit } from './canvas_kit'; +export { EntityColor, EntityType, TeamColors, Entity } from './entity'; export { gamepad } from './diep_gamepad'; export { game } from './game'; From 00a3d39666daf6667047674c7085edd349ef2f4e Mon Sep 17 00:00:00 2001 From: Cazka Date: Thu, 17 Mar 2022 00:02:01 +0100 Subject: [PATCH 3/7] add logic to link bullet to parent --- src/entity.ts | 2 +- src/entity_manager.ts | 20 ++++++++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/entity.ts b/src/entity.ts index 5708227..f43827e 100644 --- a/src/entity.ts +++ b/src/entity.ts @@ -35,7 +35,7 @@ export const TeamColors = [EntityColor.TeamBlue, EntityColor.TeamRed, EntityColo * Holds minimal information currently. */ export class Entity extends Movement { - constructor(readonly type: EntityType, readonly extras: object = {}) { + constructor(readonly type: EntityType, readonly parent: Entity, readonly extras: object = {}) { super(); } diff --git a/src/entity_manager.ts b/src/entity_manager.ts index 02fd99e..40a53b8 100644 --- a/src/entity_manager.ts +++ b/src/entity_manager.ts @@ -63,7 +63,16 @@ class EntityManager { let entity: Entity; if (entityIndex === -1) { - entity = new Entity(type, { + 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); + if (parentIndex >= 0) { + parent = this.entities[parentIndex]; + } + } + + entity = new Entity(type, parent, { id: Math.random().toString(36).slice(2, 5), timestamp: performance.now(), ...extras, @@ -80,7 +89,7 @@ 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): number { + #findEntity(type: EntityType, position: Vector, tolerance: number = 28): number { let result = -1; let shortestDistance = Number.MAX_SAFE_INTEGER; @@ -93,12 +102,11 @@ class EntityManager { } }); - //if distance is too high - if (shortestDistance > 28 /* accuracy */) { + if (shortestDistance > tolerance) { return -1; } - //sanity check - if (EntityType.UNKNOWN !== type && this.#entities[result].type !== type) { + + if (this.#entities[result].type !== type) { return -1; } From 77110227bacb3416944ffa6c1ce005329a1f4a8f Mon Sep 17 00:00:00 2001 From: Cazka Date: Thu, 17 Mar 2022 01:14:59 +0100 Subject: [PATCH 4/7] should improve bullet links --- src/entity_manager.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/entity_manager.ts b/src/entity_manager.ts index 40a53b8..9044f8c 100644 --- a/src/entity_manager.ts +++ b/src/entity_manager.ts @@ -66,7 +66,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]; } @@ -89,12 +89,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; 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; @@ -106,10 +108,6 @@ class EntityManager { return -1; } - if (this.#entities[result].type !== type) { - return -1; - } - return result; } From 226d5e8d023528b82cdf16c9c3fbebb7d40eb746 Mon Sep 17 00:00:00 2001 From: Cazka Date: Thu, 17 Mar 2022 18:01:31 +0100 Subject: [PATCH 5/7] removing initialize method --- src/entity_manager.ts | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/entity_manager.ts b/src/entity_manager.ts index 9044f8c..e5d57e7 100644 --- a/src/entity_manager.ts +++ b/src/entity_manager.ts @@ -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(); @@ -40,6 +31,10 @@ class EntityManager { this.#playerHook(); } + get entities(): Entity[] { + return this.#entities; + } + /** * * @returns The own player entity From d3b253a1dfc5af58772d2287fd7709983f7fd08a Mon Sep 17 00:00:00 2001 From: Cazka Date: Thu, 17 Mar 2022 21:45:23 +0100 Subject: [PATCH 6/7] infinity looks cooler --- src/entity_manager.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/entity_manager.ts b/src/entity_manager.ts index e5d57e7..bd92a67 100644 --- a/src/entity_manager.ts +++ b/src/entity_manager.ts @@ -86,7 +86,7 @@ class EntityManager { */ #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) => { if (x.type !== type) return; From c435e621c495e5c3f906c3b2b0caa1e988a5ad79 Mon Sep 17 00:00:00 2001 From: Cazka Date: Thu, 17 Mar 2022 21:58:11 +0100 Subject: [PATCH 7/7] remove empty lines --- src/entity_manager.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/entity_manager.ts b/src/entity_manager.ts index d6ad732..bd92a67 100644 --- a/src/entity_manager.ts +++ b/src/entity_manager.ts @@ -62,7 +62,6 @@ class EntityManager { if (type == EntityType.Bullet) { // TODO: we want to change this to EntityType.Barrel in the future? const parentIndex = this.#findEntity(EntityType.Player, position, 300); - if (parentIndex >= 0) { parent = this.entities[parentIndex]; } @@ -86,7 +85,6 @@ class EntityManager { * returns the __index__ of that entity or __-1__ if there is no match. */ #findEntity(type: EntityType, position: Vector, tolerance: number = 42): number { - let result = -1; let shortestDistance = Infinity;