timer is save now

dev_vm_saveHistorique
Rayhân HASSOU 10 months ago
parent a171478e2c
commit 86c4c79767

@ -71,6 +71,7 @@
ECF3FD312C2722C600F5E62B /* CDHistoriqueExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECF3FD302C2722C600F5E62B /* CDHistoriqueExtension.swift */; }; ECF3FD312C2722C600F5E62B /* CDHistoriqueExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECF3FD302C2722C600F5E62B /* CDHistoriqueExtension.swift */; };
ECF3FD332C27238F00F5E62B /* Historique.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECF3FD322C27238F00F5E62B /* Historique.swift */; }; ECF3FD332C27238F00F5E62B /* Historique.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECF3FD322C27238F00F5E62B /* Historique.swift */; };
ECF3FD3A2C29A20D00F5E62B /* HistoricListVM.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECF3FD392C29A20D00F5E62B /* HistoricListVM.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 */ /* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */ /* Begin PBXContainerItemProxy section */
@ -170,6 +171,7 @@
ECF3FD372C299CC500F5E62B /* Start.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = Start.mp3; sourceTree = "<group>"; }; ECF3FD372C299CC500F5E62B /* Start.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = Start.mp3; sourceTree = "<group>"; };
ECF3FD382C299CC800F5E62B /* SelectFighterSound.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = SelectFighterSound.mp3; sourceTree = "<group>"; }; ECF3FD382C299CC800F5E62B /* SelectFighterSound.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = SelectFighterSound.mp3; sourceTree = "<group>"; };
ECF3FD392C29A20D00F5E62B /* HistoricListVM.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HistoricListVM.swift; sourceTree = "<group>"; }; ECF3FD392C29A20D00F5E62B /* HistoricListVM.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HistoricListVM.swift; sourceTree = "<group>"; };
ECF3FD3C2C29B64100F5E62B /* ChronoVM.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChronoVM.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */ /* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */ /* Begin PBXFrameworksBuildPhase section */
@ -316,6 +318,7 @@
EC62C5262C11E24B0048CD0B /* Players */, EC62C5262C11E24B0048CD0B /* Players */,
646F04BC2C0F5491003C8600 /* Settings */, 646F04BC2C0F5491003C8600 /* Settings */,
646FA83F2C0730B4001466BA /* Game */, 646FA83F2C0730B4001466BA /* Game */,
ECF3FD3C2C29B64100F5E62B /* ChronoVM.swift */,
); );
path = ViewModel; path = ViewModel;
sourceTree = "<group>"; sourceTree = "<group>";
@ -691,6 +694,7 @@
EC0540C42C08A13E0032E9EF /* GameView.swift in Sources */, EC0540C42C08A13E0032E9EF /* GameView.swift in Sources */,
EC05BFC42C04C3C4000F7B19 /* SettingsView.swift in Sources */, EC05BFC42C04C3C4000F7B19 /* SettingsView.swift in Sources */,
ECF3FD312C2722C600F5E62B /* CDHistoriqueExtension.swift in Sources */, ECF3FD312C2722C600F5E62B /* CDHistoriqueExtension.swift in Sources */,
ECF3FD3D2C29B64100F5E62B /* ChronoVM.swift in Sources */,
EC62C53D2C1C69200048CD0B /* DouShouQi_App.xcdatamodeld in Sources */, EC62C53D2C1C69200048CD0B /* DouShouQi_App.xcdatamodeld in Sources */,
EC62C50F2C05D06A0048CD0B /* AddPlayerView.swift in Sources */, EC62C50F2C05D06A0048CD0B /* AddPlayerView.swift in Sources */,
EC62C53F2C1C6D1A0048CD0B /* CDPlayerExtension.swift in Sources */, EC62C53F2C1C6D1A0048CD0B /* CDPlayerExtension.swift in Sources */,

@ -7,9 +7,10 @@
import SwiftUI import SwiftUI
import DouShouQiModel import DouShouQiModel
struct TopGameBoard: View { struct TopGameBoard: View {
@ObservedObject var gameVM: PlayingGameVM @ObservedObject var gameVM: PlayingGameVM
@ObservedObject var chronoVM: ChronometerViewModel
var body: some View { var body: some View {
VStack { VStack {
@ -32,7 +33,7 @@ struct TopGameBoard: View {
Image(systemName: "clock") Image(systemName: "clock")
.resizable() .resizable()
.frame(width: 30, height: 30) .frame(width: 30, height: 30)
Text("4:11") Text(chronoVM.formattedTime())
.font(.title) .font(.title)
.foregroundColor(.black) .foregroundColor(.black)
} }
@ -43,9 +44,6 @@ struct TopGameBoard: View {
Text("Last move :") Text("Last move :")
.font(.title2) .font(.title2)
.foregroundColor(.black) .foregroundColor(.black)
/*Image(systemName: "cheetah")
.resizable()
.frame(width: 30, height: 30)*/
Text(gameVM.lastPieceMoved) Text(gameVM.lastPieceMoved)
Text(gameVM.lastMove) Text(gameVM.lastMove)
.font(.title2) .font(.title2)
@ -62,9 +60,3 @@ struct TopGameBoard: View {
.background(Color.red.opacity(0.5)) .background(Color.red.opacity(0.5))
} }
} }
//struct TopGameBoard_Previews: PreviewProvider {
// static var previews: some View {
// TopGameBoard(gameVM: vm)
// }
//}

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

@ -6,6 +6,7 @@ struct GameView: View {
var gameScene: GameScene = GameScene(size: CGSize(width: 700, height: 900)) var gameScene: GameScene = GameScene(size: CGSize(width: 700, height: 900))
@StateObject var gameVM: PlayingGameVM @StateObject var gameVM: PlayingGameVM
@StateObject var chronoVM = ChronometerViewModel()
@State private var showHome = false @State private var showHome = false
@ -16,11 +17,12 @@ struct GameView: View {
var body: some View { var body: some View {
ZStack { ZStack {
VStack { 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)) SpriteView(scene: GameScene(size: CGSize(width: 700, height: 900), gameVM: gameVM))
} }
.onAppear { .onAppear {
playSound(named: "Fight") playSound(named: "Fight")
chronoVM.startTimer()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.7) { DispatchQueue.main.asyncAfter(deadline: .now() + 0.7) {
MusicPlayer.shared.playBackgroundMusic(music: "GameMusic") MusicPlayer.shared.playBackgroundMusic(music: "GameMusic")
@ -45,6 +47,7 @@ struct GameView: View {
.shadow(radius: 10) .shadow(radius: 10)
.padding() .padding()
.onAppear { .onAppear {
chronoVM.stopTimer()
saveHistoric() saveHistoric()
Timer.scheduledTimer(withTimeInterval: 10, repeats: false) { _ in Timer.scheduledTimer(withTimeInterval: 10, repeats: false) { _ in
withAnimation { withAnimation {
@ -53,7 +56,6 @@ struct GameView: View {
} }
} }
} }
} }
.onDisappear { .onDisappear {
MusicPlayer.shared.stopBackgroundMusic() MusicPlayer.shared.stopBackgroundMusic()
@ -62,13 +64,7 @@ struct GameView: View {
} }
private func saveHistoric() { 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) historicVm.SavePlayer(historic: newHistoric)
} }
} }
/*struct GameView_Previews: PreviewProvider {
static var previews: some View {
GameView()
}
}*/

Loading…
Cancel
Save