示例#1
0
        internal Toast(Achievement achievement, AchievementEarn earn)
        {
            this.achievement = achievement;
            this.earn        = earn;

            int   height = AchievementGUI.TEX_HEIGHT + AchievementGUI.TEX_BORDER * 2;
            float top    = (Screen.height / 2 - height) / 2 + Screen.height / 2;

            // make sure it doesn't block the nav ball
            top  = Math.Min(top, Screen.height - NAV_BALL_HEIGHT - height - 20);
            rect = new Rect((Screen.width - AchievementGUI.TEX_WIDTH) / 2, top, AchievementGUI.TEX_WIDTH, height);
        }
示例#2
0
        private Dictionary <Achievement, AchievementEarn> loadEarnedAchievements(ConfigNode node)
        {
            Dictionary <Achievement, AchievementEarn> result = new Dictionary <Achievement, AchievementEarn>();

            // old way of doing things
            List <ConfigNode> legacyNodes = new List <ConfigNode>();

            foreach (ConfigNode.Value value in node.values)
            {
                string key = value.name;

                // legacy
                if (key == "launch")
                {
                    key = "launch.1";
                }

                string     time       = value.value;
                ConfigNode legacyNode = new ConfigNode(key);
                legacyNode.AddValue("time", time);
                legacyNode.AddValue("flight", Achievements.UNKNOWN_VESSEL);
                legacyNodes.Add(legacyNode);
            }
            foreach (ConfigNode legacyNode in legacyNodes)
            {
                node.RemoveValue(legacyNode.name);
                node.AddNode(legacyNode);
            }

            // new way
            foreach (Achievement achievement in achievementsList)
            {
                string key = achievement.getKey();
                if (node.HasNode(key))
                {
                    ConfigNode achievementNode = node.GetNode(key);
                    achievement.init(achievementNode);

                    if (achievementNode.HasValue("time") && achievementNode.HasValue("flight"))
                    {
                        long            time       = long.Parse(achievementNode.GetValue("time"));
                        string          flightName = achievementNode.HasValue("flight") ? achievementNode.GetValue("flight") : null;
                        AchievementEarn earn       = new AchievementEarn(time, flightName);
                        result.Add(achievement, earn);
                    }
                }
            }

            Debug.Log("loaded " + result.Count() + " earned achievements");
            return(result);
        }
        private void checkAchievements()
        {
            long now = DateTime.UtcNow.Ticks / 10000;

            if ((now - lastCheck) >= CHECK_INTERVAL)
            {
                if (EarnedAchievements.instance != null)
                {
                    foreach (Achievement achievement in EarnedAchievements.instance.achievementsList)
                    {
                        if (!EarnedAchievements.instance.earnedAchievements.ContainsKey(achievement))
                        {
                            Vessel vessel = (FlightGlobals.fetch != null) ? FlightGlobals.ActiveVessel : null;
                            try {
                                if (achievement.check(vessel))
                                {
                                    string key = achievement.getKey();
                                    Debug.Log("achievement earned: " + key);
                                    AchievementEarn earn = new AchievementEarn(now, (vessel != null) ? vessel.vesselName : Achievements.UNKNOWN_VESSEL);
                                    EarnedAchievements.instance.earnedAchievements.Add(achievement, earn);

                                    // queue for later display
                                    queuedEarnedAchievements.Add(achievement);
                                }
                            } catch (Exception e) {
                                Debug.LogException(e);
                            }
                        }
                    }

                    //long done = DateTime.UtcNow.Ticks / 10000;
                    //Debug.LogWarning("checking achievements took " + (done - now) + " ms");

                    if ((queuedEarnedAchievements.Count() > 0) && (toast == null))
                    {
                        Achievement achievement = queuedEarnedAchievements.First <Achievement>();
                        queuedEarnedAchievements.Remove(achievement);

                        toast = new Toast(achievement, EarnedAchievements.instance.earnedAchievements[achievement]);
                        playAchievementEarnedClip();
                        awardReputation(achievement);
                        highlightButton();
                    }
                }

                lastCheck = now;
            }
        }
        private void drawAchievementsList(IEnumerable <Achievement> achievements)
        {
            //achievementsScrollPos = GUILayout.BeginScrollView(achievementsScrollPos);
            bool first = true;

            foreach (Achievement achievement in achievements)
            {
                AchievementEarn earn = earnedAchievements.ContainsKey(achievement.getKey()) ? earnedAchievements[achievement.getKey()] : null;

                if (!showEarned || earn != null)
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        GUILayout.Space(5);
                    }

                    if ((earn != null) || !achievement.isHidden())
                    {
                        AchievementGUI gui = null;
                        if (achievementGuis.ContainsKey(achievement))
                        {
                            gui = achievementGuis[achievement];
                        }
                        else
                        {
                            gui = new AchievementGUI(achievement, earn);
                            gui.clickCallback = () =>
                            {
                                achievementClicked(achievement);
                            };

                            achievementGuis.Add(achievement, gui);
                        }

                        if (gui != null)
                        {
                            gui.draw(true, true, achievement == expandedAchievement);
                        }
                    }
                }
            }
            //GUILayout.EndScrollView();
        }
示例#5
0
        private void saveEarnedAchievements(ConfigNode node)
        {
            Debug.Log("saving achievements (" + earnedAchievements.Count() + " earned)");
            foreach (Achievement achievement in achievementsList)
            {
                ConfigNode achievementNode = node.AddNode(achievement.getKey());

                achievement.save(achievementNode);

                AchievementEarn earn = earnedAchievements.ContainsKey(achievement) ? earnedAchievements[achievement] : null;
                if (earn != null)
                {
                    achievementNode.AddValue("time", earn.time);
                    if (earn.flightName != null)
                    {
                        achievementNode.AddValue("flight", earn.flightName);
                    }
                }
            }
        }
示例#6
0
        internal AchievementGUI(Achievement achievement, AchievementEarn earn)
        {
            this.achievement = achievement;
            this.earn        = earn;

            // achievements from other assemblies are always addons
            addon = achievement.isAddon() || !achievement.GetType().Assembly.Equals(typeof(Achievements).Assembly);
#if false
            toastTex               = GameDatabase.Instance.GetTexture("Achievements/toast", false);
            toastNotEarnedTex      = GameDatabase.Instance.GetTexture("Achievements/toast-not-earned", false);
            toastAddonTex          = GameDatabase.Instance.GetTexture("Achievements/toast-addon", false);
            toastAddonNotEarnedTex = GameDatabase.Instance.GetTexture("Achievements/toast-addon-not-earned", false);
            toastExpandedTex       = GameDatabase.Instance.GetTexture("Achievements/toast-expanded", false);
            toastAddonExpandedTex  = GameDatabase.Instance.GetTexture("Achievements/toast-addon-expanded", false);
#endif
            toastTex               = new Texture2D(2, 2);
            toastNotEarnedTex      = new Texture2D(2, 2);
            toastAddonTex          = new Texture2D(2, 2);
            toastAddonNotEarnedTex = new Texture2D(2, 2);
            toastExpandedTex       = new Texture2D(2, 2);
            toastAddonExpandedTex  = new Texture2D(2, 2);
            if (!ToolbarControl.LoadImageFromFile(ref toastTex, "GameData/Achievements/PluginData/Textures/toast"))
            {
                Log.Error("Unable to load toast from file");
            }
            if (!ToolbarControl.LoadImageFromFile(ref toastNotEarnedTex, "GameData/Achievements/PluginData/Textures/toast-not-earned"))
            {
                Log.Error("Unable to load toast-not-earned from file");
            }
            if (!ToolbarControl.LoadImageFromFile(ref toastAddonTex, "GameData/Achievements/PluginData/Textures/toast-addon"))
            {
                Log.Error("Unable to load toast-addon from file");
            }
            if (!ToolbarControl.LoadImageFromFile(ref toastAddonNotEarnedTex, "GameData/Achievements/PluginData/Textures/toast-addon-not-earned"))
            {
                Log.Error("Unable to load toast-addon-not-earned from file");
            }
            if (!ToolbarControl.LoadImageFromFile(ref toastExpandedTex, "GameData/Achievements/PluginData/Textures/toast-expanded"))
            {
                Log.Error("Unable to load toast-expanded from file");
            }
            if (!ToolbarControl.LoadImageFromFile(ref toastAddonExpandedTex, "GameData/Achievements/PluginData/Textures/toast-addon-expanded"))
            {
                Log.Error("Unable to load toast-addon-expanded from file");
            }
            int width  = TEX_WIDTH + 300;
            int height = Screen.height / 2;

            bgStyle = new GUIStyle();
            bgStyle.normal.background = toastTex;
            bgStyle.fixedWidth        = TEX_WIDTH;
            bgStyle.fixedHeight       = TEX_HEIGHT + TEX_BORDER * 2;

            bgNotEarnedStyle = new GUIStyle();
            bgNotEarnedStyle.normal.background = toastNotEarnedTex;
            bgNotEarnedStyle.fixedWidth        = TEX_WIDTH;
            bgNotEarnedStyle.fixedHeight       = TEX_HEIGHT + TEX_BORDER * 2;

            bgAddonStyle = new GUIStyle(bgStyle);
            bgAddonStyle.normal.background = toastAddonTex;

            bgAddonNotEarnedStyle = new GUIStyle(bgNotEarnedStyle);
            bgAddonNotEarnedStyle.normal.background = toastAddonNotEarnedTex;

            bgExpandedStyle = new GUIStyle(bgStyle);
            bgExpandedStyle.normal.background = toastExpandedTex;
            bgExpandedStyle.fixedHeight       = TEX_HEIGHT + TEX_HEIGHT_EXPANSION + TEX_BORDER * 2;

            bgAddonExpandedStyle = new GUIStyle(bgStyle);
            bgAddonExpandedStyle.normal.background = toastAddonExpandedTex;
            bgAddonExpandedStyle.fixedHeight       = TEX_HEIGHT + TEX_HEIGHT_EXPANSION + TEX_BORDER * 2;

            textStyle                  = new GUIStyle();
            textStyle.alignment        = TextAnchor.MiddleCenter;
            textStyle.fontSize         = FONT_SIZE;
            textStyle.normal.textColor = Color.black;
            textStyle.padding          = new RectOffset(TEXT_MARGIN, TEXT_MARGIN, 0, 0);
            textStyle.wordWrap         = true;
            textStyle.fixedWidth       = TEX_WIDTH;

            textNotEarnedStyle                  = new GUIStyle();
            textNotEarnedStyle.alignment        = TextAnchor.MiddleCenter;
            textNotEarnedStyle.fontSize         = FONT_SIZE;
            textNotEarnedStyle.normal.textColor = Color.grey;
            textNotEarnedStyle.padding          = new RectOffset(TEXT_MARGIN, TEXT_MARGIN, 0, 0);
            textNotEarnedStyle.wordWrap         = true;
            textNotEarnedStyle.fixedWidth       = TEX_WIDTH;

            textExpansionStyle            = new GUIStyle(textStyle);
            textExpansionStyle.fontSize   = FONT_SIZE_EXPANSION;
            textExpansionStyle.fixedWidth = TEX_WIDTH - EXPANDED_INSET;

            titleStyle           = new GUIStyle(textStyle);
            titleStyle.fontSize  = TITLE_FONT_SIZE;
            titleStyle.fontStyle = FontStyle.Bold;

            titleNotEarnedStyle           = new GUIStyle(textNotEarnedStyle);
            titleNotEarnedStyle.fontSize  = TITLE_FONT_SIZE;
            titleNotEarnedStyle.fontStyle = FontStyle.Bold;
        }
        private void checkAchievements()
        {
            long now = DateTime.UtcNow.Ticks / 10000; // 1 millisecond

            if ((now - lastCheck) >= CHECK_INTERVAL)  // this delays it to every 1.5 seconds
            {
                Vessel vessel = (FlightGlobals.fetch != null) ? FlightGlobals.ActiveVessel : null;
                if (vessel != null && EarnedAchievements.instance != null)
                {
                    foreach (Achievement achievement in EarnedAchievements.instance.achievementsList)
                    {
                        if (EarnedAchievements.instance.earnedAchievements == null || !EarnedAchievements.instance.earnedAchievements.ContainsKey(achievement.getKey()))
                        {
                            string key = achievement.getKey();

                            //Vessel vessel = (FlightGlobals.fetch != null) ? FlightGlobals.ActiveVessel : null;
                            //if (vessel != null)
                            //{
                            try
                            {
                                if (achievement.check(vessel))
                                {
                                    string          key2 = achievement.getKey();
                                    AchievementEarn earn = new AchievementEarn(now, (vessel != null) ? vessel.vesselName : Achievements.UNKNOWN_VESSEL, achievement);
                                    EarnedAchievements.instance.earnedAchievements.Add(achievement.getKey(), earn);

                                    // queue for later display
                                    queuedEarnedAchievements.Add(achievement.getKey(), achievement);
                                }
                            }

                            catch (Exception e)
                            {
                                Log.Exception(e);
                            }
                        }
                    }
                }
#if DEBUG
                long done = DateTime.UtcNow.Ticks / 10000;
                Log.Info("checking achievements took " + (done - now) + " ms");
#endif
                if ((queuedEarnedAchievements.Count() > 0) && (toast == null))
                {
                    Achievement achievement = queuedEarnedAchievements[queuedEarnedAchievements.Keys.Min()];
                    queuedEarnedAchievements.Remove(achievement.getKey());
                    //if (EarnedAchievements.instance.earnedAchievements.ContainsKey(achievement))
                    toast = null;
                    //bool found = false;
                    foreach (var e in EarnedAchievements.instance.earnedAchievements)
                    {
                        if (e.Key == achievement.getKey())
                        {
                            toast = new Toast(achievement, e.Value);
                            playAchievementEarnedClip();
                            awardReputation(achievement);
                            //found = true;
                            break;
                        }
                    }
                }

                lastCheck = now;
            }
        }