Esempio n. 1
0
    /// <summary>
    /// Loads the BaseCard from resources.
    /// </summary>
    void LoadCards()
    {
        TextAsset[] textAssets = Resources.LoadAll <TextAsset> ("Json/Card");
        foreach (TextAsset tAsset in textAssets)
        {
            string jsonString = tAsset.text;
            var    dict       = Json.Deserialize(jsonString) as Dictionary <string, object>;
            if (dict.ContainsKey("card"))
            {
                Dictionary <string, object> cardInfos = dict ["card"] as Dictionary <string, object>;
                foreach (string cardName in cardInfos.Keys)
                {
                    //Detect existence of cardName
                    if (_baseCardIdTable.ContainsKey(cardName))
                    {
                        Debug.Log(string.Format("Card name has existed -name:{0} in folder: 'Resources/Json/Card/{1}'", cardName, tAsset.name));
                        throw new System.Exception(string.Format("Card name has existed -name:{0} in folder: 'Resources/Json/Card/{1}'", cardName, tAsset.name));
                    }
                    KeyValuePair <string, Dictionary <string, object> > cardInfo
                        = new KeyValuePair <string, Dictionary <string, object> > (cardName, cardInfos [cardName] as Dictionary <string, object>);

                    BaseCard baseCard;
                    try {
                        baseCard = BaseCard.GeneBaseCard(cardInfo);
                    } catch (System.Exception e) {
                        Debug.Log(string.Format("Fail to Generate BaseCard via card data that named:{0}", cardName));
                        throw e;
                    }
                    int id = baseCard.id;
                    //Detect existence of baseCard's id
                    if (_baseCardTable.ContainsKey(id))
                    {
                        Debug.Log(string.Format("Card id has existed -name:{0} in folder: 'Resources/Json/Card/{1}'", cardName, tAsset.name));
                        throw new System.Exception(string.Format("Card id has existed -name:{0} in folder: 'Resources/Json/Card/{1}'", cardName, tAsset.name));
                    }
                    //Check the role and background sprites
                    try {
                        CheckSprite(baseCard);
                    } catch (System.Exception e) {
                        Debug.Log(string.Format("Fail to check sprite texture of BaseCard named:{0}", baseCard.name));
                        throw e;
                    }
                    _baseCardIdTable.Add(cardName, id);
                    _baseCardTable.Add(id, baseCard);
                }
            }
        }
    }