diff --git a/GoldWars/GoldWars/Entities/Base.swift b/GoldWars/GoldWars/Entities/Base.swift index 1306885..e01ba62 100644 --- a/GoldWars/GoldWars/Entities/Base.swift +++ b/GoldWars/GoldWars/Entities/Base.swift @@ -40,12 +40,12 @@ class Base: GKEntity{ if ownershipPlayer == GKLocalPlayer.local { self.component(ofType: DefaultBaseComponent.self)?.labelNode.text = "\(unitCount)" } - - } func doPlayerMoveTypeToBase(base: Base, playerMoveType: PlayerMoveType, units: Int) -> [GKEntity]{ - base.changeOwnership = true + if base.ownershipPlayer != GKLocalPlayer.local { + base.changeOwnership = true + } base.ownershipPlayer = self.ownershipPlayer self.unitCount -= units base.unitCount += units diff --git a/GoldWars/GoldWars/Entities/EntityManager.swift b/GoldWars/GoldWars/Entities/EntityManager.swift index 1b6ec78..8b95144 100644 --- a/GoldWars/GoldWars/Entities/EntityManager.swift +++ b/GoldWars/GoldWars/Entities/EntityManager.swift @@ -117,7 +117,6 @@ class EntityManager { scene.addChild(fire) } } - base.changeOwnership = false } } } @@ -129,6 +128,7 @@ class EntityManager { let snapBase = self.getSnapshotBaseById(baseId: base.baseID, snapshotModel: snapshotModel) var getOwnerBySnapBase: GKPlayer? = nil base.unitCount = snapBase.unitCount + base.changeOwnership = false if snapBase.ownership != nil { getOwnerBySnapBase = GameCenterManager.sharedInstance.getGKPlayerByUsername(displayName: snapBase.ownership!) @@ -139,10 +139,9 @@ class EntityManager { if getOwnerBySnapBase != nil { if getOwnerBySnapBase == GKLocalPlayer.local { base.component(ofType: DefaultBaseComponent.self)?.labelNode.text = "\(base.unitCount)" - }else { + } else { base.component(ofType: DefaultBaseComponent.self)?.labelNode.text = "" } - base.changeOwnership = true base.ownershipPlayer = getOwnerBySnapBase if (entity.component(ofType: TeamComponent.self) != nil) { entity.component(ofType: TeamComponent.self)!.change(to: getTeamByPlayer(playerName: snapBase.ownership!), to: getOwnerBySnapBase!) diff --git a/GoldWars/GoldWars/RoundCalculatorService.swift b/GoldWars/GoldWars/RoundCalculatorService.swift index c8ac29b..7372c9e 100644 --- a/GoldWars/GoldWars/RoundCalculatorService.swift +++ b/GoldWars/GoldWars/RoundCalculatorService.swift @@ -16,6 +16,7 @@ class RoundCalculatorService { let ATK_BOOST_MULTIPLICATOR = 1.1 let DEF_BOOST_MULTIPLICATOR = 1.1 + var allPlayerMoves: [String: [PlayerMove]] = [:] // TODO: Better data structure var boosts: [String: (Bool, Bool)] = [:] // First bool is atk boost, second is def boost @@ -61,7 +62,9 @@ class RoundCalculatorService { } } } - + } + + for (baseId, playerMovesByBase) in baseSpecificMoves { var combinePotentionalForces: [String: PlayerMove] = [:] for playerMoves in playerMovesByBase { @@ -122,6 +125,26 @@ class RoundCalculatorService { } baseSpecificMoves.removeValue(forKey: baseId) } + var player1BaseCount = 0; + var player2BaseCount = 0; + let player1 = GameCenterManager.sharedInstance.hostingPlayer?.displayName + let player2 = GameCenterManager.sharedInstance.peerPlayer?.displayName + for baseEntry in currentSnapshotModel!.baseEntites { + if baseEntry.ownership == player1 { + player1BaseCount += 1 + } else if baseEntry.ownership == player2 { + player2BaseCount += 1 + } + } + currentSnapshotModel?.baseEntites = currentSnapshotModel!.baseEntites.map { (BaseEntityModel) -> BaseEntityModel in + if BaseEntityModel.ownership == player1 { + BaseEntityModel.unitCount += player1BaseCount + } else if BaseEntityModel.ownership == player2 { + BaseEntityModel.unitCount += player2BaseCount + } + return BaseEntityModel + } + allPlayerMoves.removeAll() DataService.sharedInstance.localRoundData.localPlayerMoves.removeAll() DataService.sharedInstance.localRoundData.hasAttackBoost = false DataService.sharedInstance.localRoundData.hasDefenceBoost = false @@ -133,10 +156,6 @@ class RoundCalculatorService { } func collectBaseSpecificMoves() -> [Int: [String: [PlayerMove]]] { - - var allPlayerMoves: [String: [PlayerMove]] = [:] - - // collect moves from remote player for playerMove in DataService.sharedInstance.remotePlayerMoves { allPlayerMoves[playerMove.key] = playerMove.value.localPlayerMoves } @@ -145,13 +164,11 @@ class RoundCalculatorService { DataService.sharedInstance.localRoundData.hasAttackBoost, DataService.sharedInstance.localRoundData.hasDefenceBoost ) - boosts[GameCenterManager.sharedInstance.peerPlayer!.displayName] = ( - DataService.sharedInstance.remotePlayerMoves[GameCenterManager.sharedInstance.peerPlayer!.displayName]!.hasAttackBoost, - DataService.sharedInstance.remotePlayerMoves[GameCenterManager.sharedInstance.peerPlayer!.displayName]!.hasDefenceBoost + DataService.sharedInstance.remotePlayerMoves[GameCenterManager.sharedInstance.peerPlayer!.displayName]?.hasAttackBoost ?? false, + DataService.sharedInstance.remotePlayerMoves[GameCenterManager.sharedInstance.peerPlayer!.displayName]?.hasDefenceBoost ?? false ) - - // collect moves from local player + allPlayerMoves[GKLocalPlayer.local.displayName] = DataService.sharedInstance.localRoundData.localPlayerMoves var baseSpecificMoves: [Int: [String: [PlayerMove]]] = [:] @@ -169,9 +186,7 @@ class RoundCalculatorService { } } } - DataService.sharedInstance.remotePlayerMoves.removeAll() - return baseSpecificMoves } diff --git a/GoldWars/GoldWars/RoundTimer.swift b/GoldWars/GoldWars/RoundTimer.swift index 3dd0f8f..92dfb28 100644 --- a/GoldWars/GoldWars/RoundTimer.swift +++ b/GoldWars/GoldWars/RoundTimer.swift @@ -37,7 +37,7 @@ class RoundTimer: Timer { if timeLeft == 0 { RoundCalculatorService.sharedInstance.resetNumberOfAttacksAndFormats() if !MultiplayerNetwork.sharedInstance.isSending { - MultiplayerNetwork.sharedInstance.sendPlayerMoves(playerMoves: DataService.sharedInstance.localPlayerMoves) + MultiplayerNetwork.sharedInstance.sendPlayerMoves(localRoundData: DataService.sharedInstance.localRoundData) } calculate = true } diff --git a/GoldWars/GoldWars/Scenes/GameScene.swift b/GoldWars/GoldWars/Scenes/GameScene.swift index 1f602df..892f2e3 100644 --- a/GoldWars/GoldWars/Scenes/GameScene.swift +++ b/GoldWars/GoldWars/Scenes/GameScene.swift @@ -53,7 +53,6 @@ class GameScene: SKScene{ addBaseDetails(touchLocation: touchLocation, spriteNode: spriteNode, touches: touches, event: event, entity: entity) } } - } override func touchesMoved(_ touches: Set, with event: UIEvent?) { @@ -68,9 +67,7 @@ class GameScene: SKScene{ } } checkSlider() - let bases = entityManager.getBasesByPlayer(for: GKLocalPlayer.local) - checkBases(bases: bases, touchLocation: touchLocation) } @@ -93,7 +90,6 @@ class GameScene: SKScene{ anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2), gameScene: self, currentDraggedBase: currentDraggedBase, touchLocation: touchLocation, collisionBase: collisionBase)) - } } } @@ -141,13 +137,15 @@ class GameScene: SKScene{ func checkBases(bases: Set, touchLocation: CGPoint){ for base in bases { - if atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode{ - if !isMoveTouch { - currentDraggedBase = base + if atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode { + if !base.changeOwnership { + if !isMoveTouch { + currentDraggedBase = base + } + isMoveTouch = true + moveFireAndBase(base: base, touchLocation: touchLocation) + showNearestBases(base: base) } - isMoveTouch = true - moveFireAndBase(base: base, touchLocation: touchLocation) - showNearestBases(base: base) } } }