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) }