diff --git a/App/App/View/IngameView.swift b/App/App/View/IngameView.swift index 532b6e6..12bdcf8 100644 --- a/App/App/View/IngameView.swift +++ b/App/App/View/IngameView.swift @@ -12,7 +12,6 @@ import Connect4Core struct IngameView: View { @StateObject private var vm: IngameVM - private let scene: GameScene var body: some View { VStack { @@ -33,9 +32,9 @@ struct IngameView: View { Spacer() VStack(alignment: .center) { - SpriteView(scene: self.scene, options: .allowsTransparency) + SpriteView(scene: vm.scene, options: .allowsTransparency) .aspectRatio( - self.scene.size.width / self.scene.size.height, + vm.scene.size.width / vm.scene.size.height, contentMode: .fit ) }.safeAreaPadding(.horizontal) @@ -72,7 +71,6 @@ struct IngameView: View { else { fatalError("TODO: how to handle game setup failure") } self._vm = StateObject(wrappedValue: vm) - self.scene = GameScene(viewModel: vm) } } diff --git a/App/App/ViewModel/InGameVM.swift b/App/App/ViewModel/InGameVM.swift index 5e1de17..753cd82 100644 --- a/App/App/ViewModel/InGameVM.swift +++ b/App/App/ViewModel/InGameVM.swift @@ -9,6 +9,14 @@ class IngameVM: ObservableObject { let game: Game + /// This field is a "fix" for a random double initialization of the VM + /// causing a desync (how?) between the VM of the view and the VM of the + /// scene. + /// + /// The real issue is the double initialization but I suspect it is caused + /// by a TextField loosing focus and triggering a change and recomposition. + var scene: GameScene! = nil + // @Published var rulesName: String { game.rules.name } @@ -69,6 +77,8 @@ class IngameVM: ObservableObject { self.currentBoard = self.game.board + self.scene = GameScene(viewModel: self) + game.addGameOverListener { board, result, player in print("game over") DispatchQueue.main.async {