Allow map to be loaded from predefined model
This commit is contained in:
parent
0fd409c00b
commit
71f556868f
@ -16,7 +16,7 @@ protocol MapProtocol {
|
|||||||
|
|
||||||
init(scene: SKScene, entityManager: EntityManager)
|
init(scene: SKScene, entityManager: EntityManager)
|
||||||
|
|
||||||
func load()
|
func load(withModel mapModel: MapGenerationModel)
|
||||||
}
|
}
|
||||||
|
|
||||||
protocol CenterElementProtocol {
|
protocol CenterElementProtocol {
|
||||||
@ -38,40 +38,29 @@ protocol CenterElementProtocol {
|
|||||||
class MapFactory {
|
class MapFactory {
|
||||||
let LOG = OSLog.init(subsystem: "MapGenerator", category: "MapFactory")
|
let LOG = OSLog.init(subsystem: "MapGenerator", category: "MapFactory")
|
||||||
|
|
||||||
var twoPlayerMaps: [MapProtocol]!
|
var twoPlayerMapGenerator: TwoPlayerMapGenerator
|
||||||
var threePlayerMaps: [MapProtocol]!
|
|
||||||
var fourPlayerMaps: [MapProtocol]!
|
|
||||||
|
|
||||||
init(scene: SKScene, entityManager: EntityManager) {
|
init(scene: SKScene, entityManager: EntityManager) {
|
||||||
self.twoPlayerMaps = [
|
self.twoPlayerMapGenerator = TwoPlayerMapGenerator(scene: scene, entityManager: entityManager)
|
||||||
TwoPlayerMapGenerator(scene: scene, entityManager: entityManager)
|
|
||||||
]
|
|
||||||
|
|
||||||
self.threePlayerMaps = [MapProtocol]()
|
|
||||||
self.fourPlayerMaps = [MapProtocol]()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadMap(playerCount: Int) {
|
func loadMap() -> MapGenerationModel{
|
||||||
if playerCount == 2 {
|
let mapModel = TwoPlayerMapGenerator.getNewMapModel()
|
||||||
|
loadMap(fromModel: mapModel)
|
||||||
|
return mapModel
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadMap(fromModel model: MapGenerationModel) {
|
||||||
os_log("Loading two player map", log: LOG, type: .info)
|
os_log("Loading two player map", log: LOG, type: .info)
|
||||||
twoPlayerMaps.randomElement()?.load()
|
twoPlayerMapGenerator.load(withModel: model)
|
||||||
} 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 {
|
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 let centerElements: [CenterElementProtocol.Type] = [
|
||||||
os_log("Getting new center element from provider", log: LOG, type: .info)
|
CElement0.self,
|
||||||
|
|
||||||
let centerElements: [CenterElementProtocol.Type] = [
|
|
||||||
CElement1.self,
|
CElement1.self,
|
||||||
CElement2.self,
|
CElement2.self,
|
||||||
CElement3.self,
|
CElement3.self,
|
||||||
@ -88,10 +77,12 @@ class CenterElementProvider {
|
|||||||
CElement14.self,
|
CElement14.self,
|
||||||
CElement15.self,
|
CElement15.self,
|
||||||
CElement16.self,
|
CElement16.self,
|
||||||
CElement17.self,
|
CElement17.self
|
||||||
CElement18.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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,7 @@ class TwoPlayerDefaultTestMap: MapProtocol {
|
|||||||
self.size = scene.size
|
self.size = scene.size
|
||||||
}
|
}
|
||||||
|
|
||||||
func load() {
|
func load(withModel mapModel: MapGenerationModel) {
|
||||||
|
|
||||||
// Create Bases
|
// Create Bases
|
||||||
let basePlayerOne = Base(
|
let basePlayerOne = Base(
|
||||||
|
@ -11,18 +11,45 @@ import SpriteKit
|
|||||||
import GameKit
|
import GameKit
|
||||||
import os
|
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 {
|
class TwoPlayerMapGenerator: MapProtocol {
|
||||||
|
|
||||||
let LOG = OSLog.init(subsystem: "MapGenerator", category: "TwoPlayerMapGenerator")
|
let LOG = OSLog.init(subsystem: "MapGenerator", category: "TwoPlayerMapGenerator")
|
||||||
|
|
||||||
var size: CGSize!
|
var size: CGSize!
|
||||||
var entityManager: EntityManager!
|
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) {
|
required init(scene: SKScene, entityManager: EntityManager) {
|
||||||
self.size = scene.size
|
self.size = scene.size
|
||||||
self.entityManager = entityManager
|
self.entityManager = entityManager
|
||||||
}
|
}
|
||||||
|
|
||||||
func load() {
|
func load(withModel mapModel: MapGenerationModel) {
|
||||||
os_log("Loading from TwoPlayerMapFactory", log: LOG, type: .info)
|
os_log("Loading from TwoPlayerMapFactory", log: LOG, type: .info)
|
||||||
|
|
||||||
// Generate bases structure
|
// Generate bases structure
|
||||||
@ -34,16 +61,13 @@ class TwoPlayerMapGenerator: MapProtocol {
|
|||||||
)
|
)
|
||||||
|
|
||||||
os_log("Get player one's startbases", log: LOG, type: .info)
|
os_log("Get player one's startbases", log: LOG, type: .info)
|
||||||
|
|
||||||
let p1StartBaseCount = Int.random(in: 1...3)
|
|
||||||
var p1StartBases = [String: Base]()
|
var p1StartBases = [String: Base]()
|
||||||
|
|
||||||
var ways = [Way]()
|
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))
|
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"]!))
|
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["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))
|
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["top"]!))
|
||||||
@ -91,10 +115,10 @@ class TwoPlayerMapGenerator: MapProtocol {
|
|||||||
height: gridCellHeight
|
height: gridCellHeight
|
||||||
).insetBy(dx: cellInsetX, dy: cellInsetY)
|
).insetBy(dx: cellInsetX, dy: cellInsetY)
|
||||||
|
|
||||||
let topLeft = CenterElementProvider.get(frame: gridTopLeft)
|
let topLeft = CenterElementProvider.get(inFrame: gridTopLeft, withId: mapModel.topLeftId)
|
||||||
let topRight = CenterElementProvider.get(frame: gridTopRight)
|
let topRight = CenterElementProvider.get(inFrame: gridTopRight, withId: mapModel.topRightId)
|
||||||
let bottomLeft = CenterElementProvider.get(frame: gridBottomLeft)
|
let bottomLeft = CenterElementProvider.get(inFrame: gridBottomLeft, withId: mapModel.bottomLeftId)
|
||||||
let bottomRight = CenterElementProvider.get(frame: gridBottomRight)
|
let bottomRight = CenterElementProvider.get(inFrame: gridBottomRight, withId: mapModel.bottomRightId)
|
||||||
|
|
||||||
ways.append(contentsOf: topLeft.getInternalWays())
|
ways.append(contentsOf: topLeft.getInternalWays())
|
||||||
ways.append(contentsOf: topRight.getInternalWays())
|
ways.append(contentsOf: topRight.getInternalWays())
|
||||||
@ -109,13 +133,12 @@ class TwoPlayerMapGenerator: MapProtocol {
|
|||||||
)
|
)
|
||||||
|
|
||||||
os_log("Get player two's startbases", log: LOG, type: .info)
|
os_log("Get player two's startbases", log: LOG, type: .info)
|
||||||
let p2StartBaseCount = Int.random(in: 1...3)
|
|
||||||
var p2StartBases = [String: Base]()
|
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))
|
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"]!))
|
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["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))
|
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["top"]!))
|
||||||
|
@ -26,7 +26,7 @@ class GameScene: SKScene{
|
|||||||
}
|
}
|
||||||
|
|
||||||
func initMap() {
|
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())
|
DataService.sharedInstance.setSnapshotModel(snapshotModel: EntityManager.sharedInstance.getSnapshotModel())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user