still halting game when syncing

This commit is contained in:
Chauntalle Schüle 2020-06-02 01:03:50 +02:00
parent b23071bb08
commit 079ae4a6f6
4 changed files with 101 additions and 1 deletions

View File

@ -0,0 +1,83 @@
//
// TimerComponent.swift
// GoldWars
//
// Created by Daniel Steckert on 05.05.20.
// Copyright © 2020 SP2. All rights reserved.
//
import GameplayKit
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)
self.labelNode.fontColor = UIColor.black
self.labelNode.fontSize = CGFloat(45)
self.labelNode.position = CGPoint(x: anchorPoint.x, y: anchorPoint.y - 15)
self.duration = duration + 1
super.init()
startWithDuration(duration: self.duration)
}
func startWithDuration(duration: TimeInterval){
if RoundCalculatorService.sharedInstance.roundNr != 1{
print("entered gameState in RCS calculate Round")
StateManager.sharedInstance.changeState(wantedState: .gameSt)
}
isRunning = true
endTime = Date().addingTimeInterval(duration)
RoundCalculatorService.sharedInstance.isCalculating = false
}
func timeLeft() -> Int {
if isRunning {
let remainingSeconds = Int(endTime.timeIntervalSince(Date()))
if(remainingSeconds == 0) {
isRunning = false
}
return remainingSeconds
}
// if(remainingSeconds < 0){
// startWithDuration(duration: duration)
// }
return 0
}
func isFinished() -> Bool {
return timeLeft() == 0
}
func update() {
self.labelNode.text = String(timeLeft())
if(isFinished()){
self.labelNode.text = "Synching"
print("entered syncingSt in Timer")
StateManager.sharedInstance.changeState(wantedState: .syncingSt)
RoundCalculatorService.sharedInstance.resetNumberOfAttacksAndFormats()
if !MultiplayerNetwork.sharedInstance.isSending {
print("timer update 1")
MultiplayerNetwork.sharedInstance.sendPlayerMoves(playerMoves: DataService.sharedInstance.localPlayerMoves)
}
if !RoundCalculatorService.sharedInstance.isCalculating
&& DataService.sharedInstance.didReceiveAllData()
&& GameCenterManager.sharedInstance.isServer {
print("timer update 2")
RoundCalculatorService.sharedInstance.calculateRound()
}
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

View File

@ -110,6 +110,7 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKG
} }
func match(_ match: GKMatch, didReceive data: Data, fromRemotePlayer player: GKPlayer) { func match(_ match: GKMatch, didReceive data: Data, fromRemotePlayer player: GKPlayer) {
print("I am here")
if myMatch != match { return } if myMatch != match { return }
let jsonDecoder = JSONDecoder() let jsonDecoder = JSONDecoder()
if let randomNumberFromPeer = try? jsonDecoder.decode(RandomNumber.self, from: data) { if let randomNumberFromPeer = try? jsonDecoder.decode(RandomNumber.self, from: data) {
@ -147,11 +148,13 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKG
os_log("State 2 erhalten", log: LOG, type: .info) os_log("State 2 erhalten", log: LOG, type: .info)
sendStateToPeers(state: State(state: 3)) sendStateToPeers(state: State(state: 3))
initIsFinish = true initIsFinish = true
print("entered gameSt in case 2 match")
StateManager.sharedInstance.changeState(wantedState: .gameSt) StateManager.sharedInstance.changeState(wantedState: .gameSt)
os_log("Spiel startet", log: LOG, type: .info) os_log("Spiel startet", log: LOG, type: .info)
case 3: case 3:
os_log("State 3 erhalten", log: LOG, type: .info) os_log("State 3 erhalten", log: LOG, type: .info)
initIsFinish = true initIsFinish = true
print("entered gameSt in case 3 match")
StateManager.sharedInstance.changeState(wantedState: .gameSt) StateManager.sharedInstance.changeState(wantedState: .gameSt)
os_log("Spiel startet", log: LOG, type: .info) os_log("Spiel startet", log: LOG, type: .info)
case 4: case 4:
@ -168,11 +171,15 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKG
DataService.sharedInstance.addRemotePlayerMoves(playerName: player.displayName, localRoundData: roundData) DataService.sharedInstance.addRemotePlayerMoves(playerName: player.displayName, localRoundData: roundData)
} }
if let snapshotModel = try? jsonDecoder.decode(SnapshotModel.self, from: data) { if let snapshotModel = try? jsonDecoder.decode(SnapshotModel.self, from: data) {
print("entered syncing state in match")
StateManager.sharedInstance.changeState(wantedState: .syncingSt)
DataService.sharedInstance.snapshotModel = snapshotModel DataService.sharedInstance.snapshotModel = snapshotModel
RoundCalculatorService.sharedInstance.currentRound += 1 RoundCalculatorService.sharedInstance.currentRound += 1
entityManager.getHUD()?.setCurrentRound(round: RoundCalculatorService.sharedInstance.currentRound) entityManager.getHUD()?.setCurrentRound(round: RoundCalculatorService.sharedInstance.currentRound)
entityManager.updateSnapshotModel(snapshotModel: snapshotModel) entityManager.updateSnapshotModel(snapshotModel: snapshotModel)
entityManager.getHUD()?.startWithDuration() entityManager.getHUD()?.startWithDuration()
print("entered gameSt in match snapshot")
StateManager.sharedInstance.changeState(wantedState: .gameSt)
} }
if let mapModel = try? jsonDecoder.decode(MapGenerationModel.self, from: data) { if let mapModel = try? jsonDecoder.decode(MapGenerationModel.self, from: data) {
os_log("Peer hat Map erhalten", log: LOG, type: .info) os_log("Peer hat Map erhalten", log: LOG, type: .info)
@ -245,6 +252,7 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKG
} }
func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFind match: GKMatch) { func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFind match: GKMatch) {
print("entered syncing state in didfindmatch")
StateManager.sharedInstance.changeState(wantedState: .syncingSt) StateManager.sharedInstance.changeState(wantedState: .syncingSt)
viewController.dismiss(animated: true, completion: nil) viewController.dismiss(animated: true, completion: nil)
myMatch = match myMatch = match

View File

@ -26,7 +26,12 @@ class RoundCalculatorService {
var numberOfAttacks = 0 var numberOfAttacks = 0
var numberOfOwnUnitMoves = 0 var numberOfOwnUnitMoves = 0
var roundNr = 1
func calculateRound() { func calculateRound() {
roundNr += 1
print("roundNR \(roundNr)")
os_log("Started calculating Round", log: RoundCalculatorService.LOG, type: .info) os_log("Started calculating Round", log: RoundCalculatorService.LOG, type: .info)
isCalculating = true isCalculating = true
let currentSnapshotModel = DataService.sharedInstance.snapshotModel let currentSnapshotModel = DataService.sharedInstance.snapshotModel

View File

@ -12,6 +12,7 @@ import os
class GameState: GKState { class GameState: GKState {
let LOG = OSLog.init(subsystem: "GameState", category: "GameState") let LOG = OSLog.init(subsystem: "GameState", category: "GameState")
var aufruf = 0
override func isValidNextState(_ stateClass: AnyClass) -> Bool { override func isValidNextState(_ stateClass: AnyClass) -> Bool {
return stateClass is SyncingState.Type return stateClass is SyncingState.Type
@ -22,8 +23,11 @@ class GameState: GKState {
if CommandLine.arguments.contains("--no-matchmaking") { if CommandLine.arguments.contains("--no-matchmaking") {
StateManager.sharedInstance.menuSc!.loadScene(scene: GameScene(size: StateManager.sharedInstance.menuSc!.size)) StateManager.sharedInstance.menuSc!.loadScene(scene: GameScene(size: StateManager.sharedInstance.menuSc!.size))
} }
if aufruf == 0{
StateManager.sharedInstance.menuSc!.loadScene(scene: StateManager.sharedInstance.gameSc!) StateManager.sharedInstance.menuSc!.loadScene(scene: StateManager.sharedInstance.gameSc!)
} }
aufruf += 1
}
override func update(deltaTime seconds: TimeInterval) { override func update(deltaTime seconds: TimeInterval) {
EntityManager.gameEMInstance.getBackground()?.update(deltaTime: seconds) EntityManager.gameEMInstance.getBackground()?.update(deltaTime: seconds)