Skip to content

Commit

Permalink
Grab all buckets a sprite belongs to, not just the corners
Browse files Browse the repository at this point in the history
  • Loading branch information
aznhassan committed Jul 12, 2024
1 parent 17bead5 commit bfad889
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions libs/game/spritemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ namespace sprites {
*/
private neighbors(sprite: Sprite): { [key: string]: Sprite } {
const neighbors: { [key: string]: Sprite } = {};

const layer = sprite.layer;
this.mergeAtKey(sprite.left, sprite.top, layer, neighbors)
this.mergeAtKey(sprite.left, sprite.bottom, layer, neighbors)
this.mergeAtKey(sprite.right, sprite.top, layer, neighbors)
this.mergeAtKey(sprite.right, sprite.bottom, layer, neighbors)
neighbors[sprite.id] = undefined;

for (const key of this.getBucketsKeys(sprite)) {
this.mergeAt(key, layer, neighbors)
}
neighbors[sprite.id] = undefined;
return neighbors;
}

Expand Down Expand Up @@ -105,28 +103,39 @@ namespace sprites {
return xi + yi * this.columnCount;
}

private insertAtKey(x: number, y: number, sprite: Sprite) {
const k = this.key(x, y);
let bucket = this.buckets[k];
if (!bucket)
bucket = this.buckets[k] = [];
if (bucket.indexOf(sprite) < 0)
bucket.push(sprite);
private insertAt(key: number, sprite: Sprite) {
let bucket = this.buckets[key];
if (!bucket) bucket = this.buckets[key] = [];
if (bucket.indexOf(sprite) < 0) bucket.push(sprite);
}

public insertSprite(sprite: Sprite) {
private getBucketsKeys(sprite: Sprite): number[] {
const left = sprite.left;
const top = sprite.top;
const xn = Math.idiv(sprite.width + this.cellWidth - 1, this.cellWidth);
const yn = Math.idiv(sprite.height + this.cellHeight - 1, this.cellHeight);
const xn = Math.idiv(
sprite.width + this.cellWidth - 1,
this.cellWidth
);
const yn = Math.idiv(
sprite.height + this.cellHeight - 1,
this.cellHeight
);
let keys = []
for (let x = 0; x <= xn; x++)
for (let y = 0; y <= yn; y++)
this.insertAtKey(left + Math.min(sprite.width, x * this.cellWidth), top + Math.min(sprite.height, y * this.cellHeight), sprite)
keys.push(this.key(left + Math.min(sprite.width, x * this.cellWidth),
top + Math.min(sprite.height, y * this.cellHeight)))
return keys
}

public insertSprite(sprite: Sprite) {
for (const key of this.getBucketsKeys(sprite)) {
this.insertAt(key, sprite)
}
}

private mergeAtKey(x: number, y: number, layer: number, neighbors: { [key: string]: Sprite }) {
const k = this.key(x, y);
const bucket = this.buckets[k];
private mergeAt(key: number, layer: number, neighbors: { [key: string]: Sprite }) {
const bucket = this.buckets[key];
if (bucket) {
for (const sprite of bucket) {
if ((sprite.layer & layer) && neighbors[sprite.id] === undefined) {
Expand Down

0 comments on commit bfad889

Please sign in to comment.