Skip to content

Commit 8abe470

Browse files
committed
Added Turn Based Match Maker
1 parent 89d9cc1 commit 8abe470

File tree

4 files changed

+207
-6
lines changed

4 files changed

+207
-6
lines changed

Examples/GKMatchmaker/GKMatchmaker.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Examples/GKMatchmaker/GKMatchmaker/Views/ContentView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ struct ContentView: View {
4949
GKMatchMakerView(
5050
minPlayers: 2,
5151
maxPlayers: 4,
52-
inviteMessage: "Letus play together!"
52+
inviteMessage: "Let us play together!"
5353
) {
5454
self.viewModel.showModal = false
5555
self.viewModel.currentState = "Player Canceled"

README.md

Lines changed: 89 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,96 @@ Using SPM add the following to your dependencies
3232

3333
``` 'GameKitUI', 'master', 'https://github.com/smuellner/GameKitUI.swift.git' ```
3434

35-
### CocoaPods
36-
Using CocoaPods add the following to your Podfile:
35+
## How to use?
3736

38-
```pod 'GameKitUI', '~> 1.0.0'```
37+
### GameCenter Authentication
3938

39+
To authenticate the player with GameCenter just show the authentication view **GKAuthenticationView**.
40+
41+
```swift
42+
import SwiftUI
43+
import GameKitUI
44+
45+
struct ContentView: View {
46+
var body: some View {
47+
GKAuthenticationView { (state) in
48+
switch state {
49+
case .started:
50+
print("Authentication Started")
51+
break
52+
case .failed:
53+
print("Failed")
54+
break
55+
case .deauthenticated:
56+
print("Deauthenticated")
57+
break
58+
case .succeeded:
59+
break
60+
}
61+
} failed: { (error) in
62+
print("Failed: \(error.localizedDescription)")
63+
} authenticated: { (playerName) in
64+
print("Hello \(playerName)")
65+
}
66+
}
67+
}
68+
```
69+
70+
### GameKit MatchMaker
71+
72+
Match making for a live match can be initiated via the **GKMatchMakerView**.
73+
74+
```swift
75+
import SwiftUI
76+
import GameKitUI
77+
78+
struct ContentView: View {
79+
var body: some View {
80+
GKMatchMakerView(
81+
minPlayers: 2,
82+
maxPlayers: 4,
83+
inviteMessage: "Let us play together!"
84+
) {
85+
print("Player Canceled")
86+
} failed: { (error) in
87+
print("Match Making Failed: \(error.localizedDescription)")
88+
} started: { (match) in
89+
print("Match Started")
90+
}
91+
}
92+
}
93+
```
94+
95+
96+
### GameKit TurnBasedMatchmaker
97+
98+
To start a turn based match use **GKTurnBasedMatchmakerView**.
99+
100+
```swift
101+
import SwiftUI
102+
import GameKitUI
103+
104+
struct ContentView: View {
105+
var body: some View {
106+
GKTurnBasedMatchmakerView(
107+
minPlayers: 2,
108+
maxPlayers: 4,
109+
inviteMessage: "Let us play together!"
110+
) {
111+
print("Player Canceled")
112+
} failed: { (error) in
113+
print("Match Making Failed: \(error.localizedDescription)")
114+
} started: { (match) in
115+
print("Match Started")
116+
}
117+
}
118+
}
119+
```
120+
121+
## Documentation
122+
+ [Apple Documentation GameKit](https://developer.apple.com/documentation/gamekit/)
123+
+ [raywenderlich.com: Game Center for iOS: Building a Turn-Based Game](https://www.raywenderlich.com/7544-game-center-for-ios-building-a-turn-based-game)
124+
+ [raywenderlich.com: Game Center Tutorial: How To Make A Simple Multiplayer Game with Sprite Kit: Part 1/2](https://www.raywenderlich.com/7544-game-center-for-ios-building-a-turn-based-game)
125+
+ [Medium: GameKit Real Time Multiplayer Tutorial](https://link.medium.com/Mwg3mSi4Ebb)
40126

41-
## How to use?
42127

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
///
2+
/// GKTurnBasedMatchmakerView.swift
3+
/// VocaBurn
4+
///
5+
/// Created by Sascha Müllner on 22.11.20.
6+
/// Unauthorized copying or usage of this file, via any medium is strictly prohibited.
7+
/// Proprietary and confidential.
8+
/// Copyright © 2020 Webblazer EG. All rights reserved.
9+
10+
import Foundation
11+
import GameKit
12+
import SwiftUI
13+
14+
public struct GKTurnBasedMatchmakerView: UIViewControllerRepresentable {
15+
16+
private let matchRequest: GKMatchRequest
17+
private let canceled: () -> Void
18+
private let failed: (Error) -> Void
19+
private let started: (GKTurnBasedMatch) -> Void
20+
21+
public init(matchRequest: GKMatchRequest,
22+
canceled: @escaping () -> Void,
23+
failed: @escaping (Error) -> Void,
24+
started: @escaping (GKTurnBasedMatch) -> Void) {
25+
self.matchRequest = matchRequest
26+
self.canceled = canceled
27+
self.failed = failed
28+
self.started = started
29+
}
30+
31+
public init(minPlayers: Int,
32+
maxPlayers: Int,
33+
inviteMessage: String,
34+
canceled: @escaping () -> Void,
35+
failed: @escaping (Error) -> Void,
36+
started: @escaping (GKTurnBasedMatch) -> Void) {
37+
let matchRequest = GKMatchRequest()
38+
matchRequest.minPlayers = minPlayers
39+
matchRequest.maxPlayers = maxPlayers
40+
matchRequest.inviteMessage = inviteMessage
41+
self.matchRequest = matchRequest
42+
self.canceled = canceled
43+
self.failed = failed
44+
self.started = started
45+
}
46+
47+
public func makeUIViewController(
48+
context: UIViewControllerRepresentableContext<GKTurnBasedMatchmakerView>) -> TurnBasedMatchmakerViewController {
49+
return TurnBasedMatchmakerViewController(
50+
matchRequest: self.matchRequest) {
51+
self.canceled()
52+
} failed: { (error) in
53+
self.failed(error)
54+
} started: { (match) in
55+
self.started(match)
56+
}
57+
}
58+
59+
public func updateUIViewController(
60+
_ uiViewController: TurnBasedMatchmakerViewController,
61+
context: UIViewControllerRepresentableContext<GKTurnBasedMatchmakerView>) {
62+
}
63+
}
64+
65+
public class TurnBasedMatchmakerViewController: UIViewController, GKTurnBasedMatchmakerViewControllerDelegate, GKMatchDelegate {
66+
67+
private let matchRequest: GKMatchRequest
68+
private let canceled: () -> Void
69+
private let failed: (Error) -> Void
70+
private let started: (GKTurnBasedMatch) -> Void
71+
72+
public init(matchRequest: GKMatchRequest,
73+
canceled: @escaping () -> Void,
74+
failed: @escaping (Error) -> Void,
75+
started: @escaping (GKTurnBasedMatch) -> Void) {
76+
self.matchRequest = matchRequest
77+
self.canceled = canceled
78+
self.failed = failed
79+
self.started = started
80+
super.init(nibName: nil, bundle: nil)
81+
}
82+
83+
required init?(coder: NSCoder) {
84+
fatalError("init(coder:) has not been implemented")
85+
}
86+
87+
public override func viewWillAppear(_ animated: Bool) {
88+
super.viewWillAppear(animated)
89+
let viewController = GKTurnBasedMatchmakerViewController(matchRequest: self.matchRequest)
90+
viewController.turnBasedMatchmakerDelegate = self
91+
self.addChild(viewController)
92+
viewController.view.translatesAutoresizingMaskIntoConstraints = false
93+
self.view.addSubview(viewController.view)
94+
NSLayoutConstraint.activate([
95+
viewController.view.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 0),
96+
viewController.view.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: 0),
97+
viewController.view.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 0),
98+
viewController.view.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 0)
99+
])
100+
}
101+
102+
public func turnBasedMatchmakerViewControllerWasCancelled(_ viewController: GKTurnBasedMatchmakerViewController) {
103+
viewController.dismiss(animated: true, completion: nil)
104+
self.canceled()
105+
}
106+
107+
public func turnBasedMatchmakerViewController(_ viewController: GKTurnBasedMatchmakerViewController, didFailWithError error: Error) {
108+
viewController.dismiss(animated: true, completion: nil)
109+
self.failed(error)
110+
}
111+
112+
public func turnBasedMatchmakerViewController(_ viewController: GKTurnBasedMatchmakerViewController, didFind match: GKTurnBasedMatch) {
113+
viewController.dismiss(animated: true, completion: nil)
114+
self.started(match)
115+
}
116+
}

0 commit comments

Comments
 (0)