Send map model to other player over data service
This commit is contained in:
parent
71f556868f
commit
fe1405d3fa
@ -42,6 +42,7 @@ class DataService {
|
|||||||
var remotePlayerMoves: [String: [PlayerMove]] = [:]
|
var remotePlayerMoves: [String: [PlayerMove]] = [:]
|
||||||
var snapshotModel: SnapshotModel?
|
var snapshotModel: SnapshotModel?
|
||||||
var gameHost: Host?
|
var gameHost: Host?
|
||||||
|
var mapModel: MapGenerationModel?
|
||||||
|
|
||||||
func addMove(playerMove: PlayerMove) {
|
func addMove(playerMove: PlayerMove) {
|
||||||
var equalMove = localPlayerMoves.filter { (ele) -> Bool in
|
var equalMove = localPlayerMoves.filter { (ele) -> Bool in
|
||||||
@ -69,4 +70,9 @@ class DataService {
|
|||||||
func setSnapshotModel(snapshotModel: SnapshotModel) {
|
func setSnapshotModel(snapshotModel: SnapshotModel) {
|
||||||
self.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!)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,7 @@ class TwoPlayerMapGenerator: MapProtocol {
|
|||||||
|
|
||||||
static func getNewMapModel() -> MapGenerationModel {
|
static func getNewMapModel() -> MapGenerationModel {
|
||||||
|
|
||||||
let noOfCElements = CenterElementProvider.centerElements.count
|
let noOfCElements = CenterElementProvider.centerElements.count - 1
|
||||||
|
|
||||||
return MapGenerationModel(
|
return MapGenerationModel(
|
||||||
numBasesP1: Int.random(in: 1...3),
|
numBasesP1: Int.random(in: 1...3),
|
||||||
|
@ -122,6 +122,10 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
|
|||||||
EntityManager.sharedInstance.updateSnapshotModel(snapshotModel: snapshotModel)
|
EntityManager.sharedInstance.updateSnapshotModel(snapshotModel: snapshotModel)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let mapModel = try? jsonDecoder.decode(MapGenerationModel.self, from: data) {
|
||||||
|
DataService.sharedInstance.setMapModel(model: mapModel)
|
||||||
|
}
|
||||||
|
|
||||||
MultiplayerNetwork.sharedInstance.isSending = false
|
MultiplayerNetwork.sharedInstance.isSending = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,4 +61,10 @@ class MultiplayerNetwork{
|
|||||||
sendData(data: encoded)
|
sendData(data: encoded)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func sendMapModelToPlayers(mapModel: MapGenerationModel) {
|
||||||
|
let encoder = JSONEncoder()
|
||||||
|
let encoded = (try? encoder.encode(mapModel))!
|
||||||
|
sendData(data: encoded)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,12 @@ class GameScene: SKScene{
|
|||||||
}
|
}
|
||||||
|
|
||||||
func initMap() {
|
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())
|
DataService.sharedInstance.setSnapshotModel(snapshotModel: EntityManager.sharedInstance.getSnapshotModel())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user