From f67c673ca71fc7b420120ca5ce5c0d85bd168d77 Mon Sep 17 00:00:00 2001 From: Jakob Haag Date: Mon, 11 May 2020 18:03:45 +0200 Subject: [PATCH 01/11] add gamegit and gkplayer 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 c75a391..116b64f 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, position: CGPoint, player: GKPlayer) { 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 276d2cfa2e490ed3cc8517b2d1f296992a1651ae Mon Sep 17 00:00:00 2001 From: Jakob Haag Date: Mon, 11 May 2020 18:04:35 +0200 Subject: [PATCH 02/11] add player to teamcomponent --- GoldWars/GoldWars/Entities/Base.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/GoldWars/GoldWars/Entities/Base.swift b/GoldWars/GoldWars/Entities/Base.swift index e50a690..7fd86b3 100644 --- a/GoldWars/GoldWars/Entities/Base.swift +++ b/GoldWars/GoldWars/Entities/Base.swift @@ -25,8 +25,8 @@ class Base: GKEntity { 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!, position: position, player: player!)) self.unitCount = 500 } } From 5b11dacbaa79fa214648c089eac5e926f110b9da Mon Sep 17 00:00:00 2001 From: Jakob Haag Date: Mon, 11 May 2020 18:05:05 +0200 Subject: [PATCH 03/11] add method getBaseByPlayer to entityManager --- GoldWars/GoldWars/Entities/EntityManager.swift | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/GoldWars/GoldWars/Entities/EntityManager.swift b/GoldWars/GoldWars/Entities/EntityManager.swift index 05b9f14..161d3b4 100644 --- a/GoldWars/GoldWars/Entities/EntityManager.swift +++ b/GoldWars/GoldWars/Entities/EntityManager.swift @@ -8,6 +8,7 @@ import SpriteKit import GameplayKit +import GameKit class EntityManager { @@ -92,13 +93,22 @@ class EntityManager { 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.addComponent( + TeamComponent( + team: (entities[0] as! Base).component(ofType: TeamComponent.self)!.team, + position: (base.component(ofType: DefaultBaseComponent.self)?.spriteNode.position)!, + player: (entities[0] as! Base).component(ofType: TeamComponent.self)!.player)) base.changeOwnerShip = false scene.addChild(base.component(ofType: TeamComponent.self)!.fire) } } } + + func getBaseByPlayer(for player: GKPlayer) -> Set { + return entities.filter{$0 is Base && ($0 as! Base).component(ofType: TeamComponent.self)?.player == player } as! Set + } + func getBaseByTeam(for team: Team) -> GKEntity? { for entity in entities { if let teamComponent = entity.component(ofType: TeamComponent.self), From 0c9abe0d0870ccef5d088db49ecf5978c562eeaa Mon Sep 17 00:00:00 2001 From: Jakob Haag Date: Mon, 11 May 2020 18:05:43 +0200 Subject: [PATCH 04/11] init player1 to playerOneBase and player2 to playerTwoBase on init --- GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift b/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift index 43e62f6..a45af1f 100644 --- a/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift +++ b/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift @@ -25,8 +25,7 @@ class TwoPlayerDefaultTestMap: MapProtocol { // Create Bases let basePlayerOne = Base( position: CGPoint(x: self.size.width * 0.1, y: self.size.height / 2), - // ToDo: not final version. Better would be something like MatchmakingHelper.spieler1 but does not work yet - player: GKLocalPlayer.local, + player: MatchmakingHelper.sharedInstance.mpMatch?.players[0], team: .team1 ) @@ -49,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.mpMatch?.players[1], team: .team2 ) From 282a72925045e324ac1ad04f3303e05099cc8caa Mon Sep 17 00:00:00 2001 From: Jakob Haag Date: Mon, 11 May 2020 18:06:35 +0200 Subject: [PATCH 05/11] change method getBaseByTeam to getBaseByPlayer --- GoldWars/GoldWars/Scenes/GameScene.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/GoldWars/GoldWars/Scenes/GameScene.swift b/GoldWars/GoldWars/Scenes/GameScene.swift index cd50ff6..93fbf3c 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{ @@ -105,7 +106,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.getBaseByPlayer(for: GKLocalPlayer.local) for base in bases { if atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode{ From 41d5fc8a5c19c657266a14d1b6693d4e04ebce09 Mon Sep 17 00:00:00 2001 From: Jakob Haag Date: Mon, 11 May 2020 19:18:19 +0200 Subject: [PATCH 06/11] test --- GoldWars/GoldWars/Entities/Modal.swift | 2 +- .../GoldWars/Map/TwoPlayerDefaultTestMap.swift | 5 +++-- GoldWars/GoldWars/MatchmakingHelper.swift | 15 +++++++++++---- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/GoldWars/GoldWars/Entities/Modal.swift b/GoldWars/GoldWars/Entities/Modal.swift index a8f638f..0c87e03 100644 --- a/GoldWars/GoldWars/Entities/Modal.swift +++ b/GoldWars/GoldWars/Entities/Modal.swift @@ -20,7 +20,7 @@ class Modal: GKEntity{ addComponent(ModalBackgroundComponent(anchorPoint: anchorPoint)) addComponent(ModalContentComponent( header: "Basis Information", - body: "Diese Basis enthält \(base.unitCount) Einheiten", + body: "Diese Basis enthält \(base.unitCount) Einheiten und gehört Spieler \(base.ownerShipPlayer?.displayName ?? "null")", footer: "", anchorPoint: anchorPoint ) diff --git a/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift b/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift index a45af1f..c8b3c50 100644 --- a/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift +++ b/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift @@ -22,10 +22,11 @@ class TwoPlayerDefaultTestMap: MapProtocol { func load() { + // Create Bases let basePlayerOne = Base( position: CGPoint(x: self.size.width * 0.1, y: self.size.height / 2), - player: MatchmakingHelper.sharedInstance.mpMatch?.players[0], + player: (MatchmakingHelper.sharedInstance.isServer) ? GKLocalPlayer.local : MatchmakingHelper.sharedInstance.mpMatch?.players[0], team: .team1 ) @@ -48,7 +49,7 @@ class TwoPlayerDefaultTestMap: MapProtocol { let basePlayerTwo = Base( position: CGPoint(x: self.size.width * 0.9, y: self.size.height / 2), - player: MatchmakingHelper.sharedInstance.mpMatch?.players[1], + player: (!MatchmakingHelper.sharedInstance.isServer) ? GKLocalPlayer.local : MatchmakingHelper.sharedInstance.mpMatch?.players[0], team: .team2 ) diff --git a/GoldWars/GoldWars/MatchmakingHelper.swift b/GoldWars/GoldWars/MatchmakingHelper.swift index 72e3c25..5545d25 100644 --- a/GoldWars/GoldWars/MatchmakingHelper.swift +++ b/GoldWars/GoldWars/MatchmakingHelper.swift @@ -33,7 +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,13 +153,21 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe if player == GKLocalPlayer.local { self.isServer = true - self.spieler1 = player - self.nameSpieler1 = self.spieler1!.displayName - } else { self.isServer = false } + print("====================================") + print(GKLocalPlayer.local) + for player in MatchmakingHelper.sharedInstance.mpMatch!.players { + print(player) + } + print("====================================") + + print(MatchmakingHelper.sharedInstance.mpMatch) + + print("====================================") + self.delegate?.matchStarted() self.menusc!.loadScene(scene: GameScene(size: self.menusc!.size)) }) From 9c751cc84fc50065245aaeae7e2047fbe0ce9933 Mon Sep 17 00:00:00 2001 From: Jakob Haag Date: Mon, 11 May 2020 19:37:00 +0200 Subject: [PATCH 07/11] remove code for testing --- GoldWars/GoldWars/Entities/Modal.swift | 2 +- GoldWars/GoldWars/MatchmakingHelper.swift | 12 ------------ 2 files changed, 1 insertion(+), 13 deletions(-) diff --git a/GoldWars/GoldWars/Entities/Modal.swift b/GoldWars/GoldWars/Entities/Modal.swift index 0c87e03..a8f638f 100644 --- a/GoldWars/GoldWars/Entities/Modal.swift +++ b/GoldWars/GoldWars/Entities/Modal.swift @@ -20,7 +20,7 @@ class Modal: GKEntity{ addComponent(ModalBackgroundComponent(anchorPoint: anchorPoint)) addComponent(ModalContentComponent( header: "Basis Information", - body: "Diese Basis enthält \(base.unitCount) Einheiten und gehört Spieler \(base.ownerShipPlayer?.displayName ?? "null")", + body: "Diese Basis enthält \(base.unitCount) Einheiten", footer: "", anchorPoint: anchorPoint ) diff --git a/GoldWars/GoldWars/MatchmakingHelper.swift b/GoldWars/GoldWars/MatchmakingHelper.swift index 5545d25..46d3de7 100644 --- a/GoldWars/GoldWars/MatchmakingHelper.swift +++ b/GoldWars/GoldWars/MatchmakingHelper.swift @@ -156,18 +156,6 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe } else { self.isServer = false } - - print("====================================") - print(GKLocalPlayer.local) - for player in MatchmakingHelper.sharedInstance.mpMatch!.players { - print(player) - } - print("====================================") - - print(MatchmakingHelper.sharedInstance.mpMatch) - - print("====================================") - self.delegate?.matchStarted() self.menusc!.loadScene(scene: GameScene(size: self.menusc!.size)) }) From bd5d142a4318a3d74574200996b09bdd86023a38 Mon Sep 17 00:00:00 2001 From: Jakob Haag Date: Mon, 11 May 2020 19:44:31 +0200 Subject: [PATCH 08/11] refactor variables --- GoldWars/GoldWars/Entities/Base.swift | 12 ++++++------ GoldWars/GoldWars/Entities/EntityManager.swift | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/GoldWars/GoldWars/Entities/Base.swift b/GoldWars/GoldWars/Entities/Base.swift index 7fd86b3..f41f483 100644 --- a/GoldWars/GoldWars/Entities/Base.swift +++ b/GoldWars/GoldWars/Entities/Base.swift @@ -14,14 +14,14 @@ class Base: GKEntity { var unitCount: Int var adjacencyList: Array - var changeOwnerShip: Bool - var ownerShipPlayer: GKPlayer? + var changeOwnership: Bool + var ownershipPlayer: GKPlayer? init(position: CGPoint, player: GKPlayer? = nil, team: Team? = nil) { self.unitCount = 0 self.adjacencyList = [Base]() - self.changeOwnerShip = false - self.ownerShipPlayer = player + self.changeOwnership = false + self.ownershipPlayer = player super.init() addComponent(DefaultBaseComponent(texture: SKTexture(imageNamed: "Base"), position: position)) @@ -32,8 +32,8 @@ class Base: GKEntity { } func attackBase(base: Base, units:Int) -> [GKEntity]{ - base.changeOwnerShip = true - base.ownerShipPlayer = self.ownerShipPlayer + base.changeOwnership = true + base.ownershipPlayer = self.ownershipPlayer self.unitCount -= units base.unitCount += units return [self, base] diff --git a/GoldWars/GoldWars/Entities/EntityManager.swift b/GoldWars/GoldWars/Entities/EntityManager.swift index 161d3b4..8cc49f6 100644 --- a/GoldWars/GoldWars/Entities/EntityManager.swift +++ b/GoldWars/GoldWars/Entities/EntityManager.swift @@ -92,13 +92,13 @@ class EntityManager { self.entities.update(with: entity) let base = (entity as! Base) - if base.changeOwnerShip { + if base.changeOwnership { base.addComponent( TeamComponent( team: (entities[0] as! Base).component(ofType: TeamComponent.self)!.team, position: (base.component(ofType: DefaultBaseComponent.self)?.spriteNode.position)!, player: (entities[0] as! Base).component(ofType: TeamComponent.self)!.player)) - base.changeOwnerShip = false + base.changeOwnership = false scene.addChild(base.component(ofType: TeamComponent.self)!.fire) } } From 4189d0f6345945590ecda05abfb09c3371f555b0 Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Sun, 10 May 2020 16:48:04 +0000 Subject: [PATCH 09/11] Revert "Merge branch '33-basisownership-anhand-des-gkplayers' into 'development'" This reverts merge request !54 --- GoldWars/GoldWars/AppDelegate.swift | 18 +-- .../Components/AtkBoostSkillComponent.swift | 42 +++--- .../Components/AttackActionComponent.swift | 12 +- .../Components/BackgroundComponent.swift | 12 +- GoldWars/GoldWars/Components/BaseNode.swift | 44 +++--- .../GoldWars/Components/ButtonComponent.swift | 4 +- GoldWars/GoldWars/Components/ButtonNode.swift | 54 +++---- .../Components/DefBoostSkillComponent.swift | 42 +++--- .../Components/DefaultBaseComponent.swift | 22 +-- .../Components/ModalBackgroundComponent.swift | 26 ++-- .../Components/ModalContentComponent.swift | 58 ++++---- .../GoldWars/Components/SliderComponent.swift | 22 +-- GoldWars/GoldWars/Components/SliderNode.swift | 98 ++++++------ .../Components/SpySkillComponent.swift | 42 +++--- .../GoldWars/Components/TeamComponent.swift | 14 +- .../GoldWars/Components/TimerComponent.swift | 8 +- GoldWars/GoldWars/Entities/Background.swift | 8 +- GoldWars/GoldWars/Entities/Base.swift | 3 +- GoldWars/GoldWars/Entities/Button.swift | 2 +- .../GoldWars/Entities/EntityManager.swift | 140 +++++++++--------- GoldWars/GoldWars/Entities/HUD.swift | 46 +++--- GoldWars/GoldWars/Entities/Modal.swift | 62 ++++---- GoldWars/GoldWars/Enums/ModalType.swift | 4 +- GoldWars/GoldWars/Enums/Team.swift | 2 +- GoldWars/GoldWars/GameCenterHelper.swift | 10 +- GoldWars/GoldWars/GameViewController.swift | 18 +-- GoldWars/GoldWars/MatchmakingHelper.swift | 22 +-- GoldWars/GoldWars/Scenes/GameScene.swift | 60 ++++---- GoldWars/GoldWars/Scenes/MenuScene.swift | 14 +- GoldWars/GoldWarsTests/GoldWarsTests.swift | 10 +- 30 files changed, 454 insertions(+), 465 deletions(-) diff --git a/GoldWars/GoldWars/AppDelegate.swift b/GoldWars/GoldWars/AppDelegate.swift index 7bf5733..17e9b6c 100644 --- a/GoldWars/GoldWars/AppDelegate.swift +++ b/GoldWars/GoldWars/AppDelegate.swift @@ -10,32 +10,32 @@ import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { - + var window: UIWindow? - - + + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. return true } - + func applicationWillResignActive(_ application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. } - + func applicationDidEnterBackground(_ application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. } - + func applicationWillEnterForeground(_ application: UIApplication) { // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. } - + func applicationDidBecomeActive(_ application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - - + + } diff --git a/GoldWars/GoldWars/Components/AtkBoostSkillComponent.swift b/GoldWars/GoldWars/Components/AtkBoostSkillComponent.swift index 7bb11e6..a2d36b3 100644 --- a/GoldWars/GoldWars/Components/AtkBoostSkillComponent.swift +++ b/GoldWars/GoldWars/Components/AtkBoostSkillComponent.swift @@ -9,25 +9,25 @@ import GameplayKit class AtkBoostSkillComponent: GKComponent{ - - let shapeNode: SKShapeNode - let labelNode: SKLabelNode - - init(text: String, texture: SKTexture?, anchorPoint: CGPoint) { - self.labelNode = SKLabelNode(text: text) - self.shapeNode = SKShapeNode(circleOfRadius: 30) - self.shapeNode.position = anchorPoint - self.labelNode.position = CGPoint(x: anchorPoint.x, y: anchorPoint.y - 15) - if texture != nil { - shapeNode.fillTexture = texture - }else { - shapeNode.fillColor = SKColor.gray - } - super.init() - } - - required init?(coder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } - + + let shapeNode: SKShapeNode + let labelNode: SKLabelNode + + init(text: String, texture: SKTexture?, anchorPoint: CGPoint) { + self.labelNode = SKLabelNode(text: text) + self.shapeNode = SKShapeNode(circleOfRadius: 30) + self.shapeNode.position = anchorPoint + self.labelNode.position = CGPoint(x: anchorPoint.x, y: anchorPoint.y - 15) + if texture != nil { + shapeNode.fillTexture = texture + }else { + shapeNode.fillColor = SKColor.gray + } + super.init() + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + } diff --git a/GoldWars/GoldWars/Components/AttackActionComponent.swift b/GoldWars/GoldWars/Components/AttackActionComponent.swift index e10f1b2..ba46e1a 100644 --- a/GoldWars/GoldWars/Components/AttackActionComponent.swift +++ b/GoldWars/GoldWars/Components/AttackActionComponent.swift @@ -9,19 +9,19 @@ import GameplayKit class AttackActionComponent: GKComponent { - - + + init(unitCount: Int, adjacencyList: Array, position: CGPoint) { super.init() } - + required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } - - + + func action() { // Not implemented yet } - + } diff --git a/GoldWars/GoldWars/Components/BackgroundComponent.swift b/GoldWars/GoldWars/Components/BackgroundComponent.swift index e766ad9..7239741 100644 --- a/GoldWars/GoldWars/Components/BackgroundComponent.swift +++ b/GoldWars/GoldWars/Components/BackgroundComponent.swift @@ -9,10 +9,10 @@ import GameplayKit class BackgroundComponent: GKComponent{ - + var nodes = [SKSpriteNode]() let size: CGSize - + init(size: CGSize) { self.size = size for i in 0...2 { @@ -24,8 +24,8 @@ class BackgroundComponent: GKComponent{ nodes.append(sky) } super.init() - } - + } + func update(){ for node in nodes{ node.position.x -= 2 @@ -34,9 +34,9 @@ class BackgroundComponent: GKComponent{ } } } - + required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } - + } diff --git a/GoldWars/GoldWars/Components/BaseNode.swift b/GoldWars/GoldWars/Components/BaseNode.swift index bbfee06..00da238 100644 --- a/GoldWars/GoldWars/Components/BaseNode.swift +++ b/GoldWars/GoldWars/Components/BaseNode.swift @@ -9,26 +9,26 @@ import SpriteKit class BaseNode: SKSpriteNode{ - - override func touchesBegan(_ touches: Set, with event: UIEvent?) { - // TODO: PopUp Einheiten + Close PopUp - self.run( - SKAction.sequence( - [ - SKAction.resize(byWidth: 20, height: 20, duration: 0.5), - SKAction.resize(byWidth: -20, height: -20, duration: 0.5) - ] - ) - ) - } - - override func touchesMoved(_ touches: Set, with event: UIEvent?) { - // TODO: zeige Angirff Effect - } - - override func touchesEnded(_ touches: Set, with event: UIEvent?) { - // TODO: Open Slider PopUp - } - - + + override func touchesBegan(_ touches: Set, with event: UIEvent?) { + // TODO: PopUp Einheiten + Close PopUp + self.run( + SKAction.sequence( + [ + SKAction.resize(byWidth: 20, height: 20, duration: 0.5), + SKAction.resize(byWidth: -20, height: -20, duration: 0.5) + ] + ) + ) + } + + override func touchesMoved(_ touches: Set, with event: UIEvent?) { + // TODO: zeige Angirff Effect + } + + override func touchesEnded(_ touches: Set, with event: UIEvent?) { + // TODO: Open Slider PopUp + } + + } diff --git a/GoldWars/GoldWars/Components/ButtonComponent.swift b/GoldWars/GoldWars/Components/ButtonComponent.swift index 3ad2caf..0fe059f 100644 --- a/GoldWars/GoldWars/Components/ButtonComponent.swift +++ b/GoldWars/GoldWars/Components/ButtonComponent.swift @@ -9,9 +9,9 @@ import GameplayKit class ButtonComponent: GKComponent { - + var buttonNode: ButtonNode - + init(iconName: String, text: String, position: CGPoint, isEnabled:Bool, onButtonPress: @escaping () -> ()) { buttonNode = ButtonNode(iconName: iconName, text: text, diff --git a/GoldWars/GoldWars/Components/ButtonNode.swift b/GoldWars/GoldWars/Components/ButtonNode.swift index 4161636..2ebd012 100644 --- a/GoldWars/GoldWars/Components/ButtonNode.swift +++ b/GoldWars/GoldWars/Components/ButtonNode.swift @@ -9,7 +9,7 @@ import SpriteKit class ButtonNode: SKSpriteNode { - + var isEnabled: Bool{ didSet{ if isEnabled { @@ -18,19 +18,19 @@ class ButtonNode: SKSpriteNode { } else { self.alpha = 0.3 self.childNode(withName: "label")?.alpha = 0.3 - } + } } } - + let onButtonPress: () -> () - + init(iconName: String, text: String, isEnabled: Bool, position: CGPoint, onButtonPress: @escaping () -> ()) { self.onButtonPress = onButtonPress self.isEnabled = isEnabled let texture = SKTexture(imageNamed: "yellow_button04") super.init(texture: texture, color: SKColor.white, size: texture.size()) self.position = position - + let label = SKLabelNode(fontNamed: "Courier-Bold") label.fontSize = 30 label.fontColor = SKColor.black @@ -38,7 +38,7 @@ class ButtonNode: SKSpriteNode { label.verticalAlignmentMode = .center label.text = text label.name = "label" - + if iconName.isEmpty { label.position = CGPoint(x: 0, y: 0) } else { @@ -50,26 +50,26 @@ class ButtonNode: SKSpriteNode { } self.addChild(label) isUserInteractionEnabled = true + } + + override func touchesBegan(_ touches: Set, with event: UIEvent?) { + if isEnabled { + let action = SKAction.sequence( + [ + SKAction.scale(by: (3/4), duration: 0.3), + SKAction.scale(by: (4/3), duration: 0.3), + ] + ) + + self.childNode(withName: "label")?.run(action) + self.run(action) + onButtonPress() } - - override func touchesBegan(_ touches: Set, with event: UIEvent?) { - if isEnabled { - let action = SKAction.sequence( - [ - SKAction.scale(by: (3/4), duration: 0.3), - SKAction.scale(by: (4/3), duration: 0.3), - ] - ) - - self.childNode(withName: "label")?.run(action) - self.run(action) - onButtonPress() - } - } - - - required init?(coder aDecoder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } - + } + + + required init?(coder aDecoder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + } diff --git a/GoldWars/GoldWars/Components/DefBoostSkillComponent.swift b/GoldWars/GoldWars/Components/DefBoostSkillComponent.swift index 818a23b..01c0aef 100644 --- a/GoldWars/GoldWars/Components/DefBoostSkillComponent.swift +++ b/GoldWars/GoldWars/Components/DefBoostSkillComponent.swift @@ -9,25 +9,25 @@ import GameplayKit class DefBoostSkillComponent: GKComponent{ - - let shapeNode: SKShapeNode - let labelNode: SKLabelNode - - init(text: String, texture: SKTexture?, anchorPoint: CGPoint) { - self.labelNode = SKLabelNode(text: text) - self.shapeNode = SKShapeNode(circleOfRadius: 30) - self.shapeNode.position = anchorPoint - self.labelNode.position = CGPoint(x: anchorPoint.x, y: anchorPoint.y - 15) - if texture != nil { - shapeNode.fillTexture = texture - }else { - shapeNode.fillColor = SKColor.gray - } - super.init() - } - - required init?(coder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } - + + let shapeNode: SKShapeNode + let labelNode: SKLabelNode + + init(text: String, texture: SKTexture?, anchorPoint: CGPoint) { + self.labelNode = SKLabelNode(text: text) + self.shapeNode = SKShapeNode(circleOfRadius: 30) + self.shapeNode.position = anchorPoint + self.labelNode.position = CGPoint(x: anchorPoint.x, y: anchorPoint.y - 15) + if texture != nil { + shapeNode.fillTexture = texture + }else { + shapeNode.fillColor = SKColor.gray + } + super.init() + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + } diff --git a/GoldWars/GoldWars/Components/DefaultBaseComponent.swift b/GoldWars/GoldWars/Components/DefaultBaseComponent.swift index 13665e1..715840b 100644 --- a/GoldWars/GoldWars/Components/DefaultBaseComponent.swift +++ b/GoldWars/GoldWars/Components/DefaultBaseComponent.swift @@ -10,15 +10,15 @@ import GameplayKit import SpriteKit class DefaultBaseComponent: GKComponent { - var spriteNode: BaseNode - - init(texture: SKTexture, position: CGPoint) { - spriteNode = BaseNode(texture: texture, size: CGSize(width: 80, height: 80)) - spriteNode.position = position - super.init() - } - - required init?(coder aDecoder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } + var spriteNode: BaseNode + + init(texture: SKTexture, position: CGPoint) { + spriteNode = BaseNode(texture: texture, size: CGSize(width: 80, height: 80)) + spriteNode.position = position + super.init() + } + + required init?(coder aDecoder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } } diff --git a/GoldWars/GoldWars/Components/ModalBackgroundComponent.swift b/GoldWars/GoldWars/Components/ModalBackgroundComponent.swift index 1d4a807..e81fcf9 100644 --- a/GoldWars/GoldWars/Components/ModalBackgroundComponent.swift +++ b/GoldWars/GoldWars/Components/ModalBackgroundComponent.swift @@ -10,17 +10,17 @@ import GameplayKit import SpriteKit class ModalBackgroundComponent: GKComponent { - let spriteNode: SKSpriteNode - - init(anchorPoint: CGPoint) { - let texture = SKTexture(imageNamed:"ModalBackground") - spriteNode = SKSpriteNode(texture: texture, size: texture.size()) - spriteNode.setScale(2) - spriteNode.position = anchorPoint - super.init() - } - - required init?(coder aDecoder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } + let spriteNode: SKSpriteNode + + init(anchorPoint: CGPoint) { + let texture = SKTexture(imageNamed:"ModalBackground") + spriteNode = SKSpriteNode(texture: texture, size: texture.size()) + spriteNode.setScale(2) + spriteNode.position = anchorPoint + super.init() + } + + required init?(coder aDecoder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } } diff --git a/GoldWars/GoldWars/Components/ModalContentComponent.swift b/GoldWars/GoldWars/Components/ModalContentComponent.swift index 4ee8f0c..df004a3 100644 --- a/GoldWars/GoldWars/Components/ModalContentComponent.swift +++ b/GoldWars/GoldWars/Components/ModalContentComponent.swift @@ -10,33 +10,33 @@ import GameplayKit import SpriteKit class ModalContentComponent: GKComponent{ - - var header: SKLabelNode - var body: SKLabelNode - var footer: SKLabelNode - - init(header: String, body: String, footer: String , anchorPoint: CGPoint) { - self.header = SKLabelNode(text: header) - self.header.position = CGPoint(x: anchorPoint.x, y: anchorPoint.y + 125) - self.header.fontName = "HelveticaNeue-Bold" - self.header.fontSize = 40 - - self.body = SKLabelNode(text: body) - self.body.position = CGPoint(x: anchorPoint.x, y: anchorPoint.y - 20) - self.body.numberOfLines = 2 - self.body.preferredMaxLayoutWidth = 390 - self.body.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.center - self.body.fontName = "HelveticaNeue-Bold" - self.body.fontSize = 40 - - self.footer = SKLabelNode(text: footer) - self.footer.position = CGPoint(x: anchorPoint.x, y: anchorPoint.y - 40) - self.footer.fontName = "HelveticaNeue-Bold" - self.footer.fontSize = 40 - super.init() - } - - required init?(coder aDecoder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } + + var header: SKLabelNode + var body: SKLabelNode + var footer: SKLabelNode + + init(header: String, body: String, footer: String , anchorPoint: CGPoint) { + self.header = SKLabelNode(text: header) + self.header.position = CGPoint(x: anchorPoint.x, y: anchorPoint.y + 125) + self.header.fontName = "HelveticaNeue-Bold" + self.header.fontSize = 40 + + self.body = SKLabelNode(text: body) + self.body.position = CGPoint(x: anchorPoint.x, y: anchorPoint.y - 20) + self.body.numberOfLines = 2 + self.body.preferredMaxLayoutWidth = 390 + self.body.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.center + self.body.fontName = "HelveticaNeue-Bold" + self.body.fontSize = 40 + + self.footer = SKLabelNode(text: footer) + self.footer.position = CGPoint(x: anchorPoint.x, y: anchorPoint.y - 40) + self.footer.fontName = "HelveticaNeue-Bold" + self.footer.fontSize = 40 + super.init() + } + + required init?(coder aDecoder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } } diff --git a/GoldWars/GoldWars/Components/SliderComponent.swift b/GoldWars/GoldWars/Components/SliderComponent.swift index 4eabfb3..dc46145 100644 --- a/GoldWars/GoldWars/Components/SliderComponent.swift +++ b/GoldWars/GoldWars/Components/SliderComponent.swift @@ -9,15 +9,15 @@ import GameplayKit class SliderComponent: GKComponent { - - var sliderNode: SliderNode - - init(width: CGFloat, position: CGPoint) { - sliderNode = SliderNode(width: width, position: position) - super.init() - } - - required init?(coder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } + + var sliderNode: SliderNode + + init(width: CGFloat, position: CGPoint) { + sliderNode = SliderNode(width: width, position: position) + super.init() + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } } diff --git a/GoldWars/GoldWars/Components/SliderNode.swift b/GoldWars/GoldWars/Components/SliderNode.swift index 929d78a..93f42d2 100644 --- a/GoldWars/GoldWars/Components/SliderNode.swift +++ b/GoldWars/GoldWars/Components/SliderNode.swift @@ -10,57 +10,57 @@ import SpriteKit class SliderNode :SKNode { - - var sliderLine :SKShapeNode - var sliderKnob :SliderKnob - var width: CGFloat - - var getValue: CGFloat{ - get{ - return ((sliderKnob.position.x.rounded() - sliderKnob.min) / width) - } - } - - init(width: CGFloat, position: CGPoint) { - self.width = width - sliderLine = SKShapeNode(rectOf: CGSize(width: width, height: 8)) - sliderLine.position = position - sliderLine.fillColor = SKColor.white - sliderKnob = SliderKnob(circleOfRadius: 20) - sliderKnob.min = position.x - width / 2 - sliderKnob.max = position.x + width / 2 - sliderKnob.fillColor = SKColor.red - sliderKnob.zPosition = sliderLine.zPosition + 1 - sliderKnob.position = CGPoint(x: sliderLine.position.x, y: sliderLine.position.y + 1) - super.init() - - } - - required init?(coder aDecoder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } + + var sliderLine :SKShapeNode + var sliderKnob :SliderKnob + var width: CGFloat + + var getValue: CGFloat{ + get{ + return ((sliderKnob.position.x.rounded() - sliderKnob.min) / width) + } + } + + init(width: CGFloat, position: CGPoint) { + self.width = width + sliderLine = SKShapeNode(rectOf: CGSize(width: width, height: 8)) + sliderLine.position = position + sliderLine.fillColor = SKColor.white + sliderKnob = SliderKnob(circleOfRadius: 20) + sliderKnob.min = position.x - width / 2 + sliderKnob.max = position.x + width / 2 + sliderKnob.fillColor = SKColor.red + sliderKnob.zPosition = sliderLine.zPosition + 1 + sliderKnob.position = CGPoint(x: sliderLine.position.x, y: sliderLine.position.y + 1) + super.init() + + } + + required init?(coder aDecoder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } } class SliderKnob: SKShapeNode { - var min = CGFloat() - var max = CGFloat() - - override func touchesMoved(_ touches: Set, with event: UIEvent?) { - - for touch in touches { - let touchLocation = touch.location(in: self.scene!) - - if self.position.x >= min - 1 && self.position.x <= max + 1{ - self.position.x = touchLocation.x - } - - if(self.position.x <= min){ - self.position.x = min - } - if(self.position.x >= max){ - self.position.x = max - } - } - } + var min = CGFloat() + var max = CGFloat() + + override func touchesMoved(_ touches: Set, with event: UIEvent?) { + + for touch in touches { + let touchLocation = touch.location(in: self.scene!) + + if self.position.x >= min - 1 && self.position.x <= max + 1{ + self.position.x = touchLocation.x + } + + if(self.position.x <= min){ + self.position.x = min + } + if(self.position.x >= max){ + self.position.x = max + } + } + } } diff --git a/GoldWars/GoldWars/Components/SpySkillComponent.swift b/GoldWars/GoldWars/Components/SpySkillComponent.swift index ccd54f1..b193de5 100644 --- a/GoldWars/GoldWars/Components/SpySkillComponent.swift +++ b/GoldWars/GoldWars/Components/SpySkillComponent.swift @@ -9,26 +9,26 @@ import GameplayKit class SpySkillComponent: GKComponent{ - - let shapeNode: SKShapeNode - let labelNode: SKLabelNode - - init(text: String, texture: SKTexture?, anchorPoint: CGPoint) { - self.labelNode = SKLabelNode(text: text) - self.shapeNode = SKShapeNode(circleOfRadius: 30) - self.shapeNode.position = anchorPoint - self.labelNode.position = CGPoint(x: anchorPoint.x, y: anchorPoint.y - 15) - if texture != nil { - shapeNode.fillTexture = texture - }else { - shapeNode.fillColor = SKColor.gray - } - super.init() - } - - required init?(coder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } - + + let shapeNode: SKShapeNode + let labelNode: SKLabelNode + + init(text: String, texture: SKTexture?, anchorPoint: CGPoint) { + self.labelNode = SKLabelNode(text: text) + self.shapeNode = SKShapeNode(circleOfRadius: 30) + self.shapeNode.position = anchorPoint + self.labelNode.position = CGPoint(x: anchorPoint.x, y: anchorPoint.y - 15) + if texture != nil { + shapeNode.fillTexture = texture + }else { + shapeNode.fillColor = SKColor.gray + } + super.init() + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + } diff --git a/GoldWars/GoldWars/Components/TeamComponent.swift b/GoldWars/GoldWars/Components/TeamComponent.swift index 116b64f..a6b27fa 100644 --- a/GoldWars/GoldWars/Components/TeamComponent.swift +++ b/GoldWars/GoldWars/Components/TeamComponent.swift @@ -24,17 +24,17 @@ class TeamComponent: GKComponent { fire.particleColorBlendFactor = 1.0 switch team { - case .team1: fire.particleColor = SKColor.red - case .team2: fire.particleColor = SKColor.purple - case .team3: fire.particleColor = SKColor.green - case .team4: fire.particleColor = SKColor.gray - } - + case .team1: fire.particleColor = SKColor.red + case .team2: fire.particleColor = SKColor.purple + case .team3: fire.particleColor = SKColor.green + case .team4: fire.particleColor = SKColor.gray + } + self.team = team self.player = player super.init() } - + required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } diff --git a/GoldWars/GoldWars/Components/TimerComponent.swift b/GoldWars/GoldWars/Components/TimerComponent.swift index 5c5f420..816ca04 100644 --- a/GoldWars/GoldWars/Components/TimerComponent.swift +++ b/GoldWars/GoldWars/Components/TimerComponent.swift @@ -9,12 +9,12 @@ import GameplayKit class TimerComponent: GKComponent { - + let labelNode :SKLabelNode var endTime :Date! var duration :Double - - init(text: String, anchorPoint: CGPoint, duration: TimeInterval) { + + init(text: String, anchorPoint: CGPoint, duration: TimeInterval) { self.labelNode = SKLabelNode(text: text) self.labelNode.fontColor = UIColor.black self.labelNode.fontSize = CGFloat(45) @@ -51,6 +51,6 @@ class TimerComponent: GKComponent { required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } - + } diff --git a/GoldWars/GoldWars/Entities/Background.swift b/GoldWars/GoldWars/Entities/Background.swift index b3e447b..6cc1b74 100644 --- a/GoldWars/GoldWars/Entities/Background.swift +++ b/GoldWars/GoldWars/Entities/Background.swift @@ -9,16 +9,16 @@ import GameplayKit class Background: GKEntity { - + init(size: CGSize) { super.init() addComponent(BackgroundComponent(size: size)) } - + override func update(deltaTime seconds: TimeInterval) { - component(ofType: BackgroundComponent.self)?.update() + component(ofType: BackgroundComponent.self)?.update() } - + required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } diff --git a/GoldWars/GoldWars/Entities/Base.swift b/GoldWars/GoldWars/Entities/Base.swift index f41f483..ca4d70e 100644 --- a/GoldWars/GoldWars/Entities/Base.swift +++ b/GoldWars/GoldWars/Entities/Base.swift @@ -8,7 +8,6 @@ import SpriteKit import GameplayKit -import GameKit class Base: GKEntity { @@ -17,7 +16,7 @@ class Base: GKEntity { var changeOwnership: Bool var ownershipPlayer: GKPlayer? - init(position: CGPoint, player: GKPlayer? = nil, team: Team? = nil) { + init(position: CGPoint, team: Team! = nil) { self.unitCount = 0 self.adjacencyList = [Base]() self.changeOwnership = false diff --git a/GoldWars/GoldWars/Entities/Button.swift b/GoldWars/GoldWars/Entities/Button.swift index 4312dca..8ac174d 100644 --- a/GoldWars/GoldWars/Entities/Button.swift +++ b/GoldWars/GoldWars/Entities/Button.swift @@ -12,7 +12,7 @@ class Button: GKEntity{ let name: String var isEnabled = true - + init(name: String, iconName: String, text: String, position: CGPoint, onButtonPress: @escaping () -> ()) { self.name = name super.init() diff --git a/GoldWars/GoldWars/Entities/EntityManager.swift b/GoldWars/GoldWars/Entities/EntityManager.swift index 8cc49f6..3499c82 100644 --- a/GoldWars/GoldWars/Entities/EntityManager.swift +++ b/GoldWars/GoldWars/Entities/EntityManager.swift @@ -14,42 +14,42 @@ 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) - isModal = true - } - 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) + isModal = true + } + 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) } @@ -61,29 +61,29 @@ 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) + } } func remove(_ entity: GKEntity) { if let spriteNode = entity.component(ofType: DefaultBaseComponent.self)?.spriteNode { spriteNode.removeFromParent() } - if let spriteNode = entity.component(ofType: ModalBackgroundComponent.self)?.spriteNode { - spriteNode.removeFromParent() - isModal = false - } - 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() + isModal = false + } + 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() + } entities.remove(entity) } @@ -115,37 +115,37 @@ class EntityManager { 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 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 } diff --git a/GoldWars/GoldWars/Entities/HUD.swift b/GoldWars/GoldWars/Entities/HUD.swift index 6225c0c..a161285 100644 --- a/GoldWars/GoldWars/Entities/HUD.swift +++ b/GoldWars/GoldWars/Entities/HUD.swift @@ -9,29 +9,29 @@ import GameplayKit class HUD: GKEntity { - - init(size: CGSize) { - super.init() - addComponent(SpySkillComponent(text: "Spy", - texture: nil, - anchorPoint: CGPoint(x: size.width * 0.75, y: size.height * 0.1))) - - - addComponent(AtkBoostSkillComponent(text: "Atk", - texture: nil, - anchorPoint: CGPoint(x: size.width * 0.85, y: size.height * 0.1))) - - - addComponent(DefBoostSkillComponent(text: "Def", - texture: nil, - anchorPoint: CGPoint(x: size.width * 0.95, y: size.height * 0.1))) + + init(size: CGSize) { + super.init() + addComponent(SpySkillComponent(text: "Spy", + texture: nil, + anchorPoint: CGPoint(x: size.width * 0.75, y: size.height * 0.1))) + + + addComponent(AtkBoostSkillComponent(text: "Atk", + texture: nil, + anchorPoint: CGPoint(x: size.width * 0.85, y: size.height * 0.1))) + + + addComponent(DefBoostSkillComponent(text: "Def", + texture: nil, + anchorPoint: CGPoint(x: size.width * 0.95, y: size.height * 0.1))) addComponent(TimerComponent(text: "", - anchorPoint: CGPoint(x: size.width * 0.5, y: size.height * 0.9), duration: 30)) - } - - required init?(coder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } - + anchorPoint: CGPoint(x: size.width * 0.5, y: size.height * 0.9), duration: 30)) + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + } diff --git a/GoldWars/GoldWars/Entities/Modal.swift b/GoldWars/GoldWars/Entities/Modal.swift index a8f638f..cdafa53 100644 --- a/GoldWars/GoldWars/Entities/Modal.swift +++ b/GoldWars/GoldWars/Entities/Modal.swift @@ -9,41 +9,31 @@ import GameplayKit class Modal: GKEntity{ + + var unitCount:Int - var unitCount:Int - - init(modaltype: ModalType, base: Base, anchorPoint: CGPoint) { - unitCount = base.unitCount - super.init() - switch modaltype{ - case .BaseDetails: - addComponent(ModalBackgroundComponent(anchorPoint: anchorPoint)) - addComponent(ModalContentComponent( - header: "Basis Information", - body: "Diese Basis enthält \(base.unitCount) Einheiten", - footer: "", - anchorPoint: anchorPoint - ) - ) - case .BaseAttack: - addComponent(ModalBackgroundComponent(anchorPoint: anchorPoint)) - addComponent(SliderComponent( - width: 300, - position: CGPoint(x: anchorPoint.x , y: anchorPoint.y - 80) - ) - ) - addComponent(ModalContentComponent( - header: "Angriff", - body: "Schicke \(unitCount / 2) Einheiten", - footer: "", - anchorPoint: anchorPoint - ) - ) - } - } - - required init?(coder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } - + init(modaltype: ModalType, base: Base, anchorPoint: CGPoint) { + unitCount = base.unitCount + super.init() + switch modaltype{ + case .BaseDetails: + addComponent(ModalBackgroundComponent(anchorPoint: anchorPoint)) + addComponent(ModalContentComponent(header: "Basis Information", + body: "Diese Basis enthält \(base.unitCount) Einheiten", + footer: "", + anchorPoint: anchorPoint)) + case .BaseAttack: + addComponent(ModalBackgroundComponent(anchorPoint: anchorPoint)) + addComponent(SliderComponent(width: 300, position: CGPoint(x: anchorPoint.x , y: anchorPoint.y - 80))) + addComponent(ModalContentComponent(header: "Angriff", + body: "Schicke \(unitCount / 2) Einheiten", + footer: "", + anchorPoint: anchorPoint)) + } + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + } diff --git a/GoldWars/GoldWars/Enums/ModalType.swift b/GoldWars/GoldWars/Enums/ModalType.swift index 74d3177..40bcedf 100644 --- a/GoldWars/GoldWars/Enums/ModalType.swift +++ b/GoldWars/GoldWars/Enums/ModalType.swift @@ -7,6 +7,6 @@ // enum ModalType: String{ - case BaseDetails - case BaseAttack + case BaseDetails + case BaseAttack } diff --git a/GoldWars/GoldWars/Enums/Team.swift b/GoldWars/GoldWars/Enums/Team.swift index f8921b6..15115fa 100644 --- a/GoldWars/GoldWars/Enums/Team.swift +++ b/GoldWars/GoldWars/Enums/Team.swift @@ -11,6 +11,6 @@ enum Team: Int { case team2 = 2 case team3 = 3 case team4 = 4 - + static let allValues = [team1, team2,team3,team4] } diff --git a/GoldWars/GoldWars/GameCenterHelper.swift b/GoldWars/GoldWars/GameCenterHelper.swift index 0447bad..abf9578 100644 --- a/GoldWars/GoldWars/GameCenterHelper.swift +++ b/GoldWars/GoldWars/GameCenterHelper.swift @@ -12,20 +12,20 @@ final class GameCenterHelper: NSObject { typealias CompletionBlock = (Error?) -> Void static let helper = GameCenterHelper() - + var viewController: UIViewController? static var isAuthenticated: Bool { - return GKLocalPlayer.local.isAuthenticated + return GKLocalPlayer.local.isAuthenticated } override init() { super.init() - + GKLocalPlayer.local.authenticateHandler = { gcAuthVC, error in NotificationCenter.default - .post(name: .authenticationChanged, object: GKLocalPlayer.local.isAuthenticated) - + .post(name: .authenticationChanged, object: GKLocalPlayer.local.isAuthenticated) + if GKLocalPlayer.local.isAuthenticated { print("Authenticated to Game Center!") } else if let vc = gcAuthVC { diff --git a/GoldWars/GoldWars/GameViewController.swift b/GoldWars/GoldWars/GameViewController.swift index 55c52ea..e4a2f43 100644 --- a/GoldWars/GoldWars/GameViewController.swift +++ b/GoldWars/GoldWars/GameViewController.swift @@ -11,10 +11,10 @@ import SpriteKit import GameplayKit class GameViewController: UIViewController { - + override func viewDidLoad() { super.viewDidLoad() - + if let view = self.view as! SKView? { let scene = MenuScene(size: self.view.bounds.size) scene.scaleMode = .aspectFill @@ -22,16 +22,16 @@ class GameViewController: UIViewController { //TODO: create dev profile or remove on delivery view.showsFPS = true view.showsNodeCount = true - - GameCenterHelper.helper.viewController = self - MatchmakingHelper.sharedInstance.viewController = self - } + + GameCenterHelper.helper.viewController = self + MatchmakingHelper.sharedInstance.viewController = self } - + } + override var shouldAutorotate: Bool { return true } - + override var supportedInterfaceOrientations: UIInterfaceOrientationMask { if UIDevice.current.userInterfaceIdiom == .phone { return .allButUpsideDown @@ -39,7 +39,7 @@ class GameViewController: UIViewController { return .all } } - + override var prefersStatusBarHidden: Bool { return true } diff --git a/GoldWars/GoldWars/MatchmakingHelper.swift b/GoldWars/GoldWars/MatchmakingHelper.swift index 46d3de7..26ff022 100644 --- a/GoldWars/GoldWars/MatchmakingHelper.swift +++ b/GoldWars/GoldWars/MatchmakingHelper.swift @@ -71,15 +71,15 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe /* Der User hat die Verbindung mit "Abbrechen" unterbrochen. GameCenter MatchMaking wird beendet. - */ + */ func matchmakerViewControllerWasCancelled(_ viewController: GKMatchmakerViewController) { viewController.dismiss(animated: true, completion: nil) delegate?.matchEnded() } /* - Wenn GameCenter kein match erstellen kann, wird der viewcontroller dismissed. - */ + Wenn GameCenter kein match erstellen kann, wird der viewcontroller dismissed. + */ func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFailWithError error: Error) { viewController.dismiss(animated: true, completion: nil) print("Error finding match", error.localizedDescription) @@ -90,7 +90,7 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe Gamecenter hat erfolgreich ein Match gefunden, das Spiel kann gestartet werden. expectedPlayerCount: Die verbleibende Anzahl von Spielern, die sich noch nicht mit dem Spiel verbunden haben z.B 0 gibt an, dass keine weiteren Spieler benötigt werden um das Match zu starten - */ + */ func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFind match: GKMatch) { viewController.dismiss(animated: true, completion: nil) mpMatch = match @@ -101,8 +101,8 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe } /* - Vom match erhaltene Spielerdaten - */ + Vom match erhaltene Spielerdaten + */ private func match(match: GKMatch!, didReceiveData data: NSData!,fromPlayer playerID: String!) { if mpMatch != match { return } delegate?.matchReceivedData(match: match, data: data, fromPlayer: playerID) @@ -142,15 +142,15 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe } /* - Ein Spieler wird als Host für das Match gewählt. Dieser ist Spieler 1. Im Anschluss wird die GameScene geladen. - */ + Ein Spieler wird als Host für das Match gewählt. Dieser ist Spieler 1. Im Anschluss wird die GameScene geladen. + */ func startMatch() { - + mpMatch!.chooseBestHostingPlayer(completionHandler: { (player) in self.mpMatchStarted = true - + if player == GKLocalPlayer.local { self.isServer = true } else { @@ -160,7 +160,7 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe self.menusc!.loadScene(scene: GameScene(size: self.menusc!.size)) }) } - + /* Trennt die Verbindung vom Match diff --git a/GoldWars/GoldWars/Scenes/GameScene.swift b/GoldWars/GoldWars/Scenes/GameScene.swift index 93fbf3c..c18a938 100644 --- a/GoldWars/GoldWars/Scenes/GameScene.swift +++ b/GoldWars/GoldWars/Scenes/GameScene.swift @@ -23,45 +23,45 @@ class GameScene: SKScene{ entityManager.add(Background(size: self.size)) initMap() } - + func initMap() { MapFactory(scene: self, entityManager: self.entityManager).loadMap(playerCount: 2) } - + override func touchesEnded(_ touches: Set, with event: UIEvent?) { guard let touch = touches.first else { return } - + let touchLocation = touch.location(in: self) - - if isMoveTouch{ - isMoveTouch = false + + if isMoveTouch{ + isMoveTouch = false currentDraggedBase!.component(ofType: DefaultBaseComponent.self)?.spriteNode.position = currentDraggedBasePos currentDraggedBase!.component(ofType: TeamComponent.self)?.fire.position = currentDraggedBasePos - - for base in currentDraggedBase!.adjacencyList { - if atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode { - // TODO: change interaction based on collision instead of touchlocation - - 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.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))) - } - - } - } - } + + for base in currentDraggedBase!.adjacencyList { + if atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode { + // TODO: change interaction based on collision instead of touchlocation + + 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.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))) + } + + } + } + } else { for entity in entityManager.entities { let spriteNode = entity.component(ofType: DefaultBaseComponent.self)?.spriteNode - + if entityManager.isModal && entity.isMember(of: Modal.self) { entityManager.remove(entity) for child in self.children { @@ -70,7 +70,7 @@ class GameScene: SKScene{ } } } - + if atPoint(touchLocation) == spriteNode && !entityManager.isModal { spriteNode?.touchesBegan(touches, with: event) if !entityManager.isModal { @@ -86,9 +86,9 @@ class GameScene: SKScene{ } } } - + } - + override func touchesMoved(_ touches: Set, with event: UIEvent?) { guard let touch = touches.first else { return @@ -115,7 +115,7 @@ class GameScene: SKScene{ currentDraggedBase = base } isMoveTouch = true - + base.component(ofType: DefaultBaseComponent.self)?.spriteNode.position = touchLocation base.component(ofType: TeamComponent.self)?.fire.position = touchLocation for adjacencyBase in base.adjacencyList { diff --git a/GoldWars/GoldWars/Scenes/MenuScene.swift b/GoldWars/GoldWars/Scenes/MenuScene.swift index 3f8694a..edf6bb9 100644 --- a/GoldWars/GoldWars/Scenes/MenuScene.swift +++ b/GoldWars/GoldWars/Scenes/MenuScene.swift @@ -9,9 +9,9 @@ import SpriteKit class MenuScene: SKScene { - + var entityManager: EntityManager! - + override func sceneDidLoad() { entityManager = EntityManager(scene: self) let midX = self.size.width / 2 @@ -24,24 +24,24 @@ class MenuScene: SKScene { if CommandLine.arguments.contains("--no-matchmaking") { self.loadScene(scene: GameScene(size: self.size)) } else { - MatchmakingHelper.sharedInstance.presentMatchmaker(scene: self) + MatchmakingHelper.sharedInstance.presentMatchmaker(scene: self) } - })) + })) entityManager.add(Button(name: "settingsButton", iconName: "", text: "Settings", position: CGPoint(x: midX, y: midY - 80 ), onButtonPress: { //TODO: create Settings Scene - })) + })) entityManager.add(Background(size: self.size)) } - + func loadScene(scene: SKScene) { let transition = SKTransition.flipVertical(withDuration: 0.5) self.view?.presentScene(scene, transition: transition) } - + override func update(_ currentTime: TimeInterval) { entityManager.getBackground()!.update(deltaTime: currentTime) entityManager.getButtonByName(buttonName: "startGameButton").component(ofType: ButtonComponent.self)?.buttonNode.isEnabled = GameCenterHelper.isAuthenticated diff --git a/GoldWars/GoldWarsTests/GoldWarsTests.swift b/GoldWars/GoldWarsTests/GoldWarsTests.swift index 651c277..8f24b2e 100644 --- a/GoldWars/GoldWarsTests/GoldWarsTests.swift +++ b/GoldWars/GoldWarsTests/GoldWarsTests.swift @@ -10,25 +10,25 @@ import XCTest @testable import GoldWars class GoldWarsTests: XCTestCase { - + override func setUp() { // Put setup code here. This method is called before the invocation of each test method in the class. } - + override func tearDown() { // Put teardown code here. This method is called after the invocation of each test method in the class. } - + func testExample() { // This is an example of a functional test case. // Use XCTAssert and related functions to verify your tests produce the correct results. } - + func testPerformanceExample() { // This is an example of a performance test case. self.measure { // Put the code you want to measure the time of here. } } - + } From 339be93865be960b0a6dd29843fdcf1cc5497a0f Mon Sep 17 00:00:00 2001 From: Jakob Haag Date: Mon, 11 May 2020 19:44:31 +0200 Subject: [PATCH 10/11] refactor variables --- GoldWars/GoldWars/Entities/Base.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/GoldWars/GoldWars/Entities/Base.swift b/GoldWars/GoldWars/Entities/Base.swift index ca4d70e..c04664c 100644 --- a/GoldWars/GoldWars/Entities/Base.swift +++ b/GoldWars/GoldWars/Entities/Base.swift @@ -8,6 +8,7 @@ import SpriteKit import GameplayKit +import GameKit class Base: GKEntity { From 1f5a9f2365fe0f958cd4230fdee021aaa45ee3b3 Mon Sep 17 00:00:00 2001 From: Jakob Haag Date: Tue, 12 May 2020 18:27:21 +0200 Subject: [PATCH 11/11] add player to init method --- 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 c04664c..cf63a46 100644 --- a/GoldWars/GoldWars/Entities/Base.swift +++ b/GoldWars/GoldWars/Entities/Base.swift @@ -17,7 +17,7 @@ class Base: GKEntity { 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