Allow map to be loaded from predefined model

This commit is contained in:
Marcel Schwarz 2020-05-20 18:42:57 +02:00
parent 0fd409c00b
commit 71f556868f
4 changed files with 74 additions and 60 deletions

View File

@ -16,7 +16,7 @@ protocol MapProtocol {
init(scene: SKScene, entityManager: EntityManager)
func load()
func load(withModel mapModel: MapGenerationModel)
}
protocol CenterElementProtocol {
@ -38,40 +38,29 @@ 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 {
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)
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()
}
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)
let centerElements: [CenterElementProtocol.Type] = [
static let centerElements: [CenterElementProtocol.Type] = [
CElement0.self,
CElement1.self,
CElement2.self,
CElement3.self,
@ -88,10 +77,12 @@ class CenterElementProvider {
CElement14.self,
CElement15.self,
CElement16.self,
CElement17.self,
CElement18.self
CElement17.self
]
return centerElements.randomElement()!.init(frame: frame)
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)
}
}

View File

@ -20,7 +20,7 @@ class TwoPlayerDefaultTestMap: MapProtocol {
self.size = scene.size
}
func load() {
func load(withModel mapModel: MapGenerationModel) {
// Create Bases
let basePlayerOne = Base(

View File

@ -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"]!))

View File

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