示例#1
0
文件: HeroData.cs 项目: ajm456/RPG
    public HeroData(HeroDataJsonWrapper wrapper)
    {
        name     = wrapper.name;
        color    = new Color(wrapper.color[0] / 255f, wrapper.color[1] / 255f, wrapper.color[2] / 255f);
        hp       = wrapper.hp;
        maxHp    = wrapper.maxHp;
        strength = wrapper.strength;
        agility  = wrapper.agility;
        calm     = wrapper.calm;
        strife   = wrapper.strife;

        // Filter to find this hero's abilities
        calmAbilities   = new List <AbilityData>();
        strifeAbilities = new List <AbilityData>();
        foreach (AbilityData ability in allAbilities)
        {
            if (wrapper.abilityNames.Contains(ability.name))
            {
                if (ability.isCalm)
                {
                    calmAbilities.Add(ability);
                }
                else
                {
                    strifeAbilities.Add(ability);
                }
            }
        }
    }
示例#2
0
    /// <summary>
    /// Load the player characters with given names from JSON.
    /// </summary>
    /// <param name="heroNames">The names of the heroes being loaded.</param>
    /// <returns>A list of HeroData objects for each specified hero.</returns>
    public static List <HeroData> LoadHeroes(List <string> heroNames)
    {
        SortedList <int, HeroData> heroList = new SortedList <int, HeroData>(heroNames.Count);

        foreach (string filename in Directory.EnumerateFiles(JSON_HEROES_ROOT))
        {
            // Watch out for Unity-generated .meta files
            if (filename.Contains(".meta"))
            {
                continue;
            }
            // Only deserialize files in our list of character names
            if (!heroNames.Any(s => filename.ToUpperInvariant().Contains(s.ToUpperInvariant())))
            {
                continue;
            }

            string json = File.ReadAllText(filename);
            HeroDataJsonWrapper wrapper   = JsonUtility.FromJson <HeroDataJsonWrapper>(json);
            HeroData            character = new HeroData(JsonUtility.FromJson <HeroDataJsonWrapper>(json));

            // Order correctly
            bool found = false;
            for (int i = 0; i < heroNames.Count; i++)
            {
                if (character.name.ToLowerInvariant() == heroNames[i].ToLowerInvariant())
                {
                    heroList.Add(i, character);
                    found = true;
                    break;
                }
            }
            if (!found)
            {
                Debug.Log("When trying to find which position hero " + character.name + " should go in, could not find an entry!");
                Debug.Break();
            }
        }

        return(heroList.Values.ToList());
    }