Kommentare eingefügt
Spielerzuweisung wird getestet
This commit is contained in:
parent
b8b37de349
commit
3aef30b495
@ -10,20 +10,33 @@ import GameKit
|
||||
|
||||
protocol GameKitHelperDelegate {
|
||||
func matchStarted()
|
||||
// func matchEnded()
|
||||
// func matchReceivedData(match: GKMatch, data: NSData,
|
||||
// fromPlayer player: String)
|
||||
func matchEnded()
|
||||
func matchReceivedData(match: GKMatch, data: NSData,
|
||||
fromPlayer player: String)
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
Diese Klasse wird für das Matchmaking über das Gamecenter gebraucht
|
||||
@param: delegate: erhält alle Multiplayerevents
|
||||
@param: mpMatch: repräsentiert das Netzwerk, alle Mitspieler besitzen ein gemeinsames Spiel
|
||||
@param: viewController: ist nicht der ViewController des Gamecenters, sondern des Spiels
|
||||
@param: mpMatchStarted: gibt an, ob ein Match schon gestartet wurde
|
||||
@param: isServer: gibt an, ob der local player, als der Server fungiert
|
||||
@param: spieler1: ist immer der Spieler, der der Server ist
|
||||
@param: spieler2: ist immer der Spieler, der der Client ist
|
||||
@param: sharedInstance: ist eine geteilte Instanz für alle Spieler
|
||||
*/
|
||||
class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDelegate {
|
||||
|
||||
var delegate: GameKitHelperDelegate? //will receive all multiplayer events
|
||||
var mpMatch: GKMatch? //represents network
|
||||
|
||||
var delegate: GameKitHelperDelegate?
|
||||
var mpMatch: GKMatch?
|
||||
var viewController: UIViewController?
|
||||
var mpMatchStarted: Bool
|
||||
var isServer: Bool
|
||||
var serverPlayer: GKPlayer?
|
||||
var spieler1: GKPlayer?
|
||||
var spieler2: GKPlayer?
|
||||
var nameSpieler1 = ""
|
||||
var nameSpieler2 = ""
|
||||
var menusc: MenuScene?
|
||||
let localPlayer: GKLocalPlayer = GKLocalPlayer.local
|
||||
|
||||
@ -40,6 +53,9 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
|
||||
super.init()
|
||||
}
|
||||
|
||||
/*
|
||||
Diese Methode wechselt von der MenuScene zum GameCenter ViewController. Die Spieler werden hier gesucht und gematcht. Mit minplayers/maxplayers kann bestimmt werden, wie viele Spieler insgesamt zusammen miteinander spielen.
|
||||
*/
|
||||
func presentMatchmaker(scene: MenuScene) {
|
||||
menusc = scene
|
||||
print("I'm in 43")
|
||||
@ -58,61 +74,56 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
|
||||
viewController?.present(matchmakerVC!, animated: true, completion: nil)
|
||||
}
|
||||
|
||||
func findMatchWithMinPlayers(minPlayers: Int, maxPlayers: Int, viewController: UIViewController, delegate: GameKitHelperDelegate){
|
||||
|
||||
guard GKLocalPlayer.local.isAuthenticated else {
|
||||
print("Player ist nicht authentifiziert")
|
||||
return
|
||||
}
|
||||
|
||||
let request = GKMatchRequest.init()
|
||||
request.minPlayers = minPlayers
|
||||
request.maxPlayers = maxPlayers
|
||||
|
||||
let matchmakerVC = GKMatchmakerViewController.init(matchRequest: request)
|
||||
matchmakerVC!.matchmakerDelegate = self
|
||||
|
||||
viewController.present(matchmakerVC!, animated: true, completion: nil)
|
||||
}
|
||||
|
||||
//The user has cancelled matchmaking
|
||||
/*
|
||||
Der User hat die Verbindung mit "Abbrechen" unterbrochen. GameCenter MatchMaking wird beendet.
|
||||
*/
|
||||
func matchmakerViewControllerWasCancelled(_ viewController: GKMatchmakerViewController) {
|
||||
print("I'm in 78")
|
||||
viewController.dismiss(animated: true, completion: nil)
|
||||
// delegate?.matchEnded()
|
||||
// delegate?.matchEnded()
|
||||
}
|
||||
|
||||
//Matchmaking has failed with an error
|
||||
/*
|
||||
Wenn GameCenter kein match erstellen kann, wird der viewcontroller dismissed.
|
||||
*/
|
||||
func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFailWithError error: Error) {
|
||||
print("I'm in 85")
|
||||
viewController.dismiss(animated: true, completion: nil)
|
||||
print("Error finding match", error.localizedDescription)
|
||||
// delegate?.matchEnded()
|
||||
// delegate?.matchEnded()
|
||||
}
|
||||
|
||||
// A peer-to-peer match has been found, the game should start
|
||||
/*
|
||||
Gamecenter hat erfolgreich ein Match gefunden, das Spiel kann gestartet werden.
|
||||
expectedPlayerCount: Die verbleibende Anzahl von Spielern, die sich noch nicht mit dem Spiel verbunden haben
|
||||
z.B 0 gibt an, dass keine weiteren Spieler benötigt werden um das Match zu starten
|
||||
*/
|
||||
func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFind match: GKMatch) {
|
||||
print("I'm in 93")
|
||||
viewController.dismiss(animated: true, completion: nil)
|
||||
mpMatch = match
|
||||
match.delegate = self
|
||||
//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()
|
||||
}
|
||||
}
|
||||
|
||||
// The match received data sent from the player.
|
||||
/*
|
||||
Vom match erhaltene Spielerdaten
|
||||
*/
|
||||
private func match(match: GKMatch!, didReceiveData data: NSData!,fromPlayer playerID: String!) {
|
||||
print("I'm in 106")
|
||||
|
||||
if mpMatch != match { return }
|
||||
// delegate?.matchReceivedData(match: match, data: data, fromPlayer:
|
||||
// delegate?.matchReceivedData(match: match, data: data, fromPlayer:
|
||||
// playerID)
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Verbindung ist fehlgeschlagen
|
||||
*/
|
||||
private func match(match: GKMatch!, didFailWithError error: NSError!) {
|
||||
print("Cant connect to any other player")
|
||||
if mpMatch != match {
|
||||
@ -122,52 +133,72 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
|
||||
//delegate?.matchEnded()
|
||||
}
|
||||
|
||||
// The player state changed (eg. connected or disconnected)
|
||||
/*
|
||||
Wird beim ändern des States/Zustands des Spielers aufgerufen udn gibt dessen Zustand zurück.
|
||||
*/
|
||||
func match(_ match: GKMatch, player: GKPlayer, didChange state: GKPlayerConnectionState) {
|
||||
print("I'm in 123")
|
||||
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.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()
|
||||
print("Player disconnected!")
|
||||
mpMatchStarted = false
|
||||
// delegate?.matchEnded()
|
||||
default:
|
||||
print("Player unknown status!")
|
||||
print("Player unknown status!")
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Ein Spieler wird als Host für das Match gewählt. Dieser ist Spieler 1. Im Anschluss wird die GameScene geladen.
|
||||
*/
|
||||
func startMatch() {
|
||||
print("I'm in 166")
|
||||
let p1 = mpMatch!.players[0]
|
||||
let p2 = mpMatch!.players[2]
|
||||
|
||||
mpMatch!.chooseBestHostingPlayer(completionHandler: {
|
||||
(player) in
|
||||
print("I'm in 147")
|
||||
|
||||
self.mpMatchStarted = true
|
||||
print(self.mpMatchStarted)
|
||||
|
||||
if player == GKLocalPlayer.local {
|
||||
print("I am the server")
|
||||
self.isServer = true
|
||||
self.spieler1 = player
|
||||
self.nameSpieler1 = self.spieler1!.displayName
|
||||
print("I'm in 180")
|
||||
} else {
|
||||
print("I am a client")
|
||||
self.isServer = false
|
||||
self.serverPlayer = player
|
||||
|
||||
if (player == p1){
|
||||
self.spieler2 = p1
|
||||
}else{
|
||||
self.spieler2 = p2
|
||||
}
|
||||
self.nameSpieler2 = self.spieler2!.displayName
|
||||
print("I'm in 191")
|
||||
}
|
||||
/// self.delegate?.matchStarted()
|
||||
print("spiel gefunden")
|
||||
self.delegate?.matchStarted()
|
||||
self.menusc!.loadScene(scene: GameScene(size: self.menusc!.size))
|
||||
print("scene müsste gewechselt sein")
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Trennt die Verbindung vom Match
|
||||
*/
|
||||
func disconnect() {
|
||||
print("I'm in 164")
|
||||
if mpMatch != nil {
|
||||
|
Loading…
x
Reference in New Issue
Block a user