From 5eb8267266333ad950ed57256d25fee28f31a6c1 Mon Sep 17 00:00:00 2001 From: Jakob Haag Date: Fri, 15 May 2020 11:59:05 +0200 Subject: [PATCH 1/7] import GameKit, format Code and change method to getBasesByPlayer --- GoldWars/GoldWars/Scenes/GameScene.swift | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/GoldWars/GoldWars/Scenes/GameScene.swift b/GoldWars/GoldWars/Scenes/GameScene.swift index bb47730..dae1566 100644 --- a/GoldWars/GoldWars/Scenes/GameScene.swift +++ b/GoldWars/GoldWars/Scenes/GameScene.swift @@ -8,6 +8,7 @@ import SpriteKit import GameplayKit +import GameKit class GameScene: SKScene{ @@ -46,12 +47,14 @@ class GameScene: SKScene{ if !(entityManager.getTeamByBase(base: currentDraggedBase!) == entityManager.getTeamByBase(base: base)){ entityManager.add(Modal(modaltype: .BaseAttack, base: currentDraggedBase!, - anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2), entityManager: entityManager, gameScene: self)) + anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2), + entityManager: entityManager, gameScene: self)) entityManager.update((currentDraggedBase?.attackBase(base: base, units: 100))!) }else { entityManager.add(Modal(modaltype: .BaseAttack, base: currentDraggedBase!, - anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2), entityManager: entityManager, gameScene: self)) + anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2), + entityManager: entityManager, gameScene: self)) } } @@ -71,7 +74,8 @@ class GameScene: SKScene{ } entityManager.add(Modal(modaltype: .BaseDetails, base: entity as! Base, - anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2), entityManager: entityManager, gameScene: self)) + anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2), + entityManager: entityManager, gameScene: self)) } } } @@ -96,7 +100,7 @@ class GameScene: SKScene{ body.text = "Schicke \( ((e.component(ofType: SliderComponent.self)?.sliderNode.getValue ?? 0) * CGFloat((e as! Modal).unitCount)).rounded(.up)) Einheiten " } } - let bases = entityManager.getBasesByTeam(for: .team1) + let bases = entityManager.getBasesByPlayer(for: GKLocalPlayer.local) for base in bases { if atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode{ From f2c6de058c93d18df810c5c51c62a0cc0c1a45fc Mon Sep 17 00:00:00 2001 From: Jakob Haag Date: Fri, 15 May 2020 11:59:42 +0200 Subject: [PATCH 2/7] remove unnecessary code --- GoldWars/GoldWars/MatchmakingHelper.swift | 5 ----- 1 file changed, 5 deletions(-) diff --git a/GoldWars/GoldWars/MatchmakingHelper.swift b/GoldWars/GoldWars/MatchmakingHelper.swift index 48628ce..a99bb31 100644 --- a/GoldWars/GoldWars/MatchmakingHelper.swift +++ b/GoldWars/GoldWars/MatchmakingHelper.swift @@ -33,8 +33,6 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe var viewController: UIViewController? var mpMatchStarted: Bool var isServer: Bool - var spieler1: GKPlayer? - var nameSpieler1 = "" var menusc: MenuScene? let localPlayer: GKLocalPlayer = GKLocalPlayer.local @@ -154,9 +152,6 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe if player == GKLocalPlayer.local { self.isServer = true - self.spieler1 = player - self.nameSpieler1 = self.spieler1!.displayName - } else { self.isServer = false } From c53ab5f85744b2d975dd0a08c2b1c49f07945dd9 Mon Sep 17 00:00:00 2001 From: Jakob Haag Date: Fri, 15 May 2020 12:00:01 +0200 Subject: [PATCH 3/7] init players to basePlayerOne and Two --- GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift b/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift index beff433..fa20823 100644 --- a/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift +++ b/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift @@ -8,6 +8,7 @@ import Foundation import SpriteKit +import GameKit class TwoPlayerDefaultTestMap: MapProtocol { @@ -24,6 +25,7 @@ class TwoPlayerDefaultTestMap: MapProtocol { // Create Bases let basePlayerOne = Base( position: CGPoint(x: self.size.width * 0.1, y: self.size.height / 2), + player: (MatchmakingHelper.sharedInstance.isServer) ? GKLocalPlayer.local : MatchmakingHelper.sharedInstance.mpMatch?.players[0], team: .team1 ) @@ -46,6 +48,7 @@ class TwoPlayerDefaultTestMap: MapProtocol { let basePlayerTwo = Base( position: CGPoint(x: self.size.width * 0.9, y: self.size.height / 2), + player: (!MatchmakingHelper.sharedInstance.isServer) ? GKLocalPlayer.local : MatchmakingHelper.sharedInstance.mpMatch?.players[0], team: .team2 ) From 23248876ba8db64acd938f72c36de6bfe50e7434 Mon Sep 17 00:00:00 2001 From: Jakob Haag Date: Fri, 15 May 2020 12:01:26 +0200 Subject: [PATCH 4/7] add method getBaseByPlayer and getBasesByPlayer, format code and import GameKit --- .../GoldWars/Entities/EntityManager.swift | 195 ++++++++++-------- 1 file changed, 109 insertions(+), 86 deletions(-) diff --git a/GoldWars/GoldWars/Entities/EntityManager.swift b/GoldWars/GoldWars/Entities/EntityManager.swift index 38ef9a1..6afa00e 100644 --- a/GoldWars/GoldWars/Entities/EntityManager.swift +++ b/GoldWars/GoldWars/Entities/EntityManager.swift @@ -8,46 +8,47 @@ import SpriteKit import GameplayKit +import GameKit class EntityManager { var entities = Set() let scene: SKScene - var isModal: Bool + var isModal: Bool init(scene: SKScene) { self.scene = scene - isModal = false + isModal = false } func add(_ entity: GKEntity) { entities.insert(entity) - if let spriteNode = entity.component(ofType: DefaultBaseComponent.self)?.spriteNode { - scene.addChild(spriteNode) - } - if let fire = entity.component(ofType: TeamComponent.self)?.fire{ - scene.addChild(fire) - } - if let spriteNode = entity.component(ofType: ModalBackgroundComponent.self)?.spriteNode { - scene.addChild(spriteNode) - } - if let modal = entity.component(ofType: ModalContentComponent.self) { - scene.addChild(modal.header) - scene.addChild(modal.body) - scene.addChild(modal.footer) - } - if let skill = entity.component(ofType: AtkBoostSkillComponent.self) { - scene.addChild(skill.shapeNode) - scene.addChild(skill.labelNode) - } - if let skill = entity.component(ofType: DefBoostSkillComponent.self) { - scene.addChild(skill.shapeNode) - scene.addChild(skill.labelNode) - } - if let skill = entity.component(ofType: SpySkillComponent.self) { - scene.addChild(skill.shapeNode) - scene.addChild(skill.labelNode) - } + if let spriteNode = entity.component(ofType: DefaultBaseComponent.self)?.spriteNode { + scene.addChild(spriteNode) + } + if let fire = entity.component(ofType: TeamComponent.self)?.fire{ + scene.addChild(fire) + } + if let spriteNode = entity.component(ofType: ModalBackgroundComponent.self)?.spriteNode { + scene.addChild(spriteNode) + } + if let modal = entity.component(ofType: ModalContentComponent.self) { + scene.addChild(modal.header) + scene.addChild(modal.body) + scene.addChild(modal.footer) + } + if let skill = entity.component(ofType: AtkBoostSkillComponent.self) { + scene.addChild(skill.shapeNode) + scene.addChild(skill.labelNode) + } + if let skill = entity.component(ofType: DefBoostSkillComponent.self) { + scene.addChild(skill.shapeNode) + scene.addChild(skill.labelNode) + } + if let skill = entity.component(ofType: SpySkillComponent.self) { + scene.addChild(skill.shapeNode) + scene.addChild(skill.labelNode) + } if let timer = entity.component(ofType: TimerComponent.self) { scene.addChild(timer.labelNode) } @@ -60,10 +61,10 @@ class EntityManager { scene.addChild(node) } } - if let sliderNode = entity.component(ofType: SliderComponent.self)?.sliderNode { - scene.addChild(sliderNode.sliderKnob) - scene.addChild(sliderNode.sliderLine) - } + if let sliderNode = entity.component(ofType: SliderComponent.self)?.sliderNode { + scene.addChild(sliderNode.sliderKnob) + scene.addChild(sliderNode.sliderLine) + } if let labelNode = entity.component(ofType: LabelComponent.self)?.labelNode { scene.addChild(labelNode) } @@ -73,75 +74,97 @@ class EntityManager { if let spriteNode = entity.component(ofType: DefaultBaseComponent.self)?.spriteNode { spriteNode.removeFromParent() } - if let spriteNode = entity.component(ofType: ModalBackgroundComponent.self)?.spriteNode { - spriteNode.removeFromParent() - } - if let modal = entity.component(ofType: ModalContentComponent.self) { - modal.header.removeFromParent() - modal.body.removeFromParent() - modal.footer.removeFromParent() - } - if let sliderNode = entity.component(ofType: SliderComponent.self)?.sliderNode { - sliderNode.sliderKnob.removeFromParent() - sliderNode.sliderLine.removeFromParent() - } + if let spriteNode = entity.component(ofType: ModalBackgroundComponent.self)?.spriteNode { + spriteNode.removeFromParent() + } + if let modal = entity.component(ofType: ModalContentComponent.self) { + modal.header.removeFromParent() + modal.body.removeFromParent() + modal.footer.removeFromParent() + } + if let sliderNode = entity.component(ofType: SliderComponent.self)?.sliderNode { + sliderNode.sliderKnob.removeFromParent() + sliderNode.sliderLine.removeFromParent() + } if let modalButton = entity.component(ofType: ButtonComponent.self) { modalButton.buttonNode.removeFromParent() isModal = false } entities.remove(entity) } - - func update(_ entities: [GKEntity]){ - for entity in entities { - self.entities.update(with: entity) - let base = (entity as! Base) - - if base.changeOwnerShip { - base.addComponent(TeamComponent(team: (entities[0] as! Base).component(ofType: TeamComponent.self)!.team, position: (base.component(ofType: DefaultBaseComponent.self)?.spriteNode.position)!)) - base.changeOwnerShip = false - scene.addChild(base.component(ofType: TeamComponent.self)!.fire) - } - } - - } - func getBaseByTeam(for team: Team) -> GKEntity? { - for entity in entities { - if let teamComponent = entity.component(ofType: TeamComponent.self), - let _ = entity.component(ofType: DefaultBaseComponent.self) { + + func update(_ entities: [GKEntity]){ + for entity in entities { + self.entities.update(with: entity) + let base = (entity as! Base) + + if base.changeOwnership { + base.addComponent(TeamComponent( + team: (entities[0] as! Base).component(ofType: TeamComponent.self)!.team, + player: (entities[0] as! Base).component(ofType: TeamComponent.self)!.player, + position: (base.component(ofType: DefaultBaseComponent.self)?.spriteNode.position)! + ) + ) + base.changeOwnership = false + scene.addChild(base.component(ofType: TeamComponent.self)!.fire) + } + } + + } + + func getBaseByPlayer(for player: GKPlayer) -> GKEntity? { + for entity in entities { + if let teamComponent = entity.component(ofType: TeamComponent.self), + let _ = entity.component(ofType: DefaultBaseComponent.self) { + if teamComponent.player == player { + return entity + } + } + } + return nil + } + + func getBaseByTeam(for team: Team) -> GKEntity? { + for entity in entities { + if let teamComponent = entity.component(ofType: TeamComponent.self), + let _ = entity.component(ofType: DefaultBaseComponent.self) { if teamComponent.team == team { return entity - } - } - } - return nil + } + } + } + return nil } - + func getBasesByTeam(for team: Team) -> Set { return entities.filter{$0 is Base && ($0 as! Base).component(ofType: TeamComponent.self)?.team == team } as! Set } - - func getTeamByBase(base: Base) -> Team? { - for entity in entities { - if entity is Base && entity == base{ - for component in entity.components{ - if component is TeamComponent { - return entity.component(ofType: TeamComponent.self)!.team - } - } - } - } - return nil - } - + + func getBasesByPlayer(for player: GKPlayer) -> Set { + return entities.filter{$0 is Base && ($0 as! Base).component(ofType: TeamComponent.self)?.player == player } as! Set + } + + func getTeamByBase(base: Base) -> Team? { + for entity in entities { + if entity is Base && entity == base{ + for component in entity.components{ + if component is TeamComponent { + return entity.component(ofType: TeamComponent.self)!.team + } + } + } + } + return nil + } + func getBackground() -> GKEntity? { return entities.filter{$0 is Background}[0] } - - func getBaseNodeByTeam(for team: Team) -> SKSpriteNode? { - return getBaseByTeam(for: team)?.component(ofType: DefaultBaseComponent.self)?.spriteNode - } - + + func getBaseNodeByTeam(for team: Team) -> SKSpriteNode? { + return getBaseByTeam(for: team)?.component(ofType: DefaultBaseComponent.self)?.spriteNode + } + func getButtonByName(buttonName:String) -> Button { return entities.filter{$0 is Button && ($0 as! Button).name == buttonName }[0] as! Button } From 43dd2aa8531f14025eac09721c568b15e585ff28 Mon Sep 17 00:00:00 2001 From: Jakob Haag Date: Fri, 15 May 2020 12:01:55 +0200 Subject: [PATCH 5/7] import GameKit and add ownershipPlayer to component --- GoldWars/GoldWars/Entities/Base.swift | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/GoldWars/GoldWars/Entities/Base.swift b/GoldWars/GoldWars/Entities/Base.swift index 70fe75d..5ee587e 100644 --- a/GoldWars/GoldWars/Entities/Base.swift +++ b/GoldWars/GoldWars/Entities/Base.swift @@ -8,32 +8,36 @@ import SpriteKit import GameplayKit +import GameKit class Base: GKEntity { var unitCount: Int var adjacencyList: Array - var changeOwnerShip: Bool + var changeOwnership: Bool + var ownershipPlayer: GKPlayer - init(position: CGPoint, team: Team! = nil) { + init(position: CGPoint, player: GKPlayer! = nil, team: Team! = nil) { self.unitCount = 0 self.adjacencyList = [Base]() - self.changeOwnerShip = false + self.changeOwnership = false + self.ownershipPlayer = player super.init() addComponent(DefaultBaseComponent(texture: SKTexture(imageNamed: "Base"), position: position)) - if(team != nil){ - addComponent(TeamComponent(team: team!, position: position)) + if(team != nil && player != nil){ + addComponent(TeamComponent(team: team!, player: player!, position: position)) self.unitCount = 500 } } - func attackBase(base: Base, units:Int) -> [GKEntity]{ - base.changeOwnerShip = true - self.unitCount -= units - base.unitCount += units - return [self, base] - } + func attackBase(base: Base, units:Int) -> [GKEntity]{ + base.changeOwnership = true + base.ownershipPlayer = self.ownershipPlayer + self.unitCount -= units + base.unitCount += units + return [self, base] + } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") From ac1ad05bac8b7645cbb3c9e7ce19ef3eeb4c4817 Mon Sep 17 00:00:00 2001 From: Jakob Haag Date: Fri, 15 May 2020 12:02:07 +0200 Subject: [PATCH 6/7] add player to teamcomponent --- GoldWars/GoldWars/Components/TeamComponent.swift | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/GoldWars/GoldWars/Components/TeamComponent.swift b/GoldWars/GoldWars/Components/TeamComponent.swift index 379a1cc..2369f7b 100644 --- a/GoldWars/GoldWars/Components/TeamComponent.swift +++ b/GoldWars/GoldWars/Components/TeamComponent.swift @@ -8,12 +8,14 @@ import SpriteKit import GameplayKit +import GameKit class TeamComponent: GKComponent { let team: Team let fire: SKEmitterNode + let player: GKPlayer - init(team: Team, position: CGPoint) { + init(team: Team, player: GKPlayer, position: CGPoint) { fire = SKEmitterNode(fileNamed: "Fire")! fire.zPosition = -1 fire.position = position @@ -29,6 +31,7 @@ class TeamComponent: GKComponent { } self.team = team + self.player = player super.init() } From b24d93c399d4279736b4c0b392c1642dca11a2f5 Mon Sep 17 00:00:00 2001 From: Jakob Haag Date: Fri, 15 May 2020 12:12:52 +0200 Subject: [PATCH 7/7] add nullable assigment to ownershipPlayer --- GoldWars/GoldWars/Entities/Base.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GoldWars/GoldWars/Entities/Base.swift b/GoldWars/GoldWars/Entities/Base.swift index 5ee587e..6cb0158 100644 --- a/GoldWars/GoldWars/Entities/Base.swift +++ b/GoldWars/GoldWars/Entities/Base.swift @@ -15,7 +15,7 @@ class Base: GKEntity { var unitCount: Int var adjacencyList: Array var changeOwnership: Bool - var ownershipPlayer: GKPlayer + var ownershipPlayer: GKPlayer? init(position: CGPoint, player: GKPlayer! = nil, team: Team! = nil) { self.unitCount = 0