From a171478e2c0c097b44df3cf90d0794038d7d52f9 Mon Sep 17 00:00:00 2001 From: Rayhan Date: Mon, 24 Jun 2024 16:08:03 +0200 Subject: [PATCH 1/2] historic is well saved --- .../DouShouQi_App.xcodeproj/project.pbxproj | 14 +++++- .../Class/CoreData/CoreManager.swift | 10 ++-- .../Components/Game/GameResumeFrame.swift | 25 +++------- .../Components/MainMenuButton.swift | 2 +- .../Game/Historic/HistoricListVM.swift | 32 ++++++++++++ .../ViewModel/Game/Historic/HistoricVM.swift | 27 ++++++++++ .../ViewModel/Game/HistoricVM.swift | 26 ---------- .../DouShouQi_App/Views/Game/GameView.swift | 50 ++++++++++--------- .../Views/Game/HistoricView.swift | 16 ++---- .../Views/Menu/MainMenuView.swift | 4 +- .../Views/Player/SelectPlayerView.swift | 2 +- 11 files changed, 120 insertions(+), 88 deletions(-) create mode 100644 DouShouQi_App/DouShouQi_App/ViewModel/Game/Historic/HistoricListVM.swift create mode 100644 DouShouQi_App/DouShouQi_App/ViewModel/Game/Historic/HistoricVM.swift delete mode 100644 DouShouQi_App/DouShouQi_App/ViewModel/Game/HistoricVM.swift diff --git a/DouShouQi_App/DouShouQi_App.xcodeproj/project.pbxproj b/DouShouQi_App/DouShouQi_App.xcodeproj/project.pbxproj index 06c6e23..0732476 100644 --- a/DouShouQi_App/DouShouQi_App.xcodeproj/project.pbxproj +++ b/DouShouQi_App/DouShouQi_App.xcodeproj/project.pbxproj @@ -70,6 +70,7 @@ ECE777142C2068F400D354B0 /* EditPlayerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECE777132C2068F400D354B0 /* EditPlayerView.swift */; }; 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 */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -168,6 +169,7 @@ ECF3FD352C299CC100F5E62B /* Fight.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = Fight.mp3; sourceTree = ""; }; 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 = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -321,8 +323,8 @@ 646FA83F2C0730B4001466BA /* Game */ = { isa = PBXGroup; children = ( + ECF3FD3B2C29A21200F5E62B /* Historic */, 646FA83D2C072340001466BA /* GameVM.swift */, - 646FA8422C0730D6001466BA /* HistoricVM.swift */, 6437FF122C25846F009D0EAF /* PlayingGameVM.swift */, ); path = Game; @@ -507,6 +509,15 @@ name = Products; sourceTree = ""; }; + ECF3FD3B2C29A21200F5E62B /* Historic */ = { + isa = PBXGroup; + children = ( + 646FA8422C0730D6001466BA /* HistoricVM.swift */, + ECF3FD392C29A20D00F5E62B /* HistoricListVM.swift */, + ); + path = Historic; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -711,6 +722,7 @@ EC62C52D2C197ED10048CD0B /* Player.swift in Sources */, EC62C5292C1974000048CD0B /* PlayersVM.swift in Sources */, 6437FF132C25846F009D0EAF /* PlayingGameVM.swift in Sources */, + ECF3FD3A2C29A20D00F5E62B /* HistoricListVM.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/DouShouQi_App/DouShouQi_App/AppModelAndExtension/Class/CoreData/CoreManager.swift b/DouShouQi_App/DouShouQi_App/AppModelAndExtension/Class/CoreData/CoreManager.swift index d4f3ecc..cef39cc 100644 --- a/DouShouQi_App/DouShouQi_App/AppModelAndExtension/Class/CoreData/CoreManager.swift +++ b/DouShouQi_App/DouShouQi_App/AppModelAndExtension/Class/CoreData/CoreManager.swift @@ -91,12 +91,12 @@ class CoreDataManager { } } - func saveHistorique(historique: Historique) { + func saveHistorique(historique: HistoricVM) { let historiqueEntity = CDHistorique(context: context) - historiqueEntity.player1_name = historique.player1_name - historiqueEntity.player2_name = historique.player2_name - historiqueEntity.time = historique.time - historiqueEntity.result = historique.result + historiqueEntity.player1_name = historique.historic.player1_name + historiqueEntity.player2_name = historique.historic.player2_name + historiqueEntity.time = historique.historic.time + historiqueEntity.result = historique.historic.result saveContext() } diff --git a/DouShouQi_App/DouShouQi_App/Components/Game/GameResumeFrame.swift b/DouShouQi_App/DouShouQi_App/Components/Game/GameResumeFrame.swift index 9166b94..9d40397 100644 --- a/DouShouQi_App/DouShouQi_App/Components/Game/GameResumeFrame.swift +++ b/DouShouQi_App/DouShouQi_App/Components/Game/GameResumeFrame.swift @@ -13,25 +13,25 @@ import DouShouQiModel struct GameResumeFrame: View { // Players Params - let gameVM: GameVM + let historicVm: HistoricVM var body: some View { ZStack { HStack(alignment: .center) { VStack(alignment: .leading) { - Text("\(gameVM.player1Name) vs \(gameVM.player2Name)") + Text("\(historicVm.historic.player1_name) vs \(historicVm.historic.player2_name)") .font(.headline) - Text(gameVM.gameStatus) + Text(historicVm.historic.result) + .font(.subheadline) + .foregroundColor(.gray) + + Text(historicVm.historic.time) .font(.subheadline) .foregroundColor(.gray) } Spacer() - - Text("Detail >") - .frame(width: 100) - .foregroundColor(.gray) } .padding(10) @@ -42,14 +42,3 @@ struct GameResumeFrame: View { }.padding(2) } } - -struct GameResumeFrame_Previews: PreviewProvider { - static var previews: some View { - do { - let game = try Game(withRules: VerySimpleRules(), andPlayer1: HumanPlayer(withName: "Rémi", andId: .player1)!, andPlayer2: HumanPlayer(withName: "Nathan", andId: .player2)!) - return AnyView(GameResumeFrame(gameVM: GameVM(withGame: game, time: "11:01"))) - } catch { - return AnyView(Text("Erreur lors de la création du jeu : \(error.localizedDescription)")) - } - } -} diff --git a/DouShouQi_App/DouShouQi_App/Components/MainMenuButton.swift b/DouShouQi_App/DouShouQi_App/Components/MainMenuButton.swift index 0817a21..67b55fa 100644 --- a/DouShouQi_App/DouShouQi_App/Components/MainMenuButton.swift +++ b/DouShouQi_App/DouShouQi_App/Components/MainMenuButton.swift @@ -92,6 +92,6 @@ struct RoundedCornersShape: Shape { struct MainMenuButton_Previews: PreviewProvider { static var previews: some View { - MainMenuButton(text: "test", destination: HistoricView(historicVM: HistoricVM()), sound: "TitleScreenButtonSound", horizontalAlignment: .leading, topRightCorner: 10, bottomRightCorner: 10) + MainMenuButton(text: "test", destination: HistoricView(historicListVM: HistoricListVM()), sound: "TitleScreenButtonSound", horizontalAlignment: .leading, topRightCorner: 10, bottomRightCorner: 10) } } diff --git a/DouShouQi_App/DouShouQi_App/ViewModel/Game/Historic/HistoricListVM.swift b/DouShouQi_App/DouShouQi_App/ViewModel/Game/Historic/HistoricListVM.swift new file mode 100644 index 0000000..1cd55b6 --- /dev/null +++ b/DouShouQi_App/DouShouQi_App/ViewModel/Game/Historic/HistoricListVM.swift @@ -0,0 +1,32 @@ +// +// HistoricListVM.swift +// DouShouQi_App +// +// Created by étudiant on 24/06/2024. +// + +import Foundation +import SwiftUI +import DouShouQiModel + +class HistoricListVM: ObservableObject { + + @Published var historicList: [HistoricVM] + + public func SavePlayer(historic: HistoricVM){ + historicList.append(historic) + CoreDataManager.shared.saveHistorique(historique: historic) + } + + public func getAllHistoric() -> [HistoricVM]{ + let coreDataPlayers = CoreDataManager.shared.fetchHistoriques() + self.historicList = coreDataPlayers.map { HistoricVM(historic: $0.toModel()) } + return historicList + } + + init() { + historicList = [] + + historicList = getAllHistoric() + } +} diff --git a/DouShouQi_App/DouShouQi_App/ViewModel/Game/Historic/HistoricVM.swift b/DouShouQi_App/DouShouQi_App/ViewModel/Game/Historic/HistoricVM.swift new file mode 100644 index 0000000..1cab0e4 --- /dev/null +++ b/DouShouQi_App/DouShouQi_App/ViewModel/Game/Historic/HistoricVM.swift @@ -0,0 +1,27 @@ +// +// HistoricVM.swift +// DouShouQi_App +// +// Created by Rémi REGNAULT on 29/05/2024. +// + +import Foundation +import SwiftUI +import DouShouQiModel + +class HistoricVM: ObservableObject, Identifiable, Hashable{ + + public static func == (lhs: HistoricVM, rhs: HistoricVM) -> Bool { + return lhs.id == rhs.id + } + + public func hash(into hasher: inout Hasher) { + hasher.combine(id) + } + + @Published var historic: Historique + + init(historic: Historique) { + self.historic = historic + } +} diff --git a/DouShouQi_App/DouShouQi_App/ViewModel/Game/HistoricVM.swift b/DouShouQi_App/DouShouQi_App/ViewModel/Game/HistoricVM.swift deleted file mode 100644 index 34585c8..0000000 --- a/DouShouQi_App/DouShouQi_App/ViewModel/Game/HistoricVM.swift +++ /dev/null @@ -1,26 +0,0 @@ -// -// HistoricVM.swift -// DouShouQi_App -// -// Created by Rémi REGNAULT on 29/05/2024. -// - -import Foundation -import SwiftUI -import DouShouQiModel - -class HistoricVM: ObservableObject { - // Properties - public var gameVMs: [GameVM] - - // Init - init() { - gameVMs = [] - do { - let game = try Game(withRules: VerySimpleRules(), andPlayer1: HumanPlayer(withName: "Rémi", andId: .player1)!, andPlayer2: HumanPlayer(withName: "Nathan", andId: .player2)!) - gameVMs.append(GameVM(withGame: game, time: "11:01")) - } catch { - // Do nothing - } - } -} diff --git a/DouShouQi_App/DouShouQi_App/Views/Game/GameView.swift b/DouShouQi_App/DouShouQi_App/Views/Game/GameView.swift index fe14168..5cee99f 100644 --- a/DouShouQi_App/DouShouQi_App/Views/Game/GameView.swift +++ b/DouShouQi_App/DouShouQi_App/Views/Game/GameView.swift @@ -1,10 +1,3 @@ -// -// MainMenuView.swift -// DouShouQi_App -// -// Created by Nathan Verdier on 26/05/2024. -// - import SwiftUI import SpriteKit import DouShouQiModel @@ -16,6 +9,10 @@ struct GameView: View { @State private var showHome = false + var player1_name: String + var player2_name: String + @StateObject var historicVm = HistoricListVM() + var body: some View { ZStack { VStack { @@ -25,8 +22,6 @@ struct GameView: View { .onAppear { playSound(named: "Fight") - //MusicPlayer.shared.playBackgroundMusic(music: "GameMusic") - DispatchQueue.main.asyncAfter(deadline: .now() + 0.7) { MusicPlayer.shared.playBackgroundMusic(music: "GameMusic") } @@ -35,31 +30,40 @@ struct GameView: View { MusicPlayer.shared.stopBackgroundMusic() } - if (gameVM.game.isOver) { - HStack { - Spacer() - - VStack(alignment: .center) { - Text("Game Over") - .font(.title2) - Text("Winner: \(gameVM.winner)") - } - .background(Colors.WinnerBackground) + if gameVM.game.isOver { + VStack(spacing: 20) { + Text("Game Over") + .font(.title) + .fontWeight(.bold) - Spacer() + Text("Winner: \(gameVM.winner)") + .font(.title2) } + .padding() + .background(Colors.WinnerBackground) + .cornerRadius(15) + .shadow(radius: 10) + .padding() .onAppear { + saveHistoric() Timer.scheduledTimer(withTimeInterval: 10, repeats: false) { _ in - - // Il faut retourner au home + withAnimation { + showHome = true + } } } } + } .onDisappear { MusicPlayer.shared.stopBackgroundMusic() } - .navigationBarHidden(true) // Hide the navigation bar + .navigationBarHidden(true) + } + + private func saveHistoric() { + let newHistoric = HistoricVM(historic: Historique(player1_name: player1_name, player2_name: player2_name, time: "4:11", result: gameVM.winner)) + historicVm.SavePlayer(historic: newHistoric) } } diff --git a/DouShouQi_App/DouShouQi_App/Views/Game/HistoricView.swift b/DouShouQi_App/DouShouQi_App/Views/Game/HistoricView.swift index f32ea59..cda96d9 100644 --- a/DouShouQi_App/DouShouQi_App/Views/Game/HistoricView.swift +++ b/DouShouQi_App/DouShouQi_App/Views/Game/HistoricView.swift @@ -1,10 +1,3 @@ -// -// HistoricView.swift -// DouShouQi_App -// -// Created by Rémi REGNAULT on 21/05/2024. -// - import SwiftUI #if DEBUG import DouShouQiModel @@ -12,16 +5,15 @@ import DouShouQiModel struct HistoricView: View { - // GameVMs - var historicVM: HistoricVM + @ObservedObject var historicListVM: HistoricListVM var body: some View { VStack { TitlePageFrame(Text: "Historic", ImageWidth: 200, ImageHeight: 200) VStack { - List(historicVM.gameVMs) { gameVM in - GameResumeFrame(gameVM: gameVM) + List(historicListVM.historicList) { historicVM in + GameResumeFrame(historicVm: historicVM) } } .padding(.horizontal, 10) @@ -34,6 +26,6 @@ struct HistoricView: View { struct HistoricView_Previews: PreviewProvider { static var previews: some View { - HistoricView(historicVM: HistoricVM()) + HistoricView(historicListVM: HistoricListVM()) } } diff --git a/DouShouQi_App/DouShouQi_App/Views/Menu/MainMenuView.swift b/DouShouQi_App/DouShouQi_App/Views/Menu/MainMenuView.swift index 9efa0c5..1765527 100644 --- a/DouShouQi_App/DouShouQi_App/Views/Menu/MainMenuView.swift +++ b/DouShouQi_App/DouShouQi_App/Views/Menu/MainMenuView.swift @@ -14,6 +14,8 @@ struct MainMenuView: View { @State private var timer: Timer? @State private var showImage = false + @StateObject var historicList = HistoricListVM() + let images: [String] = [AppImages.SemiLion, AppImages.SemiDog, AppImages.SemiRat, AppImages.SemiWolf, AppImages.SemiLeopard, AppImages.SemiElephant, AppImages.SemiCat] // Add your image names here var body: some View { @@ -38,7 +40,7 @@ struct MainMenuView: View { HStack { VStack(spacing: 25) { MainMenuButton(text: "Play", destination: SelectPlayerView(), sound: "TitleScreenButtonSound", topRightCorner: 10, bottomRightCorner: 10) - MainMenuButton(text: "Historique", destination: HistoricView(historicVM: HistoricVM()), sound: "TitleScreenButtonSound", topRightCorner: 10, bottomRightCorner: 10) + MainMenuButton(text: "Historique", destination: HistoricView(historicListVM: historicList), sound: "TitleScreenButtonSound", topRightCorner: 10, bottomRightCorner: 10) MainMenuButton(text: "Best Scores", destination: ScoreBoardView(), sound: "TitleScreenButtonSound", topRightCorner: 10, bottomRightCorner: 10) MainMenuButton(text: "Players", destination: PlayersView(), sound: "TitleScreenButtonSound", topRightCorner: 10, bottomRightCorner: 10) MainMenuButton(text: "Settings", destination: SettingsView(), sound: "TitleScreenButtonSound", topRightCorner: 10, bottomRightCorner: 10) diff --git a/DouShouQi_App/DouShouQi_App/Views/Player/SelectPlayerView.swift b/DouShouQi_App/DouShouQi_App/Views/Player/SelectPlayerView.swift index 72da9a0..3cf061e 100644 --- a/DouShouQi_App/DouShouQi_App/Views/Player/SelectPlayerView.swift +++ b/DouShouQi_App/DouShouQi_App/Views/Player/SelectPlayerView.swift @@ -27,7 +27,7 @@ struct SelectPlayerView: View { HStack { VStack(alignment: .trailing) { - MainMenuButton(text: "Start", destination: GameView(gameVM: PlayingGameVM(withRules: ClassicRules(), andPlayer1: HumanPlayer(withName: player1.player.name, andId: .player1)!, andPlayer2: HumanPlayer(withName: player2.player.name, andId: .player2)!)!), sound: "Start", horizontalAlignment: .trailing, topLeftCorner: 10, bottomLeftCorner: 10) + MainMenuButton(text: "Start", destination: GameView(gameVM: PlayingGameVM(withRules: ClassicRules(), andPlayer1: HumanPlayer(withName: player1.player.name, andId: .player1)!, andPlayer2: HumanPlayer(withName: player2.player.name, andId: .player2)!)!, player1_name: player1.player.name, player2_name: player2.player.name), sound: "Start", horizontalAlignment: .trailing, topLeftCorner: 10, bottomLeftCorner: 10) MainMenuButton(text: "Settings", destination: ContentView(), sound: "TitleScreenButtonSound", horizontalAlignment: .trailing, topLeftCorner: 10, bottomLeftCorner: 10) } From 86c4c797674e065cd0eb551718dd13a60cf21d85 Mon Sep 17 00:00:00 2001 From: Rayhan Date: Mon, 24 Jun 2024 16:28:48 +0200 Subject: [PATCH 2/2] timer is save now --- .../DouShouQi_App.xcodeproj/project.pbxproj | 4 +++ .../Components/Game/TopGameBoard.swift | 14 ++------ .../DouShouQi_App/ViewModel/ChronoVM.swift | 32 +++++++++++++++++++ .../DouShouQi_App/Views/Game/GameView.swift | 14 +++----- 4 files changed, 44 insertions(+), 20 deletions(-) create mode 100644 DouShouQi_App/DouShouQi_App/ViewModel/ChronoVM.swift 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() - } -}*/