Skip to content

Commit

Permalink
fix drawing and clear obstacles
Browse files Browse the repository at this point in the history
  • Loading branch information
riknoll committed Dec 14, 2023
1 parent 51f4ff2 commit bb6a205
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
23 changes: 8 additions & 15 deletions libs/game/physics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ class ArcadePhysicsEngine extends PhysicsEngine {
// clear obstacles if moving on that axis
this.sprites.forEach(s => {
if (s.vx || s.vy) s.clearObstacles();
else {
for (const ob of s._obstacles) {
if (ob && (ob.tilemap.vx || ob.tilemap.vy)) {
s.clearObstacles();
break;
}
}
}
});

this.map.clear();
Expand Down Expand Up @@ -1000,19 +1008,4 @@ function ___overlapsTilemap(sprite: Sprite, tilemap: tiles.TileMap) {
}

return false;
}

function ___overlapsTile(sprite: Sprite, left: Fx8, top: Fx8, col: number, row: number, tileScale: number) {
left = Fx.iadd(col << tileScale, left);
top = Fx.iadd(row << tileScale, top);

if (
Fx.compare(sprite._hitbox.left, Fx.iadd(1 << tileScale, left)) > 0 ||
Fx.compare(sprite._hitbox.top, Fx.iadd(1 << tileScale, top)) > 0 ||
Fx.compare(sprite._hitbox.right, left) < 0 ||
Fx.compare(sprite._hitbox.bottom, top) < 0
) {
return false;
}
return true;
}
17 changes: 9 additions & 8 deletions libs/game/tilemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,13 +469,14 @@ namespace tiles {
const offsetX = camera.drawOffsetX & bitmask;
const offsetY = camera.drawOffsetY & bitmask;

const x0 = Math.max(0, camera.drawOffsetX >> this.scale);
const xn = Math.min(this._map.width, ((camera.drawOffsetX + target.width) >> this.scale) + 1);
const y0 = Math.max(0, camera.drawOffsetY >> this.scale);
const yn = Math.min(this._map.height, ((camera.drawOffsetY + target.height) >> this.scale) + 1);
const l = Fx.toInt(this.left) - camera.drawOffsetX;
const t = Fx.toInt(this.top) - camera.drawOffsetY;

const x0 = Math.max(0, -l >> this.scale);
const xn = Math.min(this._map.width, ((-l + target.width) >> this.scale) + 1);
const y0 = Math.max(0, -t >> this.scale);
const yn = Math.min(this._map.height, ((-t + target.height) >> this.scale) + 1);

const l = Fx.toInt(this.left);
const t = Fx.toInt(this.top);

for (let x = x0; x <= xn; ++x) {
for (let y = y0; y <= yn; ++y) {
Expand All @@ -484,8 +485,8 @@ namespace tiles {
if (tile) {
target.drawTransparentImage(
tile,
l + ((x - x0) << this.scale) - offsetX,
t + ((y - y0) << this.scale) - offsetY
l + (x << this.scale),
t + (y << this.scale)
);
}
}
Expand Down

0 comments on commit bb6a205

Please sign in to comment.