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

View File

@ -259,4 +259,8 @@ class EntityManager {
return SnapshotModel(baseEntites: snapBase) 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) { if let snapshotModel = try? jsonDecoder.decode(SnapshotModel.self, from: data) {
DataService.sharedInstance.snapshotModel = snapshotModel DataService.sharedInstance.snapshotModel = snapshotModel
EntityManager.sharedInstance.updateSnapshotModel(snapshotModel: snapshotModel) EntityManager.sharedInstance.updateSnapshotModel(snapshotModel: snapshotModel)
EntityManager.sharedInstance.getTimer().startWithDuration(duration: 31)
} }
if let mapModel = try? jsonDecoder.decode(MapGenerationModel.self, from: data) { if let mapModel = try? jsonDecoder.decode(MapGenerationModel.self, from: data) {

View File

@ -111,6 +111,8 @@ class RoundCalculatorService {
MultiplayerNetwork.sharedInstance.sendSnapshotModelToPlayers() MultiplayerNetwork.sharedInstance.sendSnapshotModelToPlayers()
DataService.sharedInstance.snapshotModel = currentSnapshotModel DataService.sharedInstance.snapshotModel = currentSnapshotModel
EntityManager.sharedInstance.updateSnapshotModel(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) os_log("Finished calculating Round", log: RoundCalculatorService.LOG, type: .info)
} }