diff --git a/GoldWars/GoldWars/DataService.swift b/GoldWars/GoldWars/DataService.swift index e06dff6..d94a992 100644 --- a/GoldWars/GoldWars/DataService.swift +++ b/GoldWars/GoldWars/DataService.swift @@ -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!) + } } diff --git a/GoldWars/GoldWars/Map/TwoPlayerMapGenerator.swift b/GoldWars/GoldWars/Map/TwoPlayerMapGenerator.swift index d17add7..918bdf2 100644 --- a/GoldWars/GoldWars/Map/TwoPlayerMapGenerator.swift +++ b/GoldWars/GoldWars/Map/TwoPlayerMapGenerator.swift @@ -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), diff --git a/GoldWars/GoldWars/MatchmakingHelper.swift b/GoldWars/GoldWars/MatchmakingHelper.swift index a695d39..5e09ed6 100644 --- a/GoldWars/GoldWars/MatchmakingHelper.swift +++ b/GoldWars/GoldWars/MatchmakingHelper.swift @@ -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 } diff --git a/GoldWars/GoldWars/MultiplayerNetwork.swift b/GoldWars/GoldWars/MultiplayerNetwork.swift index 7ffbc37..34cbccd 100644 --- a/GoldWars/GoldWars/MultiplayerNetwork.swift +++ b/GoldWars/GoldWars/MultiplayerNetwork.swift @@ -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) + } } diff --git a/GoldWars/GoldWars/Scenes/GameScene.swift b/GoldWars/GoldWars/Scenes/GameScene.swift index 8e9c7fa..58a3d9d 100644 --- a/GoldWars/GoldWars/Scenes/GameScene.swift +++ b/GoldWars/GoldWars/Scenes/GameScene.swift @@ -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()) }