Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
20 changes: 14 additions & 6 deletions src/entity_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;

Expand All @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
14 changes: 14 additions & 0 deletions tools/entities_debug_tool.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { entityManager, game, arenaScaling, Vector, CanvasKit } from 'index';

entityManager.initialize();

class EntityOverlay {
#canvas: HTMLCanvasElement;
#ctx: CanvasRenderingContext2D;
Expand Down Expand Up @@ -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';
Expand Down