Esempio n. 1
0
 public static void writeToFile(string path, GeneratedCardDefine card)
 {
     using (FileStream stream = new FileStream(path, FileMode.Create))
     {
         write(card, new StreamWriter(stream));
     }
 }
Esempio n. 2
0
 void drawFileOptions()
 {
     GUILayout.BeginHorizontal();
     if (GUILayout.Button("新建"))
     {
         _card = new GeneratedCardDefine();
     }
     if (GUILayout.Button("保存"))
     {
         saveFile();
     }
     if (GUILayout.Button("另存为"))
     {
         saveAsFile();
     }
     if (GUILayout.Button("删除"))
     {
         deleteFile();
     }
     GUILayout.EndHorizontal();
 }
Esempio n. 3
0
        public static GeneratedCardDefine read(TextReader reader)
        {
            XmlDocument doc = new XmlDocument();

            doc.Load(reader);

            GeneratedCardDefine card = new GeneratedCardDefine();

            //ID与类型
            if (doc["Card"].HasAttribute("id"))
            {
                card.setProp("id", Convert.ToInt32(doc["Card"].GetAttribute("id")));
            }
            if (doc["Card"].HasAttribute("type"))
            {
                card.setProp("type", Convert.ToInt32(doc["Card"].GetAttribute("type")));
            }
            //条件
            XmlElement conditionElement = doc["Card"]["Condition"];

            if (conditionElement != null)
            {
                card.setProp("condition", conditionElement.InnerText);
            }
            //效果
            List <Effect> effectList = new List <Effect>();

            for (int i = 0; i < doc["Card"].ChildNodes.Count; i++)
            {
                XmlElement child = doc["Card"].ChildNodes[i] as XmlElement;
                if (child != null && child.Name == "Effect")
                {
                    if (child.HasAttribute("pile") && child.HasAttribute("trigger"))
                    {
                        XmlElement actionElement = child["Action"];
                        if (actionElement != null)
                        {
                            XmlElement checkElement = child["Check"];
                            if (checkElement != null)
                            {
                                effectList.Add(new GeneratedEffect(child.GetAttribute("pile"), child.GetAttribute("trigger"), checkElement.InnerText, actionElement.InnerText));
                            }
                            else
                            {
                                effectList.Add(new GeneratedEffect(child.GetAttribute("pile"), child.GetAttribute("trigger"), "return true;", actionElement.InnerText));
                            }
                        }
                        else
                        {
                            effectList.Add(new GeneratedEffect(child.GetAttribute("pile"), child.GetAttribute("trigger"), null, child.InnerText));
                        }
                    }
                }
            }
            if (effectList.Count > 0)
            {
                card.setProp("effects", effectList.ToArray());
            }
            //属性
            if (card.type == CardDefineType.SERVANT)
            {
                if (doc["Card"]["cost"] != null)
                {
                    card.setProp("cost", Convert.ToInt32(doc["Card"]["cost"].InnerText));
                }
                if (doc["Card"]["attack"] != null)
                {
                    card.setProp("attack", Convert.ToInt32(doc["Card"]["attack"].InnerText));
                }
                if (doc["Card"]["life"] != null)
                {
                    card.setProp("life", Convert.ToInt32(doc["Card"]["life"].InnerText));
                }
            }
            else if (card.type == CardDefineType.SPELL)
            {
                if (doc["Card"]["cost"] != null)
                {
                    card.setProp("cost", Convert.ToInt32(doc["Card"]["cost"].InnerText));
                }
            }
            return(card);
        }
Esempio n. 4
0
        public static void write(GeneratedCardDefine card, TextWriter writer)
        {
            XmlDocument doc = new XmlDocument();

            doc.CreateXmlDeclaration("1.0", "UTF-8", string.Empty);
            XmlElement cardEle = doc.CreateElement("Card");

            //ID与类型
            cardEle.SetAttribute("id", card.id.ToString());
            cardEle.SetAttribute("type", card.type);
            //属性
            if (card.type == CardDefineType.SERVANT)
            {
                XmlElement propEle = doc.CreateElement("cost");
                propEle.InnerText = card.getProp <int>("cost").ToString();
                cardEle.AppendChild(propEle);
                propEle           = doc.CreateElement("attack");
                propEle.InnerText = card.getProp <int>("attack").ToString();
                cardEle.AppendChild(propEle);
                propEle           = doc.CreateElement("life");
                propEle.InnerText = card.getProp <int>("life").ToString();
                cardEle.AppendChild(propEle);
            }
            else if (card.type == CardDefineType.SPELL)
            {
                XmlElement propEle = doc.CreateElement("cost");
                propEle.InnerText = card.getProp <int>("cost").ToString();
                cardEle.AppendChild(propEle);
            }
            //使用条件
            XmlElement conditionElement = doc.CreateElement("Condition");

            conditionElement.InnerText = card.getProp <string>("condition");
            cardEle.AppendChild(conditionElement);
            //效果
            GeneratedEffect[] effects = card.getProp <Effect[]>("effects") as GeneratedEffect[];
            if (effects != null)
            {
                for (int i = 0; i < effects.Length; i++)
                {
                    XmlElement effectEle = doc.CreateElement("Effect");
                    effectEle.SetAttribute("pile", effects[i].pile);
                    effectEle.SetAttribute("trigger", effects[i].trigger);
                    if (!string.IsNullOrEmpty(effects[i].actionScript))
                    {
                        if (!string.IsNullOrEmpty(effects[i].filterScript))
                        {
                            XmlElement checkElement = doc.CreateElement("Check");
                            checkElement.InnerText = effects[i].filterScript;
                            effectEle.AppendChild(checkElement);
                        }
                        XmlElement actionElement = doc.CreateElement("Action");
                        actionElement.InnerText = effects[i].actionScript;
                        effectEle.AppendChild(actionElement);
                    }
                    cardEle.AppendChild(effectEle);
                }
            }
            doc.AppendChild(cardEle);

            doc.Save(writer);
        }
        public HeartStoneRule(IGameEnvironment env, params CardDefine[] cards)
        {
            pool = new CardPool();
            //加载卡池
            //加载内置卡池
            Dictionary <int, CardDefine> dicCards = typeof(HeartStoneRule).Assembly.GetTypes().
                                                    Where(t =>
            {
                return(!t.IsAbstract && t.IsSubclassOf(typeof(CardDefine)) &&
                       (t.GetConstructor(new Type[0]) != null ||
                        t.GetConstructor(new Type[] { typeof(IGameEnvironment) }) != null));
            }).
                                                    Select(t =>
            {
                ConstructorInfo constructor = t.GetConstructor(new Type[0]);
                if (constructor != null)
                {
                    return(constructor.Invoke(new object[0]) as CardDefine);
                }
                else
                {
                    constructor = t.GetConstructor(new Type[] { typeof(IGameEnvironment) });
                    return(constructor.Invoke(new object[] { env }) as CardDefine);
                }
            }).ToDictionary(d =>
            {
                return(d.id);
            });

            //加载参数卡池
            for (int i = 0; i < cards.Length; i++)
            {
                if (!dicCards.ContainsKey(cards[i].id))
                {
                    dicCards.Add(cards[i].id, cards[i]);
                }
                else
                {
                    throw new ArgumentException("存在重复的卡片定义id" + cards[i].id);
                }
            }
            //加载外置卡池
            if (env != null)
            {
                foreach (string path in env.getFiles("Cards", "*.thcd"))
                {
                    using (TextReader reader = env.getFileReader(path))
                    {
                        GeneratedCardDefine card = CardFileHelper.read(reader);
                        if (dicCards.ContainsKey(card.id))
                        {
                            throw new ArgumentException("存在重复的卡片定义id" + card.id);
                        }
                        else
                        {
                            dicCards.Add(card.id, card);
                        }
                    }
                }
            }
            pool = new CardPool(dicCards.Values.ToArray());
        }