From c344ff82f4581979adb250f54d4a8cd616cc3a5b Mon Sep 17 00:00:00 2001 From: Aldin Duraki Date: Thu, 25 Jun 2020 11:43:59 +0200 Subject: [PATCH] Fix for #109 & #110 --- GoldWars/GoldWars.xcodeproj/project.pbxproj | 4 ---- GoldWars/GoldWars/DataService.swift | 10 +++++----- GoldWars/GoldWars/Entities/Base.swift | 6 ++---- GoldWars/GoldWars/Entities/Modal.swift | 3 +-- GoldWars/GoldWars/Enums/PlayerMoveType.swift | 13 ------------- GoldWars/GoldWars/Scenes/GameScene.swift | 3 +++ 6 files changed, 11 insertions(+), 28 deletions(-) delete mode 100644 GoldWars/GoldWars/Enums/PlayerMoveType.swift diff --git a/GoldWars/GoldWars.xcodeproj/project.pbxproj b/GoldWars/GoldWars.xcodeproj/project.pbxproj index bf2da7c..7d6a026 100644 --- a/GoldWars/GoldWars.xcodeproj/project.pbxproj +++ b/GoldWars/GoldWars.xcodeproj/project.pbxproj @@ -23,7 +23,6 @@ 3E6785422472CBEC007B9DE4 /* Way.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E6785412472CBEC007B9DE4 /* Way.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 */; }; 3F79FFE02486F7CD003F79C3 /* Explosion.sks in Resources */ = {isa = PBXBuildFile; fileRef = 3F79FFDF2486F7CD003F79C3 /* Explosion.sks */; }; 3FE19DB5246C7A22004827AB /* RoundCalculatorService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3FE19DB4246C7A22004827AB /* RoundCalculatorService.swift */; }; 8BB6FF402472B8F000162BBD /* SingeClickButtonNode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8BB6FF3F2472B8F000162BBD /* SingeClickButtonNode.swift */; }; @@ -91,7 +90,6 @@ 3E6785412472CBEC007B9DE4 /* Way.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Way.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 = ""; }; 3F79FFDF2486F7CD003F79C3 /* Explosion.sks */ = {isa = PBXFileReference; lastKnownFileType = file.sks; path = Explosion.sks; sourceTree = ""; }; 3FE19DB4246C7A22004827AB /* RoundCalculatorService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RoundCalculatorService.swift; sourceTree = ""; }; 8BB6FF3F2472B8F000162BBD /* SingeClickButtonNode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SingeClickButtonNode.swift; sourceTree = ""; }; @@ -265,7 +263,6 @@ isa = PBXGroup; children = ( 3EBD242D245D9332003CECE7 /* Team.swift */, - 3F745DEF246F48FC00CE7375 /* PlayerMoveType.swift */, ); path = Enums; sourceTree = ""; @@ -430,7 +427,6 @@ 9E174C8A245E1A0A00209FF0 /* Background.swift in Sources */, 8BB6FF402472B8F000162BBD /* SingeClickButtonNode.swift in Sources */, C064E9A8246C0EA50022B228 /* LabelNode.swift in Sources */, - 3F745DF0246F48FC00CE7375 /* PlayerMoveType.swift in Sources */, 3EAD889524801B6A0048A10A /* RoundTimer.swift in Sources */, 3E67854024728368007B9DE4 /* CElements.swift in Sources */, ABA03DA0244BD54F00A66916 /* Base.swift in Sources */, diff --git a/GoldWars/GoldWars/DataService.swift b/GoldWars/GoldWars/DataService.swift index d8a9f24..2e09c43 100644 --- a/GoldWars/GoldWars/DataService.swift +++ b/GoldWars/GoldWars/DataService.swift @@ -64,11 +64,11 @@ class DataService { var entityManager = EntityManager.gameEMInstance func addMove(playerMove: PlayerMove) { - var equalMove = localRoundData.localPlayerMoves.filter { (ele) -> Bool in - ele.fromBase == playerMove.fromBase && ele.toBase == playerMove.toBase - } - if equalMove.count == 1 { - equalMove[0].unitCount = Int(equalMove[0].unitCount) + Int(playerMove.unitCount) + let equalMoveIdx = localRoundData.localPlayerMoves.firstIndex(where: { (localPlayerMove) -> Bool in + localPlayerMove.toBase == playerMove.toBase && localPlayerMove.fromBase == playerMove.fromBase + }) + if equalMoveIdx != nil { + localRoundData.localPlayerMoves[equalMoveIdx!].unitCount = Int(localRoundData.localPlayerMoves[equalMoveIdx!].unitCount) + Int(playerMove.unitCount) } else { self.localRoundData.localPlayerMoves.append(playerMove) } diff --git a/GoldWars/GoldWars/Entities/Base.swift b/GoldWars/GoldWars/Entities/Base.swift index ba1bbdc..65e0c3c 100644 --- a/GoldWars/GoldWars/Entities/Base.swift +++ b/GoldWars/GoldWars/Entities/Base.swift @@ -42,7 +42,7 @@ class Base: GKEntity{ } } - func doPlayerMoveTypeToBase(base: Base, playerMoveType: PlayerMoveType, units: Int) -> [GKEntity]{ + func doPlayerMoveTypeToBase(base: Base, units: Int) -> [GKEntity]{ if base.ownershipPlayer != GKLocalPlayer.local { base.changeOwnership = true } @@ -53,11 +53,9 @@ class Base: GKEntity{ if base.component(ofType: TeamComponent.self)?.unitcountLabel.text != "" { base.component(ofType: TeamComponent.self)?.unitcountLabel.text = "\(base.unitCount)" } - - base.ownershipPlayer = self.ownershipPlayer DataService.sharedInstance.addMove(playerMove: PlayerMove(fromBase: self.baseID, toBase: base.baseID, - unitCount: units * playerMoveType.rawValue) + unitCount: units) ) return [self, base] } diff --git a/GoldWars/GoldWars/Entities/Modal.swift b/GoldWars/GoldWars/Entities/Modal.swift index 412e736..1658d04 100644 --- a/GoldWars/GoldWars/Entities/Modal.swift +++ b/GoldWars/GoldWars/Entities/Modal.swift @@ -109,11 +109,10 @@ class Modal: GKEntity{ } func sendUnits(currentDraggedBase: Base?, touchLocation: CGPoint, gameScene: GameScene, collisionBase: Base?){ - let moveType: PlayerMoveType = self.type == ModalType.BaseAttack ? .AtkMove : .TxnMove for base in currentDraggedBase!.adjacencyList { if base == collisionBase { RoundCalculatorService.sharedInstance.increaseMoveCounter(ownBase: currentDraggedBase?.ownershipPlayer == base.ownershipPlayer) - entityManager.update((currentDraggedBase?.doPlayerMoveTypeToBase(base: base, playerMoveType: moveType, units: Int(GameScene.sendUnits)))!) + entityManager.update((currentDraggedBase?.doPlayerMoveTypeToBase(base: base, units: Int(GameScene.sendUnits)))!) GameScene.sendUnits = 0 } } diff --git a/GoldWars/GoldWars/Enums/PlayerMoveType.swift b/GoldWars/GoldWars/Enums/PlayerMoveType.swift deleted file mode 100644 index 2f1bdd6..0000000 --- a/GoldWars/GoldWars/Enums/PlayerMoveType.swift +++ /dev/null @@ -1,13 +0,0 @@ -// -// PlayerMoveType.swift -// GoldWars -// -// Created by Aldin Duraki on 16.05.20. -// Copyright © 2020 SP2. All rights reserved. -// - -enum PlayerMoveType: Int, Codable{ - case AtkMove = 1 - case TxnMove = -1 -} - diff --git a/GoldWars/GoldWars/Scenes/GameScene.swift b/GoldWars/GoldWars/Scenes/GameScene.swift index f4f431b..315faef 100644 --- a/GoldWars/GoldWars/Scenes/GameScene.swift +++ b/GoldWars/GoldWars/Scenes/GameScene.swift @@ -264,6 +264,9 @@ class GameScene: SKScene{ } func isAttackMove() -> Bool { + if collisionBase?.ownershipPlayer == nil { + return true + } return collisionBase?.ownershipPlayer != currentDraggedBase?.ownershipPlayer && !(collisionBase?.changeOwnership ?? false) }