diff --git a/DouShouQi_App/DouShouQi_App.xcodeproj/project.pbxproj b/DouShouQi_App/DouShouQi_App.xcodeproj/project.pbxproj index 0732476..757c5e2 100644 --- a/DouShouQi_App/DouShouQi_App.xcodeproj/project.pbxproj +++ b/DouShouQi_App/DouShouQi_App.xcodeproj/project.pbxproj @@ -71,6 +71,7 @@ ECF3FD312C2722C600F5E62B /* CDHistoriqueExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECF3FD302C2722C600F5E62B /* CDHistoriqueExtension.swift */; }; ECF3FD332C27238F00F5E62B /* Historique.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECF3FD322C27238F00F5E62B /* Historique.swift */; }; ECF3FD3A2C29A20D00F5E62B /* HistoricListVM.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECF3FD392C29A20D00F5E62B /* HistoricListVM.swift */; }; + ECF3FD3D2C29B64100F5E62B /* ChronoVM.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECF3FD3C2C29B64100F5E62B /* ChronoVM.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -170,6 +171,7 @@ ECF3FD372C299CC500F5E62B /* Start.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = Start.mp3; sourceTree = ""; }; ECF3FD382C299CC800F5E62B /* SelectFighterSound.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = SelectFighterSound.mp3; sourceTree = ""; }; ECF3FD392C29A20D00F5E62B /* HistoricListVM.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HistoricListVM.swift; sourceTree = ""; }; + ECF3FD3C2C29B64100F5E62B /* ChronoVM.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChronoVM.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -316,6 +318,7 @@ EC62C5262C11E24B0048CD0B /* Players */, 646F04BC2C0F5491003C8600 /* Settings */, 646FA83F2C0730B4001466BA /* Game */, + ECF3FD3C2C29B64100F5E62B /* ChronoVM.swift */, ); path = ViewModel; sourceTree = ""; @@ -691,6 +694,7 @@ EC0540C42C08A13E0032E9EF /* GameView.swift in Sources */, EC05BFC42C04C3C4000F7B19 /* SettingsView.swift in Sources */, ECF3FD312C2722C600F5E62B /* CDHistoriqueExtension.swift in Sources */, + ECF3FD3D2C29B64100F5E62B /* ChronoVM.swift in Sources */, EC62C53D2C1C69200048CD0B /* DouShouQi_App.xcdatamodeld in Sources */, EC62C50F2C05D06A0048CD0B /* AddPlayerView.swift in Sources */, EC62C53F2C1C6D1A0048CD0B /* CDPlayerExtension.swift in Sources */, diff --git a/DouShouQi_App/DouShouQi_App/Components/Game/TopGameBoard.swift b/DouShouQi_App/DouShouQi_App/Components/Game/TopGameBoard.swift index b510902..2db60c0 100644 --- a/DouShouQi_App/DouShouQi_App/Components/Game/TopGameBoard.swift +++ b/DouShouQi_App/DouShouQi_App/Components/Game/TopGameBoard.swift @@ -7,9 +7,10 @@ import SwiftUI import DouShouQiModel - + struct TopGameBoard: View { @ObservedObject var gameVM: PlayingGameVM + @ObservedObject var chronoVM: ChronometerViewModel var body: some View { VStack { @@ -32,7 +33,7 @@ struct TopGameBoard: View { Image(systemName: "clock") .resizable() .frame(width: 30, height: 30) - Text("4:11") + Text(chronoVM.formattedTime()) .font(.title) .foregroundColor(.black) } @@ -43,9 +44,6 @@ struct TopGameBoard: View { Text("Last move :") .font(.title2) .foregroundColor(.black) - /*Image(systemName: "cheetah") - .resizable() - .frame(width: 30, height: 30)*/ Text(gameVM.lastPieceMoved) Text(gameVM.lastMove) .font(.title2) @@ -62,9 +60,3 @@ struct TopGameBoard: View { .background(Color.red.opacity(0.5)) } } - -//struct TopGameBoard_Previews: PreviewProvider { -// static var previews: some View { -// TopGameBoard(gameVM: vm) -// } -//} diff --git a/DouShouQi_App/DouShouQi_App/ViewModel/ChronoVM.swift b/DouShouQi_App/DouShouQi_App/ViewModel/ChronoVM.swift new file mode 100644 index 0000000..cd5b155 --- /dev/null +++ b/DouShouQi_App/DouShouQi_App/ViewModel/ChronoVM.swift @@ -0,0 +1,32 @@ +// +// ChronoVM.swift +// DouShouQi_App +// +// Created by étudiant on 24/06/2024. +// + +import SwiftUI +import Combine + +class ChronometerViewModel: ObservableObject { + @Published var elapsedTime: TimeInterval = 0 + private var timer: AnyCancellable? + + func startTimer() { + let startDate = Date() + timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect().sink { _ in + self.elapsedTime = Date().timeIntervalSince(startDate) + } + } + + func stopTimer() { + timer?.cancel() + } + + func formattedTime() -> String { + let minutes = Int(elapsedTime) / 60 + let seconds = Int(elapsedTime) % 60 + return String(format: "%02d:%02d", minutes, seconds) + } +} + diff --git a/DouShouQi_App/DouShouQi_App/Views/Game/GameView.swift b/DouShouQi_App/DouShouQi_App/Views/Game/GameView.swift index 5cee99f..7dc6bf5 100644 --- a/DouShouQi_App/DouShouQi_App/Views/Game/GameView.swift +++ b/DouShouQi_App/DouShouQi_App/Views/Game/GameView.swift @@ -6,6 +6,7 @@ struct GameView: View { var gameScene: GameScene = GameScene(size: CGSize(width: 700, height: 900)) @StateObject var gameVM: PlayingGameVM + @StateObject var chronoVM = ChronometerViewModel() @State private var showHome = false @@ -16,11 +17,12 @@ struct GameView: View { var body: some View { ZStack { VStack { - TopGameBoard(gameVM: gameVM).frame(maxHeight: 200) + TopGameBoard(gameVM: gameVM, chronoVM: chronoVM).frame(maxHeight: 200) SpriteView(scene: GameScene(size: CGSize(width: 700, height: 900), gameVM: gameVM)) } .onAppear { playSound(named: "Fight") + chronoVM.startTimer() DispatchQueue.main.asyncAfter(deadline: .now() + 0.7) { MusicPlayer.shared.playBackgroundMusic(music: "GameMusic") @@ -45,6 +47,7 @@ struct GameView: View { .shadow(radius: 10) .padding() .onAppear { + chronoVM.stopTimer() saveHistoric() Timer.scheduledTimer(withTimeInterval: 10, repeats: false) { _ in withAnimation { @@ -53,7 +56,6 @@ struct GameView: View { } } } - } .onDisappear { MusicPlayer.shared.stopBackgroundMusic() @@ -62,13 +64,7 @@ struct GameView: View { } private func saveHistoric() { - let newHistoric = HistoricVM(historic: Historique(player1_name: player1_name, player2_name: player2_name, time: "4:11", result: gameVM.winner)) + let newHistoric = HistoricVM(historic: Historique(player1_name: player1_name, player2_name: player2_name, time: chronoVM.formattedTime(), result: gameVM.winner)) historicVm.SavePlayer(historic: newHistoric) } } - -/*struct GameView_Previews: PreviewProvider { - static var previews: some View { - GameView() - } -}*/