Skip to content

Commit

Permalink
clean up spriteMap interface
Browse files Browse the repository at this point in the history
  • Loading branch information
vritant24 committed Jul 12, 2024
1 parent 18084a1 commit 6b2c8e2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
9 changes: 4 additions & 5 deletions libs/game/physics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ class ArcadePhysicsEngine extends PhysicsEngine {
if (s.vx || s.vy) s.clearObstacles();
});

this.map.clear();
this.map.resizeBuckets(this.sprites);
this.map.reset(this.sprites);

const MAX_STEP_COUNT = Fx.toInt(
Fx.idiv(
Expand Down Expand Up @@ -191,7 +190,7 @@ class ArcadePhysicsEngine extends PhysicsEngine {
s._y = Fx.add(s._y, stepY);

if (!(s.flags & SPRITE_NO_SPRITE_OVERLAPS)) {
this.map.insertAABB(s);
this.map.insertSprite(s);
}
if (tileMap && tileMap.enabled) {
this.tilemapCollisions(ms, tileMap);
Expand Down Expand Up @@ -316,7 +315,7 @@ class ArcadePhysicsEngine extends PhysicsEngine {
for (const ms of movedSprites) {
const sprite = ms.sprite;
if (sprite.flags & SPRITE_NO_SPRITE_OVERLAPS) continue;
const overSprites = this.map.overlaps(ms.sprite);
const overSprites = this.map.getOverlappingSprites(ms.sprite);

for (const overlapper of overSprites) {
if (overlapper.flags & SPRITE_NO_SPRITE_OVERLAPS) continue;
Expand Down Expand Up @@ -649,7 +648,7 @@ class ArcadePhysicsEngine extends PhysicsEngine {
* @param layer
*/
overlaps(sprite: Sprite): Sprite[] {
return this.map.overlaps(sprite);
return this.map.getOverlappingSprites(sprite);
}

/** moves a sprite explicitly outside of the normal velocity changes **/
Expand Down
9 changes: 4 additions & 5 deletions libs/game/physics2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,7 @@ class NewArcadePhysicsEngine implements IPhysicsEngine {
if (sprite.vx || sprite.vy) sprite.clearObstacles();
}

this.map.clear();
this.map.resizeBuckets(this.sprites);
this.map.reset(this.sprites);

const MAX_STEP_COUNT = Fx.toInt(
Fx.idiv(
Expand Down Expand Up @@ -314,7 +313,7 @@ class NewArcadePhysicsEngine implements IPhysicsEngine {
s._y = Fx.add(s._y, stepY);

if (!(s.flags & SPRITE_NO_SPRITE_OVERLAPS)) {
this.map.insertAABB(s);
this.map.insertSprite(s);
}
if (tileMap && tileMap.enabled) {
this.tilemapCollisions(ms, tileMap, this.onXAxisCollision, this.onYAxisCollision);
Expand Down Expand Up @@ -669,7 +668,7 @@ function defaultScreenEdgeCollisions(
function defaultSpriteCollisions (
movedSprites: MovingSprite[],
handlers: scene.OverlapHandler[],
map: sprites.SpriteMap
map: sprites.ISpriteMap
) {
control.enablePerfCounter("physics_sprite_collisions");
if (!handlers.length) return;
Expand All @@ -678,7 +677,7 @@ function defaultSpriteCollisions (
for (const ms of movedSprites) {
const sprite = ms.sprite;
if (sprite.flags & SPRITE_NO_SPRITE_OVERLAPS) continue;
const overSprites = map.overlaps(ms.sprite);
const overSprites = map.getOverlappingSprites(ms.sprite);

for (const overlapper of overSprites) {
if (overlapper.flags & SPRITE_NO_SPRITE_OVERLAPS) continue;
Expand Down
19 changes: 12 additions & 7 deletions libs/game/spritemap.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace sprites {
export interface ISpriteMap {
insertSprite(sprite: Sprite): void;
getOverlappingSprites(sprite: Sprite): Sprite[];
draw(): void;
resizeBuckets(sprites: Sprite[]): void;
clear(): void;
insertAABB(sprite: Sprite): void;
reset(sprites: Sprite[]): void;
}

export class SpriteMap implements ISpriteMap {
Expand Down Expand Up @@ -35,7 +35,7 @@ namespace sprites {
* Gets the overlaping sprites if any
* @param sprite
*/
public overlaps(sprite: Sprite): Sprite[] {
public getOverlappingSprites(sprite: Sprite): Sprite[] {
control.enablePerfCounter("spritemap_overlaps");
const n = this.neighbors(sprite);
const o = n.filter(neighbor => sprite.overlapsWith(neighbor));
Expand All @@ -58,7 +58,7 @@ namespace sprites {
/**
* Recompute hashes for all objects
*/
public resizeBuckets(sprites: Sprite[]) {
private resizeBuckets(sprites: Sprite[]) {
// rescale buckets
let maxWidth = 0;
let maxHeight = 0;
Expand All @@ -78,8 +78,13 @@ namespace sprites {
this.columnCount = Math.idiv(areaWidth, this.cellWidth);
}

public clear() {
/**
* Clears the spritemap and recomputes the buckets
* @param sprites
*/
public reset(sprites: Sprite[]) {
this.buckets = [];
this.resizeBuckets(sprites);
}

private key(x: number, y: number): number {
Expand All @@ -97,7 +102,7 @@ namespace sprites {
bucket.push(sprite);
}

public insertAABB(sprite: Sprite) {
public insertSprite(sprite: Sprite) {
const left = sprite.left;
const top = sprite.top;
const xn = Math.idiv(sprite.width + this.cellWidth - 1, this.cellWidth);
Expand Down

0 comments on commit 6b2c8e2

Please sign in to comment.