fix some ts errors
This commit is contained in:
parent
432ce5428f
commit
8fce5a5f25
@ -1,5 +1,5 @@
|
|||||||
import type { Coord2D, Dimension2D } from "../interfaces";
|
import type { Coord2D, Dimension2D } from "../interfaces";
|
||||||
import type { RefreshingCollisionFinderBehavior } from ".";
|
import type { BoxedEntry, RefreshingCollisionFinderBehavior } from ".";
|
||||||
|
|
||||||
export class Grid implements RefreshingCollisionFinderBehavior {
|
export class Grid implements RefreshingCollisionFinderBehavior {
|
||||||
private cellEntities: Map<number, string[]>;
|
private cellEntities: Map<number, string[]>;
|
||||||
@ -11,7 +11,7 @@ export class Grid implements RefreshingCollisionFinderBehavior {
|
|||||||
constructor(
|
constructor(
|
||||||
gridDimension: Dimension2D,
|
gridDimension: Dimension2D,
|
||||||
cellDimension: Dimension2D,
|
cellDimension: Dimension2D,
|
||||||
topLeft = { x: 0, y: 0 },
|
topLeft = { x: 0, y: 0 }
|
||||||
) {
|
) {
|
||||||
this.gridDimension = gridDimension;
|
this.gridDimension = gridDimension;
|
||||||
this.cellDimension = cellDimension;
|
this.cellDimension = cellDimension;
|
||||||
@ -25,7 +25,7 @@ export class Grid implements RefreshingCollisionFinderBehavior {
|
|||||||
if (!this.cellEntities.has(gridIdx)) {
|
if (!this.cellEntities.has(gridIdx)) {
|
||||||
this.cellEntities.set(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();
|
const neighborIds: Set<string> = new Set();
|
||||||
this.getOverlappingCells(boxedEntry).forEach((gridIdx) => {
|
this.getOverlappingCells(boxedEntry).forEach((gridIdx) => {
|
||||||
if (this.cellEntities.has(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;
|
return neighborIds;
|
||||||
@ -58,10 +58,10 @@ export class Grid implements RefreshingCollisionFinderBehavior {
|
|||||||
private getOverlappingCells(boxedEntry: BoxedEntry): number[] {
|
private getOverlappingCells(boxedEntry: BoxedEntry): number[] {
|
||||||
const { center, dimension } = boxedEntry;
|
const { center, dimension } = boxedEntry;
|
||||||
const yBoxes = Math.ceil(
|
const yBoxes = Math.ceil(
|
||||||
this.gridDimension.height / this.cellDimension.height,
|
this.gridDimension.height / this.cellDimension.height
|
||||||
);
|
);
|
||||||
const xBoxes = Math.ceil(
|
const xBoxes = Math.ceil(
|
||||||
this.gridDimension.width / this.cellDimension.width,
|
this.gridDimension.width / this.cellDimension.width
|
||||||
);
|
);
|
||||||
|
|
||||||
const translated: Coord2D = {
|
const translated: Coord2D = {
|
||||||
@ -71,18 +71,18 @@ export class Grid implements RefreshingCollisionFinderBehavior {
|
|||||||
|
|
||||||
const topLeftBox = {
|
const topLeftBox = {
|
||||||
x: Math.floor(
|
x: Math.floor(
|
||||||
(translated.x - dimension.width / 2) / this.cellDimension.width,
|
(translated.x - dimension.width / 2) / this.cellDimension.width
|
||||||
),
|
),
|
||||||
y: Math.floor(
|
y: Math.floor(
|
||||||
(translated.y - dimension.height / 2) / this.cellDimension.height,
|
(translated.y - dimension.height / 2) / this.cellDimension.height
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
const bottomRightBox = {
|
const bottomRightBox = {
|
||||||
x: Math.floor(
|
x: Math.floor(
|
||||||
(translated.x + dimension.width / 2) / this.cellDimension.width,
|
(translated.x + dimension.width / 2) / this.cellDimension.width
|
||||||
),
|
),
|
||||||
y: Math.floor(
|
y: Math.floor(
|
||||||
(translated.y + dimension.height / 2) / this.cellDimension.height,
|
(translated.y + dimension.height / 2) / this.cellDimension.height
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -30,7 +30,7 @@ export class QuadTree implements RefreshingCollisionFinderBehavior {
|
|||||||
dimension: Dimension2D,
|
dimension: Dimension2D,
|
||||||
maxLevels: number = QuadTree.QUADTREE_MAX_LEVELS,
|
maxLevels: number = QuadTree.QUADTREE_MAX_LEVELS,
|
||||||
splitThreshold: number = QuadTree.QUADTREE_SPLIT_THRESHOLD,
|
splitThreshold: number = QuadTree.QUADTREE_SPLIT_THRESHOLD,
|
||||||
level: number = 0,
|
level: number = 0
|
||||||
) {
|
) {
|
||||||
this.children = new Map<Quadrant, QuadTree>();
|
this.children = new Map<Quadrant, QuadTree>();
|
||||||
this.objects = [];
|
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>(
|
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()) {
|
if (this.hasChildren()) {
|
||||||
@ -104,7 +104,7 @@ export class QuadTree implements RefreshingCollisionFinderBehavior {
|
|||||||
Quadrant.IV,
|
Quadrant.IV,
|
||||||
{ x: this.topLeft.x + halfWidth, y: this.topLeft.y + halfHeight },
|
{ x: this.topLeft.x + halfWidth, y: this.topLeft.y + halfHeight },
|
||||||
],
|
],
|
||||||
] as [[Quadrant, Coord2D]]
|
] as [Quadrant, Coord2D][]
|
||||||
).forEach(([quadrant, pos]) => {
|
).forEach(([quadrant, pos]) => {
|
||||||
this.children.set(
|
this.children.set(
|
||||||
quadrant,
|
quadrant,
|
||||||
@ -113,8 +113,8 @@ export class QuadTree implements RefreshingCollisionFinderBehavior {
|
|||||||
{ width: halfWidth, height: halfHeight },
|
{ width: halfWidth, height: halfHeight },
|
||||||
this.maxLevels,
|
this.maxLevels,
|
||||||
this.splitThreshold,
|
this.splitThreshold,
|
||||||
this.level + 1,
|
this.level + 1
|
||||||
),
|
)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -143,18 +143,18 @@ export class QuadTree implements RefreshingCollisionFinderBehavior {
|
|||||||
Quadrant.IV,
|
Quadrant.IV,
|
||||||
(x: number, y: number) => x >= treeCenter.x && y >= treeCenter.y,
|
(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(
|
.filter(
|
||||||
([_quadrant, condition]) =>
|
([_quadrant, condition]) =>
|
||||||
condition(
|
condition(
|
||||||
boxedEntry.center.x + boxedEntry.dimension.width / 2,
|
boxedEntry.center.x + boxedEntry.dimension.width / 2,
|
||||||
boxedEntry.center.y + boxedEntry.dimension.height / 2,
|
boxedEntry.center.y + boxedEntry.dimension.height / 2
|
||||||
) ||
|
) ||
|
||||||
condition(
|
condition(
|
||||||
boxedEntry.center.x - boxedEntry.dimension.width / 2,
|
boxedEntry.center.x - boxedEntry.dimension.width / 2,
|
||||||
boxedEntry.center.y - boxedEntry.dimension.height / 2,
|
boxedEntry.center.y - boxedEntry.dimension.height / 2
|
||||||
),
|
)
|
||||||
)
|
)
|
||||||
.map(([quadrant]) => quadrant);
|
.map(([quadrant]) => quadrant);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user