Fix drag race condition and stale pointer events

Mark items as dragging before updating inventory state to prevent
the spawner from destroying containers mid-drag. Move inventory
removal to drop time for lost items, and add guards to ignore
stale pointer events on inactive containers.
This commit is contained in:
2026-04-19 00:43:43 +08:00
parent 5af7140958
commit e14e41461f
3 changed files with 18 additions and 10 deletions
@@ -37,9 +37,10 @@ export interface InventoryItemSpawnerOptions {
* Items currently being dragged are excluded from getData() to prevent
* the spawner from respawning them while they're in flight.
*/
export class InventoryItemSpawner
implements Spawner<InventoryItem<GameItemMeta>, Phaser.GameObjects.Container>
{
export class InventoryItemSpawner implements Spawner<
InventoryItem<GameItemMeta>,
Phaser.GameObjects.Container
> {
private scene: Phaser.Scene;
private gameState: MutableSignal<RunState>;
private parentContainer: Phaser.GameObjects.Container;
@@ -102,6 +103,7 @@ export class InventoryItemSpawner
obj: Phaser.GameObjects.Container,
_item: InventoryItem<GameItemMeta>,
): void {
obj.removeAllListeners();
obj.destroy();
}
@@ -248,6 +250,8 @@ export class InventoryItemSpawner
container: Phaser.GameObjects.Container,
): void {
container.on("pointerdown", (pointer: Phaser.Input.Pointer) => {
// Guard against stale events firing on destroyed containers
if (!container.scene || !container.active) return;
if (this.isLocked()) return;
if (this.isDragging()) return;
if (pointer.button === 0) {