diff --git a/GoldWars/GoldWars/GameViewController.swift b/GoldWars/GoldWars/GameViewController.swift index 4eed433..e4a2f43 100644 --- a/GoldWars/GoldWars/GameViewController.swift +++ b/GoldWars/GoldWars/GameViewController.swift @@ -24,6 +24,7 @@ class GameViewController: UIViewController { view.showsNodeCount = true GameCenterHelper.helper.viewController = self + MatchmakingHelper.sharedInstance.viewController = self } } diff --git a/GoldWars/GoldWars/MatchmakingHelper.swift b/GoldWars/GoldWars/MatchmakingHelper.swift new file mode 100644 index 0000000..e38cc92 --- /dev/null +++ b/GoldWars/GoldWars/MatchmakingHelper.swift @@ -0,0 +1,117 @@ +// +// MatchmakingHelper.swift +// GoldWars +// +// Created by Chauntalle Schüle on 03.05.20. +// Copyright © 2020 SP2. All rights reserved. +// + +import GameKit + +protocol GameKitHelperDelegate { + func matchStarted() + func matchEnded() + func matchReceivedData(match: GKMatch, data: NSData, + fromPlayer player: String) + +} + +class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDelegate { + + var delegate: GameKitHelperDelegate? //will receive all multiplayer events + var mpMatch: GKMatch? //represents network + var viewController: UIViewController? + var mpMatchStarted: Bool + let localPlayer: GKLocalPlayer = GKLocalPlayer.local + + static let sharedInstance = MatchmakingHelper() + + + static var isAuthenticated: Bool{ + return GKLocalPlayer.local.isAuthenticated + } + + override init() { + mpMatchStarted = false + super.init() + } + + func presentMatchmaker() { + // 1 + guard GKLocalPlayer.local.isAuthenticated else { + print("Player ist nicht authentifiziert") + return + } + + // 2 + let request = GKMatchRequest() + + request.minPlayers = 2 + request.maxPlayers = 2 + // 3 + request.inviteMessage = "Willst du GoldWars spielen?" + + // 4 + let vc = GKMatchmakerViewController.init(matchRequest: request) + viewController?.present(vc!, 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 + func matchmakerViewControllerWasCancelled(_ viewController: GKMatchmakerViewController) { + viewController.dismiss(animated: true, completion: nil) + delegate?.matchEnded() + } + + //Matchmaking has failed with an error + func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFailWithError error: Error) { + viewController.dismiss(animated: true, completion: nil) + print("Error finding match", error.localizedDescription) + delegate?.matchEnded() + } + + // A peer-to-peer match has been found, the game should start + func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFind match: GKMatch) { + 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() + } + } + private func match(match: GKMatch!, didReceiveData data: NSData!,fromPlayer playerID: String!) { + if mpMatch != match { return } + delegate?.matchReceivedData(match: match, data: data, fromPlayer: + playerID) + } + + private func match(match: GKMatch!, didFailWithError error: NSError!) { + if mpMatch != match { + return + } + mpMatchStarted = false + delegate?.matchEnded() + } + + func startMatch(){ + + } +} diff --git a/GoldWars/GoldWars/Scenes/MenuScene.swift b/GoldWars/GoldWars/Scenes/MenuScene.swift index 8164439..b4ff9db 100644 --- a/GoldWars/GoldWars/Scenes/MenuScene.swift +++ b/GoldWars/GoldWars/Scenes/MenuScene.swift @@ -21,7 +21,9 @@ class MenuScene: SKScene { text: "Start Game", position: CGPoint(x: midX, y: midY), onButtonPress: { - self.loadScene(scene: GameScene(size: self.size)) + MatchmakingHelper.sharedInstance.presentMatchmaker() + // self.loadScene(scene: GameScene(size: self.size)) + })) entityManager.add(Button(name: "settingsButton", iconName: "", @@ -29,6 +31,7 @@ class MenuScene: SKScene { position: CGPoint(x: midX, y: midY - 80 ), onButtonPress: { //TODO: create Settings Scene + MatchmakingHelper.sharedInstance.presentMatchmaker() })) entityManager.add(Background(size: self.size)) }