Send map model to other player over data service

This commit is contained in:
Marcel Schwarz 2020-05-20 20:30:06 +02:00
parent 71f556868f
commit fe1405d3fa
5 changed files with 23 additions and 2 deletions

View File

@ -42,6 +42,7 @@ class DataService {
var remotePlayerMoves: [String: [PlayerMove]] = [:]
var snapshotModel: SnapshotModel?
var gameHost: Host?
var mapModel: MapGenerationModel?
func addMove(playerMove: PlayerMove) {
var equalMove = localPlayerMoves.filter { (ele) -> Bool in
@ -69,4 +70,9 @@ class DataService {
func setSnapshotModel(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!)
}
}

View File

@ -31,7 +31,7 @@ class TwoPlayerMapGenerator: MapProtocol {
static func getNewMapModel() -> MapGenerationModel {
let noOfCElements = CenterElementProvider.centerElements.count
let noOfCElements = CenterElementProvider.centerElements.count - 1
return MapGenerationModel(
numBasesP1: Int.random(in: 1...3),

View File

@ -122,6 +122,10 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
EntityManager.sharedInstance.updateSnapshotModel(snapshotModel: snapshotModel)
}
if let mapModel = try? jsonDecoder.decode(MapGenerationModel.self, from: data) {
DataService.sharedInstance.setMapModel(model: mapModel)
}
MultiplayerNetwork.sharedInstance.isSending = false
}

View File

@ -60,5 +60,11 @@ class MultiplayerNetwork{
let encoded = (try? encoder.encode(DataService.sharedInstance.snapshotModel))!
sendData(data: encoded)
}
func sendMapModelToPlayers(mapModel: MapGenerationModel) {
let encoder = JSONEncoder()
let encoded = (try? encoder.encode(mapModel))!
sendData(data: encoded)
}
}

View File

@ -26,7 +26,12 @@ class GameScene: SKScene{
}
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())
}