|
1 | 1 | import { test } from 'node:test'; |
2 | 2 | import assert from 'node:assert/strict'; |
3 | | -import { TileCollisionLayer, tile, vec2 } from '../dist/littlejs.esm.js'; |
| 3 | +import { TileCollisionLayer, tileCollisionGetData, tile, vec2 } from '../dist/littlejs.esm.js'; |
4 | 4 |
|
5 | 5 | // Regression for the negative-edge ghost-collision bug: |
6 | 6 | // collisionTest() clamped minX/minY to 0 and then forced maxX/maxY to at least |
@@ -57,3 +57,26 @@ test('collisionTest: AABB entirely off the positive Y edge does not collide', () |
57 | 57 | const layer = makeLayer(); |
58 | 58 | assert.equal(layer.collisionTest(vec2(0.5, 100), vec2(1, 1)), false); |
59 | 59 | }); |
| 60 | + |
| 61 | +// Regression for the layer-offset bug: tileCollisionGetData() passed the world |
| 62 | +// position directly to layer.getCollisionData(), which expects layer-local |
| 63 | +// coordinates. A layer placed away from the origin would report collision as |
| 64 | +// if it were still at (0,0) even though rendering honored the offset. |
| 65 | + |
| 66 | +test('tileCollisionGetData: honors layer position offset', () => |
| 67 | +{ |
| 68 | + const layer = new TileCollisionLayer(vec2(2, 2), vec2(4, 4), tile(0, 16), 0, false); |
| 69 | + layer.setCollisionData(vec2(1, 1), 1); |
| 70 | + // tile at local (1,1) occupies world cell (3,3) |
| 71 | + assert.equal(tileCollisionGetData(vec2(3.5, 3.5)), 1); |
| 72 | + layer.destroy(); |
| 73 | +}); |
| 74 | + |
| 75 | +test('tileCollisionGetData: no collision at the unoffset position', () => |
| 76 | +{ |
| 77 | + const layer = new TileCollisionLayer(vec2(2, 2), vec2(4, 4), tile(0, 16), 0, false); |
| 78 | + layer.setCollisionData(vec2(1, 1), 1); |
| 79 | + // world (1.5, 1.5) is local (-0.5, -0.5), outside the layer |
| 80 | + assert.equal(tileCollisionGetData(vec2(1.5, 1.5)), 0); |
| 81 | + layer.destroy(); |
| 82 | +}); |
0 commit comments