cleaned up code

This commit is contained in:
Chauntalle Schüle 2020-05-05 11:44:33 +02:00 committed by Chauntalle Schüle
parent 3aef30b495
commit d97b124b83
2 changed files with 11 additions and 53 deletions

View File

@ -34,9 +34,7 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
var mpMatchStarted: Bool var mpMatchStarted: Bool
var isServer: Bool var isServer: Bool
var spieler1: GKPlayer? var spieler1: GKPlayer?
var spieler2: GKPlayer?
var nameSpieler1 = "" var nameSpieler1 = ""
var nameSpieler2 = ""
var menusc: MenuScene? var menusc: MenuScene?
let localPlayer: GKLocalPlayer = GKLocalPlayer.local let localPlayer: GKLocalPlayer = GKLocalPlayer.local
@ -58,9 +56,7 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
*/ */
func presentMatchmaker(scene: MenuScene) { func presentMatchmaker(scene: MenuScene) {
menusc = scene menusc = scene
print("I'm in 43")
guard GKLocalPlayer.local.isAuthenticated else { guard GKLocalPlayer.local.isAuthenticated else {
print("Player ist nicht authentifiziert")
return return
} }
let request = GKMatchRequest() let request = GKMatchRequest()
@ -78,19 +74,17 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
Der User hat die Verbindung mit "Abbrechen" unterbrochen. GameCenter MatchMaking wird beendet. Der User hat die Verbindung mit "Abbrechen" unterbrochen. GameCenter MatchMaking wird beendet.
*/ */
func matchmakerViewControllerWasCancelled(_ viewController: GKMatchmakerViewController) { func matchmakerViewControllerWasCancelled(_ viewController: GKMatchmakerViewController) {
print("I'm in 78")
viewController.dismiss(animated: true, completion: nil) viewController.dismiss(animated: true, completion: nil)
// delegate?.matchEnded() delegate?.matchEnded()
} }
/* /*
Wenn GameCenter kein match erstellen kann, wird der viewcontroller dismissed. 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")
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()
} }
/* /*
@ -99,12 +93,10 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
z.B 0 gibt an, dass keine weiteren Spieler benötigt werden um das Match zu starten 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")
viewController.dismiss(animated: true, completion: nil) viewController.dismiss(animated: true, completion: nil)
mpMatch = match mpMatch = match
match.delegate = self match.delegate = self
if !mpMatchStarted && match.expectedPlayerCount == 0 { if !mpMatchStarted && match.expectedPlayerCount == 0 {
print("Bereit das Spiel zu starten!")
startMatch() startMatch()
} }
} }
@ -113,49 +105,40 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
Vom match erhaltene Spielerdaten 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")
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 Verbindung/Matchmaking 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")
if mpMatch != match { if mpMatch != match {
return return
} }
mpMatchStarted = false mpMatchStarted = false
//delegate?.matchEnded() delegate?.matchEnded()
} }
/* /*
Wird beim ändern des States/Zustands des Spielers aufgerufen udn gibt dessen Zustand zurück. 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")
if mpMatch != match { if mpMatch != match {
return return
} }
switch (state) { switch (state) {
case GKPlayerConnectionState.connected: case GKPlayerConnectionState.connected:
print("Player connected!")
if (!mpMatchStarted && match.expectedPlayerCount == 0) { if (!mpMatchStarted && match.expectedPlayerCount == 0) {
print("Ready to start match!")
startMatch() startMatch()
} }
case GKPlayerConnectionState.disconnected: case GKPlayerConnectionState.disconnected:
print("Player disconnected!")
mpMatchStarted = false mpMatchStarted = false
// delegate?.matchEnded() delegate?.matchEnded()
default: default:
print("Player unknown status!") delegate?.matchEnded()
} }
} }
@ -163,9 +146,6 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
Ein Spieler wird als Host für das Match gewählt. Dieser ist Spieler 1. Im Anschluss wird die GameScene geladen. 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
@ -173,23 +153,14 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
self.mpMatchStarted = true self.mpMatchStarted = true
if player == GKLocalPlayer.local { if player == GKLocalPlayer.local {
print("I am the server")
self.isServer = true self.isServer = true
self.spieler1 = player self.spieler1 = player
self.nameSpieler1 = self.spieler1!.displayName self.nameSpieler1 = self.spieler1!.displayName
print("I'm in 180")
} else {
print("I am a client")
self.isServer = false
if (player == p1){ } else {
self.spieler2 = p1 self.isServer = false
}else{
self.spieler2 = p2
}
self.nameSpieler2 = self.spieler2!.displayName
print("I'm in 191")
} }
self.delegate?.matchStarted() self.delegate?.matchStarted()
self.menusc!.loadScene(scene: GameScene(size: self.menusc!.size)) self.menusc!.loadScene(scene: GameScene(size: self.menusc!.size))
}) })
@ -200,7 +171,6 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
Trennt die Verbindung vom Match Trennt die Verbindung vom Match
*/ */
func disconnect() { func disconnect() {
print("I'm in 164")
if mpMatch != nil { if mpMatch != nil {
mpMatch?.disconnect() mpMatch?.disconnect()
} }

View File

@ -21,8 +21,6 @@ class MenuScene: SKScene {
text: "Start Game", text: "Start Game",
position: CGPoint(x: midX, y: midY), position: CGPoint(x: midX, y: midY),
onButtonPress: { onButtonPress: {
//let mmh = MatchmakingHelper()
//mmh.delegate = self
MatchmakingHelper.sharedInstance.presentMatchmaker(scene: self) MatchmakingHelper.sharedInstance.presentMatchmaker(scene: self)
})) }))
entityManager.add(Button(name: "settingsButton", entityManager.add(Button(name: "settingsButton",
@ -44,14 +42,4 @@ 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("Spiel gestartet")
}
} }