microsoft / pxt-arcade

Arcade game editor based on Microsoft MakeCode
https://arcade.makecode.com
MIT License
484 stars 209 forks source link

Changing tilemap in spriteOverlapsTile callback leads to uncertain result #4819

Open felixtsu opened 2 years ago

felixtsu commented 2 years ago

Describe the bug Changing tilemap in spriteOverlapsTile callback leads to uncertain result

To Reproduce

  1. https://makecode.com/_fKuUxqDkwA3q
  2. Touching tilePath5 from left results in tilemap level 2, touching from right results in tilemap level 3

Expected behavior should result in tilemap level 2

Screenshots Screen-Recording-2022-06-19-at-0

Additional context https://github.com/microsoft/pxt-common-packages/blob/f40bb636d27d44e58ace6e6da06b41af055fc8a7/libs/game/physics.ts#L597

protected tilemapOverlaps(sprite: Sprite, overlappedTiles: tiles.Location[]) {
        const alreadyHandled: tiles.Location[] = [];

        // save current tile map before loop
        let currentTileMap = game.currentScene().tileMap

        for (const tile of overlappedTiles) {
            if (alreadyHandled.some(l => l.column === tile.column && l.row === tile.row)) {
                continue;
            }
            alreadyHandled.push(tile);

            const tileOverlapHandlers = game.currentScene().tileOverlapHandlers;
            if (tileOverlapHandlers) {
            // use saved tile map tiles, preventing tile map changed in handlers
                tileOverlapHandlers
                    .filter(h => h.spriteKind == sprite.kind() && h.tileKind.equals(currentTileMap.getTileImage(tile)))
                    .forEach(h => h.handler(sprite, tile));
            }
        }
    }
abchatra commented 2 years ago

Repros.