matchmaking funktioniert

This commit is contained in:
Chauntalle Schüle 2020-05-04 23:44:23 +02:00 committed by Chauntalle Schüle
parent e21395d0db
commit e1d5af8a4a
2 changed files with 24 additions and 14 deletions

View File

@ -10,9 +10,9 @@ import GameKit
protocol GameKitHelperDelegate { protocol GameKitHelperDelegate {
func matchStarted() func matchStarted()
func matchEnded() // func matchEnded()
func matchReceivedData(match: GKMatch, data: NSData, // func matchReceivedData(match: GKMatch, data: NSData,
fromPlayer player: String) // fromPlayer player: String)
} }
@ -24,6 +24,7 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
var mpMatchStarted: Bool var mpMatchStarted: Bool
var isServer: Bool var isServer: Bool
var serverPlayer: GKPlayer? var serverPlayer: GKPlayer?
var menusc: MenuScene?
let localPlayer: GKLocalPlayer = GKLocalPlayer.local let localPlayer: GKLocalPlayer = GKLocalPlayer.local
static let sharedInstance = MatchmakingHelper() static let sharedInstance = MatchmakingHelper()
@ -40,6 +41,7 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
} }
func presentMatchmaker() { func presentMatchmaker() {
// menusc = scene
print("I'm in 43") print("I'm in 43")
guard GKLocalPlayer.local.isAuthenticated else { guard GKLocalPlayer.local.isAuthenticated else {
print("Player ist nicht authentifiziert") print("Player ist nicht authentifiziert")
@ -77,7 +79,7 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
func matchmakerViewControllerWasCancelled(_ viewController: GKMatchmakerViewController) { func matchmakerViewControllerWasCancelled(_ viewController: GKMatchmakerViewController) {
print("I'm in 78") print("I'm in 78")
viewController.dismiss(animated: true, completion: nil) viewController.dismiss(animated: true, completion: nil)
delegate?.matchEnded() // delegate?.matchEnded()
} }
//Matchmaking has failed with an error //Matchmaking has failed with an error
@ -85,7 +87,7 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
print("I'm in 85") print("I'm in 85")
viewController.dismiss(animated: true, completion: nil) viewController.dismiss(animated: true, completion: nil)
print("Error finding match", error.localizedDescription) print("Error finding match", error.localizedDescription)
delegate?.matchEnded() // delegate?.matchEnded()
} }
// A peer-to-peer match has been found, the game should start // A peer-to-peer match has been found, the game should start
@ -104,9 +106,10 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
// The match received data sent from the player. // 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!) {
print("I'm in 106") print("I'm in 106")
if mpMatch != match { return } if mpMatch != match { return }
delegate?.matchReceivedData(match: match, data: data, fromPlayer: // delegate?.matchReceivedData(match: match, data: data, fromPlayer:
playerID) // playerID)
} }
private func match(match: GKMatch!, didFailWithError error: NSError!) { private func match(match: GKMatch!, didFailWithError error: NSError!) {
@ -115,7 +118,7 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
return return
} }
mpMatchStarted = false mpMatchStarted = false
delegate?.matchEnded() //delegate?.matchEnded()
} }
// The player state changed (eg. connected or disconnected) // The player state changed (eg. connected or disconnected)
@ -136,7 +139,7 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
case GKPlayerConnectionState.disconnected: case GKPlayerConnectionState.disconnected:
print("Player disconnected!") print("Player disconnected!")
mpMatchStarted = false mpMatchStarted = false
delegate?.matchEnded() // delegate?.matchEnded()
default: default:
print("Player unknown status!") print("Player unknown status!")
} }
@ -158,6 +161,7 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
} }
self.delegate?.matchStarted() self.delegate?.matchStarted()
// self.menusc!.loadScene(scene: GameScene(size: self.menusc!.size))
}) })
} }

View File

@ -22,11 +22,6 @@ class MenuScene: SKScene {
position: CGPoint(x: midX, y: midY), position: CGPoint(x: midX, y: midY),
onButtonPress: { onButtonPress: {
MatchmakingHelper.sharedInstance.presentMatchmaker() MatchmakingHelper.sharedInstance.presentMatchmaker()
print(MatchmakingHelper.sharedInstance.mpMatchStarted)
// if MatchmakingHelper.sharedInstance.mpMatchStarted{
self.loadScene(scene: GameScene(size: self.size))
print(MatchmakingHelper.sharedInstance.mpMatchStarted)
// }
})) }))
entityManager.add(Button(name: "settingsButton", entityManager.add(Button(name: "settingsButton",
iconName: "", iconName: "",
@ -47,4 +42,15 @@ class MenuScene: SKScene {
entityManager.getBackground()!.update(deltaTime: currentTime) entityManager.getBackground()!.update(deltaTime: currentTime)
entityManager.getButtonByName(buttonName: "startGameButton").component(ofType: ButtonComponent.self)?.buttonNode.isEnabled = GameCenterHelper.isAuthenticated entityManager.getButtonByName(buttonName: "startGameButton").component(ofType: ButtonComponent.self)?.buttonNode.isEnabled = GameCenterHelper.isAuthenticated
} }
}
extension MenuScene: GameKitHelperDelegate{
func matchStarted() {
print("halo i bims")
self.loadScene(scene: GameScene(size: self.size))
}
} }