From ffc4c05a46536e255b3debf98ddd371605415e59 Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Sat, 2 May 2020 18:48:23 +0200 Subject: [PATCH 01/10] Extract default test map, add map protocol to game scene --- GoldWars/GoldWars/Map/MapProtocoll.swift | 4 + .../Map/TwoPlayerDefaultTestMap.swift | 71 +++++- GoldWars/GoldWars/Scenes/GameScene.swift | 223 +++++++----------- 3 files changed, 159 insertions(+), 139 deletions(-) diff --git a/GoldWars/GoldWars/Map/MapProtocoll.swift b/GoldWars/GoldWars/Map/MapProtocoll.swift index 5d3412a..b452231 100644 --- a/GoldWars/GoldWars/Map/MapProtocoll.swift +++ b/GoldWars/GoldWars/Map/MapProtocoll.swift @@ -7,9 +7,13 @@ // import Foundation +import SpriteKit protocol MapProtocoll { + var size: CGSize! {get set} + + func create() func adjacent(base: Base) -> Array } diff --git a/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift b/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift index ab9387b..c5e818a 100644 --- a/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift +++ b/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift @@ -7,13 +7,78 @@ // import Foundation +import SpriteKit class TwoPlayerDefaultTestMap: MapProtocoll { - - func adjacent(base: Base) -> Array { - return Array() + var entityManager: EntityManager! + var size: CGSize! + var adjacencyList: Dictionary>! + + init(scene: SKScene, entityManager: EntityManager) { + self.entityManager = entityManager; + self.size = scene.size } + func adjacent(base: Base) -> Array { + return adjacencyList[base]! + } + func create() { + createNodes() + createNeighbourMapping() + } + + func createNodes() { + for i in 0...7 { + let base: Base + var position = CGPoint(x: 0, y: 0) + switch i { + case 0...2: + let width = self.size.width * 0.25 + + if i == 0 { + position = CGPoint(x: width, y: self.size.height * 0.25) + } + if i == 1 { + position = CGPoint(x: width, y: self.size.height * 0.5) + } + if i == 2 { + position = CGPoint(x: width, y: self.size.height * 0.75) + } + + case 3...4: + let width = self.size.width * 0.5 + + if i == 3 { + position = CGPoint(x: width, y: self.size.height * 0.333) + } + if i == 4 { + position = CGPoint(x: width, y: self.size.height * 0.666) + } + + case 5...7: + let width = self.size.width * 0.75 + + if i == 5 { + position = CGPoint(x: width, y: self.size.height * 0.25) + } + if i == 6 { + position = CGPoint(x: width, y: self.size.height * 0.5) + } + if i == 7 { + position = CGPoint(x: width, y: self.size.height * 0.75) + } + + default: + break + } + base = Base(textureName: "Base", team: nil, position: position) + entityManager.add(base) + } + } + + func createNeighbourMapping() { + + } } diff --git a/GoldWars/GoldWars/Scenes/GameScene.swift b/GoldWars/GoldWars/Scenes/GameScene.swift index e9380f2..6096538 100644 --- a/GoldWars/GoldWars/Scenes/GameScene.swift +++ b/GoldWars/GoldWars/Scenes/GameScene.swift @@ -10,140 +10,91 @@ import SpriteKit import GameplayKit class GameScene: SKScene{ - - var entityManager: EntityManager! - - override func sceneDidLoad() { - entityManager = EntityManager(scene: self) - entityManager.add(Base(textureName: "Base", - team: .team1 , - position: CGPoint(x: self.size.width * 0.1, y: self.size.height / 2))) - - entityManager.add(Base(textureName: "Base", - team: .team2 , - position: CGPoint(x: self.size.width * 0.9, y: self.size.height / 2))) - entityManager.add(HUD(size: self.size)) - initMap() - createBackground() - - } - - func createBackground() { - for i in 0...2 { - let sky = SKSpriteNode(imageNamed: "SkyBackground") - sky.name = "clouds" - sky.zPosition = -1 - sky.size = CGSize(width: (self.scene?.size.width)!, height: (self.scene?.size.height)!) - sky.position = CGPoint(x: CGFloat(i) * sky.size.width , y: (self.frame.size.height / 2)) - - self.addChild(sky) - } - } - - // TODO: Issue #24 create Map generation Service - func initMap() { - - createVirginBases() - } - - func createVirginBases() { - for i in 0...7 { - let base:Base - var position = CGPoint(x: 0, y: 0) - switch i { - case 0...2: - let width = self.size.width * 0.25 - - if i == 0 { - position = CGPoint(x: width, y: self.size.height * 0.25) - } - if i == 1 { - position = CGPoint(x: width, y: self.size.height * 0.5) - } - if i == 2{ - position = CGPoint(x: width, y: self.size.height * 0.75) - } - - case 3...4: - let width = self.size.width * 0.5 - - if i == 3{ - position = CGPoint(x: width, y: self.size.height * 0.333) - } - if i == 4{ - position = CGPoint(x: width, y: self.size.height * 0.666) - } - - case 5...7: - let width = self.size.width * 0.75 - - if i == 5{ - position = CGPoint(x: width, y: self.size.height * 0.25) - } - if i == 6{ - position = CGPoint(x: width, y: self.size.height * 0.5) - } - if i == 7{ - position = CGPoint(x: width, y: self.size.height * 0.75) - } - - default: - break - } - base = Base(textureName: "Base", team: nil, position: position) - entityManager.add(base) - } - - - } - - override func touchesBegan(_ touches: Set, with event: UIEvent?) { - guard let touch = touches.first else { - return - } - - let touchLocation = touch.location(in: self) - - 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 { - if(child.name != "fire"){ - child.alpha = 1 - } - } - - } - - if atPoint(touchLocation) == spriteNode && !entityManager.isModal { - spriteNode?.touchesBegan(touches, with: event) - if !entityManager.isModal { - for child in self.children { - if(child.name != "fire"){ - child.alpha = 0.3 - } - - } - entityManager.add(Modal(modaltype: .BaseDetails, - base: entity as! Base, - anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2))) - } - } - } - } - - - - override func update(_ currentTime: TimeInterval) { - self.enumerateChildNodes(withName: "clouds", using: ({ - (node, error) in - node.position.x -= 2 - if node.position.x < -(self.scene?.size.width)! { - node.position.x += (self.scene?.size.width)! * 3 - } - })) - } + + var entityManager: EntityManager! + var map: MapProtocoll! + + override func sceneDidLoad() { + entityManager = EntityManager(scene: self) + entityManager.add(Base(textureName: "Base", + team: .team1 , + position: CGPoint(x: self.size.width * 0.1, y: self.size.height / 2))) + + entityManager.add(Base(textureName: "Base", + team: .team2 , + position: CGPoint(x: self.size.width * 0.9, y: self.size.height / 2))) + entityManager.add(HUD(size: self.size)) + initMap() + createBackground() + + } + + func createBackground() { + for i in 0...2 { + let sky = SKSpriteNode(imageNamed: "SkyBackground") + sky.name = "clouds" + sky.zPosition = -1 + sky.size = CGSize(width: (self.scene?.size.width)!, height: (self.scene?.size.height)!) + sky.position = CGPoint(x: CGFloat(i) * sky.size.width , y: (self.frame.size.height / 2)) + + self.addChild(sky) + } + } + + // TODO: Issue #24 create Map generation Service + func initMap() { + self.map = TwoPlayerDefaultTestMap(scene: self, entityManager: self.entityManager) + self.map.create() + } + + + override func touchesBegan(_ touches: Set, with event: UIEvent?) { + guard let touch = touches.first else { + return + } + + let touchLocation = touch.location(in: self) + + 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 { + if(child.name != "fire"){ + child.alpha = 1 + } + } + + } + + if atPoint(touchLocation) == spriteNode && !entityManager.isModal { + spriteNode?.touchesBegan(touches, with: event) + if !entityManager.isModal { + for child in self.children { + if(child.name != "fire"){ + child.alpha = 0.3 + } + + } + entityManager.add(Modal(modaltype: .BaseDetails, + base: entity as! Base, + anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2))) + } + } + } + } + + + + override func update(_ currentTime: TimeInterval) { + self.enumerateChildNodes(withName: "clouds", using: ({ + (node, error) in + node.position.x -= 2 + if node.position.x < -(self.scene?.size.width)! { + node.position.x += (self.scene?.size.width)! * 3 + } + })) + } } From 5a7c29b3b88c6889d54cf5cf323e9c0a25499699 Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Sat, 2 May 2020 19:07:02 +0200 Subject: [PATCH 02/10] Extract player bases, simplify layout definition --- .../Map/TwoPlayerDefaultTestMap.swift | 79 ++++++++----------- GoldWars/GoldWars/Scenes/GameScene.swift | 7 -- 2 files changed, 34 insertions(+), 52 deletions(-) diff --git a/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift b/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift index c5e818a..ee4ab71 100644 --- a/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift +++ b/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift @@ -30,52 +30,41 @@ class TwoPlayerDefaultTestMap: MapProtocoll { } func createNodes() { - for i in 0...7 { - let base: Base - var position = CGPoint(x: 0, y: 0) - switch i { - case 0...2: - let width = self.size.width * 0.25 - - if i == 0 { - position = CGPoint(x: width, y: self.size.height * 0.25) - } - if i == 1 { - position = CGPoint(x: width, y: self.size.height * 0.5) - } - if i == 2 { - position = CGPoint(x: width, y: self.size.height * 0.75) - } - - case 3...4: - let width = self.size.width * 0.5 - - if i == 3 { - position = CGPoint(x: width, y: self.size.height * 0.333) - } - if i == 4 { - position = CGPoint(x: width, y: self.size.height * 0.666) - } - - case 5...7: - let width = self.size.width * 0.75 - - if i == 5 { - position = CGPoint(x: width, y: self.size.height * 0.25) - } - if i == 6 { - position = CGPoint(x: width, y: self.size.height * 0.5) - } - if i == 7 { - position = CGPoint(x: width, y: self.size.height * 0.75) - } - - default: - break - } - base = Base(textureName: "Base", team: nil, position: position) - entityManager.add(base) + // Player one (leftest base) + let basePlayerOne = Base( + textureName: "Base", + team: .team1 , + position: CGPoint(x: self.size.width * 0.1, y: self.size.height / 2) + ) + entityManager.add(basePlayerOne) + + var genericBasePositions = Array() + + // First column + genericBasePositions.append(CGPoint(x: self.size.width * 0.25, y: self.size.height * 0.25)) + genericBasePositions.append(CGPoint(x: self.size.width * 0.25, y: self.size.height * 0.5)) + genericBasePositions.append(CGPoint(x: self.size.width * 0.25, y: self.size.height * 0.75)) + + // Second column + genericBasePositions.append(CGPoint(x: self.size.width * 0.5, y: self.size.height * 0.333)) + genericBasePositions.append(CGPoint(x: self.size.width * 0.5, y: self.size.height * 0.666)) + + // Third column + genericBasePositions.append(CGPoint(x: self.size.width * 0.75, y: self.size.height * 0.25)) + genericBasePositions.append(CGPoint(x: self.size.width * 0.75, y: self.size.height * 0.5)) + genericBasePositions.append(CGPoint(x: self.size.width * 0.75, y: self.size.height * 0.75)) + + for pos in genericBasePositions { + entityManager.add(Base(textureName: "Base", team: nil, position: pos)) } + + // Player two (rightest base) + let basePlayerTwo = Base( + textureName: "Base", + team: .team2 , + position: CGPoint(x: self.size.width * 0.9, y: self.size.height / 2) + ) + entityManager.add(basePlayerTwo) } func createNeighbourMapping() { diff --git a/GoldWars/GoldWars/Scenes/GameScene.swift b/GoldWars/GoldWars/Scenes/GameScene.swift index 6096538..906d45b 100644 --- a/GoldWars/GoldWars/Scenes/GameScene.swift +++ b/GoldWars/GoldWars/Scenes/GameScene.swift @@ -16,13 +16,6 @@ class GameScene: SKScene{ override func sceneDidLoad() { entityManager = EntityManager(scene: self) - entityManager.add(Base(textureName: "Base", - team: .team1 , - position: CGPoint(x: self.size.width * 0.1, y: self.size.height / 2))) - - entityManager.add(Base(textureName: "Base", - team: .team2 , - position: CGPoint(x: self.size.width * 0.9, y: self.size.height / 2))) entityManager.add(HUD(size: self.size)) initMap() createBackground() From c1bb6a47de55062793ef55a1be14b3b6b56d3474 Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Sat, 2 May 2020 20:07:40 +0200 Subject: [PATCH 03/10] Create Map Factory, remove texture name from base constructor --- GoldWars/GoldWars.xcodeproj/project.pbxproj | 4 +++ GoldWars/GoldWars/Entities/Base.swift | 4 +-- GoldWars/GoldWars/Map/MapFactory.swift | 35 +++++++++++++++++++ .../Map/TwoPlayerDefaultTestMap.swift | 13 +------ GoldWars/GoldWars/Scenes/GameScene.swift | 3 +- 5 files changed, 43 insertions(+), 16 deletions(-) create mode 100644 GoldWars/GoldWars/Map/MapFactory.swift diff --git a/GoldWars/GoldWars.xcodeproj/project.pbxproj b/GoldWars/GoldWars.xcodeproj/project.pbxproj index 37f6e37..dbb539f 100644 --- a/GoldWars/GoldWars.xcodeproj/project.pbxproj +++ b/GoldWars/GoldWars.xcodeproj/project.pbxproj @@ -37,6 +37,7 @@ 9EC86BA6245C8AD000796EF3 /* ModalType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EC86BA5245C8AD000796EF3 /* ModalType.swift */; }; AB1D759C245DD18100671525 /* MapProtocoll.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB1D759A245DD18100671525 /* MapProtocoll.swift */; }; AB1D759D245DD18100671525 /* TwoPlayerDefaultTestMap.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB1D759B245DD18100671525 /* TwoPlayerDefaultTestMap.swift */; }; + AB1D75A0245DEC0500671525 /* MapFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB1D759F245DEC0500671525 /* MapFactory.swift */; }; ABA03DA0244BD54F00A66916 /* Base.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABA03D9F244BD54F00A66916 /* Base.swift */; }; /* End PBXBuildFile section */ @@ -95,6 +96,7 @@ 9ECD3699245C91F7008DEEBD /* GoldWars.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = GoldWars.entitlements; sourceTree = ""; }; AB1D759A245DD18100671525 /* MapProtocoll.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MapProtocoll.swift; sourceTree = ""; }; AB1D759B245DD18100671525 /* TwoPlayerDefaultTestMap.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TwoPlayerDefaultTestMap.swift; sourceTree = ""; }; + AB1D759F245DEC0500671525 /* MapFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapFactory.swift; sourceTree = ""; }; ABA03D9F244BD54F00A66916 /* Base.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Base.swift; sourceTree = ""; }; /* End PBXFileReference section */ @@ -259,6 +261,7 @@ isa = PBXGroup; children = ( AB1D759A245DD18100671525 /* MapProtocoll.swift */, + AB1D759F245DEC0500671525 /* MapFactory.swift */, AB1D759B245DD18100671525 /* TwoPlayerDefaultTestMap.swift */, ); path = Map; @@ -414,6 +417,7 @@ 9EA3ABED245C8143006BC61D /* ModalBackgroundComponent.swift in Sources */, 3EBD242C245D8044003CECE7 /* GameCenterHelper.swift in Sources */, AB1D759C245DD18100671525 /* MapProtocoll.swift in Sources */, + AB1D75A0245DEC0500671525 /* MapFactory.swift in Sources */, AB1D759D245DD18100671525 /* TwoPlayerDefaultTestMap.swift in Sources */, ABA03DA0244BD54F00A66916 /* Base.swift in Sources */, 110360DB244B101A008610AF /* GameViewController.swift in Sources */, diff --git a/GoldWars/GoldWars/Entities/Base.swift b/GoldWars/GoldWars/Entities/Base.swift index 75415f5..db68ee4 100644 --- a/GoldWars/GoldWars/Entities/Base.swift +++ b/GoldWars/GoldWars/Entities/Base.swift @@ -12,10 +12,10 @@ import GameplayKit class Base : GKEntity{ var unitCount:Int - init(textureName:String, team: Team?,position: CGPoint ) { + init(team: Team?, position: CGPoint ) { self.unitCount = 0 super.init() - addComponent(DefaultBaseComponent(texture: SKTexture(imageNamed: textureName), position: position)) + addComponent(DefaultBaseComponent(texture: SKTexture(imageNamed: "Base"), position: position)) if(team != nil){ addComponent(TeamComponent(team: team!, position: position)) self.unitCount = 100 diff --git a/GoldWars/GoldWars/Map/MapFactory.swift b/GoldWars/GoldWars/Map/MapFactory.swift new file mode 100644 index 0000000..160f176 --- /dev/null +++ b/GoldWars/GoldWars/Map/MapFactory.swift @@ -0,0 +1,35 @@ +// +// MapFactory.swift +// GoldWars +// +// Created by Marcel Schwarz on 02.05.20. +// Copyright © 2020 SP2. All rights reserved. +// + +import Foundation +import SpriteKit + +class MapFactory { + + var entityManager: EntityManager! + var scene: SKScene! + + init(scene: SKScene, entityManager: EntityManager) { + self.entityManager = entityManager + self.scene = scene + } + + func getTwoPlayerMap() -> MapProtocoll { + let map = TwoPlayerDefaultTestMap(scene: self.scene, entityManager: self.entityManager) + map.create() + return map + } + + func getThreePlayerMap() { + + } + + func getFourPlayerMap() { + + } +} diff --git a/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift b/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift index ee4ab71..887f78a 100644 --- a/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift +++ b/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift @@ -25,14 +25,8 @@ class TwoPlayerDefaultTestMap: MapProtocoll { } func create() { - createNodes() - createNeighbourMapping() - } - - func createNodes() { // Player one (leftest base) let basePlayerOne = Base( - textureName: "Base", team: .team1 , position: CGPoint(x: self.size.width * 0.1, y: self.size.height / 2) ) @@ -55,19 +49,14 @@ class TwoPlayerDefaultTestMap: MapProtocoll { genericBasePositions.append(CGPoint(x: self.size.width * 0.75, y: self.size.height * 0.75)) for pos in genericBasePositions { - entityManager.add(Base(textureName: "Base", team: nil, position: pos)) + entityManager.add(Base(team: nil, position: pos)) } // Player two (rightest base) let basePlayerTwo = Base( - textureName: "Base", team: .team2 , position: CGPoint(x: self.size.width * 0.9, y: self.size.height / 2) ) entityManager.add(basePlayerTwo) } - - func createNeighbourMapping() { - - } } diff --git a/GoldWars/GoldWars/Scenes/GameScene.swift b/GoldWars/GoldWars/Scenes/GameScene.swift index 906d45b..7397205 100644 --- a/GoldWars/GoldWars/Scenes/GameScene.swift +++ b/GoldWars/GoldWars/Scenes/GameScene.swift @@ -36,8 +36,7 @@ class GameScene: SKScene{ // TODO: Issue #24 create Map generation Service func initMap() { - self.map = TwoPlayerDefaultTestMap(scene: self, entityManager: self.entityManager) - self.map.create() + self.map = MapFactory(scene: self, entityManager: self.entityManager).getTwoPlayerMap() } From ffd4e0dfe0e8e7756ce88400d850bb4afe3359bc Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Sat, 2 May 2020 20:13:31 +0200 Subject: [PATCH 04/10] Initialize team in Base model with default nil --- GoldWars/GoldWars/Entities/Base.swift | 2 +- GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/GoldWars/GoldWars/Entities/Base.swift b/GoldWars/GoldWars/Entities/Base.swift index db68ee4..e7c62e1 100644 --- a/GoldWars/GoldWars/Entities/Base.swift +++ b/GoldWars/GoldWars/Entities/Base.swift @@ -12,7 +12,7 @@ import GameplayKit class Base : GKEntity{ var unitCount:Int - init(team: Team?, position: CGPoint ) { + init(position: CGPoint, team: Team! = nil) { self.unitCount = 0 super.init() addComponent(DefaultBaseComponent(texture: SKTexture(imageNamed: "Base"), position: position)) diff --git a/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift b/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift index 887f78a..d7d388c 100644 --- a/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift +++ b/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift @@ -27,8 +27,8 @@ class TwoPlayerDefaultTestMap: MapProtocoll { func create() { // Player one (leftest base) let basePlayerOne = Base( - team: .team1 , - position: CGPoint(x: self.size.width * 0.1, y: self.size.height / 2) + position: CGPoint(x: self.size.width * 0.1, y: self.size.height / 2), + team: .team1 ) entityManager.add(basePlayerOne) @@ -49,13 +49,13 @@ class TwoPlayerDefaultTestMap: MapProtocoll { genericBasePositions.append(CGPoint(x: self.size.width * 0.75, y: self.size.height * 0.75)) for pos in genericBasePositions { - entityManager.add(Base(team: nil, position: pos)) + entityManager.add(Base(position: pos)) } // Player two (rightest base) let basePlayerTwo = Base( - team: .team2 , - position: CGPoint(x: self.size.width * 0.9, y: self.size.height / 2) + position: CGPoint(x: self.size.width * 0.9, y: self.size.height / 2), + team: .team2 ) entityManager.add(basePlayerTwo) } From 918979d7509c55efc5a60e10f4e7445434c186c3 Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Sat, 2 May 2020 20:22:31 +0200 Subject: [PATCH 05/10] Introduce column based layout --- .../Map/TwoPlayerDefaultTestMap.swift | 39 ++++++++++--------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift b/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift index d7d388c..74a46d3 100644 --- a/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift +++ b/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift @@ -30,33 +30,36 @@ class TwoPlayerDefaultTestMap: MapProtocoll { position: CGPoint(x: self.size.width * 0.1, y: self.size.height / 2), team: .team1 ) - entityManager.add(basePlayerOne) - var genericBasePositions = Array() + let column1 = [ + Base(position: CGPoint(x: self.size.width * 0.25, y: self.size.height * 0.25)), + Base(position: CGPoint(x: self.size.width * 0.25, y: self.size.height * 0.5)), + Base(position: CGPoint(x: self.size.width * 0.25, y: self.size.height * 0.75)) + ] - // First column - genericBasePositions.append(CGPoint(x: self.size.width * 0.25, y: self.size.height * 0.25)) - genericBasePositions.append(CGPoint(x: self.size.width * 0.25, y: self.size.height * 0.5)) - genericBasePositions.append(CGPoint(x: self.size.width * 0.25, y: self.size.height * 0.75)) + let column2 = [ + Base(position: CGPoint(x: self.size.width * 0.5, y: self.size.height * 0.333)), + Base(position: CGPoint(x: self.size.width * 0.5, y: self.size.height * 0.666)) + ] - // Second column - genericBasePositions.append(CGPoint(x: self.size.width * 0.5, y: self.size.height * 0.333)) - genericBasePositions.append(CGPoint(x: self.size.width * 0.5, y: self.size.height * 0.666)) - - // Third column - genericBasePositions.append(CGPoint(x: self.size.width * 0.75, y: self.size.height * 0.25)) - genericBasePositions.append(CGPoint(x: self.size.width * 0.75, y: self.size.height * 0.5)) - genericBasePositions.append(CGPoint(x: self.size.width * 0.75, y: self.size.height * 0.75)) - - for pos in genericBasePositions { - entityManager.add(Base(position: pos)) - } + let column3 = [ + Base(position: CGPoint(x: self.size.width * 0.75, y: self.size.height * 0.25)), + Base(position: CGPoint(x: self.size.width * 0.75, y: self.size.height * 0.5)), + Base(position: CGPoint(x: self.size.width * 0.75, y: self.size.height * 0.75)) + ] // Player two (rightest base) let basePlayerTwo = Base( position: CGPoint(x: self.size.width * 0.9, y: self.size.height / 2), team: .team2 ) + + entityManager.add(basePlayerOne) + + column1.forEach({base in entityManager.add(base)}) + column2.forEach({base in entityManager.add(base)}) + column3.forEach({base in entityManager.add(base)}) + entityManager.add(basePlayerTwo) } } From 67a345e9bf835b4cf280b33237c42d790a4da89c Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Sat, 2 May 2020 20:47:17 +0200 Subject: [PATCH 06/10] Add adjacency mapping --- .../Map/TwoPlayerDefaultTestMap.swift | 32 +++++++++++++++++-- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift b/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift index 74a46d3..0dbcace 100644 --- a/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift +++ b/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift @@ -18,6 +18,7 @@ class TwoPlayerDefaultTestMap: MapProtocoll { init(scene: SKScene, entityManager: EntityManager) { self.entityManager = entityManager; self.size = scene.size + self.adjacencyList = Dictionary() } func adjacent(base: Base) -> Array { @@ -25,7 +26,8 @@ class TwoPlayerDefaultTestMap: MapProtocoll { } func create() { - // Player one (leftest base) + + // Create Bases let basePlayerOne = Base( position: CGPoint(x: self.size.width * 0.1, y: self.size.height / 2), team: .team1 @@ -48,12 +50,36 @@ class TwoPlayerDefaultTestMap: MapProtocoll { Base(position: CGPoint(x: self.size.width * 0.75, y: self.size.height * 0.75)) ] - // Player two (rightest base) let basePlayerTwo = Base( - position: CGPoint(x: self.size.width * 0.9, y: self.size.height / 2), + position: CGPoint(x: self.size.width * 0.9, y: self.size.height / 2), team: .team2 ) + // Create adjacency Mapping + adjacencyList.updateValue(column1, forKey: basePlayerOne) + + column1.forEach({currBase in + adjacencyList[currBase] = Array() + adjacencyList[currBase]?.append(contentsOf: column2) + adjacencyList[currBase]?.append(basePlayerOne) + }) + + column2.forEach({currBase in + adjacencyList[currBase] = Array() + adjacencyList[currBase]?.append(contentsOf: column3) + adjacencyList[currBase]?.append(contentsOf: column1) + }) + + column3.forEach({currBase in + adjacencyList[currBase] = Array() + adjacencyList[currBase]?.append(basePlayerTwo) + adjacencyList[currBase]?.append(contentsOf: column3) + }) + + adjacencyList.updateValue(column3, forKey: basePlayerTwo) + + + // Add Bases to GameScene entityManager.add(basePlayerOne) column1.forEach({base in entityManager.add(base)}) From 54813ab44da0ca003d5c5349a2d1a1e4c8eacc09 Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Sat, 2 May 2020 21:08:10 +0200 Subject: [PATCH 07/10] Fix indentation in Base entity --- GoldWars/GoldWars/Entities/Base.swift | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/GoldWars/GoldWars/Entities/Base.swift b/GoldWars/GoldWars/Entities/Base.swift index e7c62e1..f00dbb3 100644 --- a/GoldWars/GoldWars/Entities/Base.swift +++ b/GoldWars/GoldWars/Entities/Base.swift @@ -9,21 +9,21 @@ import SpriteKit import GameplayKit -class Base : GKEntity{ - var unitCount:Int - - init(position: CGPoint, team: Team! = nil) { - self.unitCount = 0 +class Base : GKEntity { + var unitCount:Int + + init(position: CGPoint, team: Team! = nil) { + self.unitCount = 0 super.init() - addComponent(DefaultBaseComponent(texture: SKTexture(imageNamed: "Base"), position: position)) - if(team != nil){ - addComponent(TeamComponent(team: team!, position: position)) + addComponent(DefaultBaseComponent(texture: SKTexture(imageNamed: "Base"), position: position)) + if(team != nil){ + addComponent(TeamComponent(team: team!, position: position)) self.unitCount = 100 } } - - required init?(coder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } - + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + } From 0dbcbe5eb0537f9418219977146de9cb99d45f27 Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Sat, 2 May 2020 21:45:21 +0200 Subject: [PATCH 08/10] Move adjacency list into base Fix typo in MapProtocol Improve MapFactory Generalize MapProtocol --- GoldWars/GoldWars.xcodeproj/project.pbxproj | 8 ++--- GoldWars/GoldWars/Entities/Base.swift | 8 ++++- GoldWars/GoldWars/Map/MapFactory.swift | 29 ++++++++++------- .../{MapProtocoll.swift => MapProtocol.swift} | 7 ++-- .../Map/TwoPlayerDefaultTestMap.swift | 32 +++++++------------ GoldWars/GoldWars/Scenes/GameScene.swift | 3 +- 6 files changed, 45 insertions(+), 42 deletions(-) rename GoldWars/GoldWars/Map/{MapProtocoll.swift => MapProtocol.swift} (60%) diff --git a/GoldWars/GoldWars.xcodeproj/project.pbxproj b/GoldWars/GoldWars.xcodeproj/project.pbxproj index dbb539f..e5de42f 100644 --- a/GoldWars/GoldWars.xcodeproj/project.pbxproj +++ b/GoldWars/GoldWars.xcodeproj/project.pbxproj @@ -35,7 +35,7 @@ 9EA3ABEF245C834B006BC61D /* ModalContentComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EA3ABEE245C834B006BC61D /* ModalContentComponent.swift */; }; 9EC86B9F245C88A300796EF3 /* Modal.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EC86B9E245C88A300796EF3 /* Modal.swift */; }; 9EC86BA6245C8AD000796EF3 /* ModalType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EC86BA5245C8AD000796EF3 /* ModalType.swift */; }; - AB1D759C245DD18100671525 /* MapProtocoll.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB1D759A245DD18100671525 /* MapProtocoll.swift */; }; + AB1D759C245DD18100671525 /* MapProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB1D759A245DD18100671525 /* MapProtocol.swift */; }; AB1D759D245DD18100671525 /* TwoPlayerDefaultTestMap.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB1D759B245DD18100671525 /* TwoPlayerDefaultTestMap.swift */; }; AB1D75A0245DEC0500671525 /* MapFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB1D759F245DEC0500671525 /* MapFactory.swift */; }; ABA03DA0244BD54F00A66916 /* Base.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABA03D9F244BD54F00A66916 /* Base.swift */; }; @@ -94,7 +94,7 @@ 9EC86B9E245C88A300796EF3 /* Modal.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Modal.swift; sourceTree = ""; }; 9EC86BA5245C8AD000796EF3 /* ModalType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ModalType.swift; sourceTree = ""; }; 9ECD3699245C91F7008DEEBD /* GoldWars.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = GoldWars.entitlements; sourceTree = ""; }; - AB1D759A245DD18100671525 /* MapProtocoll.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MapProtocoll.swift; sourceTree = ""; }; + AB1D759A245DD18100671525 /* MapProtocol.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MapProtocol.swift; sourceTree = ""; }; AB1D759B245DD18100671525 /* TwoPlayerDefaultTestMap.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TwoPlayerDefaultTestMap.swift; sourceTree = ""; }; AB1D759F245DEC0500671525 /* MapFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapFactory.swift; sourceTree = ""; }; ABA03D9F244BD54F00A66916 /* Base.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Base.swift; sourceTree = ""; }; @@ -260,7 +260,7 @@ AB1D759E245DD2EA00671525 /* Map */ = { isa = PBXGroup; children = ( - AB1D759A245DD18100671525 /* MapProtocoll.swift */, + AB1D759A245DD18100671525 /* MapProtocol.swift */, AB1D759F245DEC0500671525 /* MapFactory.swift */, AB1D759B245DD18100671525 /* TwoPlayerDefaultTestMap.swift */, ); @@ -416,7 +416,7 @@ 9EA3ABE9245C6DAA006BC61D /* DefaultBaseComponent.swift in Sources */, 9EA3ABED245C8143006BC61D /* ModalBackgroundComponent.swift in Sources */, 3EBD242C245D8044003CECE7 /* GameCenterHelper.swift in Sources */, - AB1D759C245DD18100671525 /* MapProtocoll.swift in Sources */, + AB1D759C245DD18100671525 /* MapProtocol.swift in Sources */, AB1D75A0245DEC0500671525 /* MapFactory.swift in Sources */, AB1D759D245DD18100671525 /* TwoPlayerDefaultTestMap.swift in Sources */, ABA03DA0244BD54F00A66916 /* Base.swift in Sources */, diff --git a/GoldWars/GoldWars/Entities/Base.swift b/GoldWars/GoldWars/Entities/Base.swift index f00dbb3..0455210 100644 --- a/GoldWars/GoldWars/Entities/Base.swift +++ b/GoldWars/GoldWars/Entities/Base.swift @@ -10,11 +10,15 @@ import SpriteKit import GameplayKit class Base : GKEntity { - var unitCount:Int + var unitCount: Int + var adjacencyList: Array init(position: CGPoint, team: Team! = nil) { self.unitCount = 0 + self.adjacencyList = Array() + super.init() + addComponent(DefaultBaseComponent(texture: SKTexture(imageNamed: "Base"), position: position)) if(team != nil){ addComponent(TeamComponent(team: team!, position: position)) @@ -22,6 +26,8 @@ class Base : GKEntity { } } + + required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } diff --git a/GoldWars/GoldWars/Map/MapFactory.swift b/GoldWars/GoldWars/Map/MapFactory.swift index 160f176..9234670 100644 --- a/GoldWars/GoldWars/Map/MapFactory.swift +++ b/GoldWars/GoldWars/Map/MapFactory.swift @@ -14,22 +14,29 @@ class MapFactory { var entityManager: EntityManager! var scene: SKScene! + var twoPlayerMaps: [MapProtocol]! + var threePlayerMaps: [MapProtocol]! + var fourPlayerMaps: [MapProtocol]! + init(scene: SKScene, entityManager: EntityManager) { self.entityManager = entityManager self.scene = scene - } - - func getTwoPlayerMap() -> MapProtocoll { - let map = TwoPlayerDefaultTestMap(scene: self.scene, entityManager: self.entityManager) - map.create() - return map - } - - func getThreePlayerMap() { + self.twoPlayerMaps = [ + TwoPlayerDefaultTestMap(scene: scene, entityManager: entityManager) + ] + + self.threePlayerMaps = [MapProtocol]() + self.fourPlayerMaps = [MapProtocol]() } - func getFourPlayerMap() { - + func loadMap(playerCount: Int) { + if playerCount == 2 { + twoPlayerMaps.randomElement()?.load() + } else if playerCount == 3 { + threePlayerMaps.randomElement()?.load() + } else { + fourPlayerMaps.randomElement()?.load() + } } } diff --git a/GoldWars/GoldWars/Map/MapProtocoll.swift b/GoldWars/GoldWars/Map/MapProtocol.swift similarity index 60% rename from GoldWars/GoldWars/Map/MapProtocoll.swift rename to GoldWars/GoldWars/Map/MapProtocol.swift index b452231..2560d39 100644 --- a/GoldWars/GoldWars/Map/MapProtocoll.swift +++ b/GoldWars/GoldWars/Map/MapProtocol.swift @@ -9,11 +9,12 @@ import Foundation import SpriteKit -protocol MapProtocoll { +protocol MapProtocol { var size: CGSize! {get set} + var entityManager: EntityManager! {get set} - func create() - func adjacent(base: Base) -> Array + init(scene: SKScene, entityManager: EntityManager) + func load() } diff --git a/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift b/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift index 0dbcace..a390f83 100644 --- a/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift +++ b/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift @@ -9,23 +9,17 @@ import Foundation import SpriteKit -class TwoPlayerDefaultTestMap: MapProtocoll { +class TwoPlayerDefaultTestMap: MapProtocol { var entityManager: EntityManager! var size: CGSize! - var adjacencyList: Dictionary>! - init(scene: SKScene, entityManager: EntityManager) { + required init(scene: SKScene, entityManager: EntityManager) { self.entityManager = entityManager; self.size = scene.size - self.adjacencyList = Dictionary() } - func adjacent(base: Base) -> Array { - return adjacencyList[base]! - } - - func create() { + func load() { // Create Bases let basePlayerOne = Base( @@ -56,28 +50,24 @@ class TwoPlayerDefaultTestMap: MapProtocoll { ) // Create adjacency Mapping - adjacencyList.updateValue(column1, forKey: basePlayerOne) + basePlayerOne.adjacencyList.append(contentsOf: column1) column1.forEach({currBase in - adjacencyList[currBase] = Array() - adjacencyList[currBase]?.append(contentsOf: column2) - adjacencyList[currBase]?.append(basePlayerOne) + currBase.adjacencyList.append(contentsOf: column2) + currBase.adjacencyList.append(basePlayerOne) }) column2.forEach({currBase in - adjacencyList[currBase] = Array() - adjacencyList[currBase]?.append(contentsOf: column3) - adjacencyList[currBase]?.append(contentsOf: column1) + currBase.adjacencyList.append(contentsOf: column3) + currBase.adjacencyList.append(contentsOf: column1) }) column3.forEach({currBase in - adjacencyList[currBase] = Array() - adjacencyList[currBase]?.append(basePlayerTwo) - adjacencyList[currBase]?.append(contentsOf: column3) + currBase.adjacencyList.append(basePlayerTwo) + currBase.adjacencyList.append(contentsOf: column3) }) - adjacencyList.updateValue(column3, forKey: basePlayerTwo) - + basePlayerTwo.adjacencyList.append(contentsOf: column3) // Add Bases to GameScene entityManager.add(basePlayerOne) diff --git a/GoldWars/GoldWars/Scenes/GameScene.swift b/GoldWars/GoldWars/Scenes/GameScene.swift index 7397205..7e03378 100644 --- a/GoldWars/GoldWars/Scenes/GameScene.swift +++ b/GoldWars/GoldWars/Scenes/GameScene.swift @@ -12,7 +12,6 @@ import GameplayKit class GameScene: SKScene{ var entityManager: EntityManager! - var map: MapProtocoll! override func sceneDidLoad() { entityManager = EntityManager(scene: self) @@ -36,7 +35,7 @@ class GameScene: SKScene{ // TODO: Issue #24 create Map generation Service func initMap() { - self.map = MapFactory(scene: self, entityManager: self.entityManager).getTwoPlayerMap() + MapFactory(scene: self, entityManager: self.entityManager).loadMap(playerCount: 2) } From 7fc4a5145f915ba5db179e2dc31d51fc7d173e0f Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Sat, 2 May 2020 21:51:31 +0200 Subject: [PATCH 09/10] Do some polishing --- GoldWars/GoldWars/Entities/Base.swift | 2 +- GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift | 2 +- GoldWars/GoldWars/Scenes/GameScene.swift | 1 - 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/GoldWars/GoldWars/Entities/Base.swift b/GoldWars/GoldWars/Entities/Base.swift index 0455210..6ae0a10 100644 --- a/GoldWars/GoldWars/Entities/Base.swift +++ b/GoldWars/GoldWars/Entities/Base.swift @@ -15,7 +15,7 @@ class Base : GKEntity { init(position: CGPoint, team: Team! = nil) { self.unitCount = 0 - self.adjacencyList = Array() + self.adjacencyList = [Base]() super.init() diff --git a/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift b/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift index a390f83..beff433 100644 --- a/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift +++ b/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift @@ -69,7 +69,7 @@ class TwoPlayerDefaultTestMap: MapProtocol { basePlayerTwo.adjacencyList.append(contentsOf: column3) - // Add Bases to GameScene + // Register bases with the EntityManager entityManager.add(basePlayerOne) column1.forEach({base in entityManager.add(base)}) diff --git a/GoldWars/GoldWars/Scenes/GameScene.swift b/GoldWars/GoldWars/Scenes/GameScene.swift index 7e03378..678b327 100644 --- a/GoldWars/GoldWars/Scenes/GameScene.swift +++ b/GoldWars/GoldWars/Scenes/GameScene.swift @@ -33,7 +33,6 @@ class GameScene: SKScene{ } } - // TODO: Issue #24 create Map generation Service func initMap() { MapFactory(scene: self, entityManager: self.entityManager).loadMap(playerCount: 2) } From 5eed82803b48fbd50c0b5a3c9dba4d920d4793f0 Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Sat, 2 May 2020 21:55:08 +0200 Subject: [PATCH 10/10] Remove unnecessary field form MapFactory --- GoldWars/GoldWars/Map/MapFactory.swift | 6 ------ 1 file changed, 6 deletions(-) diff --git a/GoldWars/GoldWars/Map/MapFactory.swift b/GoldWars/GoldWars/Map/MapFactory.swift index 9234670..ab4321a 100644 --- a/GoldWars/GoldWars/Map/MapFactory.swift +++ b/GoldWars/GoldWars/Map/MapFactory.swift @@ -11,17 +11,11 @@ import SpriteKit class MapFactory { - var entityManager: EntityManager! - var scene: SKScene! - var twoPlayerMaps: [MapProtocol]! var threePlayerMaps: [MapProtocol]! var fourPlayerMaps: [MapProtocol]! init(scene: SKScene, entityManager: EntityManager) { - self.entityManager = entityManager - self.scene = scene - self.twoPlayerMaps = [ TwoPlayerDefaultTestMap(scene: scene, entityManager: entityManager) ]