diff --git a/GoldWars/GoldWars/Components/TimerComponent.swift b/GoldWars/GoldWars/Components/TimerComponent.swift index e69de29..69db2eb 100644 --- a/GoldWars/GoldWars/Components/TimerComponent.swift +++ b/GoldWars/GoldWars/Components/TimerComponent.swift @@ -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") + } + +} + diff --git a/GoldWars/GoldWars/GameCenterManager.swift b/GoldWars/GoldWars/GameCenterManager.swift index dd073c1..90712b9 100644 --- a/GoldWars/GoldWars/GameCenterManager.swift +++ b/GoldWars/GoldWars/GameCenterManager.swift @@ -110,6 +110,7 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKG } func match(_ match: GKMatch, didReceive data: Data, fromRemotePlayer player: GKPlayer) { + print("I am here") if myMatch != match { return } let jsonDecoder = JSONDecoder() 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) sendStateToPeers(state: State(state: 3)) initIsFinish = true + print("entered gameSt in case 2 match") StateManager.sharedInstance.changeState(wantedState: .gameSt) os_log("Spiel startet", log: LOG, type: .info) case 3: os_log("State 3 erhalten", log: LOG, type: .info) initIsFinish = true + print("entered gameSt in case 3 match") StateManager.sharedInstance.changeState(wantedState: .gameSt) os_log("Spiel startet", log: LOG, type: .info) case 4: @@ -168,11 +171,15 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKG DataService.sharedInstance.addRemotePlayerMoves(playerName: player.displayName, localRoundData: roundData) } 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 RoundCalculatorService.sharedInstance.currentRound += 1 entityManager.getHUD()?.setCurrentRound(round: RoundCalculatorService.sharedInstance.currentRound) entityManager.updateSnapshotModel(snapshotModel: snapshotModel) entityManager.getHUD()?.startWithDuration() + print("entered gameSt in match snapshot") + StateManager.sharedInstance.changeState(wantedState: .gameSt) } if let mapModel = try? jsonDecoder.decode(MapGenerationModel.self, from: data) { 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) { + print("entered syncing state in didfindmatch") StateManager.sharedInstance.changeState(wantedState: .syncingSt) viewController.dismiss(animated: true, completion: nil) myMatch = match diff --git a/GoldWars/GoldWars/RoundCalculatorService.swift b/GoldWars/GoldWars/RoundCalculatorService.swift index 95f0c72..a68ad9c 100644 --- a/GoldWars/GoldWars/RoundCalculatorService.swift +++ b/GoldWars/GoldWars/RoundCalculatorService.swift @@ -26,7 +26,12 @@ class RoundCalculatorService { var numberOfAttacks = 0 var numberOfOwnUnitMoves = 0 + var roundNr = 1 + func calculateRound() { + roundNr += 1 + print("roundNR \(roundNr)") + os_log("Started calculating Round", log: RoundCalculatorService.LOG, type: .info) isCalculating = true let currentSnapshotModel = DataService.sharedInstance.snapshotModel diff --git a/GoldWars/GoldWars/States/GameState.swift b/GoldWars/GoldWars/States/GameState.swift index de2623f..6ca3e5a 100644 --- a/GoldWars/GoldWars/States/GameState.swift +++ b/GoldWars/GoldWars/States/GameState.swift @@ -12,6 +12,7 @@ import os class GameState: GKState { let LOG = OSLog.init(subsystem: "GameState", category: "GameState") + var aufruf = 0 override func isValidNextState(_ stateClass: AnyClass) -> Bool { return stateClass is SyncingState.Type @@ -22,7 +23,10 @@ class GameState: GKState { if CommandLine.arguments.contains("--no-matchmaking") { StateManager.sharedInstance.menuSc!.loadScene(scene: GameScene(size: StateManager.sharedInstance.menuSc!.size)) } - StateManager.sharedInstance.menuSc!.loadScene(scene: StateManager.sharedInstance.gameSc!) + if aufruf == 0{ + StateManager.sharedInstance.menuSc!.loadScene(scene: StateManager.sharedInstance.gameSc!) + } + aufruf += 1 } override func update(deltaTime seconds: TimeInterval) {