When enemy is comes into site dont tween sprite from (0,0) to correct location, instead just create at correct location

This commit is contained in:
Peter Stockings
2026-01-06 17:59:56 +11:00
parent a6bcf24cd0
commit 57fb85f62e
2 changed files with 53 additions and 17 deletions

View File

@@ -172,27 +172,33 @@ export class DungeonRenderer {
let sprite = this.enemySprites.get(a.id); let sprite = this.enemySprites.get(a.id);
const textureKey = a.type; const textureKey = a.type;
if (!sprite) {
sprite = this.scene.add.sprite(0, 0, textureKey, 0);
sprite.setDepth(99);
sprite.play(`${textureKey}-idle`);
this.enemySprites.set(a.id, sprite);
}
const tx = a.pos.x * TILE_SIZE + TILE_SIZE / 2; const tx = a.pos.x * TILE_SIZE + TILE_SIZE / 2;
const ty = a.pos.y * TILE_SIZE + TILE_SIZE / 2; const ty = a.pos.y * TILE_SIZE + TILE_SIZE / 2;
if (sprite.x !== tx || sprite.y !== ty) { if (!sprite) {
this.scene.tweens.add({ sprite = this.scene.add.sprite(tx, ty, textureKey, 0);
targets: sprite, sprite.setDepth(99);
x: tx, sprite.play(`${textureKey}-idle`);
y: ty, this.enemySprites.set(a.id, sprite);
duration: 120, sprite.setVisible(true);
ease: 'Quad.easeOut', } else {
overwrite: true if (!sprite.visible) {
}); // If it was hidden, snap to new position immediately
this.scene.tweens.killTweensOf(sprite);
sprite.setPosition(tx, ty);
sprite.setVisible(true);
} else if (sprite.x !== tx || sprite.y !== ty) {
// Only tween if it was already visible and moved
this.scene.tweens.add({
targets: sprite,
x: tx,
y: ty,
duration: 120,
ease: 'Quad.easeOut',
overwrite: true
});
}
} }
sprite.setVisible(true);
} else if (a.category === "collectible") { } else if (a.category === "collectible") {
if (a.type === "exp_orb") { if (a.type === "exp_orb") {

View File

@@ -209,4 +209,34 @@ describe('DungeonRenderer', () => {
const ratSpriteCall = mockScene.add.sprite.mock.calls.find((call: any) => call[2] === 'rat'); const ratSpriteCall = mockScene.add.sprite.mock.calls.find((call: any) => call[2] === 'rat');
expect(ratSpriteCall).toBeDefined(); expect(ratSpriteCall).toBeDefined();
}); });
it('should initialize new enemy sprites at target position and not tween them', () => {
renderer.initializeFloor(mockWorld, 1);
// Position 5,5 -> 5*16 + 8 = 88
const TILE_SIZE = 16;
const targetX = 5 * TILE_SIZE + TILE_SIZE / 2;
const targetY = 5 * TILE_SIZE + TILE_SIZE / 2;
mockWorld.actors.set(999, {
id: 999,
category: "combatant",
isPlayer: false,
type: "rat",
pos: { x: 5, y: 5 },
stats: { hp: 10, maxHp: 10 } as any,
} as any);
(renderer as any).fovManager.visibleArray[5 * mockWorld.width + 5] = 1;
mockScene.add.sprite.mockClear();
mockScene.tweens.add.mockClear();
renderer.render([]);
// Check spawn position
expect(mockScene.add.sprite).toHaveBeenCalledWith(targetX, targetY, 'rat', 0);
// Should NOT tween because it's the first spawn
expect(mockScene.tweens.add).not.toHaveBeenCalled();
});
}); });