Synching players timer since playermoves would not register before calculating

This commit is contained in:
Aldin Duraki 2020-05-21 00:29:19 +02:00
parent 5df7fd3dac
commit ad44ba8302
4 changed files with 20 additions and 4 deletions

View File

@ -13,6 +13,7 @@ class TimerComponent: GKComponent {
let labelNode :SKLabelNode
var endTime :Date!
var duration :Double
var isRunning = false
init(text: String, anchorPoint: CGPoint, duration: TimeInterval) {
self.labelNode = SKLabelNode(text: text)
@ -25,18 +26,26 @@ class TimerComponent: GKComponent {
}
func startWithDuration(duration: TimeInterval){
isRunning = true
endTime = Date().addingTimeInterval(duration)
RoundCalculatorService.sharedInstance.isCalculating = false
}
func timeLeft() -> Int {
if isRunning {
let remainingSeconds = Int(endTime.timeIntervalSince(Date()))
if(remainingSeconds < 0 && DataService.sharedInstance.didReceiveAllData()){
startWithDuration(duration: duration)
if(remainingSeconds == 0) {
isRunning = false
}
return remainingSeconds
}
// if(remainingSeconds < 0){
// startWithDuration(duration: duration)
// }
return 0
}
func isFinished() -> Bool {
return timeLeft() == 0
}

View File

@ -259,4 +259,8 @@ class EntityManager {
return SnapshotModel(baseEntites: snapBase)
}
func getTimer() -> TimerComponent {
return entities.filter{$0 is HUD}[0].component(ofType: TimerComponent.self)!
}
}

View File

@ -120,6 +120,7 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
if let snapshotModel = try? jsonDecoder.decode(SnapshotModel.self, from: data) {
DataService.sharedInstance.snapshotModel = snapshotModel
EntityManager.sharedInstance.updateSnapshotModel(snapshotModel: snapshotModel)
EntityManager.sharedInstance.getTimer().startWithDuration(duration: 31)
}
if let mapModel = try? jsonDecoder.decode(MapGenerationModel.self, from: data) {

View File

@ -111,6 +111,8 @@ class RoundCalculatorService {
MultiplayerNetwork.sharedInstance.sendSnapshotModelToPlayers()
DataService.sharedInstance.snapshotModel = currentSnapshotModel
EntityManager.sharedInstance.updateSnapshotModel(snapshotModel: currentSnapshotModel!)
sleep(1)
EntityManager.sharedInstance.getTimer().startWithDuration(duration: 31)
os_log("Finished calculating Round", log: RoundCalculatorService.LOG, type: .info)
}