Merge branch '65-achivements-einbauen' into 'development'

Resolve "Achivements einbauen"

Closes #65

See merge request marcel.schwarz/software-projekt-2!96
This commit is contained in:
Aldin Duraki 2020-06-02 18:36:18 +00:00
commit 0c2b621b05
4 changed files with 64 additions and 4 deletions

View File

@ -118,6 +118,7 @@ class EntityManager {
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",
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(

View File

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

View File

@ -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)))