From e2bd0dc7cf9eb48d308dcca3c638f2dfec11be3b Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Wed, 13 May 2020 22:24:15 +0200 Subject: [PATCH 1/6] Pull protocols in MapFactory Add TwoPlayerMapGenerator Add CenterElementProvider Introduce Logging for MapGenerator --- GoldWars/GoldWars.xcodeproj/project.pbxproj | 5 +- GoldWars/GoldWars/Map/MapFactory.swift | 37 ++++++++++++++- GoldWars/GoldWars/Map/MapProtocol.swift | 20 -------- .../GoldWars/Map/TwoPlayerMapGenerator.swift | 47 +++++++++++++++++++ 4 files changed, 87 insertions(+), 22 deletions(-) delete mode 100644 GoldWars/GoldWars/Map/MapProtocol.swift create mode 100644 GoldWars/GoldWars/Map/TwoPlayerMapGenerator.swift diff --git a/GoldWars/GoldWars.xcodeproj/project.pbxproj b/GoldWars/GoldWars.xcodeproj/project.pbxproj index 58ae06f..b3cbfaa 100644 --- a/GoldWars/GoldWars.xcodeproj/project.pbxproj +++ b/GoldWars/GoldWars.xcodeproj/project.pbxproj @@ -49,6 +49,7 @@ 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 */; }; + AB21D7D5246C748A00B09CBA /* TwoPlayerMapGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB21D7D4246C748A00B09CBA /* TwoPlayerMapGenerator.swift */; }; ABA03DA0244BD54F00A66916 /* Base.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABA03D9F244BD54F00A66916 /* Base.swift */; }; AE151589245F18EF001D363E /* MatchmakingHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE151588245F18EF001D363E /* MatchmakingHelper.swift */; }; AEBF3AFF246EB146004F7CD5 /* CancelBtnNode.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEBF3AFE246EB146004F7CD5 /* CancelBtnNode.swift */; }; @@ -120,6 +121,7 @@ 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 = ""; }; + AB21D7D4246C748A00B09CBA /* TwoPlayerMapGenerator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TwoPlayerMapGenerator.swift; sourceTree = ""; }; ABA03D9F244BD54F00A66916 /* Base.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Base.swift; sourceTree = ""; }; AE151588245F18EF001D363E /* MatchmakingHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MatchmakingHelper.swift; sourceTree = ""; }; AEBF3AFE246EB146004F7CD5 /* CancelBtnNode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CancelBtnNode.swift; sourceTree = ""; }; @@ -297,9 +299,9 @@ AB1D759E245DD2EA00671525 /* Map */ = { isa = PBXGroup; children = ( - AB1D759A245DD18100671525 /* MapProtocol.swift */, AB1D759F245DEC0500671525 /* MapFactory.swift */, AB1D759B245DD18100671525 /* TwoPlayerDefaultTestMap.swift */, + AB21D7D4246C748A00B09CBA /* TwoPlayerMapGenerator.swift */, ); path = Map; sourceTree = ""; @@ -429,6 +431,7 @@ AE151589245F18EF001D363E /* MatchmakingHelper.swift in Sources */, 11036113244B3E30008610AF /* MenuScene.swift in Sources */, C099579C246C5E5C0016AA22 /* DataService.swift in Sources */, + AB21D7D5246C748A00B09CBA /* TwoPlayerMapGenerator.swift in Sources */, 9EA3ABE9245C6DAA006BC61D /* DefaultBaseComponent.swift in Sources */, 9E174C8A245E1A0A00209FF0 /* Background.swift in Sources */, 9EA3ABED245C8143006BC61D /* ModalBackgroundComponent.swift in Sources */, diff --git a/GoldWars/GoldWars/Map/MapFactory.swift b/GoldWars/GoldWars/Map/MapFactory.swift index ab4321a..d305740 100644 --- a/GoldWars/GoldWars/Map/MapFactory.swift +++ b/GoldWars/GoldWars/Map/MapFactory.swift @@ -8,8 +8,30 @@ import Foundation import SpriteKit +import os + +protocol MapProtocol { + var size: CGSize! {get set} + var entityManager: EntityManager! {get set} + + init(scene: SKScene, entityManager: EntityManager) + + func load() +} + +protocol CenterElementProtocol { + + init(frame: CGRect) + + func getAllBases() -> [Base] + func getTopConnection() -> Base + func getRightConnection() -> Base + func getBottomConnection() -> Base + func getLeftConnection() -> Base +} class MapFactory { + let LOG = OSLog.init(subsystem: "MapGenerator", category: "MapFactory") var twoPlayerMaps: [MapProtocol]! var threePlayerMaps: [MapProtocol]! @@ -17,7 +39,7 @@ class MapFactory { init(scene: SKScene, entityManager: EntityManager) { self.twoPlayerMaps = [ - TwoPlayerDefaultTestMap(scene: scene, entityManager: entityManager) + TwoPlayerMapGenerator(scene: scene, entityManager: entityManager) ] self.threePlayerMaps = [MapProtocol]() @@ -26,11 +48,24 @@ class MapFactory { func loadMap(playerCount: Int) { if playerCount == 2 { + os_log("Loading two player map", log: LOG, type: .info) twoPlayerMaps.randomElement()?.load() } else if playerCount == 3 { + os_log("Loading three player map", log: LOG, type: .info) threePlayerMaps.randomElement()?.load() } else { + os_log("Loading four player map", log: LOG, type: .info) fourPlayerMaps.randomElement()?.load() } } } + +class CenterElementProvider { + static let LOG = OSLog.init(subsystem: "MapGenerator", category: "CenterElementProvider") + + static func get(frame: CGRect) -> CenterElementProtocol? { + // TODO: Implemented by Jakob + os_log("Getting new center from provider", log: LOG, type: .info) + return nil + } +} diff --git a/GoldWars/GoldWars/Map/MapProtocol.swift b/GoldWars/GoldWars/Map/MapProtocol.swift deleted file mode 100644 index 2560d39..0000000 --- a/GoldWars/GoldWars/Map/MapProtocol.swift +++ /dev/null @@ -1,20 +0,0 @@ -// -// MapProtocoll.swift -// GoldWars -// -// Created by Marcel Schwarz on 02.05.20. -// Copyright © 2020 SP2. All rights reserved. -// - -import Foundation -import SpriteKit - -protocol MapProtocol { - - var size: CGSize! {get set} - var entityManager: EntityManager! {get set} - - init(scene: SKScene, entityManager: EntityManager) - - func load() -} diff --git a/GoldWars/GoldWars/Map/TwoPlayerMapGenerator.swift b/GoldWars/GoldWars/Map/TwoPlayerMapGenerator.swift new file mode 100644 index 0000000..e6271ad --- /dev/null +++ b/GoldWars/GoldWars/Map/TwoPlayerMapGenerator.swift @@ -0,0 +1,47 @@ +// +// TwoPlayerMapGenerator.swift +// GoldWars +// +// Created by Marcel Schwarz on 13.05.20. +// Copyright © 2020 SP2. All rights reserved. +// + +import Foundation +import SpriteKit +import os + +class TwoPlayerMapGenerator: MapProtocol { + let LOG = OSLog.init(subsystem: "MapGenerator", category: "TwoPlayerMapGenerator") + + var size: CGSize! + var entityManager: EntityManager! + + required init(scene: SKScene, entityManager: EntityManager) { + self.size = scene.size + self.entityManager = entityManager + } + + func load() { + os_log("Loading from TwoPlayerMapFactory", log: LOG, type: .info) + getPlayerOneAndStartbases() + getMapCenter() + getPlayerTwoAndStartbases() + } + + private func getPlayerOneAndStartbases() -> [Base]{ + os_log("Get player one and his startbases", log: LOG, type: .info) + return [Base]() + } + + private func getPlayerTwoAndStartbases() -> [Base]{ + os_log("Get player two and his startbases", log: LOG, type: .info) + return [Base]() + } + + private func getMapCenter() -> [Base] { + os_log("Get centered bases", log: LOG, type: .info) + return [Base]() + } + + +} From ee031f71c1e359373b63008ce64216e297afe5d9 Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Sun, 17 May 2020 19:34:44 +0200 Subject: [PATCH 2/6] Add TwoPlayerMapGenerator --- GoldWars/GoldWars.xcodeproj/project.pbxproj | 6 +- GoldWars/GoldWars/Map/MapFactory.swift | 20 +- GoldWars/GoldWars/Map/SampleCElement.swift | 123 +++++++++++ .../GoldWars/Map/TwoPlayerMapGenerator.swift | 204 ++++++++++++++++-- 4 files changed, 325 insertions(+), 28 deletions(-) create mode 100644 GoldWars/GoldWars/Map/SampleCElement.swift diff --git a/GoldWars/GoldWars.xcodeproj/project.pbxproj b/GoldWars/GoldWars.xcodeproj/project.pbxproj index b3cbfaa..c5fe0d4 100644 --- a/GoldWars/GoldWars.xcodeproj/project.pbxproj +++ b/GoldWars/GoldWars.xcodeproj/project.pbxproj @@ -46,10 +46,10 @@ 9EC86BA6245C8AD000796EF3 /* ModalType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EC86BA5245C8AD000796EF3 /* ModalType.swift */; }; 9EEDE02D246FCD770096C735 /* SpinningLogoEntity.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EEDE02C246FCD770096C735 /* SpinningLogoEntity.swift */; }; 9EEDE02F246FCD800096C735 /* SpinningLogoComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EEDE02E246FCD800096C735 /* SpinningLogoComponent.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 */; }; AB21D7D5246C748A00B09CBA /* TwoPlayerMapGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB21D7D4246C748A00B09CBA /* TwoPlayerMapGenerator.swift */; }; + AB94038A2471528D004FCD07 /* SampleCElement.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB9403892471528D004FCD07 /* SampleCElement.swift */; }; ABA03DA0244BD54F00A66916 /* Base.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABA03D9F244BD54F00A66916 /* Base.swift */; }; AE151589245F18EF001D363E /* MatchmakingHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE151588245F18EF001D363E /* MatchmakingHelper.swift */; }; AEBF3AFF246EB146004F7CD5 /* CancelBtnNode.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEBF3AFE246EB146004F7CD5 /* CancelBtnNode.swift */; }; @@ -118,10 +118,10 @@ 9ECD3699245C91F7008DEEBD /* GoldWars.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = GoldWars.entitlements; sourceTree = ""; }; 9EEDE02C246FCD770096C735 /* SpinningLogoEntity.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SpinningLogoEntity.swift; sourceTree = ""; }; 9EEDE02E246FCD800096C735 /* SpinningLogoComponent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SpinningLogoComponent.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 = ""; }; AB21D7D4246C748A00B09CBA /* TwoPlayerMapGenerator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TwoPlayerMapGenerator.swift; sourceTree = ""; }; + AB9403892471528D004FCD07 /* SampleCElement.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SampleCElement.swift; sourceTree = ""; }; ABA03D9F244BD54F00A66916 /* Base.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Base.swift; sourceTree = ""; }; AE151588245F18EF001D363E /* MatchmakingHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MatchmakingHelper.swift; sourceTree = ""; }; AEBF3AFE246EB146004F7CD5 /* CancelBtnNode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CancelBtnNode.swift; sourceTree = ""; }; @@ -302,6 +302,7 @@ AB1D759F245DEC0500671525 /* MapFactory.swift */, AB1D759B245DD18100671525 /* TwoPlayerDefaultTestMap.swift */, AB21D7D4246C748A00B09CBA /* TwoPlayerMapGenerator.swift */, + AB9403892471528D004FCD07 /* SampleCElement.swift */, ); path = Map; sourceTree = ""; @@ -456,6 +457,7 @@ 9EC86B9F245C88A300796EF3 /* Modal.swift in Sources */, 9E78ACC2245CC9EE00526FF7 /* DefBoostSkillComponent.swift in Sources */, C05FAED62468559D0006AF2E /* SoundManager.swift in Sources */, + AB94038A2471528D004FCD07 /* SampleCElement.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/GoldWars/GoldWars/Map/MapFactory.swift b/GoldWars/GoldWars/Map/MapFactory.swift index d305740..3a6ac77 100644 --- a/GoldWars/GoldWars/Map/MapFactory.swift +++ b/GoldWars/GoldWars/Map/MapFactory.swift @@ -24,10 +24,10 @@ protocol CenterElementProtocol { init(frame: CGRect) func getAllBases() -> [Base] - func getTopConnection() -> Base - func getRightConnection() -> Base - func getBottomConnection() -> Base - func getLeftConnection() -> Base + func getTopConnection() -> Base? + func getRightConnection() -> Base? + func getBottomConnection() -> Base? + func getLeftConnection() -> Base? } class MapFactory { @@ -63,9 +63,15 @@ class MapFactory { class CenterElementProvider { static let LOG = OSLog.init(subsystem: "MapGenerator", category: "CenterElementProvider") - static func get(frame: CGRect) -> CenterElementProtocol? { + static func get(frame: CGRect) -> CenterElementProtocol { // TODO: Implemented by Jakob - os_log("Getting new center from provider", log: LOG, type: .info) - return nil + os_log("Getting new center element from provider", log: LOG, type: .info) + + let centerElements: [CenterElementProtocol.Type] = [ + SampleCElement.self, + CElement7.self + ] + + return centerElements.randomElement()!.init(frame: frame) } } diff --git a/GoldWars/GoldWars/Map/SampleCElement.swift b/GoldWars/GoldWars/Map/SampleCElement.swift new file mode 100644 index 0000000..a15f748 --- /dev/null +++ b/GoldWars/GoldWars/Map/SampleCElement.swift @@ -0,0 +1,123 @@ +// +// CElemente.swift +// GoldWars +// +// Created by Jakob Haag on 14.05.20. +// Copyright © 2020 SP2. All rights reserved. +// + +import Foundation +import SpriteKit + +class SampleCElement : CenterElementProtocol { + var frame: CGRect + var bases: [Base] = [] + var centerBase: Base + + required init(frame: CGRect) { + self.frame = frame + self.centerBase = Base( + position: CGPoint( + x: frame.midX, + y: frame.midY + ) + ) + self.bases.append(centerBase) + } + + func getAllBases() -> [Base] { + return self.bases + } + + func getTopConnection() -> Base? { + return self.centerBase + } + + func getRightConnection() -> Base? { + return self.centerBase + } + + func getBottomConnection() -> Base? { + return self.centerBase + } + + func getLeftConnection() -> Base? { + return self.centerBase + } +} + +// +// CElement7.swift +// GoldWars +// +// Created by Jakob Haag on 14.05.20. +// Copyright © 2020 SP2. All rights reserved. +// + +import Foundation +import SpriteKit + +class CElement7 : CenterElementProtocol { + var frame: CGRect + var bases: [Base] = [] + var topBase: Base + var bottomBase: Base + var leftBase: Base + var rightBase: Base + + required init(frame: CGRect) { + self.frame = frame + self.topBase = Base( + position: CGPoint( + x: frame.midX, + y: frame.minY + ) + ) + + self.bottomBase = Base( + position: CGPoint( + x: frame.midX, + y: frame.maxY + ) + ) + + self.leftBase = Base( + position: CGPoint( + x: frame.minX, + y: frame.midY + ) + ) + + self.rightBase = Base( + position: CGPoint( + x: frame.maxX, + y: frame.midY + ) + ) + self.bases.append(topBase) + self.bases.append(bottomBase) + self.bases.append(leftBase) + self.bases.append(rightBase) + } + + func getAllBases() -> [Base] { + return self.bases + } + + func getTopConnection() -> Base? { + return self.topBase + } + + func getRightConnection() -> Base? { + return self.rightBase + } + + func getBottomConnection() -> Base? { + return self.bottomBase + } + + func getLeftConnection() -> Base? { + return self.leftBase + } +} + diff --git a/GoldWars/GoldWars/Map/TwoPlayerMapGenerator.swift b/GoldWars/GoldWars/Map/TwoPlayerMapGenerator.swift index e6271ad..f6cd9b1 100644 --- a/GoldWars/GoldWars/Map/TwoPlayerMapGenerator.swift +++ b/GoldWars/GoldWars/Map/TwoPlayerMapGenerator.swift @@ -8,6 +8,7 @@ import Foundation import SpriteKit +import GameKit import os class TwoPlayerMapGenerator: MapProtocol { @@ -23,25 +24,190 @@ class TwoPlayerMapGenerator: MapProtocol { func load() { os_log("Loading from TwoPlayerMapFactory", log: LOG, type: .info) - getPlayerOneAndStartbases() - getMapCenter() - getPlayerTwoAndStartbases() + + // Generate bases structure + os_log("Get player one base", log: LOG, type: .info) + let basePlayerOne = Base( + position: CGPoint(x: self.size.width * 0.07, y: self.size.height / 2), + player: (MatchmakingHelper.sharedInstance.isServer) ? GKLocalPlayer.local : MatchmakingHelper.sharedInstance.mpMatch?.players[0], + team: .team1 + ) + + os_log("Get player one's startbases", log: LOG, type: .info) + + let p1StartBaseCount = Int.random(in: 1...3) + var p1StartBases = [String: Base]() + + if (p1StartBaseCount == 1) { + p1StartBases["mid"] = Base(position: CGPoint(x: self.size.width * 0.2, y: self.size.height * 0.5)) + } else if (p1StartBaseCount == 2) { + p1StartBases["top"] = Base(position: CGPoint(x: self.size.width * 0.2, y: self.size.height * 0.333)) + p1StartBases["bot"] = Base(position: CGPoint(x: self.size.width * 0.2, y: self.size.height * 0.666)) + } else { + p1StartBases["top"] = Base(position: CGPoint(x: self.size.width * 0.2, y: self.size.height * 0.25)) + p1StartBases["mid"] = Base(position: CGPoint(x: self.size.width * 0.2, y: self.size.height * 0.5)) + p1StartBases["bot"] = Base(position: CGPoint(x: self.size.width * 0.2, y: self.size.height * 0.75)) + } + + os_log("Generate Map center", log: LOG, type: .info) + let gridCellWidth = self.size.width * 0.2 + let gridCellHeight = self.size.height * 0.4 + let cellInsetX = CGFloat(self.size.width * 0.05) + let cellInsetY = CGFloat(self.size.height * 0.07) + + let gridTopLeft = CGRect( + x: self.size.width * 0.3, + y: self.size.height * 0.1, + width: gridCellWidth, + height: gridCellHeight + ).insetBy(dx: cellInsetX, dy: cellInsetY) + + let gridTopRight = CGRect( + x: self.size.width * 0.3, + y: self.size.height * 0.5, + width: gridCellWidth, + height: gridCellHeight + ).insetBy(dx: cellInsetX, dy: cellInsetY) + + let gridBottomLeft = CGRect( + x: self.size.width * 0.5, + y: self.size.height * 0.1, + width: gridCellWidth, + height: gridCellHeight + ).insetBy(dx: cellInsetX, dy: cellInsetY) + + let gridBottomRight = CGRect( + x: self.size.width * 0.5, + y: self.size.height * 0.5, + width: gridCellWidth, + height: gridCellHeight + ).insetBy(dx: cellInsetX, dy: cellInsetY) + + let topLeft = CenterElementProvider.get(frame: gridTopLeft) + let topRight = CenterElementProvider.get(frame: gridTopRight) + let bottomLeft = CenterElementProvider.get(frame: gridBottomLeft) + let bottomRight = CenterElementProvider.get(frame: gridBottomRight) + + os_log("Get player two base", log: LOG, type: .info) + let basePlayerTwo = Base( + position: CGPoint(x: self.size.width * 0.93, y: self.size.height / 2), + player: (!MatchmakingHelper.sharedInstance.isServer) ? GKLocalPlayer.local : MatchmakingHelper.sharedInstance.mpMatch?.players[0], + team: .team2 + ) + + os_log("Get player two's startbases", log: LOG, type: .info) + let p2StartBaseCount = Int.random(in: 1...3) + var p2StartBases = [String: Base]() + + if (p2StartBaseCount == 1) { + p2StartBases["mid"] = Base(position: CGPoint(x: self.size.width * 0.8, y: self.size.height * 0.5)) + } else if (p1StartBaseCount == 2) { + p2StartBases["top"] = Base(position: CGPoint(x: self.size.width * 0.8, y: self.size.height * 0.333)) + p2StartBases["bot"] = Base(position: CGPoint(x: self.size.width * 0.8, y: self.size.height * 0.666)) + } else { + p2StartBases["top"] = Base(position: CGPoint(x: self.size.width * 0.8, y: self.size.height * 0.25)) + p2StartBases["mid"] = Base(position: CGPoint(x: self.size.width * 0.8, y: self.size.height * 0.5)) + p2StartBases["bot"] = Base(position: CGPoint(x: self.size.width * 0.8, y: self.size.height * 0.75)) + } + + // Create adjacency mapping + os_log("Connecting base p1 and start bases", log: LOG, type: .info) + basePlayerOne.adjacencyList.append(contentsOf: p1StartBases.values) + p1StartBases.values.forEach({base in base.adjacencyList.append(basePlayerOne)}) + + if (p1StartBases["top"] != nil && topLeft.getLeftConnection() != nil) { + os_log("Connecting p1 startbase top with topLeft segment", log: LOG, type: .info) + p1StartBases["top"]!.adjacencyList.append(topLeft.getLeftConnection()!) + topLeft.getLeftConnection()!.adjacencyList.append(p1StartBases["top"]!) + } + + if (p1StartBases["mid"] != nil) { + if (topLeft.getLeftConnection() != nil) { + os_log("Connecting p1 startbase mid with topLeft segment", log: LOG, type: .info) + p1StartBases["mid"]!.adjacencyList.append(topLeft.getLeftConnection()!) + topLeft.getLeftConnection()!.adjacencyList.append(p1StartBases["mid"]!) + } + + if (bottomLeft.getLeftConnection() != nil) { + os_log("Connecting p1 startbase mid with bottomLeft segment", log: LOG, type: .info) + p1StartBases["mid"]!.adjacencyList.append(bottomLeft.getLeftConnection()!) + bottomLeft.getLeftConnection()!.adjacencyList.append(p1StartBases["mid"]!) + } + } + + if (p1StartBases["bot"] != nil && bottomLeft.getLeftConnection() != nil) { + os_log("Connecting p1 startbase bot with bottomLeft segment", log: LOG, type: .info) + p1StartBases["bot"]!.adjacencyList.append(bottomLeft.getLeftConnection()!) + bottomLeft.getLeftConnection()!.adjacencyList.append(p1StartBases["bot"]!) + } + + if (topLeft.getRightConnection() != nil && topRight.getLeftConnection() != nil) { + os_log("Connecting topLeft with topRight segment horizontally", log: LOG, type: .info) + topLeft.getRightConnection()!.adjacencyList.append(topRight.getLeftConnection()!) + topRight.getLeftConnection()!.adjacencyList.append(topLeft.getRightConnection()!) + } + + if (bottomLeft.getRightConnection() != nil && bottomRight.getLeftConnection() != nil) { + os_log("Connecting bottomLeft with bottomRight segment horizontally", log: LOG, type: .info) + bottomLeft.getRightConnection()!.adjacencyList.append(bottomRight.getLeftConnection()!) + bottomRight.getLeftConnection()!.adjacencyList.append(bottomLeft.getRightConnection()!) + } + + if (topLeft.getBottomConnection() != nil && bottomLeft.getTopConnection() != nil) { + os_log("Connecting topLeft with bottomLeft segment vertically", log: LOG, type: .info) + topLeft.getBottomConnection()!.adjacencyList.append(bottomLeft.getTopConnection()!) + bottomLeft.getTopConnection()!.adjacencyList.append(topLeft.getBottomConnection()!) + } + + if (topRight.getBottomConnection() != nil && bottomRight.getTopConnection() != nil) { + os_log("Connecting topRight with bottomRight segment vertically", log: LOG, type: .info) + topRight.getBottomConnection()!.adjacencyList.append(bottomRight.getTopConnection()!) + bottomRight.getTopConnection()!.adjacencyList.append(topRight.getBottomConnection()!) + } + + os_log("Connecting base p2 and start bases", log: LOG, type: .info) + basePlayerTwo.adjacencyList.append(contentsOf: p2StartBases.values) + p2StartBases.values.forEach({base in base.adjacencyList.append(basePlayerTwo)}) + + if (p2StartBases["top"] != nil && topRight.getRightConnection() != nil) { + os_log("Connecting p2 startbase top with topRight segment", log: LOG, type: .info) + p2StartBases["top"]!.adjacencyList.append(topRight.getRightConnection()!) + topRight.getRightConnection()!.adjacencyList.append(p2StartBases["top"]!) + } + + if (p2StartBases["mid"] != nil) { + if (topRight.getRightConnection() != nil) { + os_log("Connecting p2 startbase mid with topRight segment", log: LOG, type: .info) + p2StartBases["mid"]!.adjacencyList.append(topRight.getRightConnection()!) + topRight.getRightConnection()!.adjacencyList.append(p2StartBases["mid"]!) + } + + if (bottomRight.getRightConnection() != nil) { + os_log("Connecting p2 startbase mid with bottomRight segment", log: LOG, type: .info) + p2StartBases["mid"]!.adjacencyList.append(bottomRight.getRightConnection()!) + bottomRight.getRightConnection()!.adjacencyList.append(p2StartBases["mid"]!) + } + } + + if (p2StartBases["bot"] != nil && bottomRight.getRightConnection() != nil) { + os_log("Connecting p2 startbase bot with bottomRight segment", log: LOG, type: .info) + p2StartBases["bot"]!.adjacencyList.append(bottomRight.getRightConnection()!) + bottomRight.getRightConnection()!.adjacencyList.append(p2StartBases["bot"]!) + } + + + // Add all bases to the scene + entityManager.add(basePlayerOne) + p1StartBases.forEach({(key, base) in entityManager.add(base)}) + + topLeft.getAllBases().forEach({base in entityManager.add(base)}) + topRight.getAllBases().forEach({base in entityManager.add(base)}) + bottomLeft.getAllBases().forEach({base in entityManager.add(base)}) + bottomRight.getAllBases().forEach({base in entityManager.add(base)}) + + entityManager.add(basePlayerTwo) + p2StartBases.forEach({(key, base) in entityManager.add(base)}) + } - private func getPlayerOneAndStartbases() -> [Base]{ - os_log("Get player one and his startbases", log: LOG, type: .info) - return [Base]() - } - - private func getPlayerTwoAndStartbases() -> [Base]{ - os_log("Get player two and his startbases", log: LOG, type: .info) - return [Base]() - } - - private func getMapCenter() -> [Base] { - os_log("Get centered bases", log: LOG, type: .info) - return [Base]() - } - - } From 4fdd95bd76c5f2e0a203eabaea21277faa3f43b2 Mon Sep 17 00:00:00 2001 From: Jakob Haag Date: Mon, 18 May 2020 19:22:25 +0200 Subject: [PATCH 3/6] Add CElements and Ways --- GoldWars/GoldWars.xcodeproj/project.pbxproj | 24 +- .../Components/DefaultWayComponent.swift | 28 + GoldWars/GoldWars/Components/WayNode.swift | 25 + GoldWars/GoldWars/Entities/Base.swift | 2 + .../GoldWars/Entities/EntityManager.swift | 3 + GoldWars/GoldWars/Entities/Way.swift | 30 + GoldWars/GoldWars/Map/CElemente.swift | 1331 +++++++++++++++++ GoldWars/GoldWars/Map/MapFactory.swift | 25 +- GoldWars/GoldWars/Map/SampleCElement.swift | 2 +- .../GoldWars/Map/TwoPlayerMapGenerator.swift | 45 +- 10 files changed, 1499 insertions(+), 16 deletions(-) create mode 100644 GoldWars/GoldWars/Components/DefaultWayComponent.swift create mode 100644 GoldWars/GoldWars/Components/WayNode.swift create mode 100644 GoldWars/GoldWars/Entities/Way.swift create mode 100644 GoldWars/GoldWars/Map/CElemente.swift diff --git a/GoldWars/GoldWars.xcodeproj/project.pbxproj b/GoldWars/GoldWars.xcodeproj/project.pbxproj index c5fe0d4..0462ade 100644 --- a/GoldWars/GoldWars.xcodeproj/project.pbxproj +++ b/GoldWars/GoldWars.xcodeproj/project.pbxproj @@ -18,6 +18,9 @@ 116060F7245C57D2004E5A36 /* EntityManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 116060F6245C57D2004E5A36 /* EntityManager.swift */; }; 11738A3B24508F68004426F1 /* Unit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11738A3A24508F68004426F1 /* Unit.swift */; }; 2086465C2461B66200817C23 /* TimerComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2086465B2461B66200817C23 /* TimerComponent.swift */; }; + 3E67854024728368007B9DE4 /* CElemente.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E67853F24728368007B9DE4 /* CElemente.swift */; }; + 3E6785422472CBEC007B9DE4 /* Way.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E6785412472CBEC007B9DE4 /* Way.swift */; }; + 3E6785442472CC27007B9DE4 /* DefaultWayComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E6785432472CC27007B9DE4 /* DefaultWayComponent.swift */; }; 3EBD242C245D8044003CECE7 /* GameCenterHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3EBD242B245D8044003CECE7 /* GameCenterHelper.swift */; }; 3EBD242E245D9332003CECE7 /* Team.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3EBD242D245D9332003CECE7 /* Team.swift */; }; 3F745DF0246F48FC00CE7375 /* PlayerMoveType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F745DEF246F48FC00CE7375 /* PlayerMoveType.swift */; }; @@ -49,7 +52,6 @@ AB1D759D245DD18100671525 /* TwoPlayerDefaultTestMap.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB1D759B245DD18100671525 /* TwoPlayerDefaultTestMap.swift */; }; AB1D75A0245DEC0500671525 /* MapFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB1D759F245DEC0500671525 /* MapFactory.swift */; }; AB21D7D5246C748A00B09CBA /* TwoPlayerMapGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB21D7D4246C748A00B09CBA /* TwoPlayerMapGenerator.swift */; }; - AB94038A2471528D004FCD07 /* SampleCElement.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB9403892471528D004FCD07 /* SampleCElement.swift */; }; ABA03DA0244BD54F00A66916 /* Base.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABA03D9F244BD54F00A66916 /* Base.swift */; }; AE151589245F18EF001D363E /* MatchmakingHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE151588245F18EF001D363E /* MatchmakingHelper.swift */; }; AEBF3AFF246EB146004F7CD5 /* CancelBtnNode.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEBF3AFE246EB146004F7CD5 /* CancelBtnNode.swift */; }; @@ -89,6 +91,9 @@ 116060F6245C57D2004E5A36 /* EntityManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EntityManager.swift; sourceTree = ""; }; 11738A3A24508F68004426F1 /* Unit.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Unit.swift; sourceTree = ""; }; 2086465B2461B66200817C23 /* TimerComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimerComponent.swift; sourceTree = ""; }; + 3E67853F24728368007B9DE4 /* CElemente.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CElemente.swift; sourceTree = ""; }; + 3E6785412472CBEC007B9DE4 /* Way.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Way.swift; sourceTree = ""; }; + 3E6785432472CC27007B9DE4 /* DefaultWayComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DefaultWayComponent.swift; sourceTree = ""; }; 3EBD242B245D8044003CECE7 /* GameCenterHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GameCenterHelper.swift; sourceTree = ""; }; 3EBD242D245D9332003CECE7 /* Team.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Team.swift; sourceTree = ""; }; 3F745DEF246F48FC00CE7375 /* PlayerMoveType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayerMoveType.swift; sourceTree = ""; }; @@ -121,7 +126,6 @@ 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 = ""; }; AB21D7D4246C748A00B09CBA /* TwoPlayerMapGenerator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TwoPlayerMapGenerator.swift; sourceTree = ""; }; - AB9403892471528D004FCD07 /* SampleCElement.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SampleCElement.swift; sourceTree = ""; }; ABA03D9F244BD54F00A66916 /* Base.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Base.swift; sourceTree = ""; }; AE151588245F18EF001D363E /* MatchmakingHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MatchmakingHelper.swift; sourceTree = ""; }; AEBF3AFE246EB146004F7CD5 /* CancelBtnNode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CancelBtnNode.swift; sourceTree = ""; }; @@ -233,6 +237,7 @@ 2086465B2461B66200817C23 /* TimerComponent.swift */, 9EBFD7542462CF5A00E1E219 /* SliderComponent.swift */, 9EC7E48A2461FBF700396BCD /* SliderNode.swift */, + 3E6785432472CC27007B9DE4 /* DefaultWayComponent.swift */, ); path = Components; sourceTree = ""; @@ -240,13 +245,14 @@ 116060F5245C5709004E5A36 /* Entities */ = { isa = PBXGroup; children = ( - 9EEDE02C246FCD770096C735 /* SpinningLogoEntity.swift */, + 9E174C89245E1A0A00209FF0 /* Background.swift */, ABA03D9F244BD54F00A66916 /* Base.swift */, - 9EC86B9E245C88A300796EF3 /* Modal.swift */, + 9E174C83245DD8CE00209FF0 /* Button.swift */, 116060F6245C57D2004E5A36 /* EntityManager.swift */, 9E78ACB9245CBDAF00526FF7 /* HUD.swift */, - 9E174C83245DD8CE00209FF0 /* Button.swift */, - 9E174C89245E1A0A00209FF0 /* Background.swift */, + 9EC86B9E245C88A300796EF3 /* Modal.swift */, + 9EEDE02C246FCD770096C735 /* SpinningLogoEntity.swift */, + 3E6785412472CBEC007B9DE4 /* Way.swift */, ); path = Entities; sourceTree = ""; @@ -299,10 +305,10 @@ AB1D759E245DD2EA00671525 /* Map */ = { isa = PBXGroup; children = ( + 3E67853F24728368007B9DE4 /* CElemente.swift */, AB1D759F245DEC0500671525 /* MapFactory.swift */, AB1D759B245DD18100671525 /* TwoPlayerDefaultTestMap.swift */, AB21D7D4246C748A00B09CBA /* TwoPlayerMapGenerator.swift */, - AB9403892471528D004FCD07 /* SampleCElement.swift */, ); path = Map; sourceTree = ""; @@ -419,6 +425,7 @@ 9E78ACC4245CCA3600526FF7 /* SpySkillComponent.swift in Sources */, 9EA3ABEB245C6DFA006BC61D /* BaseNode.swift in Sources */, 9E04AFAF245E2B73002D5CFC /* AttackActionComponent.swift in Sources */, + 3E6785422472CBEC007B9DE4 /* Way.swift in Sources */, 110360D9244B101A008610AF /* GameScene.swift in Sources */, C04783F024685995004961FB /* SettingsScene.swift in Sources */, 116060F7245C57D2004E5A36 /* EntityManager.swift in Sources */, @@ -444,6 +451,7 @@ AB1D75A0245DEC0500671525 /* MapFactory.swift in Sources */, AB1D759D245DD18100671525 /* TwoPlayerDefaultTestMap.swift in Sources */, 9EBFD7552462CF5A00E1E219 /* SliderComponent.swift in Sources */, + 3E67854024728368007B9DE4 /* CElemente.swift in Sources */, ABA03DA0244BD54F00A66916 /* Base.swift in Sources */, C064E9AC246C151F0022B228 /* Label.swift in Sources */, 9E174C82245DD81D00209FF0 /* ButtonNode.swift in Sources */, @@ -452,12 +460,12 @@ 9E174C84245DD8CE00209FF0 /* Button.swift in Sources */, AEBF3AFF246EB146004F7CD5 /* CancelBtnNode.swift in Sources */, 110360DB244B101A008610AF /* GameViewController.swift in Sources */, + 3E6785442472CC27007B9DE4 /* DefaultWayComponent.swift in Sources */, 2086465C2461B66200817C23 /* TimerComponent.swift in Sources */, 110360D3244B101A008610AF /* AppDelegate.swift in Sources */, 9EC86B9F245C88A300796EF3 /* Modal.swift in Sources */, 9E78ACC2245CC9EE00526FF7 /* DefBoostSkillComponent.swift in Sources */, C05FAED62468559D0006AF2E /* SoundManager.swift in Sources */, - AB94038A2471528D004FCD07 /* SampleCElement.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/GoldWars/GoldWars/Components/DefaultWayComponent.swift b/GoldWars/GoldWars/Components/DefaultWayComponent.swift new file mode 100644 index 0000000..f72044c --- /dev/null +++ b/GoldWars/GoldWars/Components/DefaultWayComponent.swift @@ -0,0 +1,28 @@ +// +// DefaultWayComponent.swift +// GoldWars +// +// Created by Jakob Haag on 18.05.20. +// Copyright © 2020 SP2. All rights reserved. +// + +import Foundation +import GameplayKit +import SpriteKit + +class DefaultWayComponent: GKComponent { + var shapeNode: SKShapeNode + + init(pathToDraw: CGMutablePath) { + self.shapeNode = SKShapeNode() + shapeNode.path = pathToDraw + shapeNode.strokeColor = UIColor(red: 0.852, green: 0.649, blue: 0.123, alpha: 1) + shapeNode.lineWidth = 10 + shapeNode.zPosition = -1 + super.init() + } + + required init?(coder aDecoder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } +} diff --git a/GoldWars/GoldWars/Components/WayNode.swift b/GoldWars/GoldWars/Components/WayNode.swift new file mode 100644 index 0000000..41559a6 --- /dev/null +++ b/GoldWars/GoldWars/Components/WayNode.swift @@ -0,0 +1,25 @@ +// +// WayNode.swift +// GoldWars +// +// Created by Jakob Haag on 18.05.20. +// Copyright © 2020 SP2. All rights reserved. +// + +import SpriteKit + +class WayNode: SKSpriteNode{ + + override func touchesBegan(_ touches: Set, with event: UIEvent?) { + print("touched Way") + // TODO: PopUp Einheiten + Close PopUp + } + + 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/Entities/Base.swift b/GoldWars/GoldWars/Entities/Base.swift index ad05629..6609a6e 100644 --- a/GoldWars/GoldWars/Entities/Base.swift +++ b/GoldWars/GoldWars/Entities/Base.swift @@ -17,6 +17,7 @@ class Base: GKEntity{ var changeOwnership: Bool var ownershipPlayer: GKPlayer? var baseID: Int + var position: CGPoint init(position: CGPoint, player: GKPlayer! = nil, team: Team! = nil) { self.unitCount = 0 @@ -25,6 +26,7 @@ class Base: GKEntity{ self.ownershipPlayer = player self.baseID = Base.BASE_ID_COUNT Base.BASE_ID_COUNT += 1 + self.position = position super.init() addComponent(DefaultBaseComponent(texture: SKTexture(imageNamed: "Base"), position: position)) diff --git a/GoldWars/GoldWars/Entities/EntityManager.swift b/GoldWars/GoldWars/Entities/EntityManager.swift index bc1964b..1fd9f7d 100644 --- a/GoldWars/GoldWars/Entities/EntityManager.swift +++ b/GoldWars/GoldWars/Entities/EntityManager.swift @@ -82,6 +82,9 @@ class EntityManager { if let node = entity.component(ofType: SpinningLogoComponent.self)?.node { scene.addChild(node) } + if let wayNode = entity.component(ofType: DefaultWayComponent.self)?.shapeNode { + scene.addChild(wayNode) + } } func remove(_ entity: GKEntity) { diff --git a/GoldWars/GoldWars/Entities/Way.swift b/GoldWars/GoldWars/Entities/Way.swift new file mode 100644 index 0000000..6a64eb6 --- /dev/null +++ b/GoldWars/GoldWars/Entities/Way.swift @@ -0,0 +1,30 @@ +// +// Way.swift +// GoldWars +// +// Created by Jakob Haag on 18.05.20. +// Copyright © 2020 SP2. All rights reserved. +// + +import Foundation +import SpriteKit +import GameplayKit + +class Way: GKEntity { + + required init(fromBase: Base, toBase: Base) { + super.init() + + let pathToDraw = CGMutablePath() + pathToDraw.move(to: fromBase.position) + pathToDraw.addLine(to: toBase.position) + + + addComponent(DefaultWayComponent(pathToDraw: pathToDraw)) + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } +} + diff --git a/GoldWars/GoldWars/Map/CElemente.swift b/GoldWars/GoldWars/Map/CElemente.swift new file mode 100644 index 0000000..409ed30 --- /dev/null +++ b/GoldWars/GoldWars/Map/CElemente.swift @@ -0,0 +1,1331 @@ +// +// CElemente.swift +// GoldWars +// +// Created by Jakob Haag on 18.05.20. +// Copyright © 2020 SP2. All rights reserved. +// + +import SpriteKit + +class CElement1: CenterElementProtocol { + + var bases: [Base] = [] + var centerBase: Base + var ways: [Way] = [] + + required init(frame: CGRect) { + self.centerBase = Base( + position: CGPoint( + x: frame.midX, + y: frame.midY + ) + ) + self.bases.append(centerBase) + } + + func getAllBases() -> [Base] { + return self.bases + } + + func getInternalWays() -> [Way] { + return self.ways + } + + func getTopConnection() -> Base? { + return self.centerBase + } + + func getRightConnection() -> Base? { + return self.centerBase + } + + func getBottomConnection() -> Base? { + return self.centerBase + } + + func getLeftConnection() -> Base? { + return self.centerBase + } +} + +class CElement2: CenterElementProtocol { + var bases: [Base] = [] + var leftBase: Base + var rightBase: Base + var ways: [Way] = [] + + required init(frame: CGRect) { + self.leftBase = Base( + position: CGPoint( + x: frame.minX, + y: frame.midY + ) + ) + + self.rightBase = Base( + position: CGPoint( + x: frame.maxX, + y: frame.midY + ) + ) + self.bases.append(leftBase) + self.bases.append(rightBase) + + self.connectInnerBases() + self.createInternalWays() + } + + func connectInnerBases() { + self.leftBase.adjacencyList.append(self.rightBase) + self.rightBase.adjacencyList.append(self.leftBase) + } + + func createInternalWays() { + ways.append(Way(fromBase: self.leftBase, toBase: self.rightBase)) + } + + func getAllBases() -> [Base] { + return self.bases + } + + func getInternalWays() -> [Way] { + return self.ways + } + + func getTopConnection() -> Base? { + return self.leftBase + } + + func getRightConnection() -> Base? { + return self.rightBase + } + + func getBottomConnection() -> Base? { + return self.rightBase + } + + func getLeftConnection() -> Base? { + return self.leftBase + } +} + +class CElement3: CenterElementProtocol { + var bases: [Base] = [] + var leftBase: Base + var rightBase: Base + var ways: [Way] = [] + + required init(frame: CGRect) { + self.leftBase = Base( + position: CGPoint( + x: frame.minX, + y: frame.midY + ) + ) + + self.rightBase = Base( + position: CGPoint( + x: frame.maxX, + y: frame.midY + ) + ) + self.bases.append(leftBase) + self.bases.append(rightBase) + + self.connectInnerBases() + self.createInternalWays() + } + + func connectInnerBases() { + self.leftBase.adjacencyList.append(self.rightBase) + self.rightBase.adjacencyList.append(self.leftBase) + } + + func createInternalWays() { + ways.append(Way(fromBase: self.leftBase, toBase: self.rightBase)) + } + + func getAllBases() -> [Base] { + return self.bases + } + + func getInternalWays() -> [Way] { + return self.ways + } + + func getTopConnection() -> Base? { + return self.rightBase + } + + func getRightConnection() -> Base? { + return self.rightBase + } + + func getBottomConnection() -> Base? { + return self.leftBase + } + + func getLeftConnection() -> Base? { + return self.leftBase + } +} + +class CElement4: CenterElementProtocol { + var bases: [Base] = [] + var centerBase: Base + var leftBase: Base + var rightBase: Base + var ways: [Way] = [] + + required init(frame: CGRect) { + self.centerBase = Base( + position: CGPoint( + x: frame.midX, + y: frame.maxY + ) + ) + + self.leftBase = Base( + position: CGPoint( + x: frame.minX, + y: frame.minY + ) + ) + + self.rightBase = Base( + position: CGPoint( + x: frame.maxX, + y: frame.minY + ) + ) + self.bases.append(centerBase) + self.bases.append(leftBase) + self.bases.append(rightBase) + + self.connectInnerBases() + self.createInternalWays() + } + + func connectInnerBases() { + self.leftBase.adjacencyList.append(self.rightBase) + self.leftBase.adjacencyList.append(self.centerBase) + self.rightBase.adjacencyList.append(self.leftBase) + self.rightBase.adjacencyList.append(self.centerBase) + self.centerBase.adjacencyList.append(self.leftBase) + self.centerBase.adjacencyList.append(self.rightBase) + } + + func createInternalWays() { + ways.append(Way(fromBase: self.leftBase, toBase: self.rightBase)) + ways.append(Way(fromBase: self.centerBase, toBase: self.rightBase)) + ways.append(Way(fromBase: self.centerBase, toBase: self.leftBase)) + } + + func getAllBases() -> [Base] { + return self.bases + } + + func getInternalWays() -> [Way] { + return self.ways + } + + func getTopConnection() -> Base? { + return self.leftBase + } + + func getRightConnection() -> Base? { + return self.rightBase + } + + func getBottomConnection() -> Base? { + return self.centerBase + } + + func getLeftConnection() -> Base? { + return self.leftBase + } +} + +class CElement5: CenterElementProtocol { + var bases: [Base] = [] + var centerBase: Base + var leftBase: Base + var rightBase: Base + var ways: [Way] = [] + + required init(frame: CGRect) { + self.centerBase = Base( + position: CGPoint( + x: frame.midX, + y: frame.maxY + ) + ) + + self.leftBase = Base( + position: CGPoint( + x: frame.minX, + y: frame.minY + ) + ) + + self.rightBase = Base( + position: CGPoint( + x: frame.maxX, + y: frame.minY + ) + ) + self.bases.append(centerBase) + self.bases.append(leftBase) + self.bases.append(rightBase) + + self.connectInnerBases() + self.createInternalWays() + } + + func connectInnerBases() { + self.leftBase.adjacencyList.append(self.rightBase) + self.leftBase.adjacencyList.append(self.centerBase) + self.rightBase.adjacencyList.append(self.leftBase) + self.rightBase.adjacencyList.append(self.centerBase) + self.centerBase.adjacencyList.append(self.leftBase) + self.centerBase.adjacencyList.append(self.rightBase) + } + + func createInternalWays() { + ways.append(Way(fromBase: self.leftBase, toBase: self.rightBase)) + ways.append(Way(fromBase: self.centerBase, toBase: self.rightBase)) + ways.append(Way(fromBase: self.centerBase, toBase: self.leftBase)) + } + + func getAllBases() -> [Base] { + return self.bases + } + + func getInternalWays() -> [Way] { + return self.ways + } + + func getTopConnection() -> Base? { + return self.rightBase + } + + func getRightConnection() -> Base? { + return self.rightBase + } + + func getBottomConnection() -> Base? { + return self.centerBase + } + + func getLeftConnection() -> Base? { + return self.leftBase + } +} + +class CElement6: CenterElementProtocol { + var bases: [Base] = [] + var centerBase: Base + var leftBase: Base + var rightBase: Base + var ways: [Way] = [] + + required init(frame: CGRect) { + self.centerBase = Base( + position: CGPoint( + x: frame.midX, + y: frame.minY + ) + ) + + self.leftBase = Base( + position: CGPoint( + x: frame.minX, + y: frame.maxY + ) + ) + + self.rightBase = Base( + position: CGPoint( + x: frame.maxX, + y: frame.maxY + ) + ) + self.bases.append(centerBase) + self.bases.append(leftBase) + self.bases.append(rightBase) + + self.connectInnerBases() + self.createInternalWays() + } + + func connectInnerBases() { + self.leftBase.adjacencyList.append(self.rightBase) + self.leftBase.adjacencyList.append(self.centerBase) + self.rightBase.adjacencyList.append(self.leftBase) + self.rightBase.adjacencyList.append(self.centerBase) + self.centerBase.adjacencyList.append(self.leftBase) + self.centerBase.adjacencyList.append(self.rightBase) + } + + func createInternalWays() { + ways.append(Way(fromBase: self.leftBase, toBase: self.rightBase)) + ways.append(Way(fromBase: self.centerBase, toBase: self.rightBase)) + ways.append(Way(fromBase: self.centerBase, toBase: self.leftBase)) + } + + func getAllBases() -> [Base] { + return self.bases + } + + func getInternalWays() -> [Way] { + return self.ways + } + + func getTopConnection() -> Base? { + return self.centerBase + } + + func getRightConnection() -> Base? { + return self.rightBase + } + + func getBottomConnection() -> Base? { + return self.leftBase + } + + func getLeftConnection() -> Base? { + return self.leftBase + } +} + +class CElement7: CenterElementProtocol { + var bases: [Base] = [] + var centerBase: Base + var leftBase: Base + var rightBase: Base + var ways: [Way] = [] + + required init(frame: CGRect) { + self.centerBase = Base( + position: CGPoint( + x: frame.midX, + y: frame.minY + ) + ) + + self.leftBase = Base( + position: CGPoint( + x: frame.minX, + y: frame.maxY + ) + ) + + self.rightBase = Base( + position: CGPoint( + x: frame.maxX, + y: frame.maxY + ) + ) + self.bases.append(centerBase) + self.bases.append(leftBase) + self.bases.append(rightBase) + + self.connectInnerBases() + self.createInternalWays() + } + + func connectInnerBases() { + self.leftBase.adjacencyList.append(self.rightBase) + self.leftBase.adjacencyList.append(self.centerBase) + self.rightBase.adjacencyList.append(self.leftBase) + self.rightBase.adjacencyList.append(self.centerBase) + self.centerBase.adjacencyList.append(self.leftBase) + self.centerBase.adjacencyList.append(self.rightBase) + } + + func createInternalWays() { + ways.append(Way(fromBase: self.leftBase, toBase: self.rightBase)) + ways.append(Way(fromBase: self.centerBase, toBase: self.rightBase)) + ways.append(Way(fromBase: self.centerBase, toBase: self.leftBase)) + } + + func getAllBases() -> [Base] { + return self.bases + } + + func getInternalWays() -> [Way] { + return self.ways + } + + func getTopConnection() -> Base? { + return self.centerBase + } + + func getRightConnection() -> Base? { + return self.rightBase + } + + func getBottomConnection() -> Base? { + return self.rightBase + } + + func getLeftConnection() -> Base? { + return self.leftBase + } +} + +class CElement8: CenterElementProtocol { + var bases: [Base] = [] + var topBase: Base + var bottomBase: Base + var leftBase: Base + var rightBase: Base + var ways: [Way] = [] + + required init(frame: CGRect) { + self.topBase = Base( + position: CGPoint( + x: frame.midX, + y: frame.minY + ) + ) + + self.bottomBase = Base( + position: CGPoint( + x: frame.midX, + y: frame.maxY + ) + ) + + self.leftBase = Base( + position: CGPoint( + x: frame.minX, + y: frame.midY + ) + ) + + self.rightBase = Base( + position: CGPoint( + x: frame.maxX, + y: frame.midY + ) + ) + self.bases.append(topBase) + self.bases.append(bottomBase) + self.bases.append(leftBase) + self.bases.append(rightBase) + + self.connectInnerBases() + self.createInternalWays() + } + + func connectInnerBases() { + self.leftBase.adjacencyList.append(self.rightBase) + self.leftBase.adjacencyList.append(self.topBase) + self.leftBase.adjacencyList.append(self.bottomBase) + self.rightBase.adjacencyList.append(self.leftBase) + self.rightBase.adjacencyList.append(self.topBase) + self.rightBase.adjacencyList.append(self.bottomBase) + self.topBase.adjacencyList.append(self.leftBase) + self.topBase.adjacencyList.append(self.rightBase) + self.topBase.adjacencyList.append(self.bottomBase) + self.bottomBase.adjacencyList.append(self.topBase) + self.bottomBase.adjacencyList.append(self.rightBase) + self.bottomBase.adjacencyList.append(self.leftBase) + } + + func createInternalWays() { + ways.append(Way(fromBase: self.topBase, toBase: self.rightBase)) + ways.append(Way(fromBase: self.topBase, toBase: self.leftBase)) + ways.append(Way(fromBase: self.bottomBase, toBase: self.rightBase)) + ways.append(Way(fromBase: self.bottomBase, toBase: self.leftBase)) + } + + func getAllBases() -> [Base] { + return self.bases + } + + func getInternalWays() -> [Way] { + return self.ways + } + + func getTopConnection() -> Base? { + return self.topBase + } + + func getRightConnection() -> Base? { + return self.rightBase + } + + func getBottomConnection() -> Base? { + return self.bottomBase + } + + func getLeftConnection() -> Base? { + return self.leftBase + } +} + +class CElement9: CenterElementProtocol { + var bases: [Base] = [] + var leftBase: Base + var rightBase: Base + var ways: [Way] = [] + + required init(frame: CGRect) { + self.leftBase = Base( + position: CGPoint( + x: frame.minX, + y: frame.midY + ) + ) + + self.rightBase = Base( + position: CGPoint( + x: frame.maxX, + y: frame.midY + ) + ) + self.bases.append(leftBase) + self.bases.append(rightBase) + + self.connectInnerBases() + self.createInternalWays() + } + + func connectInnerBases() { + self.leftBase.adjacencyList.append(self.rightBase) + self.rightBase.adjacencyList.append(self.leftBase) + } + + func createInternalWays() { + ways.append(Way(fromBase: self.leftBase, toBase: self.rightBase)) + } + + func getAllBases() -> [Base] { + return self.bases + } + + func getInternalWays() -> [Way] { + return self.ways + } + + func getTopConnection() -> Base? { + return self.leftBase + } + + func getRightConnection() -> Base? { + return self.rightBase + } + + func getBottomConnection() -> Base? { + return nil + } + + func getLeftConnection() -> Base? { + return self.leftBase + } +} + +class CElement10: CenterElementProtocol { + var bases: [Base] = [] + var leftBase: Base + var rightBase: Base + var ways: [Way] = [] + + required init(frame: CGRect) { + self.leftBase = Base( + position: CGPoint( + x: frame.minX, + y: frame.midY + ) + ) + + self.rightBase = Base( + position: CGPoint( + x: frame.maxX, + y: frame.midY + ) + ) + self.bases.append(leftBase) + self.bases.append(rightBase) + + self.connectInnerBases() + self.createInternalWays() + } + + func connectInnerBases() { + self.leftBase.adjacencyList.append(self.rightBase) + self.rightBase.adjacencyList.append(self.leftBase) + } + + func createInternalWays() { + ways.append(Way(fromBase: self.leftBase, toBase: self.rightBase)) + } + + func getAllBases() -> [Base] { + return self.bases + } + + func getInternalWays() -> [Way] { + return self.ways + } + + func getTopConnection() -> Base? { + return nil + } + + func getRightConnection() -> Base? { + return self.rightBase + } + + func getBottomConnection() -> Base? { + return self.rightBase + } + + func getLeftConnection() -> Base? { + return self.leftBase + } +} + +class CElement11: CenterElementProtocol { + var bases: [Base] = [] + var leftBase: Base + var rightBase: Base + var ways: [Way] = [] + + required init(frame: CGRect) { + self.leftBase = Base( + position: CGPoint( + x: frame.minX, + y: frame.midY + ) + ) + + self.rightBase = Base( + position: CGPoint( + x: frame.maxX, + y: frame.midY + ) + ) + self.bases.append(leftBase) + self.bases.append(rightBase) + + self.connectInnerBases() + self.createInternalWays() + } + + func connectInnerBases() { + self.leftBase.adjacencyList.append(self.rightBase) + self.rightBase.adjacencyList.append(self.leftBase) + } + + func createInternalWays() { + ways.append(Way(fromBase: self.leftBase, toBase: self.rightBase)) + } + + func getAllBases() -> [Base] { + return self.bases + } + + func getInternalWays() -> [Way] { + return self.ways + } + + func getTopConnection() -> Base? { + return self.rightBase + } + + func getRightConnection() -> Base? { + return self.rightBase + } + + func getBottomConnection() -> Base? { + return nil + } + + func getLeftConnection() -> Base? { + return self.leftBase + } +} + +class CElement12: CenterElementProtocol { + var bases: [Base] = [] + var centerBase: Base + var leftBase: Base + var rightBase: Base + var ways: [Way] = [] + + required init(frame: CGRect) { + self.centerBase = Base( + position: CGPoint( + x: frame.midX, + y: frame.maxY + ) + ) + + self.leftBase = Base( + position: CGPoint( + x: frame.minX, + y: frame.minY + ) + ) + + self.rightBase = Base( + position: CGPoint( + x: frame.maxX, + y: frame.minY + ) + ) + self.bases.append(centerBase) + self.bases.append(leftBase) + self.bases.append(rightBase) + + self.connectInnerBases() + self.createInternalWays() + } + + func connectInnerBases() { + self.leftBase.adjacencyList.append(self.rightBase) + self.leftBase.adjacencyList.append(self.centerBase) + self.rightBase.adjacencyList.append(self.leftBase) + self.rightBase.adjacencyList.append(self.centerBase) + self.centerBase.adjacencyList.append(self.leftBase) + self.centerBase.adjacencyList.append(self.rightBase) + } + + func createInternalWays() { + ways.append(Way(fromBase: self.leftBase, toBase: self.rightBase)) + ways.append(Way(fromBase: self.centerBase, toBase: self.leftBase)) + ways.append(Way(fromBase: self.centerBase, toBase: self.rightBase)) + } + + func getAllBases() -> [Base] { + return self.bases + } + + func getInternalWays() -> [Way] { + return self.ways + } + + func getTopConnection() -> Base? { + return nil + } + + func getRightConnection() -> Base? { + return self.rightBase + } + + func getBottomConnection() -> Base? { + return self.centerBase + } + + func getLeftConnection() -> Base? { + return self.leftBase + } +} + +class CElement13: CenterElementProtocol { + var bases: [Base] = [] + var centerBase: Base + var leftBase: Base + var rightBase: Base + var ways: [Way] = [] + + required init(frame: CGRect) { + self.centerBase = Base( + position: CGPoint( + x: frame.midX, + y: frame.maxY + ) + ) + + self.leftBase = Base( + position: CGPoint( + x: frame.minX, + y: frame.minY + ) + ) + + self.rightBase = Base( + position: CGPoint( + x: frame.maxX, + y: frame.minY + ) + ) + self.bases.append(centerBase) + self.bases.append(leftBase) + self.bases.append(rightBase) + + self.connectInnerBases() + self.createInternalWays() + } + + func connectInnerBases() { + self.leftBase.adjacencyList.append(self.rightBase) + self.leftBase.adjacencyList.append(self.centerBase) + self.rightBase.adjacencyList.append(self.leftBase) + self.rightBase.adjacencyList.append(self.centerBase) + self.centerBase.adjacencyList.append(self.leftBase) + self.centerBase.adjacencyList.append(self.rightBase) + } + + func createInternalWays() { + ways.append(Way(fromBase: self.leftBase, toBase: self.rightBase)) + ways.append(Way(fromBase: self.centerBase, toBase: self.leftBase)) + ways.append(Way(fromBase: self.centerBase, toBase: self.rightBase)) + } + + func getAllBases() -> [Base] { + return self.bases + } + + func getInternalWays() -> [Way] { + return self.ways + } + + func getTopConnection() -> Base? { + return self.rightBase + } + + func getRightConnection() -> Base? { + return self.rightBase + } + + func getBottomConnection() -> Base? { + return nil + } + + func getLeftConnection() -> Base? { + return self.leftBase + } +} + +class CElement14: CenterElementProtocol { + var bases: [Base] = [] + var centerBase: Base + var leftBase: Base + var rightBase: Base + var ways: [Way] = [] + + required init(frame: CGRect) { + self.centerBase = Base( + position: CGPoint( + x: frame.midX, + y: frame.minY + ) + ) + + self.leftBase = Base( + position: CGPoint( + x: frame.minX, + y: frame.maxY + ) + ) + + self.rightBase = Base( + position: CGPoint( + x: frame.maxX, + y: frame.maxY + ) + ) + self.bases.append(centerBase) + self.bases.append(leftBase) + self.bases.append(rightBase) + + self.connectInnerBases() + self.createInternalWays() + } + + func connectInnerBases() { + self.leftBase.adjacencyList.append(self.rightBase) + self.leftBase.adjacencyList.append(self.centerBase) + self.rightBase.adjacencyList.append(self.leftBase) + self.rightBase.adjacencyList.append(self.centerBase) + self.centerBase.adjacencyList.append(self.leftBase) + self.centerBase.adjacencyList.append(self.rightBase) + } + + func createInternalWays() { + ways.append(Way(fromBase: self.leftBase, toBase: self.rightBase)) + ways.append(Way(fromBase: self.centerBase, toBase: self.leftBase)) + ways.append(Way(fromBase: self.centerBase, toBase: self.rightBase)) + } + + func getAllBases() -> [Base] { + return self.bases + } + + func getInternalWays() -> [Way] { + return self.ways + } + + func getTopConnection() -> Base? { + return nil + } + + func getRightConnection() -> Base? { + return self.rightBase + } + + func getBottomConnection() -> Base? { + return self.leftBase + } + + func getLeftConnection() -> Base? { + return self.leftBase + } +} + +class CElement15: CenterElementProtocol { + var bases: [Base] = [] + var centerBase: Base + var leftBase: Base + var rightBase: Base + var ways: [Way] = [] + + required init(frame: CGRect) { + self.centerBase = Base( + position: CGPoint( + x: frame.midX, + y: frame.minY + ) + ) + + self.leftBase = Base( + position: CGPoint( + x: frame.minX, + y: frame.maxY + ) + ) + + self.rightBase = Base( + position: CGPoint( + x: frame.maxX, + y: frame.maxY + ) + ) + self.bases.append(centerBase) + self.bases.append(leftBase) + self.bases.append(rightBase) + + self.connectInnerBases() + self.createInternalWays() + } + + func connectInnerBases() { + self.leftBase.adjacencyList.append(self.rightBase) + self.leftBase.adjacencyList.append(self.centerBase) + self.rightBase.adjacencyList.append(self.leftBase) + self.rightBase.adjacencyList.append(self.centerBase) + self.centerBase.adjacencyList.append(self.leftBase) + self.centerBase.adjacencyList.append(self.rightBase) + } + + func createInternalWays() { + ways.append(Way(fromBase: self.leftBase, toBase: self.rightBase)) + ways.append(Way(fromBase: self.centerBase, toBase: self.rightBase)) + ways.append(Way(fromBase: self.centerBase, toBase: self.leftBase)) + } + + func getAllBases() -> [Base] { + return self.bases + } + + func getInternalWays() -> [Way] { + return self.ways + } + + func getTopConnection() -> Base? { + return self.centerBase + } + + func getRightConnection() -> Base? { + return self.rightBase + } + + func getBottomConnection() -> Base? { + return nil + } + + func getLeftConnection() -> Base? { + return self.leftBase + } +} + +class CElement16: CenterElementProtocol { + var bases: [Base] = [] + var topBase: Base + var bottomBase: Base + var leftBase: Base + var rightBase: Base + var ways: [Way] = [] + + required init(frame: CGRect) { + self.topBase = Base( + position: CGPoint( + x: frame.midX, + y: frame.minY + ) + ) + + self.bottomBase = Base( + position: CGPoint( + x: frame.midX, + y: frame.maxY + ) + ) + + self.leftBase = Base( + position: CGPoint( + x: frame.minX, + y: frame.midY + ) + ) + + self.rightBase = Base( + position: CGPoint( + x: frame.maxX, + y: frame.midY + ) + ) + self.bases.append(topBase) + self.bases.append(bottomBase) + self.bases.append(leftBase) + self.bases.append(rightBase) + + self.connectInnerBases() + self.createInternalWays() + } + + func connectInnerBases() { + self.leftBase.adjacencyList.append(self.rightBase) + self.leftBase.adjacencyList.append(self.topBase) + self.leftBase.adjacencyList.append(self.bottomBase) + self.rightBase.adjacencyList.append(self.leftBase) + self.rightBase.adjacencyList.append(self.topBase) + self.rightBase.adjacencyList.append(self.bottomBase) + self.topBase.adjacencyList.append(self.leftBase) + self.topBase.adjacencyList.append(self.rightBase) + self.topBase.adjacencyList.append(self.bottomBase) + self.bottomBase.adjacencyList.append(self.topBase) + self.bottomBase.adjacencyList.append(self.rightBase) + self.bottomBase.adjacencyList.append(self.leftBase) + } + + func createInternalWays() { + ways.append(Way(fromBase: self.topBase, toBase: self.rightBase)) + ways.append(Way(fromBase: self.topBase, toBase: self.leftBase)) + ways.append(Way(fromBase: self.bottomBase, toBase: self.rightBase)) + ways.append(Way(fromBase: self.bottomBase, toBase: self.leftBase)) + } + + func getAllBases() -> [Base] { + return self.bases + } + + func getInternalWays() -> [Way] { + return self.ways + } + + func getTopConnection() -> Base? { + return self.topBase + } + + func getRightConnection() -> Base? { + return self.rightBase + } + + func getBottomConnection() -> Base? { + return nil + } + + func getLeftConnection() -> Base? { + return self.leftBase + } +} + +class CElement17: CenterElementProtocol { + var bases: [Base] = [] + var topBase: Base + var bottomBase: Base + var leftBase: Base + var rightBase: Base + var ways: [Way] = [] + + required init(frame: CGRect) { + self.topBase = Base( + position: CGPoint( + x: frame.midX, + y: frame.minY + ) + ) + + self.bottomBase = Base( + position: CGPoint( + x: frame.midX, + y: frame.maxY + ) + ) + + self.leftBase = Base( + position: CGPoint( + x: frame.minX, + y: frame.midY + ) + ) + + self.rightBase = Base( + position: CGPoint( + x: frame.maxX, + y: frame.midY + ) + ) + self.bases.append(topBase) + self.bases.append(bottomBase) + self.bases.append(leftBase) + self.bases.append(rightBase) + + self.connectInnerBases() + self.createInternalWays() + } + + func connectInnerBases() { + self.leftBase.adjacencyList.append(self.rightBase) + self.leftBase.adjacencyList.append(self.topBase) + self.leftBase.adjacencyList.append(self.bottomBase) + self.rightBase.adjacencyList.append(self.leftBase) + self.rightBase.adjacencyList.append(self.topBase) + self.rightBase.adjacencyList.append(self.bottomBase) + self.topBase.adjacencyList.append(self.leftBase) + self.topBase.adjacencyList.append(self.rightBase) + self.topBase.adjacencyList.append(self.bottomBase) + self.bottomBase.adjacencyList.append(self.topBase) + self.bottomBase.adjacencyList.append(self.rightBase) + self.bottomBase.adjacencyList.append(self.leftBase) + } + + func createInternalWays() { + ways.append(Way(fromBase: self.topBase, toBase: self.rightBase)) + ways.append(Way(fromBase: self.topBase, toBase: self.leftBase)) + ways.append(Way(fromBase: self.bottomBase, toBase: self.rightBase)) + ways.append(Way(fromBase: self.bottomBase, toBase: self.leftBase)) + } + + func getAllBases() -> [Base] { + return self.bases + } + + func getInternalWays() -> [Way] { + return self.ways + } + + func getTopConnection() -> Base? { + return nil + } + + func getRightConnection() -> Base? { + return self.rightBase + } + + func getBottomConnection() -> Base? { + return self.bottomBase + } + + func getLeftConnection() -> Base? { + return self.leftBase + } +} + +class CElement18: CenterElementProtocol { + var bases: [Base] = [] + var topBase: Base + var bottomBase: Base + var leftBase: Base + var rightBase: Base + var ways: [Way] = [] + + required init(frame: CGRect) { + self.topBase = Base( + position: CGPoint( + x: frame.midX, + y: frame.minY + ) + ) + + self.bottomBase = Base( + position: CGPoint( + x: frame.midX, + y: frame.maxY + ) + ) + + self.leftBase = Base( + position: CGPoint( + x: frame.minX, + y: frame.midY + ) + ) + + self.rightBase = Base( + position: CGPoint( + x: frame.maxX, + y: frame.midY + ) + ) + self.bases.append(topBase) + self.bases.append(bottomBase) + self.bases.append(leftBase) + self.bases.append(rightBase) + + self.connectInnerBases() + self.createInternalWays() + } + + func connectInnerBases() { + self.leftBase.adjacencyList.append(self.rightBase) + self.leftBase.adjacencyList.append(self.topBase) + self.leftBase.adjacencyList.append(self.bottomBase) + self.rightBase.adjacencyList.append(self.leftBase) + self.rightBase.adjacencyList.append(self.topBase) + self.rightBase.adjacencyList.append(self.bottomBase) + self.topBase.adjacencyList.append(self.leftBase) + self.topBase.adjacencyList.append(self.rightBase) + self.topBase.adjacencyList.append(self.bottomBase) + self.bottomBase.adjacencyList.append(self.topBase) + self.bottomBase.adjacencyList.append(self.rightBase) + self.bottomBase.adjacencyList.append(self.leftBase) + } + + func createInternalWays() { + ways.append(Way(fromBase: self.topBase, toBase: self.rightBase)) + ways.append(Way(fromBase: self.topBase, toBase: self.leftBase)) + ways.append(Way(fromBase: self.bottomBase, toBase: self.rightBase)) + ways.append(Way(fromBase: self.bottomBase, toBase: self.leftBase)) + } + + func getAllBases() -> [Base] { + return self.bases + } + + func getInternalWays() -> [Way] { + return self.ways + } + + func getTopConnection() -> Base? { + return nil + } + + func getRightConnection() -> Base? { + return self.rightBase + } + + func getBottomConnection() -> Base? { + return nil + } + + func getLeftConnection() -> Base? { + return self.leftBase + } +} diff --git a/GoldWars/GoldWars/Map/MapFactory.swift b/GoldWars/GoldWars/Map/MapFactory.swift index 3a6ac77..7da0ea5 100644 --- a/GoldWars/GoldWars/Map/MapFactory.swift +++ b/GoldWars/GoldWars/Map/MapFactory.swift @@ -21,9 +21,13 @@ protocol MapProtocol { protocol CenterElementProtocol { + var bases: [Base] {get set} + var ways: [Way] {get set} + init(frame: CGRect) func getAllBases() -> [Base] + func getInternalWays() -> [Way] func getTopConnection() -> Base? func getRightConnection() -> Base? func getBottomConnection() -> Base? @@ -64,12 +68,27 @@ class CenterElementProvider { static let LOG = OSLog.init(subsystem: "MapGenerator", category: "CenterElementProvider") static func get(frame: CGRect) -> CenterElementProtocol { - // TODO: Implemented by Jakob os_log("Getting new center element from provider", log: LOG, type: .info) let centerElements: [CenterElementProtocol.Type] = [ - SampleCElement.self, - CElement7.self + CElement1.self, + CElement2.self, + CElement3.self, + CElement4.self, + CElement5.self, + CElement6.self, + CElement7.self, + CElement8.self, + CElement9.self, + CElement10.self, + CElement11.self, + CElement12.self, + CElement13.self, + CElement14.self, + CElement15.self, + CElement16.self, + CElement17.self, + CElement18.self ] return centerElements.randomElement()!.init(frame: frame) diff --git a/GoldWars/GoldWars/Map/SampleCElement.swift b/GoldWars/GoldWars/Map/SampleCElement.swift index a15f748..e71d78c 100644 --- a/GoldWars/GoldWars/Map/SampleCElement.swift +++ b/GoldWars/GoldWars/Map/SampleCElement.swift @@ -57,7 +57,7 @@ class SampleCElement : CenterElementProtocol { import Foundation import SpriteKit -class CElement7 : CenterElementProtocol { +class CElement1700 : CenterElementProtocol { var frame: CGRect var bases: [Base] = [] var topBase: Base diff --git a/GoldWars/GoldWars/Map/TwoPlayerMapGenerator.swift b/GoldWars/GoldWars/Map/TwoPlayerMapGenerator.swift index f6cd9b1..776dddb 100644 --- a/GoldWars/GoldWars/Map/TwoPlayerMapGenerator.swift +++ b/GoldWars/GoldWars/Map/TwoPlayerMapGenerator.swift @@ -38,15 +38,23 @@ class TwoPlayerMapGenerator: MapProtocol { let p1StartBaseCount = Int.random(in: 1...3) var p1StartBases = [String: Base]() + var ways = [Way]() + if (p1StartBaseCount == 1) { p1StartBases["mid"] = Base(position: CGPoint(x: self.size.width * 0.2, y: self.size.height * 0.5)) + ways.append(Way(fromBase: basePlayerOne, toBase: p1StartBases["mid"]!)) } else if (p1StartBaseCount == 2) { p1StartBases["top"] = Base(position: CGPoint(x: self.size.width * 0.2, y: self.size.height * 0.333)) p1StartBases["bot"] = Base(position: CGPoint(x: self.size.width * 0.2, y: self.size.height * 0.666)) + ways.append(Way(fromBase: basePlayerOne, toBase: p1StartBases["top"]!)) + ways.append(Way(fromBase: basePlayerOne, toBase: p1StartBases["bot"]!)) } else { p1StartBases["top"] = Base(position: CGPoint(x: self.size.width * 0.2, y: self.size.height * 0.25)) p1StartBases["mid"] = Base(position: CGPoint(x: self.size.width * 0.2, y: self.size.height * 0.5)) p1StartBases["bot"] = Base(position: CGPoint(x: self.size.width * 0.2, y: self.size.height * 0.75)) + ways.append(Way(fromBase: basePlayerOne, toBase: p1StartBases["top"]!)) + ways.append(Way(fromBase: basePlayerOne, toBase: p1StartBases["mid"]!)) + ways.append(Way(fromBase: basePlayerOne, toBase: p1StartBases["bot"]!)) } os_log("Generate Map center", log: LOG, type: .info) @@ -63,15 +71,15 @@ class TwoPlayerMapGenerator: MapProtocol { ).insetBy(dx: cellInsetX, dy: cellInsetY) let gridTopRight = CGRect( - x: self.size.width * 0.3, - y: self.size.height * 0.5, + x: self.size.width * 0.5, + y: self.size.height * 0.1, width: gridCellWidth, height: gridCellHeight ).insetBy(dx: cellInsetX, dy: cellInsetY) let gridBottomLeft = CGRect( - x: self.size.width * 0.5, - y: self.size.height * 0.1, + x: self.size.width * 0.3, + y: self.size.height * 0.5, width: gridCellWidth, height: gridCellHeight ).insetBy(dx: cellInsetX, dy: cellInsetY) @@ -88,6 +96,11 @@ class TwoPlayerMapGenerator: MapProtocol { let bottomLeft = CenterElementProvider.get(frame: gridBottomLeft) let bottomRight = CenterElementProvider.get(frame: gridBottomRight) + ways.append(contentsOf: topLeft.getInternalWays()) + ways.append(contentsOf: topRight.getInternalWays()) + ways.append(contentsOf: bottomLeft.getInternalWays()) + ways.append(contentsOf: bottomRight.getInternalWays()) + os_log("Get player two base", log: LOG, type: .info) let basePlayerTwo = Base( position: CGPoint(x: self.size.width * 0.93, y: self.size.height / 2), @@ -101,13 +114,19 @@ class TwoPlayerMapGenerator: MapProtocol { if (p2StartBaseCount == 1) { p2StartBases["mid"] = Base(position: CGPoint(x: self.size.width * 0.8, y: self.size.height * 0.5)) + ways.append(Way(fromBase: basePlayerTwo, toBase: p2StartBases["mid"]!)) } else if (p1StartBaseCount == 2) { p2StartBases["top"] = Base(position: CGPoint(x: self.size.width * 0.8, y: self.size.height * 0.333)) p2StartBases["bot"] = Base(position: CGPoint(x: self.size.width * 0.8, y: self.size.height * 0.666)) + ways.append(Way(fromBase: basePlayerTwo, toBase: p2StartBases["top"]!)) + ways.append(Way(fromBase: basePlayerTwo, toBase: p2StartBases["bot"]!)) } else { p2StartBases["top"] = Base(position: CGPoint(x: self.size.width * 0.8, y: self.size.height * 0.25)) p2StartBases["mid"] = Base(position: CGPoint(x: self.size.width * 0.8, y: self.size.height * 0.5)) p2StartBases["bot"] = Base(position: CGPoint(x: self.size.width * 0.8, y: self.size.height * 0.75)) + ways.append(Way(fromBase: basePlayerTwo, toBase: p2StartBases["top"]!)) + ways.append(Way(fromBase: basePlayerTwo, toBase: p2StartBases["mid"]!)) + ways.append(Way(fromBase: basePlayerTwo, toBase: p2StartBases["bot"]!)) } // Create adjacency mapping @@ -119,6 +138,7 @@ class TwoPlayerMapGenerator: MapProtocol { os_log("Connecting p1 startbase top with topLeft segment", log: LOG, type: .info) p1StartBases["top"]!.adjacencyList.append(topLeft.getLeftConnection()!) topLeft.getLeftConnection()!.adjacencyList.append(p1StartBases["top"]!) + ways.append(Way(fromBase: p1StartBases["top"]!, toBase: topLeft.getLeftConnection()!)) } if (p1StartBases["mid"] != nil) { @@ -126,12 +146,14 @@ class TwoPlayerMapGenerator: MapProtocol { os_log("Connecting p1 startbase mid with topLeft segment", log: LOG, type: .info) p1StartBases["mid"]!.adjacencyList.append(topLeft.getLeftConnection()!) topLeft.getLeftConnection()!.adjacencyList.append(p1StartBases["mid"]!) + ways.append(Way(fromBase: p1StartBases["mid"]!, toBase: topLeft.getLeftConnection()!)) } if (bottomLeft.getLeftConnection() != nil) { os_log("Connecting p1 startbase mid with bottomLeft segment", log: LOG, type: .info) p1StartBases["mid"]!.adjacencyList.append(bottomLeft.getLeftConnection()!) bottomLeft.getLeftConnection()!.adjacencyList.append(p1StartBases["mid"]!) + ways.append(Way(fromBase: p1StartBases["mid"]!, toBase: bottomLeft.getLeftConnection()!)) } } @@ -139,30 +161,35 @@ class TwoPlayerMapGenerator: MapProtocol { os_log("Connecting p1 startbase bot with bottomLeft segment", log: LOG, type: .info) p1StartBases["bot"]!.adjacencyList.append(bottomLeft.getLeftConnection()!) bottomLeft.getLeftConnection()!.adjacencyList.append(p1StartBases["bot"]!) + ways.append(Way(fromBase: p1StartBases["bot"]!, toBase: bottomLeft.getLeftConnection()!)) } if (topLeft.getRightConnection() != nil && topRight.getLeftConnection() != nil) { os_log("Connecting topLeft with topRight segment horizontally", log: LOG, type: .info) topLeft.getRightConnection()!.adjacencyList.append(topRight.getLeftConnection()!) topRight.getLeftConnection()!.adjacencyList.append(topLeft.getRightConnection()!) + ways.append(Way(fromBase: topLeft.getRightConnection()!, toBase: topRight.getLeftConnection()!)) } if (bottomLeft.getRightConnection() != nil && bottomRight.getLeftConnection() != nil) { os_log("Connecting bottomLeft with bottomRight segment horizontally", log: LOG, type: .info) bottomLeft.getRightConnection()!.adjacencyList.append(bottomRight.getLeftConnection()!) bottomRight.getLeftConnection()!.adjacencyList.append(bottomLeft.getRightConnection()!) + ways.append(Way(fromBase: bottomLeft.getRightConnection()!, toBase: bottomRight.getLeftConnection()!)) } if (topLeft.getBottomConnection() != nil && bottomLeft.getTopConnection() != nil) { os_log("Connecting topLeft with bottomLeft segment vertically", log: LOG, type: .info) topLeft.getBottomConnection()!.adjacencyList.append(bottomLeft.getTopConnection()!) bottomLeft.getTopConnection()!.adjacencyList.append(topLeft.getBottomConnection()!) + ways.append(Way(fromBase: topLeft.getBottomConnection()!, toBase: bottomLeft.getTopConnection()!)) } if (topRight.getBottomConnection() != nil && bottomRight.getTopConnection() != nil) { os_log("Connecting topRight with bottomRight segment vertically", log: LOG, type: .info) topRight.getBottomConnection()!.adjacencyList.append(bottomRight.getTopConnection()!) bottomRight.getTopConnection()!.adjacencyList.append(topRight.getBottomConnection()!) + ways.append(Way(fromBase: topRight.getBottomConnection()!, toBase: bottomRight.getTopConnection()!)) } os_log("Connecting base p2 and start bases", log: LOG, type: .info) @@ -173,6 +200,7 @@ class TwoPlayerMapGenerator: MapProtocol { os_log("Connecting p2 startbase top with topRight segment", log: LOG, type: .info) p2StartBases["top"]!.adjacencyList.append(topRight.getRightConnection()!) topRight.getRightConnection()!.adjacencyList.append(p2StartBases["top"]!) + ways.append(Way(fromBase: p2StartBases["top"]!, toBase: topRight.getRightConnection()!)) } if (p2StartBases["mid"] != nil) { @@ -180,12 +208,14 @@ class TwoPlayerMapGenerator: MapProtocol { os_log("Connecting p2 startbase mid with topRight segment", log: LOG, type: .info) p2StartBases["mid"]!.adjacencyList.append(topRight.getRightConnection()!) topRight.getRightConnection()!.adjacencyList.append(p2StartBases["mid"]!) + ways.append(Way(fromBase: topRight.getRightConnection()!, toBase: p2StartBases["mid"]!)) } if (bottomRight.getRightConnection() != nil) { os_log("Connecting p2 startbase mid with bottomRight segment", log: LOG, type: .info) p2StartBases["mid"]!.adjacencyList.append(bottomRight.getRightConnection()!) bottomRight.getRightConnection()!.adjacencyList.append(p2StartBases["mid"]!) + ways.append(Way(fromBase: p2StartBases["mid"]!, toBase: bottomRight.getRightConnection()!)) } } @@ -193,6 +223,7 @@ class TwoPlayerMapGenerator: MapProtocol { os_log("Connecting p2 startbase bot with bottomRight segment", log: LOG, type: .info) p2StartBases["bot"]!.adjacencyList.append(bottomRight.getRightConnection()!) bottomRight.getRightConnection()!.adjacencyList.append(p2StartBases["bot"]!) + ways.append(Way(fromBase: p2StartBases["bot"]!, toBase: bottomRight.getRightConnection()!)) } @@ -208,6 +239,12 @@ class TwoPlayerMapGenerator: MapProtocol { entityManager.add(basePlayerTwo) p2StartBases.forEach({(key, base) in entityManager.add(base)}) + ways.forEach({way in entityManager.add(way)}) } + + func createPathFromBaseToBase(fromBase: Base, toBase: Base) { + let path = Way(fromBase: fromBase, toBase: toBase) + entityManager.add(path) + } } From 0fd409c00b37b08bd270a5aaf53a43b4a9d4e91c Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Wed, 20 May 2020 17:54:05 +0200 Subject: [PATCH 4/6] Add Ids to C-Elements and corresponding protocol --- GoldWars/GoldWars.xcodeproj/project.pbxproj | 9 +- .../Map/{CElemente.swift => CElements.swift} | 674 +++++++++--------- GoldWars/GoldWars/Map/MapFactory.swift | 1 + 3 files changed, 351 insertions(+), 333 deletions(-) rename GoldWars/GoldWars/Map/{CElemente.swift => CElements.swift} (98%) diff --git a/GoldWars/GoldWars.xcodeproj/project.pbxproj b/GoldWars/GoldWars.xcodeproj/project.pbxproj index 0462ade..fcbd525 100644 --- a/GoldWars/GoldWars.xcodeproj/project.pbxproj +++ b/GoldWars/GoldWars.xcodeproj/project.pbxproj @@ -18,7 +18,7 @@ 116060F7245C57D2004E5A36 /* EntityManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 116060F6245C57D2004E5A36 /* EntityManager.swift */; }; 11738A3B24508F68004426F1 /* Unit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11738A3A24508F68004426F1 /* Unit.swift */; }; 2086465C2461B66200817C23 /* TimerComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2086465B2461B66200817C23 /* TimerComponent.swift */; }; - 3E67854024728368007B9DE4 /* CElemente.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E67853F24728368007B9DE4 /* CElemente.swift */; }; + 3E67854024728368007B9DE4 /* CElements.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E67853F24728368007B9DE4 /* CElements.swift */; }; 3E6785422472CBEC007B9DE4 /* Way.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E6785412472CBEC007B9DE4 /* Way.swift */; }; 3E6785442472CC27007B9DE4 /* DefaultWayComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E6785432472CC27007B9DE4 /* DefaultWayComponent.swift */; }; 3EBD242C245D8044003CECE7 /* GameCenterHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3EBD242B245D8044003CECE7 /* GameCenterHelper.swift */; }; @@ -91,7 +91,7 @@ 116060F6245C57D2004E5A36 /* EntityManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EntityManager.swift; sourceTree = ""; }; 11738A3A24508F68004426F1 /* Unit.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Unit.swift; sourceTree = ""; }; 2086465B2461B66200817C23 /* TimerComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimerComponent.swift; sourceTree = ""; }; - 3E67853F24728368007B9DE4 /* CElemente.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CElemente.swift; sourceTree = ""; }; + 3E67853F24728368007B9DE4 /* CElements.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CElements.swift; sourceTree = ""; }; 3E6785412472CBEC007B9DE4 /* Way.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Way.swift; sourceTree = ""; }; 3E6785432472CC27007B9DE4 /* DefaultWayComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DefaultWayComponent.swift; sourceTree = ""; }; 3EBD242B245D8044003CECE7 /* GameCenterHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GameCenterHelper.swift; sourceTree = ""; }; @@ -305,7 +305,7 @@ AB1D759E245DD2EA00671525 /* Map */ = { isa = PBXGroup; children = ( - 3E67853F24728368007B9DE4 /* CElemente.swift */, + 3E67853F24728368007B9DE4 /* CElements.swift */, AB1D759F245DEC0500671525 /* MapFactory.swift */, AB1D759B245DD18100671525 /* TwoPlayerDefaultTestMap.swift */, AB21D7D4246C748A00B09CBA /* TwoPlayerMapGenerator.swift */, @@ -446,12 +446,11 @@ C064E9A8246C0EA50022B228 /* LabelNode.swift in Sources */, 3EBD242C245D8044003CECE7 /* GameCenterHelper.swift in Sources */, 3F745DF0246F48FC00CE7375 /* PlayerMoveType.swift in Sources */, - AB1D759C245DD18100671525 /* MapProtocol.swift in Sources */, AEBF3B01246EB187004F7CD5 /* CancelBtnComponent.swift in Sources */, AB1D75A0245DEC0500671525 /* MapFactory.swift in Sources */, AB1D759D245DD18100671525 /* TwoPlayerDefaultTestMap.swift in Sources */, 9EBFD7552462CF5A00E1E219 /* SliderComponent.swift in Sources */, - 3E67854024728368007B9DE4 /* CElemente.swift in Sources */, + 3E67854024728368007B9DE4 /* CElements.swift in Sources */, ABA03DA0244BD54F00A66916 /* Base.swift in Sources */, C064E9AC246C151F0022B228 /* Label.swift in Sources */, 9E174C82245DD81D00209FF0 /* ButtonNode.swift in Sources */, diff --git a/GoldWars/GoldWars/Map/CElemente.swift b/GoldWars/GoldWars/Map/CElements.swift similarity index 98% rename from GoldWars/GoldWars/Map/CElemente.swift rename to GoldWars/GoldWars/Map/CElements.swift index 409ed30..22fc4dc 100644 --- a/GoldWars/GoldWars/Map/CElemente.swift +++ b/GoldWars/GoldWars/Map/CElements.swift @@ -8,11 +8,12 @@ import SpriteKit -class CElement1: CenterElementProtocol { +class CElement0: CenterElementProtocol { var bases: [Base] = [] var centerBase: Base var ways: [Way] = [] + var id: Int = 0 required init(frame: CGRect) { self.centerBase = Base( @@ -49,11 +50,12 @@ class CElement1: CenterElementProtocol { } } -class CElement2: CenterElementProtocol { +class CElement1: CenterElementProtocol { var bases: [Base] = [] var leftBase: Base var rightBase: Base var ways: [Way] = [] + var id: Int = 1 required init(frame: CGRect) { self.leftBase = Base( @@ -110,73 +112,75 @@ class CElement2: CenterElementProtocol { } } +class CElement2: CenterElementProtocol { + var bases: [Base] = [] + var leftBase: Base + var rightBase: Base + var ways: [Way] = [] + var id: Int = 2 + + required init(frame: CGRect) { + self.leftBase = Base( + position: CGPoint( + x: frame.minX, + y: frame.midY + ) + ) + + self.rightBase = Base( + position: CGPoint( + x: frame.maxX, + y: frame.midY + ) + ) + self.bases.append(leftBase) + self.bases.append(rightBase) + + self.connectInnerBases() + self.createInternalWays() + } + + func connectInnerBases() { + self.leftBase.adjacencyList.append(self.rightBase) + self.rightBase.adjacencyList.append(self.leftBase) + } + + func createInternalWays() { + ways.append(Way(fromBase: self.leftBase, toBase: self.rightBase)) + } + + func getAllBases() -> [Base] { + return self.bases + } + + func getInternalWays() -> [Way] { + return self.ways + } + + func getTopConnection() -> Base? { + return self.rightBase + } + + func getRightConnection() -> Base? { + return self.rightBase + } + + func getBottomConnection() -> Base? { + return self.leftBase + } + + func getLeftConnection() -> Base? { + return self.leftBase + } +} + class CElement3: CenterElementProtocol { - var bases: [Base] = [] - var leftBase: Base - var rightBase: Base - var ways: [Way] = [] - - required init(frame: CGRect) { - self.leftBase = Base( - position: CGPoint( - x: frame.minX, - y: frame.midY - ) - ) - - self.rightBase = Base( - position: CGPoint( - x: frame.maxX, - y: frame.midY - ) - ) - self.bases.append(leftBase) - self.bases.append(rightBase) - - self.connectInnerBases() - self.createInternalWays() - } - - func connectInnerBases() { - self.leftBase.adjacencyList.append(self.rightBase) - self.rightBase.adjacencyList.append(self.leftBase) - } - - func createInternalWays() { - ways.append(Way(fromBase: self.leftBase, toBase: self.rightBase)) - } - - func getAllBases() -> [Base] { - return self.bases - } - - func getInternalWays() -> [Way] { - return self.ways - } - - func getTopConnection() -> Base? { - return self.rightBase - } - - func getRightConnection() -> Base? { - return self.rightBase - } - - func getBottomConnection() -> Base? { - return self.leftBase - } - - func getLeftConnection() -> Base? { - return self.leftBase - } -} - -class CElement4: CenterElementProtocol { var bases: [Base] = [] var centerBase: Base var leftBase: Base var rightBase: Base var ways: [Way] = [] + var id: Int = 3 required init(frame: CGRect) { self.centerBase = Base( @@ -247,32 +251,110 @@ class CElement4: CenterElementProtocol { } } +class CElement4: CenterElementProtocol { + var bases: [Base] = [] + var centerBase: Base + var leftBase: Base + var rightBase: Base + var ways: [Way] = [] + var id: Int = 4 + + required init(frame: CGRect) { + self.centerBase = Base( + position: CGPoint( + x: frame.midX, + y: frame.maxY + ) + ) + + self.leftBase = Base( + position: CGPoint( + x: frame.minX, + y: frame.minY + ) + ) + + self.rightBase = Base( + position: CGPoint( + x: frame.maxX, + y: frame.minY + ) + ) + self.bases.append(centerBase) + self.bases.append(leftBase) + self.bases.append(rightBase) + + self.connectInnerBases() + self.createInternalWays() + } + + func connectInnerBases() { + self.leftBase.adjacencyList.append(self.rightBase) + self.leftBase.adjacencyList.append(self.centerBase) + self.rightBase.adjacencyList.append(self.leftBase) + self.rightBase.adjacencyList.append(self.centerBase) + self.centerBase.adjacencyList.append(self.leftBase) + self.centerBase.adjacencyList.append(self.rightBase) + } + + func createInternalWays() { + ways.append(Way(fromBase: self.leftBase, toBase: self.rightBase)) + ways.append(Way(fromBase: self.centerBase, toBase: self.rightBase)) + ways.append(Way(fromBase: self.centerBase, toBase: self.leftBase)) + } + + func getAllBases() -> [Base] { + return self.bases + } + + func getInternalWays() -> [Way] { + return self.ways + } + + func getTopConnection() -> Base? { + return self.rightBase + } + + func getRightConnection() -> Base? { + return self.rightBase + } + + func getBottomConnection() -> Base? { + return self.centerBase + } + + func getLeftConnection() -> Base? { + return self.leftBase + } +} + class CElement5: CenterElementProtocol { var bases: [Base] = [] var centerBase: Base var leftBase: Base var rightBase: Base var ways: [Way] = [] + var id: Int = 5 required init(frame: CGRect) { self.centerBase = Base( position: CGPoint( x: frame.midX, - y: frame.maxY + y: frame.minY ) ) self.leftBase = Base( position: CGPoint( x: frame.minX, - y: frame.minY + y: frame.maxY ) ) self.rightBase = Base( position: CGPoint( x: frame.maxX, - y: frame.minY + y: frame.maxY ) ) self.bases.append(centerBase) @@ -307,7 +389,7 @@ class CElement5: CenterElementProtocol { } func getTopConnection() -> Base? { - return self.rightBase + return self.centerBase } func getRightConnection() -> Base? { @@ -315,7 +397,7 @@ class CElement5: CenterElementProtocol { } func getBottomConnection() -> Base? { - return self.centerBase + return self.leftBase } func getLeftConnection() -> Base? { @@ -329,6 +411,7 @@ class CElement6: CenterElementProtocol { var leftBase: Base var rightBase: Base var ways: [Way] = [] + var id: Int = 6 required init(frame: CGRect) { self.centerBase = Base( @@ -391,7 +474,7 @@ class CElement6: CenterElementProtocol { } func getBottomConnection() -> Base? { - return self.leftBase + return self.rightBase } func getLeftConnection() -> Base? { @@ -400,88 +483,13 @@ class CElement6: CenterElementProtocol { } class CElement7: CenterElementProtocol { - var bases: [Base] = [] - var centerBase: Base - var leftBase: Base - var rightBase: Base - var ways: [Way] = [] - - required init(frame: CGRect) { - self.centerBase = Base( - position: CGPoint( - x: frame.midX, - y: frame.minY - ) - ) - - self.leftBase = Base( - position: CGPoint( - x: frame.minX, - y: frame.maxY - ) - ) - - self.rightBase = Base( - position: CGPoint( - x: frame.maxX, - y: frame.maxY - ) - ) - self.bases.append(centerBase) - self.bases.append(leftBase) - self.bases.append(rightBase) - - self.connectInnerBases() - self.createInternalWays() - } - - func connectInnerBases() { - self.leftBase.adjacencyList.append(self.rightBase) - self.leftBase.adjacencyList.append(self.centerBase) - self.rightBase.adjacencyList.append(self.leftBase) - self.rightBase.adjacencyList.append(self.centerBase) - self.centerBase.adjacencyList.append(self.leftBase) - self.centerBase.adjacencyList.append(self.rightBase) - } - - func createInternalWays() { - ways.append(Way(fromBase: self.leftBase, toBase: self.rightBase)) - ways.append(Way(fromBase: self.centerBase, toBase: self.rightBase)) - ways.append(Way(fromBase: self.centerBase, toBase: self.leftBase)) - } - - func getAllBases() -> [Base] { - return self.bases - } - - func getInternalWays() -> [Way] { - return self.ways - } - - func getTopConnection() -> Base? { - return self.centerBase - } - - func getRightConnection() -> Base? { - return self.rightBase - } - - func getBottomConnection() -> Base? { - return self.rightBase - } - - func getLeftConnection() -> Base? { - return self.leftBase - } -} - -class CElement8: CenterElementProtocol { var bases: [Base] = [] var topBase: Base var bottomBase: Base var leftBase: Base var rightBase: Base var ways: [Way] = [] + var id: Int = 7 required init(frame: CGRect) { self.topBase = Base( @@ -567,11 +575,12 @@ class CElement8: CenterElementProtocol { } } -class CElement9: CenterElementProtocol { +class CElement8: CenterElementProtocol { var bases: [Base] = [] var leftBase: Base var rightBase: Base var ways: [Way] = [] + var id: Int = 8 required init(frame: CGRect) { self.leftBase = Base( @@ -628,11 +637,74 @@ class CElement9: CenterElementProtocol { } } +class CElement9: CenterElementProtocol { + var bases: [Base] = [] + var leftBase: Base + var rightBase: Base + var ways: [Way] = [] + var id: Int = 9 + + required init(frame: CGRect) { + self.leftBase = Base( + position: CGPoint( + x: frame.minX, + y: frame.midY + ) + ) + + self.rightBase = Base( + position: CGPoint( + x: frame.maxX, + y: frame.midY + ) + ) + self.bases.append(leftBase) + self.bases.append(rightBase) + + self.connectInnerBases() + self.createInternalWays() + } + + func connectInnerBases() { + self.leftBase.adjacencyList.append(self.rightBase) + self.rightBase.adjacencyList.append(self.leftBase) + } + + func createInternalWays() { + ways.append(Way(fromBase: self.leftBase, toBase: self.rightBase)) + } + + func getAllBases() -> [Base] { + return self.bases + } + + func getInternalWays() -> [Way] { + return self.ways + } + + func getTopConnection() -> Base? { + return nil + } + + func getRightConnection() -> Base? { + return self.rightBase + } + + func getBottomConnection() -> Base? { + return self.rightBase + } + + func getLeftConnection() -> Base? { + return self.leftBase + } +} + class CElement10: CenterElementProtocol { var bases: [Base] = [] var leftBase: Base var rightBase: Base var ways: [Way] = [] + var id: Int = 10 required init(frame: CGRect) { self.leftBase = Base( @@ -673,7 +745,7 @@ class CElement10: CenterElementProtocol { } func getTopConnection() -> Base? { - return nil + return self.rightBase } func getRightConnection() -> Base? { @@ -681,7 +753,7 @@ class CElement10: CenterElementProtocol { } func getBottomConnection() -> Base? { - return self.rightBase + return nil } func getLeftConnection() -> Base? { @@ -691,24 +763,34 @@ class CElement10: CenterElementProtocol { class CElement11: CenterElementProtocol { var bases: [Base] = [] + var centerBase: Base var leftBase: Base var rightBase: Base var ways: [Way] = [] + var id: Int = 11 required init(frame: CGRect) { + self.centerBase = Base( + position: CGPoint( + x: frame.midX, + y: frame.maxY + ) + ) + self.leftBase = Base( position: CGPoint( x: frame.minX, - y: frame.midY + y: frame.minY ) ) self.rightBase = Base( position: CGPoint( x: frame.maxX, - y: frame.midY + y: frame.minY ) ) + self.bases.append(centerBase) self.bases.append(leftBase) self.bases.append(rightBase) @@ -718,11 +800,17 @@ class CElement11: CenterElementProtocol { func connectInnerBases() { self.leftBase.adjacencyList.append(self.rightBase) + self.leftBase.adjacencyList.append(self.centerBase) self.rightBase.adjacencyList.append(self.leftBase) + self.rightBase.adjacencyList.append(self.centerBase) + self.centerBase.adjacencyList.append(self.leftBase) + self.centerBase.adjacencyList.append(self.rightBase) } func createInternalWays() { ways.append(Way(fromBase: self.leftBase, toBase: self.rightBase)) + ways.append(Way(fromBase: self.centerBase, toBase: self.leftBase)) + ways.append(Way(fromBase: self.centerBase, toBase: self.rightBase)) } func getAllBases() -> [Base] { @@ -734,7 +822,7 @@ class CElement11: CenterElementProtocol { } func getTopConnection() -> Base? { - return self.rightBase + return nil } func getRightConnection() -> Base? { @@ -742,7 +830,7 @@ class CElement11: CenterElementProtocol { } func getBottomConnection() -> Base? { - return nil + return self.centerBase } func getLeftConnection() -> Base? { @@ -756,6 +844,7 @@ class CElement12: CenterElementProtocol { var leftBase: Base var rightBase: Base var ways: [Way] = [] + var id: Int = 12 required init(frame: CGRect) { self.centerBase = Base( @@ -810,7 +899,7 @@ class CElement12: CenterElementProtocol { } func getTopConnection() -> Base? { - return nil + return self.rightBase } func getRightConnection() -> Base? { @@ -818,7 +907,7 @@ class CElement12: CenterElementProtocol { } func getBottomConnection() -> Base? { - return self.centerBase + return nil } func getLeftConnection() -> Base? { @@ -832,26 +921,27 @@ class CElement13: CenterElementProtocol { var leftBase: Base var rightBase: Base var ways: [Way] = [] + var id: Int = 13 required init(frame: CGRect) { self.centerBase = Base( position: CGPoint( x: frame.midX, - y: frame.maxY + y: frame.minY ) ) self.leftBase = Base( position: CGPoint( x: frame.minX, - y: frame.minY + y: frame.maxY ) ) self.rightBase = Base( position: CGPoint( x: frame.maxX, - y: frame.minY + y: frame.maxY ) ) self.bases.append(centerBase) @@ -886,7 +976,7 @@ class CElement13: CenterElementProtocol { } func getTopConnection() -> Base? { - return self.rightBase + return nil } func getRightConnection() -> Base? { @@ -894,7 +984,7 @@ class CElement13: CenterElementProtocol { } func getBottomConnection() -> Base? { - return nil + return self.leftBase } func getLeftConnection() -> Base? { @@ -908,82 +998,7 @@ class CElement14: CenterElementProtocol { var leftBase: Base var rightBase: Base var ways: [Way] = [] - - required init(frame: CGRect) { - self.centerBase = Base( - position: CGPoint( - x: frame.midX, - y: frame.minY - ) - ) - - self.leftBase = Base( - position: CGPoint( - x: frame.minX, - y: frame.maxY - ) - ) - - self.rightBase = Base( - position: CGPoint( - x: frame.maxX, - y: frame.maxY - ) - ) - self.bases.append(centerBase) - self.bases.append(leftBase) - self.bases.append(rightBase) - - self.connectInnerBases() - self.createInternalWays() - } - - func connectInnerBases() { - self.leftBase.adjacencyList.append(self.rightBase) - self.leftBase.adjacencyList.append(self.centerBase) - self.rightBase.adjacencyList.append(self.leftBase) - self.rightBase.adjacencyList.append(self.centerBase) - self.centerBase.adjacencyList.append(self.leftBase) - self.centerBase.adjacencyList.append(self.rightBase) - } - - func createInternalWays() { - ways.append(Way(fromBase: self.leftBase, toBase: self.rightBase)) - ways.append(Way(fromBase: self.centerBase, toBase: self.leftBase)) - ways.append(Way(fromBase: self.centerBase, toBase: self.rightBase)) - } - - func getAllBases() -> [Base] { - return self.bases - } - - func getInternalWays() -> [Way] { - return self.ways - } - - func getTopConnection() -> Base? { - return nil - } - - func getRightConnection() -> Base? { - return self.rightBase - } - - func getBottomConnection() -> Base? { - return self.leftBase - } - - func getLeftConnection() -> Base? { - return self.leftBase - } -} - -class CElement15: CenterElementProtocol { - var bases: [Base] = [] - var centerBase: Base - var leftBase: Base - var rightBase: Base - var ways: [Way] = [] + var id: Int = 14 required init(frame: CGRect) { self.centerBase = Base( @@ -1054,6 +1069,99 @@ class CElement15: CenterElementProtocol { } } +class CElement15: CenterElementProtocol { + var bases: [Base] = [] + var topBase: Base + var bottomBase: Base + var leftBase: Base + var rightBase: Base + var ways: [Way] = [] + var id: Int = 15 + + required init(frame: CGRect) { + self.topBase = Base( + position: CGPoint( + x: frame.midX, + y: frame.minY + ) + ) + + self.bottomBase = Base( + position: CGPoint( + x: frame.midX, + y: frame.maxY + ) + ) + + self.leftBase = Base( + position: CGPoint( + x: frame.minX, + y: frame.midY + ) + ) + + self.rightBase = Base( + position: CGPoint( + x: frame.maxX, + y: frame.midY + ) + ) + self.bases.append(topBase) + self.bases.append(bottomBase) + self.bases.append(leftBase) + self.bases.append(rightBase) + + self.connectInnerBases() + self.createInternalWays() + } + + func connectInnerBases() { + self.leftBase.adjacencyList.append(self.rightBase) + self.leftBase.adjacencyList.append(self.topBase) + self.leftBase.adjacencyList.append(self.bottomBase) + self.rightBase.adjacencyList.append(self.leftBase) + self.rightBase.adjacencyList.append(self.topBase) + self.rightBase.adjacencyList.append(self.bottomBase) + self.topBase.adjacencyList.append(self.leftBase) + self.topBase.adjacencyList.append(self.rightBase) + self.topBase.adjacencyList.append(self.bottomBase) + self.bottomBase.adjacencyList.append(self.topBase) + self.bottomBase.adjacencyList.append(self.rightBase) + self.bottomBase.adjacencyList.append(self.leftBase) + } + + func createInternalWays() { + ways.append(Way(fromBase: self.topBase, toBase: self.rightBase)) + ways.append(Way(fromBase: self.topBase, toBase: self.leftBase)) + ways.append(Way(fromBase: self.bottomBase, toBase: self.rightBase)) + ways.append(Way(fromBase: self.bottomBase, toBase: self.leftBase)) + } + + func getAllBases() -> [Base] { + return self.bases + } + + func getInternalWays() -> [Way] { + return self.ways + } + + func getTopConnection() -> Base? { + return self.topBase + } + + func getRightConnection() -> Base? { + return self.rightBase + } + + func getBottomConnection() -> Base? { + return nil + } + + func getLeftConnection() -> Base? { + return self.leftBase + } +} + class CElement16: CenterElementProtocol { var bases: [Base] = [] var topBase: Base @@ -1061,98 +1169,7 @@ class CElement16: CenterElementProtocol { var leftBase: Base var rightBase: Base var ways: [Way] = [] - - required init(frame: CGRect) { - self.topBase = Base( - position: CGPoint( - x: frame.midX, - y: frame.minY - ) - ) - - self.bottomBase = Base( - position: CGPoint( - x: frame.midX, - y: frame.maxY - ) - ) - - self.leftBase = Base( - position: CGPoint( - x: frame.minX, - y: frame.midY - ) - ) - - self.rightBase = Base( - position: CGPoint( - x: frame.maxX, - y: frame.midY - ) - ) - self.bases.append(topBase) - self.bases.append(bottomBase) - self.bases.append(leftBase) - self.bases.append(rightBase) - - self.connectInnerBases() - self.createInternalWays() - } - - func connectInnerBases() { - self.leftBase.adjacencyList.append(self.rightBase) - self.leftBase.adjacencyList.append(self.topBase) - self.leftBase.adjacencyList.append(self.bottomBase) - self.rightBase.adjacencyList.append(self.leftBase) - self.rightBase.adjacencyList.append(self.topBase) - self.rightBase.adjacencyList.append(self.bottomBase) - self.topBase.adjacencyList.append(self.leftBase) - self.topBase.adjacencyList.append(self.rightBase) - self.topBase.adjacencyList.append(self.bottomBase) - self.bottomBase.adjacencyList.append(self.topBase) - self.bottomBase.adjacencyList.append(self.rightBase) - self.bottomBase.adjacencyList.append(self.leftBase) - } - - func createInternalWays() { - ways.append(Way(fromBase: self.topBase, toBase: self.rightBase)) - ways.append(Way(fromBase: self.topBase, toBase: self.leftBase)) - ways.append(Way(fromBase: self.bottomBase, toBase: self.rightBase)) - ways.append(Way(fromBase: self.bottomBase, toBase: self.leftBase)) - } - - func getAllBases() -> [Base] { - return self.bases - } - - func getInternalWays() -> [Way] { - return self.ways - } - - func getTopConnection() -> Base? { - return self.topBase - } - - func getRightConnection() -> Base? { - return self.rightBase - } - - func getBottomConnection() -> Base? { - return nil - } - - func getLeftConnection() -> Base? { - return self.leftBase - } -} - -class CElement17: CenterElementProtocol { - var bases: [Base] = [] - var topBase: Base - var bottomBase: Base - var leftBase: Base - var rightBase: Base - var ways: [Way] = [] + var id: Int = 16 required init(frame: CGRect) { self.topBase = Base( @@ -1238,13 +1255,14 @@ class CElement17: CenterElementProtocol { } } -class CElement18: CenterElementProtocol { +class CElement17: CenterElementProtocol { var bases: [Base] = [] var topBase: Base var bottomBase: Base var leftBase: Base var rightBase: Base var ways: [Way] = [] + var id: Int = 17 required init(frame: CGRect) { self.topBase = Base( diff --git a/GoldWars/GoldWars/Map/MapFactory.swift b/GoldWars/GoldWars/Map/MapFactory.swift index 7da0ea5..0aab86f 100644 --- a/GoldWars/GoldWars/Map/MapFactory.swift +++ b/GoldWars/GoldWars/Map/MapFactory.swift @@ -23,6 +23,7 @@ protocol CenterElementProtocol { var bases: [Base] {get set} var ways: [Way] {get set} + var id: Int { get } init(frame: CGRect) From 71f556868fa581c95fb43982354a658d4af45b23 Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Wed, 20 May 2020 18:42:57 +0200 Subject: [PATCH 5/6] Allow map to be loaded from predefined model --- GoldWars/GoldWars/Map/MapFactory.swift | 81 +++++++++---------- .../Map/TwoPlayerDefaultTestMap.swift | 2 +- .../GoldWars/Map/TwoPlayerMapGenerator.swift | 49 ++++++++--- GoldWars/GoldWars/Scenes/GameScene.swift | 2 +- 4 files changed, 74 insertions(+), 60 deletions(-) diff --git a/GoldWars/GoldWars/Map/MapFactory.swift b/GoldWars/GoldWars/Map/MapFactory.swift index 0aab86f..fd7dc3c 100644 --- a/GoldWars/GoldWars/Map/MapFactory.swift +++ b/GoldWars/GoldWars/Map/MapFactory.swift @@ -16,7 +16,7 @@ protocol MapProtocol { init(scene: SKScene, entityManager: EntityManager) - func load() + func load(withModel mapModel: MapGenerationModel) } protocol CenterElementProtocol { @@ -38,60 +38,51 @@ protocol CenterElementProtocol { class MapFactory { let LOG = OSLog.init(subsystem: "MapGenerator", category: "MapFactory") - var twoPlayerMaps: [MapProtocol]! - var threePlayerMaps: [MapProtocol]! - var fourPlayerMaps: [MapProtocol]! + var twoPlayerMapGenerator: TwoPlayerMapGenerator init(scene: SKScene, entityManager: EntityManager) { - self.twoPlayerMaps = [ - TwoPlayerMapGenerator(scene: scene, entityManager: entityManager) - ] - - self.threePlayerMaps = [MapProtocol]() - self.fourPlayerMaps = [MapProtocol]() + self.twoPlayerMapGenerator = TwoPlayerMapGenerator(scene: scene, entityManager: entityManager) } - func loadMap(playerCount: Int) { - if playerCount == 2 { - os_log("Loading two player map", log: LOG, type: .info) - twoPlayerMaps.randomElement()?.load() - } else if playerCount == 3 { - os_log("Loading three player map", log: LOG, type: .info) - threePlayerMaps.randomElement()?.load() - } else { - os_log("Loading four player map", log: LOG, type: .info) - fourPlayerMaps.randomElement()?.load() - } + func loadMap() -> MapGenerationModel{ + let mapModel = TwoPlayerMapGenerator.getNewMapModel() + loadMap(fromModel: mapModel) + return mapModel + } + + func loadMap(fromModel model: MapGenerationModel) { + os_log("Loading two player map", log: LOG, type: .info) + twoPlayerMapGenerator.load(withModel: model) } } class CenterElementProvider { static let LOG = OSLog.init(subsystem: "MapGenerator", category: "CenterElementProvider") - static func get(frame: CGRect) -> CenterElementProtocol { - os_log("Getting new center element from provider", log: LOG, type: .info) + static let centerElements: [CenterElementProtocol.Type] = [ + CElement0.self, + CElement1.self, + CElement2.self, + CElement3.self, + CElement4.self, + CElement5.self, + CElement6.self, + CElement7.self, + CElement8.self, + CElement9.self, + CElement10.self, + CElement11.self, + CElement12.self, + CElement13.self, + CElement14.self, + CElement15.self, + CElement16.self, + CElement17.self + ] + + static func get(inFrame frame: CGRect, withId id: Int) -> CenterElementProtocol { + os_log("Getting new predifined center element from provider", log: LOG, type: .info) - let centerElements: [CenterElementProtocol.Type] = [ - CElement1.self, - CElement2.self, - CElement3.self, - CElement4.self, - CElement5.self, - CElement6.self, - CElement7.self, - CElement8.self, - CElement9.self, - CElement10.self, - CElement11.self, - CElement12.self, - CElement13.self, - CElement14.self, - CElement15.self, - CElement16.self, - CElement17.self, - CElement18.self - ] - - return centerElements.randomElement()!.init(frame: frame) + return centerElements[id].init(frame: frame) } } diff --git a/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift b/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift index fa20823..5f67cd7 100644 --- a/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift +++ b/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift @@ -20,7 +20,7 @@ class TwoPlayerDefaultTestMap: MapProtocol { self.size = scene.size } - func load() { + func load(withModel mapModel: MapGenerationModel) { // Create Bases let basePlayerOne = Base( diff --git a/GoldWars/GoldWars/Map/TwoPlayerMapGenerator.swift b/GoldWars/GoldWars/Map/TwoPlayerMapGenerator.swift index 776dddb..d17add7 100644 --- a/GoldWars/GoldWars/Map/TwoPlayerMapGenerator.swift +++ b/GoldWars/GoldWars/Map/TwoPlayerMapGenerator.swift @@ -11,18 +11,45 @@ import SpriteKit import GameKit import os +struct MapGenerationModel: Codable { + + let numBasesP1: Int + let numBasesP2: Int + let topLeftId: Int + let topRightId: Int + let bottomLeftId: Int + let bottomRightId: Int + +} + class TwoPlayerMapGenerator: MapProtocol { + let LOG = OSLog.init(subsystem: "MapGenerator", category: "TwoPlayerMapGenerator") var size: CGSize! var entityManager: EntityManager! + static func getNewMapModel() -> MapGenerationModel { + + let noOfCElements = CenterElementProvider.centerElements.count + + return MapGenerationModel( + numBasesP1: Int.random(in: 1...3), + numBasesP2: Int.random(in: 1...3), + topLeftId: Int.random(in: 0...noOfCElements), + topRightId: Int.random(in: 0...noOfCElements), + bottomLeftId: Int.random(in: 0...noOfCElements), + bottomRightId: Int.random(in: 0...noOfCElements) + ) + + } + required init(scene: SKScene, entityManager: EntityManager) { self.size = scene.size self.entityManager = entityManager } - func load() { + func load(withModel mapModel: MapGenerationModel) { os_log("Loading from TwoPlayerMapFactory", log: LOG, type: .info) // Generate bases structure @@ -34,16 +61,13 @@ class TwoPlayerMapGenerator: MapProtocol { ) os_log("Get player one's startbases", log: LOG, type: .info) - - let p1StartBaseCount = Int.random(in: 1...3) var p1StartBases = [String: Base]() - var ways = [Way]() - if (p1StartBaseCount == 1) { + if (mapModel.numBasesP1 == 1) { p1StartBases["mid"] = Base(position: CGPoint(x: self.size.width * 0.2, y: self.size.height * 0.5)) ways.append(Way(fromBase: basePlayerOne, toBase: p1StartBases["mid"]!)) - } else if (p1StartBaseCount == 2) { + } else if (mapModel.numBasesP1 == 2) { p1StartBases["top"] = Base(position: CGPoint(x: self.size.width * 0.2, y: self.size.height * 0.333)) p1StartBases["bot"] = Base(position: CGPoint(x: self.size.width * 0.2, y: self.size.height * 0.666)) ways.append(Way(fromBase: basePlayerOne, toBase: p1StartBases["top"]!)) @@ -91,10 +115,10 @@ class TwoPlayerMapGenerator: MapProtocol { height: gridCellHeight ).insetBy(dx: cellInsetX, dy: cellInsetY) - let topLeft = CenterElementProvider.get(frame: gridTopLeft) - let topRight = CenterElementProvider.get(frame: gridTopRight) - let bottomLeft = CenterElementProvider.get(frame: gridBottomLeft) - let bottomRight = CenterElementProvider.get(frame: gridBottomRight) + let topLeft = CenterElementProvider.get(inFrame: gridTopLeft, withId: mapModel.topLeftId) + let topRight = CenterElementProvider.get(inFrame: gridTopRight, withId: mapModel.topRightId) + let bottomLeft = CenterElementProvider.get(inFrame: gridBottomLeft, withId: mapModel.bottomLeftId) + let bottomRight = CenterElementProvider.get(inFrame: gridBottomRight, withId: mapModel.bottomRightId) ways.append(contentsOf: topLeft.getInternalWays()) ways.append(contentsOf: topRight.getInternalWays()) @@ -109,13 +133,12 @@ class TwoPlayerMapGenerator: MapProtocol { ) os_log("Get player two's startbases", log: LOG, type: .info) - let p2StartBaseCount = Int.random(in: 1...3) var p2StartBases = [String: Base]() - if (p2StartBaseCount == 1) { + if (mapModel.numBasesP2 == 1) { p2StartBases["mid"] = Base(position: CGPoint(x: self.size.width * 0.8, y: self.size.height * 0.5)) ways.append(Way(fromBase: basePlayerTwo, toBase: p2StartBases["mid"]!)) - } else if (p1StartBaseCount == 2) { + } else if (mapModel.numBasesP2 == 2) { p2StartBases["top"] = Base(position: CGPoint(x: self.size.width * 0.8, y: self.size.height * 0.333)) p2StartBases["bot"] = Base(position: CGPoint(x: self.size.width * 0.8, y: self.size.height * 0.666)) ways.append(Way(fromBase: basePlayerTwo, toBase: p2StartBases["top"]!)) diff --git a/GoldWars/GoldWars/Scenes/GameScene.swift b/GoldWars/GoldWars/Scenes/GameScene.swift index 7b91850..8e9c7fa 100644 --- a/GoldWars/GoldWars/Scenes/GameScene.swift +++ b/GoldWars/GoldWars/Scenes/GameScene.swift @@ -26,7 +26,7 @@ class GameScene: SKScene{ } func initMap() { - MapFactory(scene: self, entityManager: EntityManager.sharedInstance).loadMap(playerCount: 2) + MapFactory(scene: self, entityManager: EntityManager.sharedInstance).loadMap(fromModel: MapGenerationModel(numBasesP1: 1, numBasesP2: 1, topLeftId: 3, topRightId: 3, bottomLeftId: 3, bottomRightId: 3)) DataService.sharedInstance.setSnapshotModel(snapshotModel: EntityManager.sharedInstance.getSnapshotModel()) } From fe1405d3faafbcb9fadca3990b7509a54e052a66 Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Wed, 20 May 2020 20:30:06 +0200 Subject: [PATCH 6/6] Send map model to other player over data service --- GoldWars/GoldWars/DataService.swift | 6 ++++++ GoldWars/GoldWars/Map/TwoPlayerMapGenerator.swift | 2 +- GoldWars/GoldWars/MatchmakingHelper.swift | 4 ++++ GoldWars/GoldWars/MultiplayerNetwork.swift | 6 ++++++ GoldWars/GoldWars/Scenes/GameScene.swift | 7 ++++++- 5 files changed, 23 insertions(+), 2 deletions(-) diff --git a/GoldWars/GoldWars/DataService.swift b/GoldWars/GoldWars/DataService.swift index e06dff6..d94a992 100644 --- a/GoldWars/GoldWars/DataService.swift +++ b/GoldWars/GoldWars/DataService.swift @@ -42,6 +42,7 @@ class DataService { var remotePlayerMoves: [String: [PlayerMove]] = [:] var snapshotModel: SnapshotModel? var gameHost: Host? + var mapModel: MapGenerationModel? func addMove(playerMove: PlayerMove) { var equalMove = localPlayerMoves.filter { (ele) -> Bool in @@ -69,4 +70,9 @@ class DataService { func setSnapshotModel(snapshotModel: SnapshotModel) { self.snapshotModel = snapshotModel } + + func setMapModel(model: MapGenerationModel) { + self.mapModel = model + MapFactory(scene: EntityManager.sharedInstance.scene, entityManager: EntityManager.sharedInstance).loadMap(fromModel: DataService.sharedInstance.mapModel!) + } } diff --git a/GoldWars/GoldWars/Map/TwoPlayerMapGenerator.swift b/GoldWars/GoldWars/Map/TwoPlayerMapGenerator.swift index d17add7..918bdf2 100644 --- a/GoldWars/GoldWars/Map/TwoPlayerMapGenerator.swift +++ b/GoldWars/GoldWars/Map/TwoPlayerMapGenerator.swift @@ -31,7 +31,7 @@ class TwoPlayerMapGenerator: MapProtocol { static func getNewMapModel() -> MapGenerationModel { - let noOfCElements = CenterElementProvider.centerElements.count + let noOfCElements = CenterElementProvider.centerElements.count - 1 return MapGenerationModel( numBasesP1: Int.random(in: 1...3), diff --git a/GoldWars/GoldWars/MatchmakingHelper.swift b/GoldWars/GoldWars/MatchmakingHelper.swift index a695d39..5e09ed6 100644 --- a/GoldWars/GoldWars/MatchmakingHelper.swift +++ b/GoldWars/GoldWars/MatchmakingHelper.swift @@ -122,6 +122,10 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe EntityManager.sharedInstance.updateSnapshotModel(snapshotModel: snapshotModel) } + if let mapModel = try? jsonDecoder.decode(MapGenerationModel.self, from: data) { + DataService.sharedInstance.setMapModel(model: mapModel) + } + MultiplayerNetwork.sharedInstance.isSending = false } diff --git a/GoldWars/GoldWars/MultiplayerNetwork.swift b/GoldWars/GoldWars/MultiplayerNetwork.swift index 7ffbc37..34cbccd 100644 --- a/GoldWars/GoldWars/MultiplayerNetwork.swift +++ b/GoldWars/GoldWars/MultiplayerNetwork.swift @@ -60,5 +60,11 @@ class MultiplayerNetwork{ let encoded = (try? encoder.encode(DataService.sharedInstance.snapshotModel))! sendData(data: encoded) } + + func sendMapModelToPlayers(mapModel: MapGenerationModel) { + let encoder = JSONEncoder() + let encoded = (try? encoder.encode(mapModel))! + sendData(data: encoded) + } } diff --git a/GoldWars/GoldWars/Scenes/GameScene.swift b/GoldWars/GoldWars/Scenes/GameScene.swift index 8e9c7fa..58a3d9d 100644 --- a/GoldWars/GoldWars/Scenes/GameScene.swift +++ b/GoldWars/GoldWars/Scenes/GameScene.swift @@ -26,7 +26,12 @@ class GameScene: SKScene{ } func initMap() { - MapFactory(scene: self, entityManager: EntityManager.sharedInstance).loadMap(fromModel: MapGenerationModel(numBasesP1: 1, numBasesP2: 1, topLeftId: 3, topRightId: 3, bottomLeftId: 3, bottomRightId: 3)) + + if (DataService.sharedInstance.gameHost?.playerName == GKLocalPlayer.local.displayName) { + let mapModel = MapFactory(scene: self, entityManager: EntityManager.sharedInstance).loadMap() + MultiplayerNetwork.sharedInstance.sendMapModelToPlayers(mapModel: mapModel) + } + DataService.sharedInstance.setSnapshotModel(snapshotModel: EntityManager.sharedInstance.getSnapshotModel()) }