From 56d0e4214509b0357d95ef0e03eac009016ff40b Mon Sep 17 00:00:00 2001 From: Cazka Date: Thu, 17 Mar 2022 00:01:33 +0100 Subject: [PATCH 1/3] 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/3] 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/3] 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; }