diff --git a/GoldWars/GoldWars/Entities/EntityManager.swift b/GoldWars/GoldWars/Entities/EntityManager.swift index 69cda81..ebeec1b 100644 --- a/GoldWars/GoldWars/Entities/EntityManager.swift +++ b/GoldWars/GoldWars/Entities/EntityManager.swift @@ -118,6 +118,7 @@ class EntityManager { scene.addChild(fire) } } + GameCenterManager.sharedInstance.addAchievementProgress(identifier: "de.hft.stuttgart.ip2.goldwars.capture.fifty.bases", increasePercentComplete: 2) } } } diff --git a/GoldWars/GoldWars/Entities/HUD.swift b/GoldWars/GoldWars/Entities/HUD.swift index 24f92a0..324e742 100644 --- a/GoldWars/GoldWars/Entities/HUD.swift +++ b/GoldWars/GoldWars/Entities/HUD.swift @@ -50,21 +50,33 @@ class HUD: GKEntity { text: "Spy", isEnabled: true, 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( textureName: "yellow_circle", text: "Def", isEnabled: true, 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( textureName: "yellow_circle", text: "Atk", isEnabled: true, 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( diff --git a/GoldWars/GoldWars/GameCenterManager.swift b/GoldWars/GoldWars/GameCenterManager.swift index e159b18..64bfd7a 100644 --- a/GoldWars/GoldWars/GameCenterManager.swift +++ b/GoldWars/GoldWars/GameCenterManager.swift @@ -25,7 +25,7 @@ struct State: Codable { let state: Int } -final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate ,GKMatchDelegate,GKLocalPlayerListener{ +final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKGameCenterControllerDelegate ,GKMatchDelegate,GKLocalPlayerListener{ static let sharedInstance = GameCenterManager() @@ -75,6 +75,38 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate ,GKM 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) { if myMatch != match { return } 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() { if myMatch != nil { myMatch?.disconnect() diff --git a/GoldWars/GoldWars/Scenes/MenuScene.swift b/GoldWars/GoldWars/Scenes/MenuScene.swift index 6281713..82469a3 100644 --- a/GoldWars/GoldWars/Scenes/MenuScene.swift +++ b/GoldWars/GoldWars/Scenes/MenuScene.swift @@ -42,6 +42,17 @@ class MenuScene: SKScene { let scene = SettingsScene(size: self.size) 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(SpinningLogoEntity(position: CGPoint(x: midX, y: midY + 200)))