From b210e04d38da8e39323a0c1834321a04c13d2ca2 Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Fri, 29 May 2020 16:09:42 +0200 Subject: [PATCH 1/6] Remove TestMap --- GoldWars/GoldWars.xcodeproj/project.pbxproj | 4 - GoldWars/GoldWars/Map/MapFactory.swift | 2 +- .../Map/TwoPlayerDefaultTestMap.swift | 84 ------------------- 3 files changed, 1 insertion(+), 89 deletions(-) delete mode 100644 GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift diff --git a/GoldWars/GoldWars.xcodeproj/project.pbxproj b/GoldWars/GoldWars.xcodeproj/project.pbxproj index e16e8ec..5261a4e 100644 --- a/GoldWars/GoldWars.xcodeproj/project.pbxproj +++ b/GoldWars/GoldWars.xcodeproj/project.pbxproj @@ -44,7 +44,6 @@ 9EEDE02D246FCD770096C735 /* SpinningLogoEntity.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EEDE02C246FCD770096C735 /* SpinningLogoEntity.swift */; }; 9EEDE02F246FCD800096C735 /* SpinningLogoComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EEDE02E246FCD800096C735 /* SpinningLogoComponent.swift */; }; AB0B88F6247AD89200C8DF66 /* SkillComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB0B88F5247AD89200C8DF66 /* SkillComponent.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 */; }; @@ -111,7 +110,6 @@ 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 = ""; }; AB0B88F5247AD89200C8DF66 /* SkillComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SkillComponent.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 = ""; }; @@ -286,7 +284,6 @@ children = ( 3E67853F24728368007B9DE4 /* CElements.swift */, AB1D759F245DEC0500671525 /* MapFactory.swift */, - AB1D759B245DD18100671525 /* TwoPlayerDefaultTestMap.swift */, AB21D7D4246C748A00B09CBA /* TwoPlayerMapGenerator.swift */, ); path = Map; @@ -422,7 +419,6 @@ C064E9A8246C0EA50022B228 /* LabelNode.swift in Sources */, 3F745DF0246F48FC00CE7375 /* PlayerMoveType.swift in Sources */, AB1D75A0245DEC0500671525 /* MapFactory.swift in Sources */, - AB1D759D245DD18100671525 /* TwoPlayerDefaultTestMap.swift in Sources */, 3E67854024728368007B9DE4 /* CElements.swift in Sources */, ABA03DA0244BD54F00A66916 /* Base.swift in Sources */, C064E9AC246C151F0022B228 /* Label.swift in Sources */, diff --git a/GoldWars/GoldWars/Map/MapFactory.swift b/GoldWars/GoldWars/Map/MapFactory.swift index fd7dc3c..3f28737 100644 --- a/GoldWars/GoldWars/Map/MapFactory.swift +++ b/GoldWars/GoldWars/Map/MapFactory.swift @@ -44,7 +44,7 @@ class MapFactory { self.twoPlayerMapGenerator = TwoPlayerMapGenerator(scene: scene, entityManager: entityManager) } - func loadMap() -> MapGenerationModel{ + func loadMap() -> MapGenerationModel { let mapModel = TwoPlayerMapGenerator.getNewMapModel() loadMap(fromModel: mapModel) return mapModel diff --git a/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift b/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift deleted file mode 100644 index 84e4a48..0000000 --- a/GoldWars/GoldWars/Map/TwoPlayerDefaultTestMap.swift +++ /dev/null @@ -1,84 +0,0 @@ -// -// DefaultTestMap.swift -// GoldWars -// -// Created by Marcel Schwarz on 02.05.20. -// Copyright © 2020 SP2. All rights reserved. -// - -import Foundation -import SpriteKit -import GameKit - -class TwoPlayerDefaultTestMap: MapProtocol { - - var entityManager: EntityManager! - var size: CGSize! - - required init(scene: SKScene, entityManager: EntityManager) { - self.entityManager = entityManager; - self.size = scene.size - } - - func load(withModel mapModel: MapGenerationModel) { - - // Create Bases - let basePlayerOne = Base( - position: CGPoint(x: self.size.width * 0.1, y: self.size.height / 2), - player: (GameCenterManager.sharedInstance.isServer) ? GKLocalPlayer.local : GameCenterManager.sharedInstance.myMatch?.players[0], - team: .team1 - ) - - let column1 = [ - Base(position: CGPoint(x: self.size.width * 0.25, y: self.size.height * 0.25)), - Base(position: CGPoint(x: self.size.width * 0.25, y: self.size.height * 0.5)), - Base(position: CGPoint(x: self.size.width * 0.25, y: self.size.height * 0.75)) - ] - - let column2 = [ - Base(position: CGPoint(x: self.size.width * 0.5, y: self.size.height * 0.333)), - Base(position: CGPoint(x: self.size.width * 0.5, y: self.size.height * 0.666)) - ] - - let column3 = [ - Base(position: CGPoint(x: self.size.width * 0.75, y: self.size.height * 0.25)), - Base(position: CGPoint(x: self.size.width * 0.75, y: self.size.height * 0.5)), - Base(position: CGPoint(x: self.size.width * 0.75, y: self.size.height * 0.75)) - ] - - let basePlayerTwo = Base( - position: CGPoint(x: self.size.width * 0.9, y: self.size.height / 2), - player: (!GameCenterManager.sharedInstance.isServer) ? GKLocalPlayer.local : GameCenterManager.sharedInstance.myMatch?.players[0], - team: .team2 - ) - - // Create adjacency Mapping - basePlayerOne.adjacencyList.append(contentsOf: column1) - - column1.forEach({currBase in - currBase.adjacencyList.append(contentsOf: column2) - currBase.adjacencyList.append(basePlayerOne) - }) - - column2.forEach({currBase in - currBase.adjacencyList.append(contentsOf: column3) - currBase.adjacencyList.append(contentsOf: column1) - }) - - column3.forEach({currBase in - currBase.adjacencyList.append(basePlayerTwo) - currBase.adjacencyList.append(contentsOf: column3) - }) - - basePlayerTwo.adjacencyList.append(contentsOf: column3) - - // Register bases with the EntityManager - entityManager.add(basePlayerOne) - - column1.forEach({base in entityManager.add(base)}) - column2.forEach({base in entityManager.add(base)}) - column3.forEach({base in entityManager.add(base)}) - - entityManager.add(basePlayerTwo) - } -} From 40ba0f4468978a67c371cfdbf5d95076ebece993 Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Fri, 29 May 2020 16:14:07 +0200 Subject: [PATCH 2/6] Remove MapProtocol --- GoldWars/GoldWars/Map/MapFactory.swift | 8 -------- GoldWars/GoldWars/Map/TwoPlayerMapGenerator.swift | 8 +------- 2 files changed, 1 insertion(+), 15 deletions(-) diff --git a/GoldWars/GoldWars/Map/MapFactory.swift b/GoldWars/GoldWars/Map/MapFactory.swift index 3f28737..42d2aca 100644 --- a/GoldWars/GoldWars/Map/MapFactory.swift +++ b/GoldWars/GoldWars/Map/MapFactory.swift @@ -10,14 +10,6 @@ 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(withModel mapModel: MapGenerationModel) -} protocol CenterElementProtocol { diff --git a/GoldWars/GoldWars/Map/TwoPlayerMapGenerator.swift b/GoldWars/GoldWars/Map/TwoPlayerMapGenerator.swift index 52e303d..07731cb 100644 --- a/GoldWars/GoldWars/Map/TwoPlayerMapGenerator.swift +++ b/GoldWars/GoldWars/Map/TwoPlayerMapGenerator.swift @@ -22,7 +22,7 @@ struct MapGenerationModel: Codable { } -class TwoPlayerMapGenerator: MapProtocol { +class TwoPlayerMapGenerator { let LOG = OSLog.init(subsystem: "MapGenerator", category: "TwoPlayerMapGenerator") @@ -264,10 +264,4 @@ class TwoPlayerMapGenerator: MapProtocol { ways.forEach({way in entityManager.add(way)}) } - - - func createPathFromBaseToBase(fromBase: Base, toBase: Base) { - let path = Way(fromBase: fromBase, toBase: toBase) - entityManager.add(path) - } } From 08dfe6a9220c215f8676dbecd285741d3fdbca72 Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Fri, 29 May 2020 16:23:56 +0200 Subject: [PATCH 3/6] Pull TwoPlayerMapGenerator in as MapFactory --- GoldWars/GoldWars.xcodeproj/project.pbxproj | 16 +- GoldWars/GoldWars/DataService.swift | 2 +- GoldWars/GoldWars/GameCenterManager.swift | 2 +- GoldWars/GoldWars/Map/MapFactory.swift | 300 ++++++++++++++---- GoldWars/GoldWars/Map/MapUtils.swift | 68 ++++ .../GoldWars/Map/TwoPlayerMapGenerator.swift | 267 ---------------- GoldWars/GoldWars/Scenes/GameScene.swift | 2 +- 7 files changed, 320 insertions(+), 337 deletions(-) create mode 100644 GoldWars/GoldWars/Map/MapUtils.swift delete mode 100644 GoldWars/GoldWars/Map/TwoPlayerMapGenerator.swift diff --git a/GoldWars/GoldWars.xcodeproj/project.pbxproj b/GoldWars/GoldWars.xcodeproj/project.pbxproj index 5261a4e..7371bab 100644 --- a/GoldWars/GoldWars.xcodeproj/project.pbxproj +++ b/GoldWars/GoldWars.xcodeproj/project.pbxproj @@ -44,9 +44,9 @@ 9EEDE02D246FCD770096C735 /* SpinningLogoEntity.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EEDE02C246FCD770096C735 /* SpinningLogoEntity.swift */; }; 9EEDE02F246FCD800096C735 /* SpinningLogoComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EEDE02E246FCD800096C735 /* SpinningLogoComponent.swift */; }; AB0B88F6247AD89200C8DF66 /* SkillComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB0B88F5247AD89200C8DF66 /* SkillComponent.swift */; }; - AB1D75A0245DEC0500671525 /* MapFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB1D759F245DEC0500671525 /* MapFactory.swift */; }; - AB21D7D5246C748A00B09CBA /* TwoPlayerMapGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB21D7D4246C748A00B09CBA /* TwoPlayerMapGenerator.swift */; }; + AB21D7D5246C748A00B09CBA /* MapFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB21D7D4246C748A00B09CBA /* MapFactory.swift */; }; ABA03DA0244BD54F00A66916 /* Base.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABA03D9F244BD54F00A66916 /* Base.swift */; }; + ABC0C3732481509300387B8F /* MapUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABC0C3722481509300387B8F /* MapUtils.swift */; }; C04783EE2468583F004961FB /* intro-music.mp3 in Resources */ = {isa = PBXBuildFile; fileRef = C04783ED2468583F004961FB /* intro-music.mp3 */; }; C04783F024685995004961FB /* SettingsScene.swift in Sources */ = {isa = PBXBuildFile; fileRef = C04783EF24685995004961FB /* SettingsScene.swift */; }; C05BB9C4247D890C00411249 /* SliderComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = C05BB9C3247D890C00411249 /* SliderComponent.swift */; }; @@ -110,9 +110,9 @@ 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 = ""; }; AB0B88F5247AD89200C8DF66 /* SkillComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SkillComponent.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 = ""; }; + AB21D7D4246C748A00B09CBA /* MapFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapFactory.swift; sourceTree = ""; }; ABA03D9F244BD54F00A66916 /* Base.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Base.swift; sourceTree = ""; }; + ABC0C3722481509300387B8F /* MapUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapUtils.swift; sourceTree = ""; }; C04783ED2468583F004961FB /* intro-music.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = "intro-music.mp3"; sourceTree = ""; }; C04783EF24685995004961FB /* SettingsScene.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsScene.swift; sourceTree = ""; }; C05BB9C3247D890C00411249 /* SliderComponent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SliderComponent.swift; sourceTree = ""; }; @@ -283,8 +283,8 @@ isa = PBXGroup; children = ( 3E67853F24728368007B9DE4 /* CElements.swift */, - AB1D759F245DEC0500671525 /* MapFactory.swift */, - AB21D7D4246C748A00B09CBA /* TwoPlayerMapGenerator.swift */, + AB21D7D4246C748A00B09CBA /* MapFactory.swift */, + ABC0C3722481509300387B8F /* MapUtils.swift */, ); path = Map; sourceTree = ""; @@ -411,17 +411,17 @@ 9E174C86245DD91500209FF0 /* ButtonComponent.swift in Sources */, 11036113244B3E30008610AF /* MenuScene.swift in Sources */, C099579C246C5E5C0016AA22 /* DataService.swift in Sources */, - AB21D7D5246C748A00B09CBA /* TwoPlayerMapGenerator.swift in Sources */, + AB21D7D5246C748A00B09CBA /* MapFactory.swift in Sources */, 9EA3ABE9245C6DAA006BC61D /* DefaultBaseComponent.swift in Sources */, 9E174C8A245E1A0A00209FF0 /* Background.swift in Sources */, 8BB6FF402472B8F000162BBD /* SingeClickButtonNode.swift in Sources */, AB0B88F6247AD89200C8DF66 /* SkillComponent.swift in Sources */, C064E9A8246C0EA50022B228 /* LabelNode.swift in Sources */, 3F745DF0246F48FC00CE7375 /* PlayerMoveType.swift in Sources */, - AB1D75A0245DEC0500671525 /* MapFactory.swift in Sources */, 3E67854024728368007B9DE4 /* CElements.swift in Sources */, ABA03DA0244BD54F00A66916 /* Base.swift in Sources */, C064E9AC246C151F0022B228 /* Label.swift in Sources */, + ABC0C3732481509300387B8F /* MapUtils.swift in Sources */, 9E174C82245DD81D00209FF0 /* ButtonNode.swift in Sources */, 9EC7E48B2461FBF700396BCD /* SliderNode.swift in Sources */, 9EEDE02F246FCD800096C735 /* SpinningLogoComponent.swift in Sources */, diff --git a/GoldWars/GoldWars/DataService.swift b/GoldWars/GoldWars/DataService.swift index 5ae7da0..cba4f4d 100644 --- a/GoldWars/GoldWars/DataService.swift +++ b/GoldWars/GoldWars/DataService.swift @@ -67,6 +67,6 @@ class DataService { func setMapModel(model: MapGenerationModel) { self.mapModel = model - MapFactory(scene: entityManager.scene, entityManager: entityManager).loadMap(fromModel: DataService.sharedInstance.mapModel!) + MapFactory(scene: entityManager.scene, entityManager: entityManager).load(fromModel: DataService.sharedInstance.mapModel!) } } diff --git a/GoldWars/GoldWars/GameCenterManager.swift b/GoldWars/GoldWars/GameCenterManager.swift index ba0ce93..7571212 100644 --- a/GoldWars/GoldWars/GameCenterManager.swift +++ b/GoldWars/GoldWars/GameCenterManager.swift @@ -148,7 +148,7 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate ,GKM func initAndSendMap() -> Void { self.gameScene = GameScene(size: self.menusc!.size) - let mapModel = MapFactory(scene: self.gameScene!, entityManager: entityManager).loadMap() + let mapModel = MapFactory(scene: self.gameScene!, entityManager: entityManager).load() os_log("Map wurde erstellt", log: LOG, type: .info) MultiplayerNetwork.sharedInstance.sendMapModelToPlayers(mapModel: mapModel) os_log("Map wurde an Peer gesendet", log: LOG, type: .info) diff --git a/GoldWars/GoldWars/Map/MapFactory.swift b/GoldWars/GoldWars/Map/MapFactory.swift index 42d2aca..bcddf4d 100644 --- a/GoldWars/GoldWars/Map/MapFactory.swift +++ b/GoldWars/GoldWars/Map/MapFactory.swift @@ -1,80 +1,262 @@ // -// MapFactory.swift +// TwoPlayerMapGenerator.swift // GoldWars // -// Created by Marcel Schwarz on 02.05.20. +// Created by Marcel Schwarz on 13.05.20. // Copyright © 2020 SP2. All rights reserved. // import Foundation import SpriteKit +import GameKit import os - -protocol CenterElementProtocol { - - var bases: [Base] {get set} - var ways: [Way] {get set} - var id: Int { get } - - init(frame: CGRect) - - func getAllBases() -> [Base] - func getInternalWays() -> [Way] - func getTopConnection() -> Base? - func getRightConnection() -> Base? - func getBottomConnection() -> Base? - func getLeftConnection() -> Base? -} - class MapFactory { - let LOG = OSLog.init(subsystem: "MapGenerator", category: "MapFactory") - var twoPlayerMapGenerator: TwoPlayerMapGenerator + let LOG = OSLog.init(subsystem: "MapGenerator", category: "TwoPlayerMapGenerator") - init(scene: SKScene, entityManager: EntityManager) { - self.twoPlayerMapGenerator = TwoPlayerMapGenerator(scene: scene, entityManager: entityManager) + var size: CGSize! + var entityManager: EntityManager! + + static func getNewMapModel() -> MapGenerationModel { + + let noOfCElements = CenterElementProvider.centerElements.count - 1 + + 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) + ) + } - func loadMap() -> MapGenerationModel { - let mapModel = TwoPlayerMapGenerator.getNewMapModel() - loadMap(fromModel: mapModel) + required init(scene: SKScene, entityManager: EntityManager) { + self.size = scene.size + self.entityManager = entityManager + } + + func load() -> MapGenerationModel { + let mapModel = MapFactory.getNewMapModel() + load(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 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) + func load(fromModel mapModel: MapGenerationModel) { + os_log("Loading from TwoPlayerMapFactory", log: LOG, type: .info) - return centerElements[id].init(frame: frame) + // 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: (GameCenterManager.sharedInstance.isServer) ? GKLocalPlayer.local : GameCenterManager.sharedInstance.myMatch?.players[0], + team: .team1 + ) + + os_log("Get player one's startbases", log: LOG, type: .info) + var p1StartBases = [String: Base]() + var ways = [Way]() + + 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 (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"]!)) + 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) + 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.5, + y: self.size.height * 0.1, + width: gridCellWidth, + height: gridCellHeight + ).insetBy(dx: cellInsetX, dy: cellInsetY) + + let gridBottomLeft = CGRect( + x: self.size.width * 0.3, + y: self.size.height * 0.5, + 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(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()) + 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), + player: (!GameCenterManager.sharedInstance.isServer) ? GKLocalPlayer.local : GameCenterManager.sharedInstance.myMatch?.players[0], + team: .team2 + ) + + os_log("Get player two's startbases", log: LOG, type: .info) + var p2StartBases = [String: Base]() + + 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 (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"]!)) + 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 + 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"]!) + ways.append(Way(fromBase: p1StartBases["top"]!, toBase: topLeft.getLeftConnection()!)) + } + + 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"]!) + 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()!)) + } + } + + 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"]!) + 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) + 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"]!) + ways.append(Way(fromBase: p2StartBases["top"]!, toBase: topRight.getRightConnection()!)) + } + + 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"]!) + 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()!)) + } + } + + 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"]!) + ways.append(Way(fromBase: p2StartBases["bot"]!, toBase: bottomRight.getRightConnection()!)) + } + + + // 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)}) + + ways.forEach({way in entityManager.add(way)}) } } diff --git a/GoldWars/GoldWars/Map/MapUtils.swift b/GoldWars/GoldWars/Map/MapUtils.swift new file mode 100644 index 0000000..0c636fc --- /dev/null +++ b/GoldWars/GoldWars/Map/MapUtils.swift @@ -0,0 +1,68 @@ +// +// MapUtils.swift +// GoldWars +// +// Created by Marcel Schwarz on 29.05.20. +// Copyright © 2020 SP2. All rights reserved. +// + +import GameKit +import os + +protocol CenterElementProtocol { + + var bases: [Base] {get set} + var ways: [Way] {get set} + var id: Int { get } + + init(frame: CGRect) + + func getAllBases() -> [Base] + func getInternalWays() -> [Way] + func getTopConnection() -> Base? + func getRightConnection() -> Base? + func getBottomConnection() -> Base? + func getLeftConnection() -> Base? +} + +struct MapGenerationModel: Codable { + + let numBasesP1: Int + let numBasesP2: Int + let topLeftId: Int + let topRightId: Int + let bottomLeftId: Int + let bottomRightId: Int + +} + +class CenterElementProvider { + static let LOG = OSLog.init(subsystem: "MapGenerator", category: "CenterElementProvider") + + 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) + + return centerElements[id].init(frame: frame) + } +} diff --git a/GoldWars/GoldWars/Map/TwoPlayerMapGenerator.swift b/GoldWars/GoldWars/Map/TwoPlayerMapGenerator.swift deleted file mode 100644 index 07731cb..0000000 --- a/GoldWars/GoldWars/Map/TwoPlayerMapGenerator.swift +++ /dev/null @@ -1,267 +0,0 @@ -// -// TwoPlayerMapGenerator.swift -// GoldWars -// -// Created by Marcel Schwarz on 13.05.20. -// Copyright © 2020 SP2. All rights reserved. -// - -import Foundation -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 { - - let LOG = OSLog.init(subsystem: "MapGenerator", category: "TwoPlayerMapGenerator") - - var size: CGSize! - var entityManager: EntityManager! - - static func getNewMapModel() -> MapGenerationModel { - - let noOfCElements = CenterElementProvider.centerElements.count - 1 - - 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(withModel mapModel: MapGenerationModel) { - os_log("Loading from TwoPlayerMapFactory", log: LOG, type: .info) - - // 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: (GameCenterManager.sharedInstance.isServer) ? GKLocalPlayer.local : GameCenterManager.sharedInstance.myMatch?.players[0], - team: .team1 - ) - - os_log("Get player one's startbases", log: LOG, type: .info) - var p1StartBases = [String: Base]() - var ways = [Way]() - - 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 (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"]!)) - 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) - 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.5, - y: self.size.height * 0.1, - width: gridCellWidth, - height: gridCellHeight - ).insetBy(dx: cellInsetX, dy: cellInsetY) - - let gridBottomLeft = CGRect( - x: self.size.width * 0.3, - y: self.size.height * 0.5, - 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(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()) - 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), - player: (!GameCenterManager.sharedInstance.isServer) ? GKLocalPlayer.local : GameCenterManager.sharedInstance.myMatch?.players[0], - team: .team2 - ) - - os_log("Get player two's startbases", log: LOG, type: .info) - var p2StartBases = [String: Base]() - - 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 (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"]!)) - 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 - 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"]!) - ways.append(Way(fromBase: p1StartBases["top"]!, toBase: topLeft.getLeftConnection()!)) - } - - 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"]!) - 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()!)) - } - } - - 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"]!) - 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) - 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"]!) - ways.append(Way(fromBase: p2StartBases["top"]!, toBase: topRight.getRightConnection()!)) - } - - 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"]!) - 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()!)) - } - } - - 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"]!) - ways.append(Way(fromBase: p2StartBases["bot"]!, toBase: bottomRight.getRightConnection()!)) - } - - - // 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)}) - - ways.forEach({way in entityManager.add(way)}) - } -} diff --git a/GoldWars/GoldWars/Scenes/GameScene.swift b/GoldWars/GoldWars/Scenes/GameScene.swift index b38524c..7fa2a3b 100644 --- a/GoldWars/GoldWars/Scenes/GameScene.swift +++ b/GoldWars/GoldWars/Scenes/GameScene.swift @@ -24,7 +24,7 @@ class GameScene: SKScene{ entityManager.add(HUD(size: self.size)) entityManager.add(Background(size: self.size)) if CommandLine.arguments.contains("--no-matchmaking") { - _ = MapFactory(scene: self, entityManager: entityManager).loadMap() + _ = MapFactory(scene: self, entityManager: entityManager).load() return } } From 387303af3a9736ef9f013cabbccb6923b3eb3b8f Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Fri, 29 May 2020 16:32:17 +0200 Subject: [PATCH 4/6] Pull getMapModel in the MapModelStruct --- GoldWars/GoldWars/Map/MapFactory.swift | 17 +---------------- GoldWars/GoldWars/Map/MapUtils.swift | 13 +++++++++++++ 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/GoldWars/GoldWars/Map/MapFactory.swift b/GoldWars/GoldWars/Map/MapFactory.swift index bcddf4d..5664233 100644 --- a/GoldWars/GoldWars/Map/MapFactory.swift +++ b/GoldWars/GoldWars/Map/MapFactory.swift @@ -18,28 +18,13 @@ class MapFactory { var size: CGSize! var entityManager: EntityManager! - static func getNewMapModel() -> MapGenerationModel { - - let noOfCElements = CenterElementProvider.centerElements.count - 1 - - 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() -> MapGenerationModel { - let mapModel = MapFactory.getNewMapModel() + let mapModel = MapGenerationModel.new() load(fromModel: mapModel) return mapModel } diff --git a/GoldWars/GoldWars/Map/MapUtils.swift b/GoldWars/GoldWars/Map/MapUtils.swift index 0c636fc..732c93a 100644 --- a/GoldWars/GoldWars/Map/MapUtils.swift +++ b/GoldWars/GoldWars/Map/MapUtils.swift @@ -34,6 +34,19 @@ struct MapGenerationModel: Codable { let bottomLeftId: Int let bottomRightId: Int + static func new() -> MapGenerationModel { + let noOfCElements = CenterElementProvider.centerElements.count - 1 + + 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) + ) + } + } class CenterElementProvider { From ed5d5caa153369181c495583b19a1fd9439f4145 Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Fri, 29 May 2020 16:39:12 +0200 Subject: [PATCH 5/6] Remove orphan files --- GoldWars/GoldWars/Components/WayNode.swift | 25 ---- GoldWars/GoldWars/Map/SampleCElement.swift | 123 ------------------ GoldWars/GoldWars/PlayerMovesService.swift | 32 ----- .../Storyboards/RoundSimulatorService.swift | 13 -- 4 files changed, 193 deletions(-) delete mode 100644 GoldWars/GoldWars/Components/WayNode.swift delete mode 100644 GoldWars/GoldWars/Map/SampleCElement.swift delete mode 100644 GoldWars/GoldWars/PlayerMovesService.swift delete mode 100644 GoldWars/GoldWars/Storyboards/RoundSimulatorService.swift diff --git a/GoldWars/GoldWars/Components/WayNode.swift b/GoldWars/GoldWars/Components/WayNode.swift deleted file mode 100644 index 41559a6..0000000 --- a/GoldWars/GoldWars/Components/WayNode.swift +++ /dev/null @@ -1,25 +0,0 @@ -// -// 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/Map/SampleCElement.swift b/GoldWars/GoldWars/Map/SampleCElement.swift deleted file mode 100644 index e71d78c..0000000 --- a/GoldWars/GoldWars/Map/SampleCElement.swift +++ /dev/null @@ -1,123 +0,0 @@ -// -// 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 CElement1700 : 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/PlayerMovesService.swift b/GoldWars/GoldWars/PlayerMovesService.swift deleted file mode 100644 index 3c0f41d..0000000 --- a/GoldWars/GoldWars/PlayerMovesService.swift +++ /dev/null @@ -1,32 +0,0 @@ -// -// PlayerMovesService.swift -// GoldWars -// -// Created by Tim Herbst on 13.05.20. -// Copyright © 2020 SP2. All rights reserved. -// - -struct PlayerMove : Codable{ - let fromBase: Int - let toBase: Int - let unitCount: Int -} - -struct Host: Codable { - let playerName: String -} - -class DataService { - static let sharedInstance = DataService() - var playerMoves: [PlayerMove] = [] - var gameHost: Host? - - - func addMove(playerMove: PlayerMove) { - self.playerMoves.append(playerMove) - } - - func setGameHost(host: Host) { - self.gameHost = host - } -} diff --git a/GoldWars/GoldWars/Storyboards/RoundSimulatorService.swift b/GoldWars/GoldWars/Storyboards/RoundSimulatorService.swift deleted file mode 100644 index 4847831..0000000 --- a/GoldWars/GoldWars/Storyboards/RoundSimulatorService.swift +++ /dev/null @@ -1,13 +0,0 @@ -// -// RoundSimulatorService.swift -// GoldWars -// -// Created by student on 13.05.20. -// Copyright © 2020 SP2. All rights reserved. -// - -import Foundation - -class RoundSimulatorService { - -} From 61f3ab14fc5a4af6ecbfe985398b15351d9c48f0 Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Fri, 29 May 2020 16:51:43 +0200 Subject: [PATCH 6/6] Update log subsystem in MapFactory --- GoldWars/GoldWars/Map/MapFactory.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GoldWars/GoldWars/Map/MapFactory.swift b/GoldWars/GoldWars/Map/MapFactory.swift index 5664233..9b8da1c 100644 --- a/GoldWars/GoldWars/Map/MapFactory.swift +++ b/GoldWars/GoldWars/Map/MapFactory.swift @@ -13,7 +13,7 @@ import os class MapFactory { - let LOG = OSLog.init(subsystem: "MapGenerator", category: "TwoPlayerMapGenerator") + let LOG = OSLog.init(subsystem: "MapGenerator", category: "MapFactory") var size: CGSize! var entityManager: EntityManager!