Update MatchMakerHelper, added methods

This commit is contained in:
Chauntalle Schüle 2020-05-04 20:34:58 +02:00 committed by Chauntalle Schüle
parent 017e99183b
commit 54d31d929d
2 changed files with 64 additions and 21 deletions

View File

@ -22,6 +22,8 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
var mpMatch: GKMatch? //represents network var mpMatch: GKMatch? //represents network
var viewController: UIViewController? var viewController: UIViewController?
var mpMatchStarted: Bool var mpMatchStarted: Bool
var isServer: Bool
var serverPlayer: GKPlayer?
let localPlayer: GKLocalPlayer = GKLocalPlayer.local let localPlayer: GKLocalPlayer = GKLocalPlayer.local
static let sharedInstance = MatchmakingHelper() static let sharedInstance = MatchmakingHelper()
@ -33,27 +35,24 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
override init() { override init() {
mpMatchStarted = false mpMatchStarted = false
isServer = false
super.init() super.init()
} }
func presentMatchmaker() { func presentMatchmaker() {
// 1
guard GKLocalPlayer.local.isAuthenticated else { guard GKLocalPlayer.local.isAuthenticated else {
print("Player ist nicht authentifiziert") print("Player ist nicht authentifiziert")
return return
} }
// 2
let request = GKMatchRequest() let request = GKMatchRequest()
request.minPlayers = 2 request.minPlayers = 2
request.maxPlayers = 2 request.maxPlayers = 2
// 3
request.inviteMessage = "Willst du GoldWars spielen?" request.inviteMessage = "Willst du GoldWars spielen?"
// 4 let matchmakerVC = GKMatchmakerViewController.init(matchRequest: request)
let vc = GKMatchmakerViewController.init(matchRequest: request) matchmakerVC!.matchmakerDelegate = self
viewController?.present(vc!, animated: true, completion: nil) viewController?.present(matchmakerVC!, animated: true, completion: nil)
} }
func findMatchWithMinPlayers(minPlayers: Int, maxPlayers: Int, viewController: UIViewController, delegate: GameKitHelperDelegate){ func findMatchWithMinPlayers(minPlayers: Int, maxPlayers: Int, viewController: UIViewController, delegate: GameKitHelperDelegate){
@ -94,9 +93,11 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
//expectedPlayerCount : The remaining number of players who have not yet connected to the match //expectedPlayerCount : The remaining number of players who have not yet connected to the match
if !mpMatchStarted && match.expectedPlayerCount == 0 { if !mpMatchStarted && match.expectedPlayerCount == 0 {
print("Bereit das Spiel zu starten!") print("Bereit das Spiel zu starten!")
//startMatch() startMatch()
} }
} }
// The match received data sent from the player.
private func match(match: GKMatch!, didReceiveData data: NSData!,fromPlayer playerID: String!) { private func match(match: GKMatch!, didReceiveData data: NSData!,fromPlayer playerID: String!) {
if mpMatch != match { return } if mpMatch != match { return }
delegate?.matchReceivedData(match: match, data: data, fromPlayer: delegate?.matchReceivedData(match: match, data: data, fromPlayer:
@ -111,7 +112,49 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
delegate?.matchEnded() delegate?.matchEnded()
} }
// The player state changed (eg. connected or disconnected)
func match(_ match: GKMatch, player: GKPlayer, didChange state: GKPlayerConnectionState) {
if mpMatch != match {
return
}
switch (state) {
case GKPlayerConnectionState.connected:
print("Player connected!")
if (!mpMatchStarted && match.expectedPlayerCount == 0) {
print("Ready to start match!")
startMatch()
}
case GKPlayerConnectionState.disconnected:
print("Player disconnected!")
mpMatchStarted = false
delegate!.matchEnded()
default:
print("Player unknown status!")
}
}
func startMatch() { func startMatch() {
mpMatch!.chooseBestHostingPlayer(completionHandler: {
(player) in
self.mpMatchStarted = true
if player == GKLocalPlayer.local {
print("I am the server")
self.isServer = true
} else {
print("I am a client")
self.isServer = false
self.serverPlayer = player
} }
self.delegate!.matchStarted()
})
}
func disconnect() {
if mpMatch != nil {
mpMatch?.disconnect()
}
}
} }

View File

@ -22,7 +22,7 @@ class MenuScene: SKScene {
position: CGPoint(x: midX, y: midY), position: CGPoint(x: midX, y: midY),
onButtonPress: { onButtonPress: {
MatchmakingHelper.sharedInstance.presentMatchmaker() MatchmakingHelper.sharedInstance.presentMatchmaker()
// self.loadScene(scene: GameScene(size: self.size)) self.loadScene(scene: GameScene(size: self.size))
})) }))
entityManager.add(Button(name: "settingsButton", entityManager.add(Button(name: "settingsButton",