Kommentare eingefügt
Spielerzuweisung wird getestet
This commit is contained in:
parent
b8b37de349
commit
3aef30b495
@ -10,20 +10,33 @@ 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)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
|
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 {
|
class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDelegate {
|
||||||
|
|
||||||
var delegate: GameKitHelperDelegate? //will receive all multiplayer events
|
var delegate: GameKitHelperDelegate?
|
||||||
var mpMatch: GKMatch? //represents network
|
var mpMatch: GKMatch?
|
||||||
var viewController: UIViewController?
|
var viewController: UIViewController?
|
||||||
var mpMatchStarted: Bool
|
var mpMatchStarted: Bool
|
||||||
var isServer: Bool
|
var isServer: Bool
|
||||||
var serverPlayer: GKPlayer?
|
var spieler1: GKPlayer?
|
||||||
|
var spieler2: GKPlayer?
|
||||||
|
var nameSpieler1 = ""
|
||||||
|
var nameSpieler2 = ""
|
||||||
var menusc: MenuScene?
|
var menusc: MenuScene?
|
||||||
let localPlayer: GKLocalPlayer = GKLocalPlayer.local
|
let localPlayer: GKLocalPlayer = GKLocalPlayer.local
|
||||||
|
|
||||||
@ -40,6 +53,9 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
|
|||||||
super.init()
|
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) {
|
func presentMatchmaker(scene: MenuScene) {
|
||||||
menusc = scene
|
menusc = scene
|
||||||
print("I'm in 43")
|
print("I'm in 43")
|
||||||
@ -58,61 +74,56 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
|
|||||||
viewController?.present(matchmakerVC!, animated: true, completion: nil)
|
viewController?.present(matchmakerVC!, animated: true, completion: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func findMatchWithMinPlayers(minPlayers: Int, maxPlayers: Int, viewController: UIViewController, delegate: GameKitHelperDelegate){
|
/*
|
||||||
|
Der User hat die Verbindung mit "Abbrechen" unterbrochen. GameCenter MatchMaking wird beendet.
|
||||||
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
|
|
||||||
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
|
/*
|
||||||
|
Wenn GameCenter kein match erstellen kann, wird der viewcontroller dismissed.
|
||||||
|
*/
|
||||||
func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFailWithError error: Error) {
|
func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFailWithError error: Error) {
|
||||||
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
|
/*
|
||||||
|
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) {
|
func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFind match: GKMatch) {
|
||||||
print("I'm in 93")
|
print("I'm in 93")
|
||||||
viewController.dismiss(animated: true, completion: nil)
|
viewController.dismiss(animated: true, completion: nil)
|
||||||
mpMatch = match
|
mpMatch = match
|
||||||
match.delegate = self
|
match.delegate = self
|
||||||
//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.
|
/*
|
||||||
|
Vom match erhaltene Spielerdaten
|
||||||
|
*/
|
||||||
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)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Verbindung ist fehlgeschlagen
|
||||||
|
*/
|
||||||
private func match(match: GKMatch!, didFailWithError error: NSError!) {
|
private func match(match: GKMatch!, didFailWithError error: NSError!) {
|
||||||
print("Cant connect to any other player")
|
print("Cant connect to any other player")
|
||||||
if mpMatch != match {
|
if mpMatch != match {
|
||||||
@ -122,52 +133,72 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
|
|||||||
//delegate?.matchEnded()
|
//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) {
|
func match(_ match: GKMatch, player: GKPlayer, didChange state: GKPlayerConnectionState) {
|
||||||
print("I'm in 123")
|
print("I'm in 123")
|
||||||
if mpMatch != match {
|
if mpMatch != match {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case GKPlayerConnectionState.connected:
|
case GKPlayerConnectionState.connected:
|
||||||
print("Player connected!")
|
print("Player connected!")
|
||||||
|
|
||||||
if (!mpMatchStarted && match.expectedPlayerCount == 0) {
|
if (!mpMatchStarted && match.expectedPlayerCount == 0) {
|
||||||
print("Ready to start match!")
|
print("Ready to start match!")
|
||||||
startMatch()
|
startMatch()
|
||||||
}
|
}
|
||||||
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!")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Ein Spieler wird als Host für das Match gewählt. Dieser ist Spieler 1. Im Anschluss wird die GameScene geladen.
|
||||||
|
*/
|
||||||
func startMatch() {
|
func startMatch() {
|
||||||
|
print("I'm in 166")
|
||||||
|
let p1 = mpMatch!.players[0]
|
||||||
|
let p2 = mpMatch!.players[2]
|
||||||
|
|
||||||
mpMatch!.chooseBestHostingPlayer(completionHandler: {
|
mpMatch!.chooseBestHostingPlayer(completionHandler: {
|
||||||
(player) in
|
(player) in
|
||||||
print("I'm in 147")
|
|
||||||
self.mpMatchStarted = true
|
self.mpMatchStarted = true
|
||||||
print(self.mpMatchStarted)
|
|
||||||
if player == GKLocalPlayer.local {
|
if player == GKLocalPlayer.local {
|
||||||
print("I am the server")
|
print("I am the server")
|
||||||
self.isServer = true
|
self.isServer = true
|
||||||
|
self.spieler1 = player
|
||||||
|
self.nameSpieler1 = self.spieler1!.displayName
|
||||||
|
print("I'm in 180")
|
||||||
} else {
|
} else {
|
||||||
print("I am a client")
|
print("I am a client")
|
||||||
self.isServer = false
|
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()
|
self.delegate?.matchStarted()
|
||||||
print("spiel gefunden")
|
|
||||||
self.menusc!.loadScene(scene: GameScene(size: self.menusc!.size))
|
self.menusc!.loadScene(scene: GameScene(size: self.menusc!.size))
|
||||||
print("scene müsste gewechselt sein")
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
Trennt die Verbindung vom Match
|
||||||
|
*/
|
||||||
func disconnect() {
|
func disconnect() {
|
||||||
print("I'm in 164")
|
print("I'm in 164")
|
||||||
if mpMatch != nil {
|
if mpMatch != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user