Created
September 14, 2024 22:33
-
-
Save Matt54/92ae309024fa60d3928c8c30f87076ab to your computer and use it in GitHub Desktop.
Trying to figure out the boilerplate for a simple GameKit implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import GameKit | |
import SwiftUI | |
struct GameTestView: View { | |
@StateObject private var gameKitManager = GameKitManager() | |
@State private var timer: Timer? | |
var body: some View { | |
VStack { | |
Text("Score: \(gameKitManager.score)") | |
Text("Time: \(gameKitManager.timeRemaining)") | |
Button("Shoot Target") { | |
gameKitManager.incrementScore() | |
} | |
.disabled(gameKitManager.isGameOver) | |
Button("Show Leaderboard") { | |
gameKitManager.showLeaderboard() | |
} | |
} | |
.onAppear { | |
startGame() | |
} | |
.alert(isPresented: $gameKitManager.isGameOver) { | |
Alert(title: Text("Game Over"), | |
message: Text("Your score: \(gameKitManager.score)"), | |
dismissButton: .default(Text("OK")) { | |
gameKitManager.submitScore() | |
resetGame() | |
}) | |
} | |
} | |
func startGame() { | |
timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { _ in | |
if gameKitManager.timeRemaining > 0 { | |
gameKitManager.timeRemaining -= 1 | |
} else { | |
endGame() | |
} | |
} | |
} | |
func endGame() { | |
timer?.invalidate() | |
gameKitManager.isGameOver = true | |
} | |
func resetGame() { | |
gameKitManager.score = 0 | |
gameKitManager.timeRemaining = 60 | |
gameKitManager.isGameOver = false | |
startGame() | |
} | |
} | |
class GameKitManager: NSObject, GKGameCenterControllerDelegate, ObservableObject { | |
@Published var isAuthenticated = false | |
@Published var score = 0 | |
@Published var timeRemaining = 60 | |
@Published var isGameOver = false | |
override init() { | |
super.init() | |
authenticatePlayer() | |
} | |
func authenticatePlayer() { | |
GKLocalPlayer.local.authenticateHandler = { viewController, error in | |
if let viewController = viewController { | |
// Present the view controller if needed | |
} else if let error = error { | |
print("Error: \(error.localizedDescription).") | |
} else { | |
self.isAuthenticated = true | |
self.setupAccessPoint() | |
} | |
} | |
} | |
func setupAccessPoint() { | |
GKAccessPoint.shared.location = .topLeading | |
GKAccessPoint.shared.showHighlights = true | |
GKAccessPoint.shared.isActive = true | |
} | |
func incrementScore() { | |
score += 1 | |
} | |
func submitScore() { | |
GKLeaderboard.submitScore(score, context: 0, player: GKLocalPlayer.local, | |
leaderboardIDs: ["your_leaderboard_id"]) { error in | |
if let error = error { | |
print("Error submitting score: \(error.localizedDescription)") | |
} | |
} | |
} | |
func showLeaderboard() { | |
if let rootViewController = UIApplication.shared.windows.first?.rootViewController { | |
let viewController = GKGameCenterViewController(leaderboardID: "your_leaderboard_id", | |
playerScope: .global, | |
timeScope: .allTime) | |
viewController.gameCenterDelegate = self | |
rootViewController.present(viewController, animated: true) | |
} | |
} | |
func gameCenterViewControllerDidFinish(_ gameCenterViewController: GKGameCenterViewController) { | |
gameCenterViewController.dismiss(animated: true) | |
} | |
} | |
extension GameKitManager { | |
func reportAchievement(identifier: String, percentComplete: Double) { | |
let achievement = GKAchievement(identifier: identifier) | |
achievement.percentComplete = percentComplete | |
GKAchievement.report([achievement]) { error in | |
if let error = error { | |
print("Error reporting achievement: \(error.localizedDescription)") | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment