From 123c91c776d1c70297ed72fe6d2e32eb2a4d8295 Mon Sep 17 00:00:00 2001 From: Benjamin Kindle Date: Sat, 5 Mar 2022 11:25:57 -0500 Subject: [PATCH] fix: avoid delay when resuming game and unpause when starting new game --- Wonky Blocks/Support/GameState.swift | 1 + Wonky Blocks/UI/GameView.swift | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Wonky Blocks/Support/GameState.swift b/Wonky Blocks/Support/GameState.swift index 28aaf64..c8f7844 100644 --- a/Wonky Blocks/Support/GameState.swift +++ b/Wonky Blocks/Support/GameState.swift @@ -92,6 +92,7 @@ class WonkyGameState: ObservableObject, JoystickState { score = 0 lineCount = 0 level = 1 + paused = false gameOver = false newHighScore = false activeTet = nextTet diff --git a/Wonky Blocks/UI/GameView.swift b/Wonky Blocks/UI/GameView.swift index 4981b4d..121ae85 100644 --- a/Wonky Blocks/UI/GameView.swift +++ b/Wonky Blocks/UI/GameView.swift @@ -22,7 +22,11 @@ struct GameView: UIViewControllerRepresentable { _ uiViewController: WonkyGameViewController, context: Context ) { - uiViewController.spriteKitView.isPaused = gameState.paused + // pausing the scene causes a delay when unpausing. We'll stop the phyiscs instead to avoid this. + // unfortunately, this means there will be consistent CPU usage while the app is running. + // I tried pausing only when the window is inactive, + //but macOS only counts as inactive when you minimize - and spritekit already pauses when this happens. + uiViewController.spriteKitView.scene?.physicsWorld.speed = gameState.paused ? 0.0 : 1.0 } static func dismantleUIViewController(