Add MatchmakingViewController

This commit is contained in:
Chauntalle Schüle 2020-05-04 15:13:04 +02:00 committed by Chauntalle Schüle
parent bf88e1af83
commit 71a59f2fa0
3 changed files with 122 additions and 1 deletions

View File

@ -24,6 +24,7 @@ class GameViewController: UIViewController {
view.showsNodeCount = true view.showsNodeCount = true
GameCenterHelper.helper.viewController = self GameCenterHelper.helper.viewController = self
MatchmakingHelper.sharedInstance.viewController = self
} }
} }

View File

@ -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(){
}
}

View File

@ -21,7 +21,9 @@ class MenuScene: SKScene {
text: "Start Game", text: "Start Game",
position: CGPoint(x: midX, y: midY), position: CGPoint(x: midX, y: midY),
onButtonPress: { onButtonPress: {
self.loadScene(scene: GameScene(size: self.size)) MatchmakingHelper.sharedInstance.presentMatchmaker()
// self.loadScene(scene: GameScene(size: self.size))
})) }))
entityManager.add(Button(name: "settingsButton", entityManager.add(Button(name: "settingsButton",
iconName: "", iconName: "",
@ -29,6 +31,7 @@ class MenuScene: SKScene {
position: CGPoint(x: midX, y: midY - 80 ), position: CGPoint(x: midX, y: midY - 80 ),
onButtonPress: { onButtonPress: {
//TODO: create Settings Scene //TODO: create Settings Scene
MatchmakingHelper.sharedInstance.presentMatchmaker()
})) }))
entityManager.add(Background(size: self.size)) entityManager.add(Background(size: self.size))
} }