From 263913959fe83e9093c2c3a418d38ba812ef664c Mon Sep 17 00:00:00 2001 From: Mathieu GROUSSEAU Date: Mon, 23 Jun 2025 11:38:25 +0200 Subject: [PATCH] Move game vm initialisation --- App/App/View/IngameView.swift | 14 ++------------ App/App/View/NewGameView.swift | 6 +++++- App/App/ViewModel/InGameVM.swift | 15 +++++---------- 3 files changed, 12 insertions(+), 23 deletions(-) diff --git a/App/App/View/IngameView.swift b/App/App/View/IngameView.swift index 532b6e6..3b8275d 100644 --- a/App/App/View/IngameView.swift +++ b/App/App/View/IngameView.swift @@ -1,10 +1,3 @@ -// -// IngameView.swift -// App -// -// Created by etudiant2 on 21/05/2025. -// - import SwiftUI import SpriteKit import Connect4Core @@ -67,10 +60,7 @@ struct IngameView: View { } } - init(settings: NewGameVM, player1: PlayerSettingsVM, player2: PlayerSettingsVM) { - guard let vm = IngameVM(settings: settings, player1: player1, player2: player2) - else { fatalError("TODO: how to handle game setup failure") } - + init(vm: IngameVM) { self._vm = StateObject(wrappedValue: vm) self.scene = GameScene(viewModel: vm) } @@ -142,5 +132,5 @@ private struct PlayerView: View { } #Preview { - IngameView(settings: NewGameVM(), player1: PlayerSettingsVM(type: .Human), player2: PlayerSettingsVM(type: .AISimpleNegaMax)) + IngameView(vm: IngameVM(settings: NewGameVM(), player1: PlayerSettingsVM(type: .Human), player2: PlayerSettingsVM(type: .AISimpleNegaMax))) } diff --git a/App/App/View/NewGameView.swift b/App/App/View/NewGameView.swift index 68e1037..42477ac 100644 --- a/App/App/View/NewGameView.swift +++ b/App/App/View/NewGameView.swift @@ -49,7 +49,11 @@ struct NewGameView: View { } }.toolbar { NavigationLink { - NavigationLazyView(IngameView(settings: vm, player1: p1, player2: p2)) + // NavigationLazyView( + let vm = IngameVM(settings: vm, player1: p1, player2: p2) + + IngameView(vm: vm) + // ) } label: { Label("newGame.play", systemImage: "play") } diff --git a/App/App/ViewModel/InGameVM.swift b/App/App/ViewModel/InGameVM.swift index 5e1de17..113a23c 100644 --- a/App/App/ViewModel/InGameVM.swift +++ b/App/App/ViewModel/InGameVM.swift @@ -43,7 +43,7 @@ class IngameVM: ObservableObject { private var running: Bool = false - init?(settings: NewGameVM, player1: PlayerSettingsVM, player2: PlayerSettingsVM) { + init(settings: NewGameVM, player1: PlayerSettingsVM, player2: PlayerSettingsVM) { let fmt = DateFormatter() fmt.locale = Locale(identifier: "EN") // No "root" locale? self.gameName = fmt.string(from: Date.now) @@ -57,10 +57,10 @@ class IngameVM: ObservableObject { Int(settings.height), Int(settings.width), Int(settings.alignedTokens) - ) else { return nil } + ) else { fatalError("rule initialisation failed") } - guard let player1 = Self.playerOf(settings: player1, id: .player1) else { return nil } - guard let player2 = Self.playerOf(settings: player2, id: .player2) else { return nil } + guard let player1 = Self.playerOf(settings: player1, id: .player1) else { fatalError("player initialisation failed") } + guard let player2 = Self.playerOf(settings: player2, id: .player2) else { fatalError("player initialisation failed") } self.game = try! Game(withRules: rules, andPlayer1: player1, andPlayer2: player2) @@ -70,11 +70,9 @@ class IngameVM: ObservableObject { self.currentBoard = self.game.board game.addGameOverListener { board, result, player in - print("game over") DispatchQueue.main.async { self.currentPlayer = .noOne } - // TODO } game.addGameChangedListener { game, result in if game.players.contains(where: { $0.value is ReplayPlayer }) { @@ -82,9 +80,7 @@ class IngameVM: ObservableObject { return } - defer { - print("game saved") - } + defer { print("game saved") } if result == .notFinished { _ = try await Persistance.saveGame(withName: "\(self.gameName).co4", andGame: self.game, withFolderName: "connect4.games") @@ -94,7 +90,6 @@ class IngameVM: ObservableObject { _ = try await Persistance.saveGameResult(withName: "savedGames.json", andGame: game, andResult: result, withFolderName: "connect4.games") } game.addGameStartedListener { board in - print("game started") // TODO } game.addBoardChangedListener { board, lastCell in