From 5c6ee9fd7e44bf60c87992b9d9871905cf3ac245 Mon Sep 17 00:00:00 2001 From: Jakob Haag Date: Thu, 28 May 2020 18:21:09 +0200 Subject: [PATCH 1/3] Add RoundTimer and adjust TimerComponent --- GoldWars/GoldWars.xcodeproj/project.pbxproj | 5 ++ .../GoldWars/Components/TimerComponent.swift | 37 ++++---------- GoldWars/GoldWars/Entities/HUD.swift | 2 +- GoldWars/GoldWars/GameCenterManager.swift | 2 +- GoldWars/GoldWars/MultiplayerNetwork.swift | 1 + .../GoldWars/RoundCalculatorService.swift | 2 +- GoldWars/GoldWars/RoundTimer.swift | 51 +++++++++++++++++++ 7 files changed, 69 insertions(+), 31 deletions(-) create mode 100644 GoldWars/GoldWars/RoundTimer.swift diff --git a/GoldWars/GoldWars.xcodeproj/project.pbxproj b/GoldWars/GoldWars.xcodeproj/project.pbxproj index daf1999..4123594 100644 --- a/GoldWars/GoldWars.xcodeproj/project.pbxproj +++ b/GoldWars/GoldWars.xcodeproj/project.pbxproj @@ -20,6 +20,7 @@ 3E67854024728368007B9DE4 /* CElements.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E67853F24728368007B9DE4 /* CElements.swift */; }; 3E6785422472CBEC007B9DE4 /* Way.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E6785412472CBEC007B9DE4 /* Way.swift */; }; 3E6785442472CC27007B9DE4 /* DefaultWayComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E6785432472CC27007B9DE4 /* DefaultWayComponent.swift */; }; + 3EAD889524801B6A0048A10A /* RoundTimer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3EAD889424801B6A0048A10A /* RoundTimer.swift */; }; 3EBD242E245D9332003CECE7 /* Team.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3EBD242D245D9332003CECE7 /* Team.swift */; }; 3F745DF0246F48FC00CE7375 /* PlayerMoveType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F745DEF246F48FC00CE7375 /* PlayerMoveType.swift */; }; 3FE19DB5246C7A22004827AB /* RoundCalculatorService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3FE19DB4246C7A22004827AB /* RoundCalculatorService.swift */; }; @@ -83,6 +84,7 @@ 3E67853F24728368007B9DE4 /* CElements.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CElements.swift; sourceTree = ""; }; 3E6785412472CBEC007B9DE4 /* Way.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Way.swift; sourceTree = ""; }; 3E6785432472CC27007B9DE4 /* DefaultWayComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DefaultWayComponent.swift; sourceTree = ""; }; + 3EAD889424801B6A0048A10A /* RoundTimer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RoundTimer.swift; sourceTree = ""; }; 3EBD242D245D9332003CECE7 /* Team.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Team.swift; sourceTree = ""; }; 3F745DEF246F48FC00CE7375 /* PlayerMoveType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayerMoveType.swift; sourceTree = ""; }; 3FE19DB4246C7A22004827AB /* RoundCalculatorService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RoundCalculatorService.swift; sourceTree = ""; }; @@ -179,6 +181,7 @@ 3FE19DB4246C7A22004827AB /* RoundCalculatorService.swift */, 9E0E459624796262009817A6 /* GameCenterManager.swift */, C04783EF24685995004961FB /* SettingsScene.swift */, + 3EAD889424801B6A0048A10A /* RoundTimer.swift */, ); path = GoldWars; sourceTree = ""; @@ -410,6 +413,8 @@ 8BB6FF402472B8F000162BBD /* SingeClickButtonNode.swift in Sources */, C064E9A8246C0EA50022B228 /* LabelNode.swift in Sources */, 3F745DF0246F48FC00CE7375 /* PlayerMoveType.swift in Sources */, + AB1D75A0245DEC0500671525 /* MapFactory.swift in Sources */, + 3EAD889524801B6A0048A10A /* RoundTimer.swift in Sources */, 3E67854024728368007B9DE4 /* CElements.swift in Sources */, ABA03DA0244BD54F00A66916 /* Base.swift in Sources */, C064E9AC246C151F0022B228 /* Label.swift in Sources */, diff --git a/GoldWars/GoldWars/Components/TimerComponent.swift b/GoldWars/GoldWars/Components/TimerComponent.swift index 1a7e678..c55b1b9 100644 --- a/GoldWars/GoldWars/Components/TimerComponent.swift +++ b/GoldWars/GoldWars/Components/TimerComponent.swift @@ -11,43 +11,25 @@ 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) + let roundTimer: RoundTimer + init(anchorPoint: CGPoint) { + self.labelNode = SKLabelNode(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 + self.roundTimer = RoundTimer() super.init() - startWithDuration(duration: self.duration) + startWithDuration() } - func startWithDuration(duration: TimeInterval){ - isRunning = true - endTime = Date().addingTimeInterval(duration) + func startWithDuration(){ + print("startWithDuration") + roundTimer.startTimer() 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 + self.roundTimer.timeLeft } func update() { @@ -72,4 +54,3 @@ class TimerComponent: GKComponent { } } - diff --git a/GoldWars/GoldWars/Entities/HUD.swift b/GoldWars/GoldWars/Entities/HUD.swift index 4229f3f..3c8a37c 100644 --- a/GoldWars/GoldWars/Entities/HUD.swift +++ b/GoldWars/GoldWars/Entities/HUD.swift @@ -62,7 +62,7 @@ class HUD: GKEntity { peerUnitsLabel.position = CGPoint(x: size.width * 0.95, y: size.height * 0.9) setColor(labelNodes: [hostLabel,hostUnitsLabel,peerLabel,peerUnitsLabel]) - addComponent(TimerComponent(text: "", anchorPoint: CGPoint(x: size.width * 0.5, y: size.height * 0.9), duration: 30)) + addComponent(TimerComponent(anchorPoint: CGPoint(x: size.width * 0.5, y: size.height * 0.9))) } func updateUnitSum(){ diff --git a/GoldWars/GoldWars/GameCenterManager.swift b/GoldWars/GoldWars/GameCenterManager.swift index 8b7ab1e..d5b043a 100644 --- a/GoldWars/GoldWars/GameCenterManager.swift +++ b/GoldWars/GoldWars/GameCenterManager.swift @@ -128,7 +128,7 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate ,GKM if let snapshotModel = try? jsonDecoder.decode(SnapshotModel.self, from: data) { DataService.sharedInstance.snapshotModel = snapshotModel entityManager.updateSnapshotModel(snapshotModel: snapshotModel) - entityManager.getTimer().startWithDuration(duration: 31) + entityManager.getTimer().startWithDuration() } if let mapModel = try? jsonDecoder.decode(MapGenerationModel.self, from: data) { os_log("Peer hat Map erhalten", log: LOG, type: .info) diff --git a/GoldWars/GoldWars/MultiplayerNetwork.swift b/GoldWars/GoldWars/MultiplayerNetwork.swift index 985ec14..aaac30d 100644 --- a/GoldWars/GoldWars/MultiplayerNetwork.swift +++ b/GoldWars/GoldWars/MultiplayerNetwork.swift @@ -38,6 +38,7 @@ class MultiplayerNetwork{ func sendPlayerMoves(localRoundData: LocalRoundData) { if GameCenterManager.sharedInstance.isServer == false { + print("I am client") self.isSending = true let encoder = JSONEncoder() let encoded = (try? encoder.encode(localRoundData))! diff --git a/GoldWars/GoldWars/RoundCalculatorService.swift b/GoldWars/GoldWars/RoundCalculatorService.swift index af824c4..a3bc482 100644 --- a/GoldWars/GoldWars/RoundCalculatorService.swift +++ b/GoldWars/GoldWars/RoundCalculatorService.swift @@ -129,7 +129,7 @@ class RoundCalculatorService { DataService.sharedInstance.snapshotModel = currentSnapshotModel entityManager.updateSnapshotModel(snapshotModel: currentSnapshotModel!) sleep(1) - entityManager.getTimer().startWithDuration(duration: 31) + entityManager.getTimer().startWithDuration() os_log("Finished calculating Round", log: RoundCalculatorService.LOG, type: .info) } diff --git a/GoldWars/GoldWars/RoundTimer.swift b/GoldWars/GoldWars/RoundTimer.swift new file mode 100644 index 0000000..0af4c8a --- /dev/null +++ b/GoldWars/GoldWars/RoundTimer.swift @@ -0,0 +1,51 @@ +// +// RoundTimer.swift +// GoldWars +// +// Created by Jakob Haag on 28.05.20. +// Copyright © 2020 SP2. All rights reserved. +// + +import Foundation + +class RoundTimer: Timer { + + var timer: Timer? + var timeLeft: Int = 0 + + func startTimer() { + print("start Timer") + self.timeLeft = 10 + timer = Timer.scheduledTimer( + timeInterval: 1.0, + target: self, + selector: #selector(onTimerFires), + userInfo: nil, + repeats: true + ) + } + + @objc func onTimerFires() + { + print(timeLeft) + self.timeLeft = self.timeLeft - 1 + + if timeLeft == 0 { + print("Time is up") + RoundCalculatorService.sharedInstance.resetNumberOfAttacksAndFormats() + print(!MultiplayerNetwork.sharedInstance.isSending) + if !MultiplayerNetwork.sharedInstance.isSending { + MultiplayerNetwork.sharedInstance.sendPlayerMoves(playerMoves: DataService.sharedInstance.localPlayerMoves) + } + print(!RoundCalculatorService.sharedInstance.isCalculating) + print(DataService.sharedInstance.didReceiveAllData()) + print(GameCenterManager.sharedInstance.isServer) + if !RoundCalculatorService.sharedInstance.isCalculating + && DataService.sharedInstance.didReceiveAllData() + && GameCenterManager.sharedInstance.isServer { + RoundCalculatorService.sharedInstance.calculateRound() + } + print("end method \(timeLeft)") + } + } +} From f882b7e615b418baf206aadbc8d6a2539d78ba36 Mon Sep 17 00:00:00 2001 From: Jakob Haag Date: Sat, 30 May 2020 14:46:42 +0200 Subject: [PATCH 2/3] Remove TimeComponent and set TimerLabel in the HUD. Reset Timer via RoundTimer --- GoldWars/GoldWars.xcodeproj/project.pbxproj | 5 --- GoldWars/GoldWars/DataService.swift | 2 +- .../GoldWars/Entities/EntityManager.swift | 31 ++++++++++--------- GoldWars/GoldWars/Entities/HUD.swift | 17 +++++++++- GoldWars/GoldWars/GameCenterManager.swift | 2 +- .../GoldWars/RoundCalculatorService.swift | 3 +- GoldWars/GoldWars/RoundTimer.swift | 29 +++++++++-------- GoldWars/GoldWars/Scenes/GameScene.swift | 1 - 8 files changed, 52 insertions(+), 38 deletions(-) diff --git a/GoldWars/GoldWars.xcodeproj/project.pbxproj b/GoldWars/GoldWars.xcodeproj/project.pbxproj index 4123594..38ce07b 100644 --- a/GoldWars/GoldWars.xcodeproj/project.pbxproj +++ b/GoldWars/GoldWars.xcodeproj/project.pbxproj @@ -16,7 +16,6 @@ 110360EE244B101B008610AF /* GoldWarsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 110360ED244B101B008610AF /* GoldWarsTests.swift */; }; 11036113244B3E30008610AF /* MenuScene.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11036112244B3E30008610AF /* MenuScene.swift */; }; 116060F7245C57D2004E5A36 /* EntityManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 116060F6245C57D2004E5A36 /* EntityManager.swift */; }; - 2086465C2461B66200817C23 /* TimerComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2086465B2461B66200817C23 /* TimerComponent.swift */; }; 3E67854024728368007B9DE4 /* CElements.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E67853F24728368007B9DE4 /* CElements.swift */; }; 3E6785422472CBEC007B9DE4 /* Way.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E6785412472CBEC007B9DE4 /* Way.swift */; }; 3E6785442472CC27007B9DE4 /* DefaultWayComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E6785432472CC27007B9DE4 /* DefaultWayComponent.swift */; }; @@ -80,7 +79,6 @@ 110360EF244B101B008610AF /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 11036112244B3E30008610AF /* MenuScene.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MenuScene.swift; sourceTree = ""; }; 116060F6245C57D2004E5A36 /* EntityManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EntityManager.swift; sourceTree = ""; }; - 2086465B2461B66200817C23 /* TimerComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimerComponent.swift; sourceTree = ""; }; 3E67853F24728368007B9DE4 /* CElements.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CElements.swift; sourceTree = ""; }; 3E6785412472CBEC007B9DE4 /* Way.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Way.swift; sourceTree = ""; }; 3E6785432472CC27007B9DE4 /* DefaultWayComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DefaultWayComponent.swift; sourceTree = ""; }; @@ -212,7 +210,6 @@ 9EC7E48A2461FBF700396BCD /* SliderNode.swift */, 9EEDE02E246FCD800096C735 /* SpinningLogoComponent.swift */, 9E78ACB7245CB75B00526FF7 /* TeamComponent.swift */, - 2086465B2461B66200817C23 /* TimerComponent.swift */, ); path = Components; sourceTree = ""; @@ -413,7 +410,6 @@ 8BB6FF402472B8F000162BBD /* SingeClickButtonNode.swift in Sources */, C064E9A8246C0EA50022B228 /* LabelNode.swift in Sources */, 3F745DF0246F48FC00CE7375 /* PlayerMoveType.swift in Sources */, - AB1D75A0245DEC0500671525 /* MapFactory.swift in Sources */, 3EAD889524801B6A0048A10A /* RoundTimer.swift in Sources */, 3E67854024728368007B9DE4 /* CElements.swift in Sources */, ABA03DA0244BD54F00A66916 /* Base.swift in Sources */, @@ -425,7 +421,6 @@ 9E174C84245DD8CE00209FF0 /* Button.swift in Sources */, 110360DB244B101A008610AF /* GameViewController.swift in Sources */, 3E6785442472CC27007B9DE4 /* DefaultWayComponent.swift in Sources */, - 2086465C2461B66200817C23 /* TimerComponent.swift in Sources */, C05BB9C4247D890C00411249 /* SliderComponent.swift in Sources */, 110360D3244B101A008610AF /* AppDelegate.swift in Sources */, 9EC86B9F245C88A300796EF3 /* Modal.swift in Sources */, diff --git a/GoldWars/GoldWars/DataService.swift b/GoldWars/GoldWars/DataService.swift index 23fe5f2..506478d 100644 --- a/GoldWars/GoldWars/DataService.swift +++ b/GoldWars/GoldWars/DataService.swift @@ -53,7 +53,7 @@ class DataService { var mapModel: MapGenerationModel? var entityManager = EntityManager.gameEMInstance - + func addMove(playerMove: PlayerMove) { var equalMove = localRoundData.localPlayerMoves.filter { (ele) -> Bool in ele.fromBase == playerMove.fromBase && ele.toBase == playerMove.toBase diff --git a/GoldWars/GoldWars/Entities/EntityManager.swift b/GoldWars/GoldWars/Entities/EntityManager.swift index 04e02fc..1b6ec78 100644 --- a/GoldWars/GoldWars/Entities/EntityManager.swift +++ b/GoldWars/GoldWars/Entities/EntityManager.swift @@ -42,14 +42,15 @@ class EntityManager { isModal = true } - if let hudEnitity = entity as? HUD { - scene.addChild(hudEnitity.hostLabel) - scene.addChild(hudEnitity.hostUnitsLabel) - scene.addChild(hudEnitity.peerLabel) - scene.addChild(hudEnitity.peerUnitsLabel) - scene.addChild(hudEnitity.defSkill) - scene.addChild(hudEnitity.atkSkill) - scene.addChild(hudEnitity.spySkill) + if let hudEntitiy = entity as? HUD { + scene.addChild(hudEntitiy.hostLabel) + scene.addChild(hudEntitiy.hostUnitsLabel) + scene.addChild(hudEntitiy.peerLabel) + scene.addChild(hudEntitiy.peerUnitsLabel) + scene.addChild(hudEntitiy.defSkill) + scene.addChild(hudEntitiy.atkSkill) + scene.addChild(hudEntitiy.spySkill) + scene.addChild(hudEntitiy.roundTimerLabel) isModal = true } @@ -60,9 +61,6 @@ class EntityManager { if let fire = entity.component(ofType: TeamComponent.self)?.fire{ scene.addChild(fire) } - if let timer = entity.component(ofType: TimerComponent.self) { - scene.addChild(timer.labelNode) - } if let buttonNode = entity.component(ofType: ButtonComponent.self)?.buttonNode { scene.addChild(buttonNode) } @@ -264,9 +262,6 @@ class EntityManager { return SnapshotModel(baseEntites: snapBase) } - func getTimer() -> TimerComponent { - return entities.filter{$0 is HUD}[0].component(ofType: TimerComponent.self)! - } func getUnitSum(by player: GKPlayer) -> Int{ let bases = getBasesByPlayer(for: player) @@ -307,4 +302,12 @@ class EntityManager { } }) } + + func getTimer() -> RoundTimer? { + return getHUD()?.roundTimer + } + + func updateTime(time: String) { + getHUD()?.roundTimerLabel.text = time + } } diff --git a/GoldWars/GoldWars/Entities/HUD.swift b/GoldWars/GoldWars/Entities/HUD.swift index 3c8a37c..0d6c4f7 100644 --- a/GoldWars/GoldWars/Entities/HUD.swift +++ b/GoldWars/GoldWars/Entities/HUD.swift @@ -24,6 +24,9 @@ class HUD: GKEntity { var defSkill: SingeClickButtonNode var atkSkill: SingeClickButtonNode + var roundTimerLabel: SKLabelNode + let roundTimer: RoundTimer + init(size: CGSize) { host = GameCenterManager.sharedInstance.hostingPlayer peer = GameCenterManager.sharedInstance.peerPlayer @@ -32,6 +35,13 @@ class HUD: GKEntity { peerLabel = SKLabelNode(text: peer?.displayName) peerUnitsLabel = SKLabelNode(text: "500") + roundTimerLabel = SKLabelNode(text: "") + roundTimerLabel.fontColor = UIColor.black + roundTimerLabel.fontSize = CGFloat(45) + roundTimerLabel.position = CGPoint(x: size.width * 0.5, y: size.height * 0.9 - 15) + + self.roundTimer = RoundTimer() + spySkill = SingeClickButtonNode( textureName: "yellow_circle", text: "Spy", @@ -62,7 +72,8 @@ class HUD: GKEntity { peerUnitsLabel.position = CGPoint(x: size.width * 0.95, y: size.height * 0.9) setColor(labelNodes: [hostLabel,hostUnitsLabel,peerLabel,peerUnitsLabel]) - addComponent(TimerComponent(anchorPoint: CGPoint(x: size.width * 0.5, y: size.height * 0.9))) + roundTimer.initTimer() + startWithDuration() } func updateUnitSum(){ @@ -80,4 +91,8 @@ class HUD: GKEntity { fatalError("init(coder:) has not been implemented") } + func startWithDuration(){ + roundTimer.startTimer() + RoundCalculatorService.sharedInstance.isCalculating = false + } } diff --git a/GoldWars/GoldWars/GameCenterManager.swift b/GoldWars/GoldWars/GameCenterManager.swift index d5b043a..e159b18 100644 --- a/GoldWars/GoldWars/GameCenterManager.swift +++ b/GoldWars/GoldWars/GameCenterManager.swift @@ -128,7 +128,7 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate ,GKM if let snapshotModel = try? jsonDecoder.decode(SnapshotModel.self, from: data) { DataService.sharedInstance.snapshotModel = snapshotModel entityManager.updateSnapshotModel(snapshotModel: snapshotModel) - entityManager.getTimer().startWithDuration() + entityManager.getHUD()?.startWithDuration() } if let mapModel = try? jsonDecoder.decode(MapGenerationModel.self, from: data) { os_log("Peer hat Map erhalten", log: LOG, type: .info) diff --git a/GoldWars/GoldWars/RoundCalculatorService.swift b/GoldWars/GoldWars/RoundCalculatorService.swift index a3bc482..c8ac29b 100644 --- a/GoldWars/GoldWars/RoundCalculatorService.swift +++ b/GoldWars/GoldWars/RoundCalculatorService.swift @@ -128,8 +128,7 @@ class RoundCalculatorService { MultiplayerNetwork.sharedInstance.sendSnapshotModelToPlayers() DataService.sharedInstance.snapshotModel = currentSnapshotModel entityManager.updateSnapshotModel(snapshotModel: currentSnapshotModel!) - sleep(1) - entityManager.getTimer().startWithDuration() + entityManager.getHUD()?.startWithDuration() os_log("Finished calculating Round", log: RoundCalculatorService.LOG, type: .info) } diff --git a/GoldWars/GoldWars/RoundTimer.swift b/GoldWars/GoldWars/RoundTimer.swift index 0af4c8a..3dd0f8f 100644 --- a/GoldWars/GoldWars/RoundTimer.swift +++ b/GoldWars/GoldWars/RoundTimer.swift @@ -12,10 +12,9 @@ class RoundTimer: Timer { var timer: Timer? var timeLeft: Int = 0 + var calculate = false - func startTimer() { - print("start Timer") - self.timeLeft = 10 + func initTimer() { timer = Timer.scheduledTimer( timeInterval: 1.0, target: self, @@ -25,27 +24,31 @@ class RoundTimer: Timer { ) } + func startTimer() { + timeLeft = 30 + } + @objc func onTimerFires() { - print(timeLeft) - self.timeLeft = self.timeLeft - 1 + timeLeft = timeLeft - 1 + + EntityManager.gameEMInstance.updateTime(time: (timeLeft > 0 ? String(timeLeft) : "Syncing")) if timeLeft == 0 { - print("Time is up") RoundCalculatorService.sharedInstance.resetNumberOfAttacksAndFormats() - print(!MultiplayerNetwork.sharedInstance.isSending) if !MultiplayerNetwork.sharedInstance.isSending { MultiplayerNetwork.sharedInstance.sendPlayerMoves(playerMoves: DataService.sharedInstance.localPlayerMoves) } - print(!RoundCalculatorService.sharedInstance.isCalculating) - print(DataService.sharedInstance.didReceiveAllData()) - print(GameCenterManager.sharedInstance.isServer) - if !RoundCalculatorService.sharedInstance.isCalculating + calculate = true + } + if timeLeft <= 0 { + if calculate + && !RoundCalculatorService.sharedInstance.isCalculating && DataService.sharedInstance.didReceiveAllData() && GameCenterManager.sharedInstance.isServer { - RoundCalculatorService.sharedInstance.calculateRound() + RoundCalculatorService.sharedInstance.calculateRound() + calculate = false } - print("end method \(timeLeft)") } } } diff --git a/GoldWars/GoldWars/Scenes/GameScene.swift b/GoldWars/GoldWars/Scenes/GameScene.swift index 7fa2a3b..1f602df 100644 --- a/GoldWars/GoldWars/Scenes/GameScene.swift +++ b/GoldWars/GoldWars/Scenes/GameScene.swift @@ -76,7 +76,6 @@ class GameScene: SKScene{ override func update(_ currentTime: TimeInterval) { entityManager.getBackground()?.update(deltaTime: currentTime) - entityManager.getHUD()?.component(ofType: TimerComponent.self)?.update() } func addBaseDetails(touchLocation: CGPoint, spriteNode: SKNode?, touches: Set, event: UIEvent?, entity: GKEntity){ From 729ee968b3844692cdfd26ce9fbcd4d18e627222 Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Mon, 1 Jun 2020 19:22:13 +0200 Subject: [PATCH 3/3] Delete TimerComponent file --- .../GoldWars/Components/TimerComponent.swift | 56 ------------------- 1 file changed, 56 deletions(-) delete mode 100644 GoldWars/GoldWars/Components/TimerComponent.swift diff --git a/GoldWars/GoldWars/Components/TimerComponent.swift b/GoldWars/GoldWars/Components/TimerComponent.swift deleted file mode 100644 index c55b1b9..0000000 --- a/GoldWars/GoldWars/Components/TimerComponent.swift +++ /dev/null @@ -1,56 +0,0 @@ -// -// 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 - let roundTimer: RoundTimer - init(anchorPoint: CGPoint) { - self.labelNode = SKLabelNode(text: "") - self.labelNode.fontColor = UIColor.black - self.labelNode.fontSize = CGFloat(45) - self.labelNode.position = CGPoint(x: anchorPoint.x, y: anchorPoint.y - 15) - self.roundTimer = RoundTimer() - super.init() - startWithDuration() - } - - func startWithDuration(){ - print("startWithDuration") - roundTimer.startTimer() - RoundCalculatorService.sharedInstance.isCalculating = false - } - - func timeLeft() -> Int { - self.roundTimer.timeLeft - } - - func update() { - self.labelNode.text = String(timeLeft()) - - if(isFinished()){ - self.labelNode.text = "Synching" - RoundCalculatorService.sharedInstance.resetNumberOfAttacksAndFormats() - if !MultiplayerNetwork.sharedInstance.isSending { - MultiplayerNetwork.sharedInstance.sendPlayerMoves(localRoundData: DataService.sharedInstance.localRoundData) - } - if !RoundCalculatorService.sharedInstance.isCalculating - && DataService.sharedInstance.didReceiveAllData() - && GameCenterManager.sharedInstance.isServer { - RoundCalculatorService.sharedInstance.calculateRound() - } - } - } - - required init?(coder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } - -}