diff --git a/GoldWars/GoldWars/Components/ButtonNode.swift b/GoldWars/GoldWars/Components/ButtonNode.swift index ab92e38..b98164a 100644 --- a/GoldWars/GoldWars/Components/ButtonNode.swift +++ b/GoldWars/GoldWars/Components/ButtonNode.swift @@ -22,7 +22,7 @@ class ButtonNode: SKSpriteNode { } } - let onButtonPress: () -> () + var onButtonPress: () -> () init(textureName: String, text: String, isEnabled: Bool, position: CGPoint, onButtonPress: @escaping () -> ()) { self.onButtonPress = onButtonPress diff --git a/GoldWars/GoldWars/Components/TimerComponent.swift b/GoldWars/GoldWars/Components/TimerComponent.swift new file mode 100644 index 0000000..e69de29 diff --git a/GoldWars/GoldWars/Entities/EntityManager.swift b/GoldWars/GoldWars/Entities/EntityManager.swift index 8b95144..b739eca 100644 --- a/GoldWars/GoldWars/Entities/EntityManager.swift +++ b/GoldWars/GoldWars/Entities/EntityManager.swift @@ -51,7 +51,7 @@ class EntityManager { scene.addChild(hudEntitiy.atkSkill) scene.addChild(hudEntitiy.spySkill) scene.addChild(hudEntitiy.roundTimerLabel) - isModal = true + scene.addChild(hudEntitiy.finishButton) } if let spriteNode = entity.component(ofType: DefaultBaseComponent.self) { diff --git a/GoldWars/GoldWars/Entities/HUD.swift b/GoldWars/GoldWars/Entities/HUD.swift index 0d6c4f7..6ff9a2d 100644 --- a/GoldWars/GoldWars/Entities/HUD.swift +++ b/GoldWars/GoldWars/Entities/HUD.swift @@ -27,6 +27,8 @@ class HUD: GKEntity { var roundTimerLabel: SKLabelNode let roundTimer: RoundTimer + var finishButton: ButtonNode + init(size: CGSize) { host = GameCenterManager.sharedInstance.hostingPlayer peer = GameCenterManager.sharedInstance.peerPlayer @@ -34,11 +36,12 @@ class HUD: GKEntity { hostUnitsLabel = SKLabelNode(text: "500" ) 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) + roundTimerLabel.position = CGPoint(x: size.width * 0.5, y: size.height * 0.9) + roundTimerLabel.horizontalAlignmentMode = .center self.roundTimer = RoundTimer() @@ -63,7 +66,24 @@ class HUD: GKEntity { position: CGPoint(x: EntityManager.gameEMInstance.scene.size.width * 0.95, y: EntityManager.gameEMInstance.scene.size.height * 0.1), onButtonPress: {DataService.sharedInstance.localRoundData.hasAttackBoost = true} ) + + finishButton = SingeClickButtonNode( + textureName: "yellow_button04", + text: "Done", + isEnabled: true, + position: CGPoint( + x: EntityManager.gameEMInstance.scene.size.width * 0.1, + y: EntityManager.gameEMInstance.scene.size.height * 0.1), + onButtonPress: { } + ) + finishButton.size = CGSize(width: 80, height: 40) + finishButton.zPosition = 4 super.init() + + finishButton.onButtonPress = { [unowned self] in + self.finishRound() + } + hostLabel.position = CGPoint(x: size.width * 0.02, y: size.height * 0.95) hostLabel.horizontalAlignmentMode = .left peerLabel.position = CGPoint(x: size.width * 0.98, y: size.height * 0.95) @@ -93,6 +113,13 @@ class HUD: GKEntity { func startWithDuration(){ roundTimer.startTimer() + finishButton.isEnabled = true + self.roundTimer.roundEnded = "Syncing" RoundCalculatorService.sharedInstance.isCalculating = false } + + func finishRound() -> () { + self.roundTimer.timeLeft = 1; + self.roundTimer.roundEnded = "Waiting for other player..." + } } diff --git a/GoldWars/GoldWars/RoundCalculatorService.swift b/GoldWars/GoldWars/RoundCalculatorService.swift index 7372c9e..fc57f96 100644 --- a/GoldWars/GoldWars/RoundCalculatorService.swift +++ b/GoldWars/GoldWars/RoundCalculatorService.swift @@ -35,7 +35,7 @@ class RoundCalculatorService { var baseSpecificMoves = collectBaseSpecificMoves() // TODO: Refactor to a less complex way - for (baseId, var playerMovesByBase) in baseSpecificMoves { + for (baseId, playerMovesByBase) in baseSpecificMoves { let targetBase = currentSnapshotModel?.baseEntites.filter { $0.baseId == baseId }[0] let possiblyOwnershipMoves = playerMovesByBase.filter { $0.key == targetBase?.ownership} @@ -50,10 +50,10 @@ class RoundCalculatorService { } } } - playerMovesByBase.removeValue(forKey: playerName) + baseSpecificMoves[baseId]!.removeValue(forKey: playerName) } - for (_, playerMoves) in playerMovesByBase { + for playerMoves in baseSpecificMoves[baseId]!.values { for playerMove in playerMoves { for base in currentSnapshotModel!.baseEntites { if base.baseId == playerMove.fromBase { @@ -201,9 +201,9 @@ class RoundCalculatorService { func increaseMoveCounter(ownBase: Bool!) { if ownBase { - self.numberOfOwnUnitMoves = self.numberOfOwnUnitMoves + 1 + self.numberOfOwnUnitMoves += 1 } else { - self.numberOfAttacks = self.numberOfAttacks + 1 + self.numberOfAttacks += 1 } } } diff --git a/GoldWars/GoldWars/RoundTimer.swift b/GoldWars/GoldWars/RoundTimer.swift index 92dfb28..8f27a60 100644 --- a/GoldWars/GoldWars/RoundTimer.swift +++ b/GoldWars/GoldWars/RoundTimer.swift @@ -13,6 +13,7 @@ class RoundTimer: Timer { var timer: Timer? var timeLeft: Int = 0 var calculate = false + var roundEnded = "Syncing" func initTimer() { timer = Timer.scheduledTimer( @@ -30,9 +31,9 @@ class RoundTimer: Timer { @objc func onTimerFires() { - timeLeft = timeLeft - 1 + timeLeft -= 1 - EntityManager.gameEMInstance.updateTime(time: (timeLeft > 0 ? String(timeLeft) : "Syncing")) + EntityManager.gameEMInstance.updateTime(time: (timeLeft > 0 ? String(timeLeft) : roundEnded)) if timeLeft == 0 { RoundCalculatorService.sharedInstance.resetNumberOfAttacksAndFormats()