Add TwoPlayerMapGenerator

This commit is contained in:
Marcel Schwarz 2020-05-17 19:34:44 +02:00
parent e2bd0dc7cf
commit ee031f71c1
4 changed files with 325 additions and 28 deletions

View File

@ -46,10 +46,10 @@
9EC86BA6245C8AD000796EF3 /* ModalType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EC86BA5245C8AD000796EF3 /* ModalType.swift */; }; 9EC86BA6245C8AD000796EF3 /* ModalType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EC86BA5245C8AD000796EF3 /* ModalType.swift */; };
9EEDE02D246FCD770096C735 /* SpinningLogoEntity.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EEDE02C246FCD770096C735 /* SpinningLogoEntity.swift */; }; 9EEDE02D246FCD770096C735 /* SpinningLogoEntity.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EEDE02C246FCD770096C735 /* SpinningLogoEntity.swift */; };
9EEDE02F246FCD800096C735 /* SpinningLogoComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EEDE02E246FCD800096C735 /* SpinningLogoComponent.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 */; }; AB1D759D245DD18100671525 /* TwoPlayerDefaultTestMap.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB1D759B245DD18100671525 /* TwoPlayerDefaultTestMap.swift */; };
AB1D75A0245DEC0500671525 /* MapFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB1D759F245DEC0500671525 /* MapFactory.swift */; }; AB1D75A0245DEC0500671525 /* MapFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB1D759F245DEC0500671525 /* MapFactory.swift */; };
AB21D7D5246C748A00B09CBA /* TwoPlayerMapGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB21D7D4246C748A00B09CBA /* TwoPlayerMapGenerator.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 */; }; ABA03DA0244BD54F00A66916 /* Base.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABA03D9F244BD54F00A66916 /* Base.swift */; };
AE151589245F18EF001D363E /* MatchmakingHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE151588245F18EF001D363E /* MatchmakingHelper.swift */; }; AE151589245F18EF001D363E /* MatchmakingHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE151588245F18EF001D363E /* MatchmakingHelper.swift */; };
AEBF3AFF246EB146004F7CD5 /* CancelBtnNode.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEBF3AFE246EB146004F7CD5 /* CancelBtnNode.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 = "<group>"; }; 9ECD3699245C91F7008DEEBD /* GoldWars.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = GoldWars.entitlements; sourceTree = "<group>"; };
9EEDE02C246FCD770096C735 /* SpinningLogoEntity.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SpinningLogoEntity.swift; sourceTree = "<group>"; }; 9EEDE02C246FCD770096C735 /* SpinningLogoEntity.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SpinningLogoEntity.swift; sourceTree = "<group>"; };
9EEDE02E246FCD800096C735 /* SpinningLogoComponent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SpinningLogoComponent.swift; sourceTree = "<group>"; }; 9EEDE02E246FCD800096C735 /* SpinningLogoComponent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SpinningLogoComponent.swift; sourceTree = "<group>"; };
AB1D759A245DD18100671525 /* MapProtocol.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MapProtocol.swift; sourceTree = "<group>"; };
AB1D759B245DD18100671525 /* TwoPlayerDefaultTestMap.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TwoPlayerDefaultTestMap.swift; sourceTree = "<group>"; }; AB1D759B245DD18100671525 /* TwoPlayerDefaultTestMap.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TwoPlayerDefaultTestMap.swift; sourceTree = "<group>"; };
AB1D759F245DEC0500671525 /* MapFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapFactory.swift; sourceTree = "<group>"; }; AB1D759F245DEC0500671525 /* MapFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapFactory.swift; sourceTree = "<group>"; };
AB21D7D4246C748A00B09CBA /* TwoPlayerMapGenerator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TwoPlayerMapGenerator.swift; sourceTree = "<group>"; }; AB21D7D4246C748A00B09CBA /* TwoPlayerMapGenerator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TwoPlayerMapGenerator.swift; sourceTree = "<group>"; };
AB9403892471528D004FCD07 /* SampleCElement.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SampleCElement.swift; sourceTree = "<group>"; };
ABA03D9F244BD54F00A66916 /* Base.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Base.swift; sourceTree = "<group>"; }; ABA03D9F244BD54F00A66916 /* Base.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Base.swift; sourceTree = "<group>"; };
AE151588245F18EF001D363E /* MatchmakingHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MatchmakingHelper.swift; sourceTree = "<group>"; }; AE151588245F18EF001D363E /* MatchmakingHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MatchmakingHelper.swift; sourceTree = "<group>"; };
AEBF3AFE246EB146004F7CD5 /* CancelBtnNode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CancelBtnNode.swift; sourceTree = "<group>"; }; AEBF3AFE246EB146004F7CD5 /* CancelBtnNode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CancelBtnNode.swift; sourceTree = "<group>"; };
@ -302,6 +302,7 @@
AB1D759F245DEC0500671525 /* MapFactory.swift */, AB1D759F245DEC0500671525 /* MapFactory.swift */,
AB1D759B245DD18100671525 /* TwoPlayerDefaultTestMap.swift */, AB1D759B245DD18100671525 /* TwoPlayerDefaultTestMap.swift */,
AB21D7D4246C748A00B09CBA /* TwoPlayerMapGenerator.swift */, AB21D7D4246C748A00B09CBA /* TwoPlayerMapGenerator.swift */,
AB9403892471528D004FCD07 /* SampleCElement.swift */,
); );
path = Map; path = Map;
sourceTree = "<group>"; sourceTree = "<group>";
@ -456,6 +457,7 @@
9EC86B9F245C88A300796EF3 /* Modal.swift in Sources */, 9EC86B9F245C88A300796EF3 /* Modal.swift in Sources */,
9E78ACC2245CC9EE00526FF7 /* DefBoostSkillComponent.swift in Sources */, 9E78ACC2245CC9EE00526FF7 /* DefBoostSkillComponent.swift in Sources */,
C05FAED62468559D0006AF2E /* SoundManager.swift in Sources */, C05FAED62468559D0006AF2E /* SoundManager.swift in Sources */,
AB94038A2471528D004FCD07 /* SampleCElement.swift in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };

View File

@ -24,10 +24,10 @@ protocol CenterElementProtocol {
init(frame: CGRect) init(frame: CGRect)
func getAllBases() -> [Base] func getAllBases() -> [Base]
func getTopConnection() -> Base func getTopConnection() -> Base?
func getRightConnection() -> Base func getRightConnection() -> Base?
func getBottomConnection() -> Base func getBottomConnection() -> Base?
func getLeftConnection() -> Base func getLeftConnection() -> Base?
} }
class MapFactory { class MapFactory {
@ -63,9 +63,15 @@ class MapFactory {
class CenterElementProvider { class CenterElementProvider {
static let LOG = OSLog.init(subsystem: "MapGenerator", category: "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 // TODO: Implemented by Jakob
os_log("Getting new center from provider", log: LOG, type: .info) os_log("Getting new center element from provider", log: LOG, type: .info)
return nil
let centerElements: [CenterElementProtocol.Type] = [
SampleCElement.self,
CElement7.self
]
return centerElements.randomElement()!.init(frame: frame)
} }
} }

View File

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

View File

@ -8,6 +8,7 @@
import Foundation import Foundation
import SpriteKit import SpriteKit
import GameKit
import os import os
class TwoPlayerMapGenerator: MapProtocol { class TwoPlayerMapGenerator: MapProtocol {
@ -23,25 +24,190 @@ class TwoPlayerMapGenerator: MapProtocol {
func load() { func load() {
os_log("Loading from TwoPlayerMapFactory", log: LOG, type: .info) os_log("Loading from TwoPlayerMapFactory", log: LOG, type: .info)
getPlayerOneAndStartbases()
getMapCenter() // Generate bases structure
getPlayerTwoAndStartbases() 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))
} }
private func getPlayerOneAndStartbases() -> [Base]{ os_log("Generate Map center", log: LOG, type: .info)
os_log("Get player one and his startbases", log: LOG, type: .info) let gridCellWidth = self.size.width * 0.2
return [Base]() 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))
} }
private func getPlayerTwoAndStartbases() -> [Base]{ // Create adjacency mapping
os_log("Get player two and his startbases", log: LOG, type: .info) os_log("Connecting base p1 and start bases", log: LOG, type: .info)
return [Base]() 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"]!)
} }
private func getMapCenter() -> [Base] { if (p1StartBases["mid"] != nil) {
os_log("Get centered bases", log: LOG, type: .info) if (topLeft.getLeftConnection() != nil) {
return [Base]() 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)})
}
} }