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 isServer: Bool
var spieler1: GKPlayer?
var spieler2: GKPlayer?
var nameSpieler1 = ""
var nameSpieler2 = ""
var menusc: MenuScene?
let localPlayer: GKLocalPlayer = GKLocalPlayer.local
@ -58,9 +56,7 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
*/
func presentMatchmaker(scene: MenuScene) {
menusc = scene
print("I'm in 43")
guard GKLocalPlayer.local.isAuthenticated else {
print("Player ist nicht authentifiziert")
return
}
let request = GKMatchRequest()
@ -78,19 +74,17 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
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()
}
/*
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()
}
/*
@ -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
*/
func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFind match: GKMatch) {
print("I'm in 93")
viewController.dismiss(animated: true, completion: nil)
mpMatch = match
match.delegate = self
if !mpMatchStarted && match.expectedPlayerCount == 0 {
print("Bereit das Spiel zu starten!")
startMatch()
}
}
@ -113,49 +105,40 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
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:
// playerID)
delegate?.matchReceivedData(match: match, data: data, fromPlayer: playerID)
}
/*
Verbindung ist fehlgeschlagen
Verbindung/Matchmaking ist fehlgeschlagen
*/
private func match(match: GKMatch!, didFailWithError error: NSError!) {
print("Cant connect to any other player")
if mpMatch != match {
return
}
mpMatchStarted = false
//delegate?.matchEnded()
delegate?.matchEnded()
}
/*
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.disconnected:
print("Player disconnected!")
mpMatchStarted = false
// delegate?.matchEnded()
delegate?.matchEnded()
default:
print("Player unknown status!")
delegate?.matchEnded()
}
}
@ -163,33 +146,21 @@ 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.
*/
func startMatch() {
print("I'm in 166")
let p1 = mpMatch!.players[0]
let p2 = mpMatch!.players[2]
mpMatch!.chooseBestHostingPlayer(completionHandler: {
(player) in
self.mpMatchStarted = true
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
if (player == p1){
self.spieler2 = p1
}else{
self.spieler2 = p2
}
self.nameSpieler2 = self.spieler2!.displayName
print("I'm in 191")
} else {
self.isServer = false
}
self.delegate?.matchStarted()
self.menusc!.loadScene(scene: GameScene(size: self.menusc!.size))
})
@ -200,7 +171,6 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
Trennt die Verbindung vom Match
*/
func disconnect() {
print("I'm in 164")
if mpMatch != nil {
mpMatch?.disconnect()
}

View File

@ -21,8 +21,6 @@ class MenuScene: SKScene {
text: "Start Game",
position: CGPoint(x: midX, y: midY),
onButtonPress: {
//let mmh = MatchmakingHelper()
//mmh.delegate = self
MatchmakingHelper.sharedInstance.presentMatchmaker(scene: self)
}))
entityManager.add(Button(name: "settingsButton",
@ -44,14 +42,4 @@ class MenuScene: SKScene {
entityManager.getBackground()!.update(deltaTime: currentTime)
entityManager.getButtonByName(buttonName: "startGameButton").component(ofType: ButtonComponent.self)?.buttonNode.isEnabled = GameCenterHelper.isAuthenticated
}
}
extension MenuScene: GameKitHelperDelegate{
func matchStarted() {
print("Spiel gestartet")
}
}