Fix for bug where when switching levels the player would jump between entrance to exit locations

This commit is contained in:
Peter Stockings
2026-01-05 14:17:12 +11:00
parent d638d1a821
commit dba0f054db
3 changed files with 22 additions and 6 deletions

View File

@@ -29,7 +29,7 @@ export class DungeonRenderer {
this.fxRenderer = new FxRenderer(scene); this.fxRenderer = new FxRenderer(scene);
} }
initializeFloor(world: World) { initializeFloor(world: World, playerId: EntityId) {
this.world = world; this.world = world;
this.fovManager.initialize(world); this.fovManager.initialize(world);
@@ -55,6 +55,21 @@ export class DungeonRenderer {
this.fxRenderer.clearCorpses(); this.fxRenderer.clearCorpses();
this.setupAnimations(); this.setupAnimations();
this.minimapRenderer.positionMinimap(); this.minimapRenderer.positionMinimap();
// Reset player sprite position to prevent tween animation from old floor
if (this.playerSprite) {
// Kill any active tweens on the player sprite
this.scene.tweens.killTweensOf(this.playerSprite);
// Get player position in new world using provided playerId
const player = world.actors.get(playerId);
if (player && player.category === "combatant") {
this.playerSprite.setPosition(
player.pos.x * TILE_SIZE + TILE_SIZE / 2,
player.pos.y * TILE_SIZE + TILE_SIZE / 2
);
}
}
} }
private setupAnimations() { private setupAnimations() {

View File

@@ -108,6 +108,7 @@ describe('DungeonRenderer', () => {
}, },
tweens: { tweens: {
add: vi.fn(), add: vi.fn(),
killTweensOf: vi.fn(),
}, },
}; };
@@ -124,7 +125,7 @@ describe('DungeonRenderer', () => {
}); });
it('should track and clear corpse sprites on floor initialization', () => { it('should track and clear corpse sprites on floor initialization', () => {
renderer.initializeFloor(mockWorld); renderer.initializeFloor(mockWorld, 1);
// Spawn a couple of corpses // Spawn a couple of corpses
@@ -138,7 +139,7 @@ describe('DungeonRenderer', () => {
expect(mockScene.add.sprite).toHaveBeenCalledTimes(3); expect(mockScene.add.sprite).toHaveBeenCalledTimes(3);
// Initialize floor again (changing level) // Initialize floor again (changing level)
renderer.initializeFloor(mockWorld); renderer.initializeFloor(mockWorld, 1);
// Verify destroy was called on both corpse sprites // Verify destroy was called on both corpse sprites
@@ -147,7 +148,7 @@ describe('DungeonRenderer', () => {
}); });
it('should render exp_orb as a circle and not as an enemy sprite', () => { it('should render exp_orb as a circle and not as an enemy sprite', () => {
renderer.initializeFloor(mockWorld); renderer.initializeFloor(mockWorld, 1);
// Add an exp_orb to the world // Add an exp_orb to the world
mockWorld.actors.set(2, { mockWorld.actors.set(2, {
@@ -185,7 +186,7 @@ describe('DungeonRenderer', () => {
}); });
it('should render any enemy type defined in config as a sprite', () => { it('should render any enemy type defined in config as a sprite', () => {
renderer.initializeFloor(mockWorld); renderer.initializeFloor(mockWorld, 1);
// Add a rat (defined in config) // Add a rat (defined in config)
mockWorld.actors.set(3, { mockWorld.actors.set(3, {

View File

@@ -333,7 +333,7 @@ export class GameScene extends Phaser.Scene {
this.cameras.main.setBounds(0, 0, this.world.width * TILE_SIZE, this.world.height * TILE_SIZE); this.cameras.main.setBounds(0, 0, this.world.width * TILE_SIZE, this.world.height * TILE_SIZE);
// Initialize Renderer for new floor // Initialize Renderer for new floor
this.dungeonRenderer.initializeFloor(this.world); this.dungeonRenderer.initializeFloor(this.world, this.playerId);
// Step until player turn // Step until player turn
const enemyStep = stepUntilPlayerTurn(this.world, this.playerId, this.entityManager); const enemyStep = stepUntilPlayerTurn(this.world, this.playerId, this.entityManager);