Update MatchMakerHelper, added methods
This commit is contained in:
parent
017e99183b
commit
54d31d929d
@ -22,6 +22,8 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
|
||||
var mpMatch: GKMatch? //represents network
|
||||
var viewController: UIViewController?
|
||||
var mpMatchStarted: Bool
|
||||
var isServer: Bool
|
||||
var serverPlayer: GKPlayer?
|
||||
let localPlayer: GKLocalPlayer = GKLocalPlayer.local
|
||||
|
||||
static let sharedInstance = MatchmakingHelper()
|
||||
@ -33,27 +35,24 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
|
||||
|
||||
override init() {
|
||||
mpMatchStarted = false
|
||||
isServer = false
|
||||
super.init()
|
||||
}
|
||||
|
||||
func presentMatchmaker() {
|
||||
// 1
|
||||
guard GKLocalPlayer.local.isAuthenticated else {
|
||||
print("Player ist nicht authentifiziert")
|
||||
return
|
||||
}
|
||||
|
||||
// 2
|
||||
let request = GKMatchRequest()
|
||||
|
||||
request.minPlayers = 2
|
||||
request.maxPlayers = 2
|
||||
// 3
|
||||
request.inviteMessage = "Willst du GoldWars spielen?"
|
||||
|
||||
// 4
|
||||
let vc = GKMatchmakerViewController.init(matchRequest: request)
|
||||
viewController?.present(vc!, animated: true, completion: nil)
|
||||
let matchmakerVC = GKMatchmakerViewController.init(matchRequest: request)
|
||||
matchmakerVC!.matchmakerDelegate = self
|
||||
viewController?.present(matchmakerVC!, animated: true, completion: nil)
|
||||
}
|
||||
|
||||
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
|
||||
if !mpMatchStarted && match.expectedPlayerCount == 0 {
|
||||
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!) {
|
||||
if mpMatch != match { return }
|
||||
delegate?.matchReceivedData(match: match, data: data, fromPlayer:
|
||||
@ -111,7 +112,49 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
|
||||
delegate?.matchEnded()
|
||||
}
|
||||
|
||||
func startMatch(){
|
||||
// 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() {
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ class MenuScene: SKScene {
|
||||
position: CGPoint(x: midX, y: midY),
|
||||
onButtonPress: {
|
||||
MatchmakingHelper.sharedInstance.presentMatchmaker()
|
||||
// self.loadScene(scene: GameScene(size: self.size))
|
||||
self.loadScene(scene: GameScene(size: self.size))
|
||||
|
||||
}))
|
||||
entityManager.add(Button(name: "settingsButton",
|
||||
|
Loading…
Reference in New Issue
Block a user