Esempio n. 1
0
        public IEnumerable <IEntityTemplate> Load()
        {
            List <IEntityTemplate> entities = new List <IEntityTemplate>();

            string[] files =
                Directory.GetFiles(
                    Directory.GetCurrentDirectory() +
                    GlobalConstants.ASSETS_FOLDER +
                    GlobalConstants.DATA_FOLDER +
                    "Entities",
                    "*.json",
                    SearchOption.AllDirectories);

            foreach (string file in files)
            {
                JSONParseResult result = JSON.Parse(File.ReadAllText(file));

                if (result.Error != Error.Ok)
                {
                    GlobalConstants.ActionLog.Log("Could not load entity templates from " + file, LogLevel.Warning);
                    GlobalConstants.ActionLog.Log(result.ErrorString, LogLevel.Warning);
                    GlobalConstants.ActionLog.Log("On line: " + result.ErrorLine, LogLevel.Warning);
                    continue;
                }

                if (!(result.Result is Dictionary dictionary))
                {
                    GlobalConstants.ActionLog.Log("Could not parse JSON to Dictionary from " + file, LogLevel.Warning);
                    continue;
                }

                Array templateArray = this.ValueExtractor.GetValueFromDictionary <Array>(dictionary, "Entities");

                foreach (Dictionary templateDict in templateArray)
                {
                    string creatureType =
                        this.ValueExtractor.GetValueFromDictionary <string>(templateDict, "CreatureType");
                    string  type       = this.ValueExtractor.GetValueFromDictionary <string>(templateDict, "Type");
                    string  visionType = this.ValueExtractor.GetValueFromDictionary <string>(templateDict, "VisionType") ?? "diurnal vision";
                    IVision vision     = this.VisionProviderHandler.Get(visionType);

                    string description =
                        this.ValueExtractor.GetValueFromDictionary <string>(templateDict, "Description");

                    IDictionary <string, IEntityStatistic> statistics =
                        new System.Collections.Generic.Dictionary <string, IEntityStatistic>();
                    ICollection <Dictionary> statisticsCollection =
                        this.ValueExtractor.GetCollectionFromArray <Dictionary>(
                            this.ValueExtractor.GetValueFromDictionary <Array>(templateDict, "Statistics"));
                    foreach (Dictionary innerDict in statisticsCollection)
                    {
                        string statName  = this.ValueExtractor.GetValueFromDictionary <string>(innerDict, "Name");
                        int    statValue = this.ValueExtractor.GetValueFromDictionary <int>(innerDict, "Value");
                        int    threshold = innerDict.Contains("Threshold")
                        ? this.ValueExtractor.GetValueFromDictionary <int>(innerDict, "Threshold")
                        : GlobalConstants.DEFAULT_SUCCESS_THRESHOLD;

                        IEntityStatistic statistic = GlobalConstants.GameManager.StatisticHandler.Get(statName);
                        statistic.SetValue(statValue);
                        statistic.SetThreshold(threshold);

                        statistics.Add(
                            statName,
                            statistic);
                    }

                    IDictionary <string, IEntitySkill> skills =
                        new System.Collections.Generic.Dictionary <string, IEntitySkill>();
                    if (templateDict.Contains("Skills"))
                    {
                        ICollection <Dictionary> skillCollection =
                            this.ValueExtractor.GetCollectionFromArray <Dictionary>(
                                this.ValueExtractor.GetValueFromDictionary <Array>(templateDict, "Skills"));
                        foreach (Dictionary innerDict in skillCollection)
                        {
                            string skillName  = this.ValueExtractor.GetValueFromDictionary <string>(innerDict, "Name");
                            int    skillValue = this.ValueExtractor.GetValueFromDictionary <int>(innerDict, "Value");
                            int    threshold  = innerDict.Contains("Threshold")
                                ? this.ValueExtractor.GetValueFromDictionary <int>(innerDict, "Threshold")
                                : GlobalConstants.DEFAULT_SUCCESS_THRESHOLD;

                            IEntitySkill skill = GlobalConstants.GameManager.SkillHandler.Get(skillName);
                            skill.SetValue(skillValue);
                            skill.SetThreshold(threshold);
                            skills.Add(
                                skillName,
                                skill);
                        }
                    }

                    ICollection <string> tags = this.ValueExtractor.GetCollectionFromArray <string>(
                        this.ValueExtractor.GetValueFromDictionary <Array>(templateDict, "Tags"));

                    ICollection <string> slots = this.ValueExtractor.GetCollectionFromArray <string>(
                        this.ValueExtractor.GetValueFromDictionary <Array>(templateDict, "Slots"));

                    ICollection <string> needs = this.ValueExtractor.GetCollectionFromArray <string>(
                        this.ValueExtractor.GetValueFromDictionary <Array>(templateDict, "Needs"));

                    ICollection <IAbility> abilities = new List <IAbility>();
                    if (templateDict.Contains("Abilities"))
                    {
                        ICollection <string> abilityNames = this.ValueExtractor.GetCollectionFromArray <string>(
                            this.ValueExtractor.GetValueFromDictionary <Array>(templateDict, "Abilities"));

                        foreach (string name in abilityNames)
                        {
                            abilities.Add(this.AbilityHandler.Get(name));
                        }
                    }

                    int size = this.ValueExtractor.GetValueFromDictionary <int>(templateDict, "Size");

                    entities.Add(
                        new EntityTemplate(
                            statistics,
                            skills,
                            needs.ToArray(),
                            abilities.ToArray(),
                            slots.ToArray(),
                            size,
                            vision,
                            creatureType,
                            type,
                            description,
                            tags.ToArray()));
                }
            }

            return(entities);
        }