"Fix" initialization issue

main
Mathieu GROUSSEAU 6 days ago
parent d5bf6d8e5e
commit de993965a7

@ -12,7 +12,6 @@ import Connect4Core
struct IngameView: View { struct IngameView: View {
@StateObject @StateObject
private var vm: IngameVM private var vm: IngameVM
private let scene: GameScene
var body: some View { var body: some View {
VStack { VStack {
@ -33,9 +32,9 @@ struct IngameView: View {
Spacer() Spacer()
VStack(alignment: .center) { VStack(alignment: .center) {
SpriteView(scene: self.scene, options: .allowsTransparency) SpriteView(scene: vm.scene, options: .allowsTransparency)
.aspectRatio( .aspectRatio(
self.scene.size.width / self.scene.size.height, vm.scene.size.width / vm.scene.size.height,
contentMode: .fit contentMode: .fit
) )
}.safeAreaPadding(.horizontal) }.safeAreaPadding(.horizontal)
@ -72,7 +71,6 @@ struct IngameView: View {
else { fatalError("TODO: how to handle game setup failure") } else { fatalError("TODO: how to handle game setup failure") }
self._vm = StateObject(wrappedValue: vm) self._vm = StateObject(wrappedValue: vm)
self.scene = GameScene(viewModel: vm)
} }
} }

@ -9,6 +9,14 @@ class IngameVM: ObservableObject {
let game: Game 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 // @Published
var rulesName: String { game.rules.name } var rulesName: String { game.rules.name }
@ -69,6 +77,8 @@ class IngameVM: ObservableObject {
self.currentBoard = self.game.board self.currentBoard = self.game.board
self.scene = GameScene(viewModel: self)
game.addGameOverListener { board, result, player in game.addGameOverListener { board, result, player in
print("game over") print("game over")
DispatchQueue.main.async { DispatchQueue.main.async {

Loading…
Cancel
Save