コード例 #1
0
    public Item LoadItemFromJsonData(JsonData dataToLoadFrom, string namePrefix)
    {
        Item baseItem;

        ComponentTypeAndAmount[] tempList = new ComponentTypeAndAmount[1];
        int componentCount = 0;

        if (dataToLoadFrom.Keys.Contains(String.Format("{0}ComponentCount", namePrefix)))
        {
            componentCount = (int)dataToLoadFrom[(String.Format("{0}ComponentCount", namePrefix))];
            tempList       = new ComponentTypeAndAmount[componentCount];
            for (int c = 0; c < componentCount; c++)
            {
                tempList[c].thisComponent         = (ComponentType)System.Enum.Parse(typeof(ComponentType), (string)dataToLoadFrom[(String.Format("{0}{1}ComponentType", namePrefix, c))]);
                tempList[c].amountOfThisComponent = (int)dataToLoadFrom[(String.Format("{0}{1}ComponentAmount", namePrefix, c))];
            }
        }

        else
        {
            componentCount                    = 0;
            tempList[0].thisComponent         = ComponentType.Nothing;
            tempList[0].amountOfThisComponent = 0;
        }

        if ((int)dataToLoadFrom[String.Format("{0}ID", namePrefix)] == -1)
        {
            baseItem = new Item();
        }

        else
        {
            baseItem = new Item
                       (
                (int)dataToLoadFrom[String.Format("{0}Type", namePrefix)],
                (int)dataToLoadFrom[String.Format("{0}ID", namePrefix)],
                (string)dataToLoadFrom[String.Format("{0}Title", namePrefix)],
                (string)dataToLoadFrom[String.Format("{0}SpriteName", namePrefix)],
                (string)dataToLoadFrom[String.Format("{0}DropModel", namePrefix)],
                (int)dataToLoadFrom[String.Format("{0}Value", namePrefix)],
                (int)dataToLoadFrom[String.Format("{0}StackSize", namePrefix)],
                (int)dataToLoadFrom[String.Format("{0}MaxStackSize", namePrefix)],
                (string)dataToLoadFrom[String.Format("{0}Description", namePrefix)],
                componentCount,
                tempList
                       );
        }

        if ((int)dataToLoadFrom[String.Format("{0}Type", namePrefix)] == 0)
        {
            return(baseItem);
        }

        else if ((int)dataToLoadFrom[String.Format("{0}Type", namePrefix)] == 1)
        {
            Melee conversion = new Melee();
            conversion.ID           = baseItem.ID;
            conversion.Title        = baseItem.Title;
            conversion.SpriteName   = baseItem.SpriteName;
            conversion.DropModel    = baseItem.DropModel;
            conversion.Type         = baseItem.Type;
            conversion.StackSize    = baseItem.StackSize;
            conversion.MaxStackSize = baseItem.MaxStackSize;
            conversion.Description  = baseItem.Description;
            conversion.Value        = baseItem.Value;

            conversion.ComponentCount = baseItem.ComponentCount;

            conversion.componentsHave = baseItem.componentsHave;

            conversion.equipPrefab = (string)dataToLoadFrom[String.Format("{0}EquipPrefab", namePrefix)];

            conversion.range = (double)dataToLoadFrom[String.Format("{0}Range", namePrefix)];

            conversion.attackSpeed = (double)dataToLoadFrom[String.Format("{0}AttackSpeed", namePrefix)];

            conversion.dmgProperties = new DamageType();

            conversion.dmgProperties.primaryDamageCount = (int)dataToLoadFrom[String.Format("{0}MainDamageCount", namePrefix)];

            conversion.dmgProperties.primaryDamageAmounts = new double[conversion.dmgProperties.primaryDamageCount];

            conversion.dmgProperties.primaryDamageTypes = new PrimaryDamageType[conversion.dmgProperties.primaryDamageCount];


            conversion.dmgProperties.additonalDamageCount = (int)dataToLoadFrom[String.Format("{0}AdditonalDamageCount", namePrefix)];

            conversion.dmgProperties.additonalDamageAmounts = new double[conversion.dmgProperties.primaryDamageCount];

            conversion.dmgProperties.thisAdditonalDamageTypes = new AdditionalDamageType[conversion.dmgProperties.primaryDamageCount];

            int index = 0;
            if (dataToLoadFrom.Keys.Contains(String.Format("{0}Melee", namePrefix)))
            {
                conversion.dmgProperties.primaryDamageAmounts[index] = (double)dataToLoadFrom[String.Format("{0}Melee", namePrefix)];
                conversion.dmgProperties.primaryDamageTypes[index]   = PrimaryDamageType.Melee;
                index += 1;
            }

            if (dataToLoadFrom.Keys.Contains(String.Format("{0}Magic", namePrefix)))
            {
                conversion.dmgProperties.primaryDamageAmounts[index] = (double)dataToLoadFrom[String.Format("{0}Magic", namePrefix)];
                conversion.dmgProperties.primaryDamageTypes[index]   = PrimaryDamageType.Magic;
                index += 1;
            }

            if (dataToLoadFrom.Keys.Contains(String.Format("{0}Ranged", namePrefix)))
            {
                conversion.dmgProperties.primaryDamageAmounts[index] = (double)dataToLoadFrom[String.Format("{0}Ranged", namePrefix)];
                conversion.dmgProperties.primaryDamageTypes[index]   = PrimaryDamageType.Ranged;
                index += 1;
            }
            return(conversion);
        }

        return(null);
    }
コード例 #2
0
ファイル: ItemDatabase.cs プロジェクト: josh12x/Assets
    //construct the database using the information/data we have from the json file
    void ConstructItemDatabase()
    {
        //for every item that is in the json data/every seperate thing
        for (int i = 0; i < itemData.Count; i++)
        {
            //the item we start with, every item has basic information, we fill that in first, then add any extra.
            Item baseItem;

            ComponentTypeAndAmount[] tempList = new ComponentTypeAndAmount[1];
            int componentCount = 0;

            if (itemData[i].Keys.Contains("ComponentCount"))
            {
                componentCount = (int)itemData[i]["ComponentCount"];
                tempList       = new ComponentTypeAndAmount[componentCount];
                for (int c = 0; c < componentCount; c++)
                {
                    tempList[c].thisComponent         = (ComponentType)System.Enum.Parse(typeof(ComponentType), (string)itemData[i][c + "ComponentType"]);
                    tempList[c].amountOfThisComponent = (int)itemData[i][c + "ComponentAmount"];
                }
            }

            else
            {
                componentCount                    = 0;
                tempList[0].thisComponent         = ComponentType.Nothing;
                tempList[0].amountOfThisComponent = (int)itemData[i][0];
            }

            //fill out basic information from the json data
            baseItem =
                new Item
                (
                    (int)itemData[i]["Type"],
                    (int)itemData[i]["ID"],
                    (string)itemData[i]["Title"],
                    (string)itemData[i]["SpriteName"],
                    (string)itemData[i]["DropModel"],
                    (int)itemData[i]["Value"],
                    (int)itemData[i]["StackSize"],
                    (int)itemData[i]["MaxStackSize"],
                    (string)itemData[i]["Description"],
                    componentCount,
                    tempList
                );

            //if the json data item type is 0, then its nothing special, the base info is all we really actually need
            if ((int)itemData[i]["Type"] == 0)
            {
                database.Add(baseItem);
            }

            //if its a melee item, then we need more data, lets add that onto the base item, then add it to the database
            if ((int)itemData[i]["Type"] == 1)
            {
                Melee conversion = new Melee();
                conversion.ID           = baseItem.ID;
                conversion.Title        = baseItem.Title;
                conversion.SpriteName   = baseItem.SpriteName;
                conversion.DropModel    = baseItem.DropModel;
                conversion.Type         = baseItem.Type;
                conversion.StackSize    = baseItem.StackSize;
                conversion.MaxStackSize = baseItem.MaxStackSize;
                conversion.Description  = baseItem.Description;

                conversion.ComponentCount = componentCount;
                conversion.componentsHave = tempList;

                conversion.Value = baseItem.Value;

                //the extra data we need for a melee weapons
                //equip prefab
                conversion.equipPrefab = (string)itemData[i]["EquipPrefab"];

                //how far away a weapon can attack
                conversion.range = (double)itemData[i]["Range"];

                //how fast can we attack
                conversion.attackSpeed = (double)itemData[i]["AttackSpeed"];

                conversion.dmgProperties = new DamageType();

                //what type and how much damage does this weapon do
                if (itemData[i].Keys.Contains("MainDamageCount"))
                {
                    conversion.dmgProperties.primaryDamageTypes = new PrimaryDamageType[(int)itemData[i]["MainDamageCount"]] as PrimaryDamageType[];

                    conversion.dmgProperties.primaryDamageAmounts = new double[(int)itemData[i]["MainDamageCount"]];

                    int currentIndex = 0;
                    //if it has the keyword blunt, add blunt damage to the list of damage we can do
                    if (itemData[i].Keys.Contains("Melee"))
                    {
                        conversion.dmgProperties.primaryDamageTypes[currentIndex]   = PrimaryDamageType.Melee;
                        conversion.dmgProperties.primaryDamageAmounts[currentIndex] = (double)itemData[i]["Melee"];
                        currentIndex += 1;
                    }

                    if (itemData[i].Keys.Contains("Magic"))
                    {
                        conversion.dmgProperties.primaryDamageTypes[currentIndex]   = PrimaryDamageType.Magic;
                        conversion.dmgProperties.primaryDamageAmounts[currentIndex] = (double)itemData[i]["Magic"];
                        currentIndex += 1;
                    }

                    if (itemData[i].Keys.Contains("Ranged"))
                    {
                        conversion.dmgProperties.primaryDamageTypes[currentIndex]   = PrimaryDamageType.Ranged;
                        conversion.dmgProperties.primaryDamageAmounts[currentIndex] = (double)itemData[i]["Ranged"];
                        currentIndex += 1;
                    }
                }

                if (itemData[i].Keys.Contains("AdditonalDamageCount"))
                {
                    conversion.dmgProperties.thisAdditonalDamageTypes = new AdditionalDamageType[(int)itemData[i]["AdditonalDamageCount"]];

                    conversion.dmgProperties.additonalDamageAmounts = new double[(int)itemData[i]["AdditonalDamageCount"]];
                }


                //add the melee item to the database now that we fill out the important information from the json data we read from
                database.Add(conversion);
            }
        }
    }