fix some ts errors

This commit is contained in:
Elizabeth Hunt 2023-08-21 14:19:02 -06:00
parent 432ce5428f
commit 8fce5a5f25
2 changed files with 20 additions and 20 deletions

View File

@ -1,5 +1,5 @@
import type { Coord2D, Dimension2D } from "../interfaces";
import type { RefreshingCollisionFinderBehavior } from ".";
import type { BoxedEntry, RefreshingCollisionFinderBehavior } from ".";
export class Grid implements RefreshingCollisionFinderBehavior {
private cellEntities: Map<number, string[]>;
@ -11,7 +11,7 @@ export class Grid implements RefreshingCollisionFinderBehavior {
constructor(
gridDimension: Dimension2D,
cellDimension: Dimension2D,
topLeft = { x: 0, y: 0 },
topLeft = { x: 0, y: 0 }
) {
this.gridDimension = gridDimension;
this.cellDimension = cellDimension;
@ -25,7 +25,7 @@ export class Grid implements RefreshingCollisionFinderBehavior {
if (!this.cellEntities.has(gridIdx)) {
this.cellEntities.set(gridIdx, []);
}
this.cellEntities.get(gridIdx).push(boxedEntry.id);
this.cellEntities.get(gridIdx)!.push(boxedEntry.id);
});
}
@ -33,7 +33,7 @@ export class Grid implements RefreshingCollisionFinderBehavior {
const neighborIds: Set<string> = new Set();
this.getOverlappingCells(boxedEntry).forEach((gridIdx) => {
if (this.cellEntities.has(gridIdx)) {
this.cellEntities.get(gridIdx).forEach((id) => neighborIds.add(id));
this.cellEntities.get(gridIdx)!.forEach((id) => neighborIds.add(id));
}
});
return neighborIds;
@ -58,10 +58,10 @@ export class Grid implements RefreshingCollisionFinderBehavior {
private getOverlappingCells(boxedEntry: BoxedEntry): number[] {
const { center, dimension } = boxedEntry;
const yBoxes = Math.ceil(
this.gridDimension.height / this.cellDimension.height,
this.gridDimension.height / this.cellDimension.height
);
const xBoxes = Math.ceil(
this.gridDimension.width / this.cellDimension.width,
this.gridDimension.width / this.cellDimension.width
);
const translated: Coord2D = {
@ -71,18 +71,18 @@ export class Grid implements RefreshingCollisionFinderBehavior {
const topLeftBox = {
x: Math.floor(
(translated.x - dimension.width / 2) / this.cellDimension.width,
(translated.x - dimension.width / 2) / this.cellDimension.width
),
y: Math.floor(
(translated.y - dimension.height / 2) / this.cellDimension.height,
(translated.y - dimension.height / 2) / this.cellDimension.height
),
};
const bottomRightBox = {
x: Math.floor(
(translated.x + dimension.width / 2) / this.cellDimension.width,
(translated.x + dimension.width / 2) / this.cellDimension.width
),
y: Math.floor(
(translated.y + dimension.height / 2) / this.cellDimension.height,
(translated.y + dimension.height / 2) / this.cellDimension.height
),
};

View File

@ -30,7 +30,7 @@ export class QuadTree implements RefreshingCollisionFinderBehavior {
dimension: Dimension2D,
maxLevels: number = QuadTree.QUADTREE_MAX_LEVELS,
splitThreshold: number = QuadTree.QUADTREE_SPLIT_THRESHOLD,
level: number = 0,
level: number = 0
) {
this.children = new Map<Quadrant, QuadTree>();
this.objects = [];
@ -74,9 +74,9 @@ export class QuadTree implements RefreshingCollisionFinderBehavior {
}
}
public getNeighborIds(boxedEntry: BoxedEntry): string[] {
public getNeighborIds(boxedEntry: BoxedEntry): Set<string> {
const neighbors = new Set<string>(
this.objects.map(({ id }) => id).filter((id) => id != boxedEntry.id),
this.objects.map(({ id }) => id).filter((id) => id != boxedEntry.id)
);
if (this.hasChildren()) {
@ -104,7 +104,7 @@ export class QuadTree implements RefreshingCollisionFinderBehavior {
Quadrant.IV,
{ x: this.topLeft.x + halfWidth, y: this.topLeft.y + halfHeight },
],
] as [[Quadrant, Coord2D]]
] as [Quadrant, Coord2D][]
).forEach(([quadrant, pos]) => {
this.children.set(
quadrant,
@ -113,8 +113,8 @@ export class QuadTree implements RefreshingCollisionFinderBehavior {
{ width: halfWidth, height: halfHeight },
this.maxLevels,
this.splitThreshold,
this.level + 1,
),
this.level + 1
)
);
});
}
@ -143,18 +143,18 @@ export class QuadTree implements RefreshingCollisionFinderBehavior {
Quadrant.IV,
(x: number, y: number) => x >= treeCenter.x && y >= treeCenter.y,
],
] as [[Quadrant, (x: number, y: number) => boolean]]
] as [Quadrant, (x: number, y: number) => boolean][]
)
.filter(
([_quadrant, condition]) =>
condition(
boxedEntry.center.x + boxedEntry.dimension.width / 2,
boxedEntry.center.y + boxedEntry.dimension.height / 2,
boxedEntry.center.y + boxedEntry.dimension.height / 2
) ||
condition(
boxedEntry.center.x - boxedEntry.dimension.width / 2,
boxedEntry.center.y - boxedEntry.dimension.height / 2,
),
boxedEntry.center.y - boxedEntry.dimension.height / 2
)
)
.map(([quadrant]) => quadrant);
}