Ejemplo n.º 1
0
        private void saveButton_Click(object sender, EventArgs e)
        {
            if (classNameTextBox.Text.Length < 1 || strTextBox.Text.Length < 1 ||
                dexTextBox.Text.Length < 1 || agiTextBox.Text.Length < 1 ||
                staTextBox.Text.Length < 1)
            {
                MessageBox.Show("Not All Fields Have Values");
                return;
            }

            playerClass.name = classNameTextBox.Text;

            if (heavyCheckBox.Checked)
                playerClass.equippableArmors.Add("Heavy");
            if (mediumCheckBox.Checked)
                playerClass.equippableArmors.Add("Medium");
            if (lightCheckBox.Checked)
                playerClass.equippableArmors.Add("Light");
            if (leatherCheckBox.Checked)
                playerClass.equippableArmors.Add("Leather");
            if (clothCheckBox.Checked)
                playerClass.equippableArmors.Add("Cloth");

            if (swordCheckBox1.Checked)
                playerClass.equippableWeapons.Add("1H Sword");
            if (swordCheckBox2.Checked)
                playerClass.equippableWeapons.Add("2H Sword");
            if (daggerCheckBox.Checked)
                playerClass.equippableWeapons.Add("Dagger");
            if (staffCheckBox.Checked)
                playerClass.equippableWeapons.Add("Staff");
            if (axeCheckBox1.Checked)
                playerClass.equippableWeapons.Add("1H Axe");
            if (axeCheckBox2.Checked)
                playerClass.equippableWeapons.Add("2H Axe");
            if (bowCheckBox.Checked)
                playerClass.equippableWeapons.Add("Bow");
            if (harpCheckBox.Checked)
                playerClass.equippableWeapons.Add("Harp");

            playerClass.str = int.Parse(strTextBox.Text);
            playerClass.dex = int.Parse(dexTextBox.Text);
            playerClass.agi = int.Parse(agiTextBox.Text);
            playerClass.sta = int.Parse(staTextBox.Text);

            foreach (CheckBox chk in skillPanel.Controls)
            {
                if (chk.Checked)
                    playerClass.skills.Add(chk.Name);
            }

            foreach (CheckBox chk in spellPanel.Controls)
            {
                if (chk.Checked)
                        playerClass.spells.Add(chk.Name);
            }

            if (existingClassComboBox.SelectedItem == null)
            {
                playerClassList.Add(playerClass.name, playerClass);
                existingClassComboBox.Items.Add(playerClass.name);
                existingClassComboBox.SelectedItem = playerClass.name;
            }
            else
            {
                if (existingClassComboBox.Text != playerClass.name)
                {
                    playerClassList.Add(playerClass.name, playerClass);
                    playerClassList.Remove(existingClassComboBox.Text);
                    existingClassComboBox.Items.Remove(existingClassComboBox.SelectedItem);
                    existingClassComboBox.Items.Add(playerClass.name);
                    existingClassComboBox.SelectedItem = playerClass.name;
                }

                else
                    playerClassList[playerClass.name] = playerClass;
            }

            playerClass = new PlayerClass();

            XmlDocument doc = new XmlDocument();

            XmlElement rootElement = doc.CreateElement("PlayerClasses");
            doc.AppendChild(rootElement);

            foreach (PlayerClass pc in playerClassList.Values)
            {
                XmlElement playerClassElement = doc.CreateElement("PlayerClass");
                rootElement.AppendChild(playerClassElement);
                XmlAttribute pcAttr = doc.CreateAttribute("Name");
                pcAttr.Value = pc.name;
                playerClassElement.Attributes.Append(pcAttr);
                XmlElement armorElement = doc.CreateElement("Armor");
                playerClassElement.AppendChild(armorElement);
                XmlAttribute atAttr = doc.CreateAttribute("type");
                foreach (string ar in pc.equippableArmors)
                {
                    atAttr.Value += ar + ",";
                }
                armorElement.Attributes.Append(atAttr);
                XmlElement weaponElement = doc.CreateElement("Weapon");
                playerClassElement.AppendChild(weaponElement);
                XmlAttribute wtAttr = doc.CreateAttribute("type");
                foreach (string we in pc.equippableWeapons)
                {
                    wtAttr.Value += we + ",";
                }
                weaponElement.Attributes.Append(wtAttr);

                XmlElement attribElement = doc.CreateElement("Attributes");
                playerClassElement.AppendChild(attribElement);
                XmlElement strElement = doc.CreateElement("Str");
                strElement.InnerText = pc.str.ToString();
                attribElement.AppendChild(strElement);
                XmlElement dexElement = doc.CreateElement("Dex");
                dexElement.InnerText = pc.dex.ToString();
                attribElement.AppendChild(dexElement);
                XmlElement agiElement = doc.CreateElement("Agi");
                agiElement.InnerText = pc.agi.ToString();
                attribElement.AppendChild(agiElement);
                XmlElement staElement = doc.CreateElement("Sta");
                staElement.InnerText = pc.sta.ToString();
                attribElement.AppendChild(staElement);

                XmlElement skillsElement = doc.CreateElement("Skills");
                playerClassElement.AppendChild(skillsElement);
                foreach (string skill in pc.skills)
                {
                    XmlElement skillElement = doc.CreateElement("Skill");
                    skillElement.InnerText = skill;
                    skillsElement.AppendChild(skillElement);
                }
                XmlElement magicElement = doc.CreateElement("Magic");
                playerClassElement.AppendChild(magicElement);
                foreach (string spell in pc.spells)
                {
                    XmlElement spellElement = doc.CreateElement("Spell");
                    spellElement.InnerText = spell;
                    magicElement.AppendChild(spellElement);
                }

            }
            doc.Save(contentPath + "\\" + "Classes.item");
        }
Ejemplo n.º 2
0
        private void loadData()
        {
            XmlTextReader reader = new XmlTextReader(contentPath + "\\Classes.item");
            reader.WhitespaceHandling = WhitespaceHandling.None;

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.Name == "PlayerClass")
                        playerClass.name = reader["Name"];
                    if (reader.Name == "Armor")
                    {
                        reader.MoveToAttribute("type");
                        string[] eachArmor = reader.Value.Split(',');
                        foreach (string s in eachArmor)
                            playerClass.equippableArmors.Add(s);
                    }
                    if (reader.Name == "Weapon")
                    {
                        reader.MoveToAttribute("type");
                        string[] eachWeapon = reader.Value.Split(',');
                        foreach (string s in eachWeapon)
                            playerClass.equippableWeapons.Add(s);
                    }

                    if (reader.Name == "Str")
                        playerClass.str = int.Parse(reader.ReadInnerXml());
                    if (reader.Name == "Dex")
                        playerClass.dex = int.Parse(reader.ReadInnerXml());
                    if (reader.Name == "Agi")
                        playerClass.agi = int.Parse(reader.ReadInnerXml());
                    if (reader.Name == "Sta")
                        playerClass.sta = int.Parse(reader.ReadInnerXml());

                    if (reader.Name == "Skill")
                        playerClass.skills.Add(reader.ReadInnerXml());
                    if (reader.Name == "Spell")
                        playerClass.spells.Add(reader.ReadInnerXml());
                }

                else if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "PlayerClass")
                {
                    playerClassList.Add(playerClass.name, playerClass);
                    existingClassComboBox.Items.Add(playerClass.name);
                    playerClass = new PlayerClass();
                }
            }
            reader.Close();
        }