From b369b773c31f09b20affdfc161f404c5d727f810 Mon Sep 17 00:00:00 2001 From: Hassan Sufi Date: Fri, 12 Jul 2024 14:53:21 -0700 Subject: [PATCH] Insert a sprite into the map when we check for overlaps --- libs/game/physics.ts | 2 +- libs/game/physics2.ts | 3 --- libs/game/spritemap.ts | 45 ++++++++++++++++-------------------------- 3 files changed, 18 insertions(+), 32 deletions(-) diff --git a/libs/game/physics.ts b/libs/game/physics.ts index 3778ace43..52237299b 100644 --- a/libs/game/physics.ts +++ b/libs/game/physics.ts @@ -190,7 +190,7 @@ class ArcadePhysicsEngine extends PhysicsEngine { s._y = Fx.add(s._y, stepY); if (!(s.flags & SPRITE_NO_SPRITE_OVERLAPS)) { - this.map.insertSprite(s); + // this.map.insertSprite(s); } if (tileMap && tileMap.enabled) { this.tilemapCollisions(ms, tileMap); diff --git a/libs/game/physics2.ts b/libs/game/physics2.ts index 2cf57094a..c59388878 100644 --- a/libs/game/physics2.ts +++ b/libs/game/physics2.ts @@ -322,9 +322,6 @@ class NewArcadePhysicsEngine implements IPhysicsEngine { s._x = Fx.add(s._x, stepX); s._y = Fx.add(s._y, stepY); - if (!(s.flags & SPRITE_NO_SPRITE_OVERLAPS)) { - this.map.insertSprite(s); - } if (tileMap && tileMap.enabled) { this.tilemapCollisions(ms, tileMap, this.onXAxisCollision, this.onYAxisCollision); } diff --git a/libs/game/spritemap.ts b/libs/game/spritemap.ts index 72cdcf7d5..ed7cdc324 100644 --- a/libs/game/spritemap.ts +++ b/libs/game/spritemap.ts @@ -1,6 +1,5 @@ namespace sprites { export interface ISpriteMap { - insertSprite(sprite: Sprite): void; getOverlappingSprites(sprite: Sprite): Sprite[]; draw(camera: scene.Camera): void; reset(sprites: Sprite[]): void; @@ -24,8 +23,23 @@ namespace sprites { const neighbors: { [key: string]: Sprite } = {}; const layer = sprite.layer; - for (const key of this.getBucketsKeys(sprite)) { - this.mergeAt(key, layer, neighbors) + 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 + ); + for (let x = 0; x <= xn; x++) { + for (let y = 0; y <= yn; y++) { + const key = this.key(left + Math.min(sprite.width, x * this.cellWidth), + top + Math.min(sprite.height, y * this.cellHeight)) + this.insertAt(key, sprite) + this.mergeAt(key, layer, neighbors) + } } neighbors[sprite.id] = undefined; return neighbors; @@ -109,31 +123,6 @@ namespace sprites { if (bucket.indexOf(sprite) < 0) bucket.push(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 - ); - let keys = [] - for (let x = 0; x <= xn; x++) - for (let y = 0; y <= yn; y++) - 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 mergeAt(key: number, layer: number, neighbors: { [key: string]: Sprite }) { const bucket = this.buckets[key]; if (bucket) {