You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
145 lines
4.2 KiB
145 lines
4.2 KiB
//
|
|
// IngameView.swift
|
|
// App
|
|
//
|
|
// Created by etudiant2 on 21/05/2025.
|
|
//
|
|
|
|
import SwiftUI
|
|
import SpriteKit
|
|
import Connect4Core
|
|
|
|
struct IngameView: View {
|
|
@StateObject
|
|
private var vm: IngameVM
|
|
|
|
var body: some View {
|
|
VStack {
|
|
HStack {
|
|
PlayerView(
|
|
who: vm.player1,
|
|
dock_left: true,
|
|
isTurn: self.$vm.currentPlayer.map { $0 == .player1 }
|
|
)
|
|
Spacer()
|
|
PlayerView(
|
|
who: vm.player2,
|
|
dock_left: false,
|
|
isTurn: self.$vm.currentPlayer.map { $0 == .player2 }
|
|
)
|
|
}
|
|
|
|
Spacer()
|
|
|
|
VStack(alignment: .center) {
|
|
SpriteView(scene: vm.scene, options: .allowsTransparency)
|
|
.aspectRatio(
|
|
vm.scene.size.width / vm.scene.size.height,
|
|
contentMode: .fit
|
|
)
|
|
}.safeAreaPadding(.horizontal)
|
|
|
|
Spacer()
|
|
|
|
// let style: (key: LocalizedStringKey, img: String) = if vm.paused {
|
|
// ("inGame.ppButton.resume", "play.circle")
|
|
// } else {
|
|
// ("inGame.ppButton.pause", "pause.circle")
|
|
// }
|
|
// Button(style.key, systemImage: style.img) {
|
|
// vm.paused.toggle()
|
|
// }
|
|
// .`if`(vm.paused, { $0.buttonStyle(.bordered) })
|
|
// .`if`(!vm.paused, { $0.buttonStyle(.borderedProminent) })
|
|
// //For some reason the following crashes the compiler:
|
|
// // .either(vm.paused,
|
|
// // `true`: { $0.buttonStyle(.borderedProminent) },
|
|
// // `false`: { $0.buttonStyle(.bordered) }
|
|
// // )
|
|
// .tint(.accentColor)
|
|
//
|
|
// Spacer()
|
|
|
|
Text("inGame.currentRules \(vm.rulesName)")
|
|
}.onAppear {
|
|
vm.start()
|
|
}
|
|
}
|
|
|
|
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") }
|
|
|
|
self._vm = StateObject(wrappedValue: vm)
|
|
}
|
|
}
|
|
|
|
private struct PlayerView: View {
|
|
let dockLeft: Bool
|
|
let who: PlayerVM
|
|
|
|
@Binding
|
|
var isTurn: Bool
|
|
|
|
var body: some View {
|
|
let textAlignment: HorizontalAlignment = if self.dockLeft {
|
|
.leading
|
|
} else {
|
|
.trailing
|
|
}
|
|
|
|
VStack {
|
|
HStack {
|
|
let img = Circle().frame(width: 50, height: 50)
|
|
|
|
if self.dockLeft {
|
|
img
|
|
}
|
|
|
|
VStack(alignment: textAlignment) {
|
|
Text(who.name)
|
|
|
|
Text(LocalizedStringKey(who.type.baseTranslationKey))
|
|
}
|
|
|
|
if !self.dockLeft {
|
|
img
|
|
}
|
|
}
|
|
|
|
HStack {
|
|
let wheel = ProgressView()
|
|
.progressViewStyle(.circular)
|
|
.tint(.accentColor)
|
|
.transition(.scale)
|
|
.if(!self.isTurn, { $0.hidden() })
|
|
|
|
if !self.dockLeft {
|
|
wheel
|
|
}
|
|
|
|
// TODO
|
|
// let time = "??:??:??"
|
|
// Text(time).padding(.horizontal, 10)
|
|
|
|
if self.dockLeft {
|
|
wheel
|
|
}
|
|
}
|
|
}.padding(.all, 5)
|
|
.border(who.id.pieceColor)
|
|
.containerShape(RoundedRectangle(cornerRadius: 5))
|
|
.padding(.all, 5)
|
|
}
|
|
|
|
init(who owner: PlayerVM, dock_left dockLeft: Bool, isTurn: Binding<Bool>) {
|
|
self.who = owner
|
|
self.dockLeft = dockLeft
|
|
self._isTurn = isTurn
|
|
}
|
|
}
|
|
|
|
#Preview {
|
|
IngameView(settings: NewGameVM(), player1: PlayerSettingsVM(type: .Human), player2: PlayerSettingsVM(type: .AISimpleNegaMax))
|
|
}
|