"Fix" initialization issue

main
Mathieu GROUSSEAU 6 days ago
parent d5bf6d8e5e
commit de993965a7

@ -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)
}
}

@ -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 {

Loading…
Cancel
Save