Ejemplo n.º 1
0
 public void submitAchievement(string identifier, double percentComplete, string achievementName)
 {
     if (earnedAchievementCache == null)
     {
         GKAchievement.LoadAchievements(new GKCompletionHandler(delegate(GKAchievement[] achievements, NSError error) {
             NSMutableDictionary tempCache = new NSMutableDictionary();
             if (achievements != null)
             {
                 foreach (var achievement in achievements)
                 {
                     tempCache.Add(new NSString(achievement.Identifier), achievement);
                 }
             }
             earnedAchievementCache = tempCache;
             submitAchievement(identifier, percentComplete, achievementName);
         }));
     }
     else
     {
         GKAchievement achievement = (GKAchievement)earnedAchievementCache.ValueForKey(new NSString(identifier));
         if (achievement != null)
         {
             if (achievement.PercentComplete >= 100.0 || achievement.PercentComplete >= percentComplete)
             {
                 achievement = null;
             }
             else
             {
                 achievement.PercentComplete = percentComplete;
             }
         }
         else
         {
             achievement = new GKAchievement(identifier);
             achievement.PercentComplete = percentComplete;
             earnedAchievementCache.Add(new NSString(achievement.Identifier), achievement);
         }
         if (achievement != null)
         {
             achievement.ReportAchievement(new Action <NSError> ((error) => {
                 if (error == null && achievement != null)
                 {
                     if (percentComplete == 100)
                     {
                         new UIAlertView("Achievement Earned", "Great job!  You earned an achievement: " + achievementName, null, "OK", null).Show();
                     }
                     else if (percentComplete > 0)
                     {
                         new UIAlertView("Achievement Progress", "Great job!  You're " + percentComplete + " % of the way to " + achievementName, null, "OK", null).Show();
                     }
                 }
                 else
                 {
                     new UIAlertView("Achievement submittion failed", "Submittion failed because: " + error, null, "OK", null).Show();
                 }
             }));
         }
     }
 }
Ejemplo n.º 2
0
        void LoadAchievementInfo(GKAchievementDescription[] descriptions, NSError error)
        {
            if (descriptions == null)
            {
                return;
            }

            GKAchievement.LoadAchievements(delegate(GKAchievement[] achievements, NSError err) {
                int completeOffset   = 0;
                int incompleteOffset = 0;

                foreach (var description in descriptions)
                {
                    bool completed = false;
                    foreach (var achievement in achievements)
                    {
                        if (description.Identifier == achievement.Identifier)
                        {
                            completed |= achievement.Completed;
                        }
                    }

                    int xOffset = completed ? -75 : 75;
                    int yOffset = completed ? completeOffset : incompleteOffset;

                    var achievementLabel = new SKLabelNode("GillSans-Bold")
                    {
                        Text     = description.Title,
                        FontSize = 10,
                        Position = new CGPoint(FrameMidX + xOffset, FrameMidY + 50 + yOffset + 25)
                    };
                    AddChild(achievementLabel);

                    description.LoadImage(delegate(UIImage image, NSError imageError) {
                        if (image == null)
                        {
                            image = UIImage.FromFile("Images/DefaultPlayerPhoto.png");
                        }
                        var sprite      = SKSpriteNode.FromTexture(SKTexture.FromImage(image), new CGSize(32, 32));
                        sprite.Position = new CGPoint(FrameMidX + xOffset, FrameMidY + 50 + yOffset + 50);
                        AddChild(sprite);
                    });

                    if (completed)
                    {
                        completeOffset -= 50;
                    }
                    else
                    {
                        incompleteOffset -= 50;
                    }
                }
            });
        }
Ejemplo n.º 3
0
        public AchievementCollection GetAchievements()
        {
            if (IsSignedInToLive)
            {
                if (gamerAchievements == null)
                {
                    gamerAchievements = new AchievementCollection();
                }

                GKAchievementDescription.LoadAchievementDescriptions(delegate(GKAchievementDescription[] achievements, NSError error)
                {
                    if (achievements != null)
                    {
                        foreach (GKAchievementDescription a in achievements)
                        {
                            gamerAchievements.Add(new Achievement()
                            {
                                Name                = a.Title,
                                Key                 = a.Identifier,
                                Description         = a.AchievedDescription,
                                HowToEarn           = a.UnachievedDescription,
                                DisplayBeforeEarned = !a.Hidden
                            });
                        }
                    }
                });

                GKAchievement.LoadAchievements(delegate(GKAchievement[] achievements, NSError error)
                {
                    if (achievements != null)
                    {
                        foreach (GKAchievement a in achievements)
                        {
                            foreach (Achievement ac in gamerAchievements)
                            {
                                if (ac.Key == a.Identifier)
                                {
                                    ac.IsEarned       = a.Completed;
                                    ac.EarnedDateTime = a.LastReportedDate.NSDateToDateTime();
                                }
                            }
                        }
                    }
                });
            }
            return(gamerAchievements);
        }
Ejemplo n.º 4
0
        public void Login()
        {
            GKLocalPlayer player = GKLocalPlayer.LocalPlayer;

            if (!player.Authenticated)
            {
                player.AuthenticateHandler = (UIViewController controller, NSError error) =>
                {
                    if (controller != null)
                    {
                        UIViewController parent = AppMain.Current.Services.GetService(typeof(UIViewController)) as UIViewController;
                        parent.PresentViewController(controller, true, null);
                    }
                    else
                    {
                        if (GKLocalPlayer.LocalPlayer.Authenticated)
                        {
                            _enabled = true;

                            GKLeaderboard.LoadLeaderboards((GKLeaderboard[] leaderboards, NSError error3) =>
                            {
                                if (leaderboards != null && leaderboards.Length > 0 && _leaderboardInfo != null)
                                {
                                    LeaderboardInfo[] info = new LeaderboardInfo[leaderboards.Length];

                                    for (int idx = 0; idx < leaderboards.Length; ++idx)
                                    {
                                        GKLeaderboard lb = leaderboards[idx];

                                        int score = 0;

                                        if (lb.LocalPlayerScore != null)
                                        {
                                            score = (int)lb.LocalPlayerScore.Value;
                                        }

                                        info[idx] = new LeaderboardInfo(lb.Identifier)
                                        {
                                            Score = score
                                        };
                                    }

                                    _leaderboardInfo(info);
                                }
                            });

                            GKAchievement.LoadAchievements((GKAchievement[] achievements, NSError error2) =>
                            {
                                if (achievements != null && achievements.Length > 0 && _achievementInfo != null)
                                {
                                    AchievementInfo[] info = new AchievementInfo[achievements.Length];

                                    for (int idx = 0; idx < achievements.Length; ++idx)
                                    {
                                        GKAchievement ach = achievements[idx];

                                        info[idx] = new AchievementInfo(ach.Identifier)
                                        {
                                            Completion = ach.Completed ? Achievement.Completed : (int)ach.PercentComplete
                                        };
                                    }

                                    _achievementInfo(info);
                                }
                            });
                        }
                    }
                };
            }
        }
Ejemplo n.º 5
0
        public void unlockAchievement(MainGame.Achievement achieve)
        {
            if (!gamer.IsSignedInToLive)
            {
                return;
            }

            /*
             * ac = new AchievementCollection();
             * ac = gamer.GetAchievements();
             *
             * foreach (Achievement a in ac)
             * {
             *  if(a.IsEarned)
             *  achievements.Add(a.Key);
             * }
             */
            GKAchievement.LoadAchievements(delegate(GKAchievement[] achievements, NSError error) {
                if (error == null)
                {
                    string code = null;
                    string msg  = null;
                    List <string> achievementids = new List <string>();

                    if (achievements != null)
                    {
                        foreach (GKAchievement a in achievements)
                        {
                            if (a.Completed)
                            {
                                achievementids.Add(a.Identifier);
                            }
                        }
                    }
                    switch (achieve)
                    {
                    case MainGame.Achievement.BabyStep:

                        code = "grp.1shot.babystep";
                        msg  = "Baby Step";
                        break;

                    case MainGame.Achievement._50Points:
                        code = "grp.1shot.50points";
                        msg  = "50 Points!";
                        break;

                    case MainGame.Achievement._100Points:
                        code = "grp.1shot.100point";
                        msg  = "A hundred points.";
                        break;

                    case MainGame.Achievement._70Points1Life:
                        code = "grp.1shot.70points1life";
                        msg  = "1 Life, 70 Points.";
                        break;

                    case MainGame.Achievement.OutOfTheEarth:
                        code = "grp.1shot.outoftheearth";
                        msg  = "Out of the Earth";
                        break;

                    case MainGame.Achievement.Interstellar:
                        code = "grp.1shot.interstellar";
                        msg  = "Interstellar";
                        break;

                    case MainGame.Achievement.Doge:
                        code = "grp.1shot.doge";
                        msg  = "DogeDogeDogeDoge";
                        break;
                    }

                    if (code != null)
                    {
                        if (!achievementids.Contains(code))
                        {
                            gamer.AwardAchievement(code);
                            GKNotificationBanner.Show("Achievement Unlocked!", msg, delegate { });
                        }
                    }
                }
            });
        }
 public void SubmitAchievement(string identifier, double percentComplete, string achievementName)
 {
     if (earnedAchievementCache == null)
     {
         GKAchievement.LoadAchievements(new GKCompletionHandler(delegate(GKAchievement[] achievements, NSError error) {
             NSMutableDictionary tempCache = new NSMutableDictionary();
             if (achievements != null)
             {
                 foreach (var achievement in achievements)
                 {
                     tempCache.Add(new NSString(achievement.Identifier), achievement);
                 }
             }
             earnedAchievementCache = tempCache;
             SubmitAchievement(identifier, percentComplete, achievementName);
         }));
     }
     else
     {
         GKAchievement achievement = (GKAchievement)earnedAchievementCache.ValueForKey(new NSString(identifier));
         if (achievement != null)
         {
             if (achievement.PercentComplete >= 100.0 || achievement.PercentComplete >= percentComplete)
             {
                 achievement = null;
             }
             else
             {
                 achievement.PercentComplete = percentComplete;
             }
         }
         else
         {
             achievement = new GKAchievement(identifier)
             {
                 PercentComplete = percentComplete
             };
             earnedAchievementCache.Add((NSString)achievement.Identifier, achievement);
         }
         if (achievement != null)
         {
             achievement.ReportAchievement(error => {
                 if (error == null && achievement != null)
                 {
                     if (percentComplete == 100)
                     {
                         ShowAlert("Achievement Earned", string.Format("Great job! You earned an achievement: {0}", achievementName));
                     }
                     else if (percentComplete > 0)
                     {
                         ShowAlert("Achievement Progress", string.Format("Great job! You're {0} % of the way to {1}", percentComplete, achievementName));
                     }
                 }
                 else
                 {
                     ShowAlert("Achievement submittion failed", string.Format("Submittion failed because: {0}", error));
                 }
             });
         }
     }
 }