Implement GameCenter button, achivement progress func

This commit is contained in:
Simon Kellner 2020-06-02 15:29:53 +02:00
parent 5bb1b6764a
commit bc42399fe3
4 changed files with 64 additions and 4 deletions

View File

@ -118,6 +118,7 @@ class EntityManager {
scene.addChild(fire) scene.addChild(fire)
} }
} }
GameCenterManager.sharedInstance.addAchievementProgress(identifier: "de.hft.stuttgart.ip2.goldwars.capture.fifty.bases", increasePercentComplete: 2)
} }
} }
} }

View File

@ -50,21 +50,33 @@ class HUD: GKEntity {
text: "Spy", text: "Spy",
isEnabled: true, isEnabled: true,
position: CGPoint(x: EntityManager.gameEMInstance.scene.size.width * 0.75, y: EntityManager.gameEMInstance.scene.size.height * 0.1), position: CGPoint(x: EntityManager.gameEMInstance.scene.size.width * 0.75, y: EntityManager.gameEMInstance.scene.size.height * 0.1),
onButtonPress: { print("Not implemented") } onButtonPress: {
print("Not implemented")
GameCenterManager.sharedInstance.addAchievementProgress(identifier: "de.hft.stuttgart.ip2.goldwars.skill.first.time", increasePercentComplete: 100)
GameCenterManager.sharedInstance.addAchievementProgress(identifier: "de.hft.stuttgart.ip2.goldwars.skill.spy.ten", increasePercentComplete: 10)
}
) )
defSkill = SingeClickButtonNode( defSkill = SingeClickButtonNode(
textureName: "yellow_circle", textureName: "yellow_circle",
text: "Def", text: "Def",
isEnabled: true, isEnabled: true,
position: CGPoint(x: EntityManager.gameEMInstance.scene.size.width * 0.85, y: EntityManager.gameEMInstance.scene.size.height * 0.1), position: CGPoint(x: EntityManager.gameEMInstance.scene.size.width * 0.85, y: EntityManager.gameEMInstance.scene.size.height * 0.1),
onButtonPress: {DataService.sharedInstance.localRoundData.hasDefenceBoost = true} onButtonPress: {
DataService.sharedInstance.localRoundData.hasDefenceBoost = true
GameCenterManager.sharedInstance.addAchievementProgress(identifier: "de.hft.stuttgart.ip2.goldwars.skill.first.time", increasePercentComplete: 100)
GameCenterManager.sharedInstance.addAchievementProgress(identifier: "de.hft.stuttgart.ip2.goldwars.skill.def.ten", increasePercentComplete: 10)
}
) )
atkSkill = SingeClickButtonNode( atkSkill = SingeClickButtonNode(
textureName: "yellow_circle", textureName: "yellow_circle",
text: "Atk", text: "Atk",
isEnabled: true, isEnabled: true,
position: CGPoint(x: EntityManager.gameEMInstance.scene.size.width * 0.95, y: EntityManager.gameEMInstance.scene.size.height * 0.1), position: CGPoint(x: EntityManager.gameEMInstance.scene.size.width * 0.95, y: EntityManager.gameEMInstance.scene.size.height * 0.1),
onButtonPress: {DataService.sharedInstance.localRoundData.hasAttackBoost = true} onButtonPress: {
DataService.sharedInstance.localRoundData.hasAttackBoost = true
GameCenterManager.sharedInstance.addAchievementProgress(identifier: "de.hft.stuttgart.ip2.goldwars.skill.first.time", increasePercentComplete: 100)
GameCenterManager.sharedInstance.addAchievementProgress(identifier: "de.hft.stuttgart.ip2.goldwars.skill.atk.ten", increasePercentComplete: 10)
}
) )
finishButton = SingeClickButtonNode( finishButton = SingeClickButtonNode(

View File

@ -25,7 +25,7 @@ struct State: Codable {
let state: Int let state: Int
} }
final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate ,GKMatchDelegate,GKLocalPlayerListener{ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKGameCenterControllerDelegate ,GKMatchDelegate,GKLocalPlayerListener{
static let sharedInstance = GameCenterManager() static let sharedInstance = GameCenterManager()
@ -75,6 +75,38 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate ,GKM
viewController?.present(matchmakerVC!, animated: true, completion: nil) viewController?.present(matchmakerVC!, animated: true, completion: nil)
} }
func presentGameCenter() {
let gameCenterController: GKGameCenterViewController = GKGameCenterViewController.init()
gameCenterController.gameCenterDelegate = self
gameCenterController.viewState = .achievements
viewController?.present(gameCenterController, animated: true, completion: nil)
}
func addAchievementProgress(identifier: String, increasePercentComplete: Double) {
GKAchievement.loadAchievements { (achievements: [GKAchievement]?, err: Error?) in
var achievementExists: Bool = false
achievements?.forEach({ (achievement: GKAchievement) in
print(achievement.identifier)
if achievement.identifier == identifier {
achievementExists = true
achievement.percentComplete += increasePercentComplete
achievement.showsCompletionBanner = true
GKAchievement.report([achievement]) { (error) in
print(error?.localizedDescription ?? "")
}
}
})
if !achievementExists {
let newAchievement: GKAchievement = GKAchievement.init(identifier: identifier)
newAchievement.showsCompletionBanner = true
newAchievement.percentComplete = increasePercentComplete
GKAchievement.report([newAchievement]) { (error) in
print(error?.localizedDescription ?? "")
}
}
}
}
func match(_ match: GKMatch, didReceive data: Data, fromRemotePlayer player: GKPlayer) { func match(_ match: GKMatch, didReceive data: Data, fromRemotePlayer player: GKPlayer) {
if myMatch != match { return } if myMatch != match { return }
let jsonDecoder = JSONDecoder() let jsonDecoder = JSONDecoder()
@ -206,6 +238,10 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate ,GKM
} }
} }
func gameCenterViewControllerDidFinish(_ gameCenterViewController: GKGameCenterViewController) {
gameCenterViewController.dismiss(animated: true, completion: nil)
}
func disconnect() { func disconnect() {
if myMatch != nil { if myMatch != nil {
myMatch?.disconnect() myMatch?.disconnect()

View File

@ -42,6 +42,17 @@ class MenuScene: SKScene {
let scene = SettingsScene(size: self.size) let scene = SettingsScene(size: self.size)
self.loadScene(scene: scene) self.loadScene(scene: scene)
})) }))
entityManager.add(Button(name: "gameCenterButton",
textureName: "yellow_button04",
text: "GameCenter",
position: CGPoint(x: midX, y: midY - 160),
onButtonPress: {
if GameCenterManager.isAuthenticated {
GameCenterManager.sharedInstance.presentGameCenter()
}else {
GameCenterManager.sharedInstance.authUser()
}
}))
entityManager.add(Background(size: self.size)) entityManager.add(Background(size: self.size))
entityManager.add(SpinningLogoEntity(position: CGPoint(x: midX, y: midY + 200))) entityManager.add(SpinningLogoEntity(position: CGPoint(x: midX, y: midY + 200)))