M1.1: Fix PathfindingSystem tile size — 64→32 via config injection
PathfindingSystem constructor now accepts optional {tileWidth, tileHeight}
config param with 64px defaults for backward compat.
SystemOrchestrator.initPathfinding() reads tilemap.tileWidth/tileHeight
and passes them through. Verification console.log added to initGrid().
6 new tests:
- Constructor accepts config / defaults to 64 / empty config → 64
- worldToTileCoords: 32px tiles → (1,1), 64px tiles → (2,1), default
All 32 PathfindingSystem tests pass, zero regressions (167 total).
This commit is contained in:
@@ -11,8 +11,9 @@ export default class PathfindingSystem {
|
||||
/**
|
||||
* @param {Phaser.Scene} scene
|
||||
* @param {Phaser.Tilemaps.Tilemap} tilemap
|
||||
* @param {{tileWidth?: number, tileHeight?: number}} [config]
|
||||
*/
|
||||
constructor(scene, tilemap) {
|
||||
constructor(scene, tilemap, config = {}) {
|
||||
this.scene = scene;
|
||||
this.tilemap = tilemap;
|
||||
|
||||
@@ -26,10 +27,10 @@ export default class PathfindingSystem {
|
||||
this.grid = [];
|
||||
|
||||
/** @type {number} tile width in pixels (default 64) */
|
||||
this.tileWidth = 64;
|
||||
this.tileWidth = config.tileWidth || 64;
|
||||
|
||||
/** @type {number} tile height in pixels (default 64) */
|
||||
this.tileHeight = 64;
|
||||
this.tileHeight = config.tileHeight || 64;
|
||||
|
||||
/** @type {boolean} whether the grid has been initialized */
|
||||
this._initialized = false;
|
||||
@@ -109,6 +110,10 @@ export default class PathfindingSystem {
|
||||
// Set diagonal cost multiplier
|
||||
this.easystar.setAdditionalPointCost(1.5);
|
||||
|
||||
console.log(
|
||||
`[PathfindingSystem] initGrid: ${width}x${height} grid, tile size ${this.tileWidth}x${this.tileHeight}px`
|
||||
);
|
||||
|
||||
this._initialized = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -159,9 +159,14 @@ export default class SystemOrchestrator {
|
||||
this.systems.pathfinding = null;
|
||||
}
|
||||
|
||||
const tilemap = this.systems.map.tilemap;
|
||||
const tileWidth = tilemap.tileWidth || 32;
|
||||
const tileHeight = tilemap.tileHeight || 32;
|
||||
|
||||
this.systems.pathfinding = new PathfindingSystem(
|
||||
this.scene,
|
||||
this.systems.map.tilemap,
|
||||
tilemap,
|
||||
{ tileWidth, tileHeight },
|
||||
);
|
||||
|
||||
this.systems.pathfinding.initGrid('Rocks', 'Floor');
|
||||
|
||||
@@ -339,6 +339,57 @@ describe('PathfindingSystem', () => {
|
||||
});
|
||||
});
|
||||
|
||||
// ── config constructor ──────────────────────────────────────────
|
||||
describe('config constructor', () => {
|
||||
test('constructor accepts config for tileWidth and tileHeight', () => {
|
||||
const pf = new PathfindingSystem(mockScene, mockTilemap, {
|
||||
tileWidth: 32,
|
||||
tileHeight: 32,
|
||||
});
|
||||
expect(pf.tileWidth).toBe(32);
|
||||
expect(pf.tileHeight).toBe(32);
|
||||
});
|
||||
|
||||
test('constructor defaults to 64 when no config provided', () => {
|
||||
const pf = new PathfindingSystem(mockScene, mockTilemap);
|
||||
expect(pf.tileWidth).toBe(64);
|
||||
expect(pf.tileHeight).toBe(64);
|
||||
});
|
||||
|
||||
test('constructor defaults to 64 when config lacks tile dimensions', () => {
|
||||
const pf = new PathfindingSystem(mockScene, mockTilemap, {});
|
||||
expect(pf.tileWidth).toBe(64);
|
||||
expect(pf.tileHeight).toBe(64);
|
||||
});
|
||||
});
|
||||
|
||||
// ── worldToTileCoords with configurable tile size ────────────────
|
||||
describe('worldToTileCoords with config', () => {
|
||||
test('worldToTileCoords(32, 32) returns (1, 1) with 32px tiles', () => {
|
||||
const pf = new PathfindingSystem(mockScene, mockTilemap, {
|
||||
tileWidth: 32,
|
||||
tileHeight: 32,
|
||||
});
|
||||
const result = pf.worldToTileCoords(32, 32);
|
||||
expect(result).toEqual({ x: 1, y: 1 });
|
||||
});
|
||||
|
||||
test('worldToTileCoords(128, 64) returns (2, 1) with 64px tiles (backward compat)', () => {
|
||||
const pf = new PathfindingSystem(mockScene, mockTilemap, {
|
||||
tileWidth: 64,
|
||||
tileHeight: 64,
|
||||
});
|
||||
const result = pf.worldToTileCoords(128, 64);
|
||||
expect(result).toEqual({ x: 2, y: 1 });
|
||||
});
|
||||
|
||||
test('worldToTileCoords with default 64px tiles matches legacy behavior', () => {
|
||||
const pf = new PathfindingSystem(mockScene, mockTilemap);
|
||||
const result = pf.worldToTileCoords(150, 200);
|
||||
expect(result).toEqual({ x: 2, y: 3 });
|
||||
});
|
||||
});
|
||||
|
||||
// ── update loop ─────────────────────────────────────────────────
|
||||
describe('update', () => {
|
||||
test('does not throw when called', () => {
|
||||
|
||||
Reference in New Issue
Block a user