private void mBtnAdd_Click(object sender, EventArgs e) { if (mLstObjects.SelectedIndex == -1) { MessageBox.Show("You must select an object!", "World Builder", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (mTxtName.Text == "") { MessageBox.Show("You must select a name for the object!", "World Builder", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } else { mItem = mLstObjects.SelectedItem.ToString(); Bitmap image = null; switch (mItem) { case "Enemy": image = new Bitmap(@"C:\Users\Mark\Documents\Visual Studio 2008\Projects\SuperNova\SuperNova\Content\Textures\BadSpaceship.png"); break; case "Asteroid": image = new Bitmap(@"C:\Users\Mark\Documents\Visual Studio 2008\Projects\SuperNova\SuperNova\Content\Textures\Asteroid.png"); break; } mObject = new WorldObject(new Point((int)numericUpDown1.Value, (int)numericUpDown2.Value), image, mTxtName.Text); this.DialogResult = DialogResult.OK; } }
private void AddWorldData(string typeofData, WorldObject obj) { XmlDocument xdoc; XmlElement element; XmlElement name; XmlElement position; switch (typeofData) { //Then we don't care about obj case "Square": //This code will add a entire empty square to the universe xdoc = new XmlDocument(); //loads the document from the saved path xdoc.Load(xmlSavePath); if (xdoc.DocumentElement.Name == "GameData") { for (int i = 0; i < xdoc.DocumentElement.ChildNodes.Count; i++) { XmlNode node = xdoc.DocumentElement.ChildNodes.Item(i); if (node.Name == "LandscapeData") { element = xdoc.CreateElement("Square"); XmlAttribute attribute = xdoc.CreateAttribute("uPosition"); attribute.InnerText = mCurrentPosition.X.ToString() + "," + mCurrentPosition.Y.ToString(); element.Attributes.Append(attribute); node.AppendChild(element); xdoc.Save(xmlSavePath); } } } break; case "Enemy": xdoc = new XmlDocument(); xdoc.Load(xmlSavePath); if (xdoc.DocumentElement.Name == "GameData") { for (int i = 0; i < xdoc.DocumentElement.ChildNodes.Count; i++) { XmlNode node = xdoc.DocumentElement.ChildNodes.Item(i); if (node.Name == "LandscapeData") { for (int j = 0; j < node.ChildNodes.Count; j++) { XmlNode landscape = node.ChildNodes.Item(j); if (landscape.Name == "Square") { XmlAttributeCollection newxac = landscape.Attributes; for (int o = 0; o < newxac.Count; o++) { XmlNode newattribute = newxac.Item(o); //Split the string and basically manually parse the point string[] temp = newattribute.Value.Split(new char[] { ',' }); //Then look for the correct square to start parsing data from if (mCurrentPosition == new Point(int.Parse(temp[0]), int.Parse(temp[1]))) { //Create the elements element = xdoc.CreateElement("Enemy"); name = xdoc.CreateElement("Name"); name.InnerText = obj.Name; position = xdoc.CreateElement("Position"); position.InnerText = obj.Position.X.ToString() + "," + obj.Position.Y.ToString(); element.AppendChild(name); element.AppendChild(position); landscape.AppendChild(element); xdoc.Save(xmlSavePath); } } } } } } } break; case "Asteroid": xdoc = new XmlDocument(); xdoc.Load(mOpenFile.FileName); if (xdoc.DocumentElement.Name == "GameData") { for (int i = 0; i < xdoc.DocumentElement.ChildNodes.Count; i++) { XmlNode node = xdoc.DocumentElement.ChildNodes.Item(i); if (node.Name == "LandscapeData") { for (int j = 0; j < node.ChildNodes.Count; j++) { XmlNode landscape = node.ChildNodes.Item(j); if (landscape.Name == "Square") { XmlAttributeCollection newxac = landscape.Attributes; for (int o = 0; o < newxac.Count; o++) { XmlNode newattribute = newxac.Item(o); //Split the string and basically manually parse the point string[] temp = newattribute.Value.Split(new char[] { ',' }); //Then look for the correct square to start parsing data from if (mCurrentPosition == new Point(int.Parse(temp[0]), int.Parse(temp[1]))) { element = xdoc.CreateElement("Asteroid"); name = xdoc.CreateElement("Name"); name.InnerText = obj.Name; position = xdoc.CreateElement("Position"); position.InnerText = obj.Position.X.ToString() + "," + obj.Position.Y.ToString(); element.AppendChild(name); element.AppendChild(position); landscape.AppendChild(element); xdoc.Save(xmlSavePath); } } } } } } } break; } }
private void mMainPic_MouseClick(object sender, MouseEventArgs e) { if (mSelectedObject != null) { return; } foreach (WorldObject obj in mWorldObjects) { if(obj.WasClicked(e.Location)) { mSelectedObject = obj; } } }
private bool LoadXmlData() { //Called when the file is selected //we need to first make sure that it is a actual world file XmlDocument xdoc = new XmlDocument(); xdoc.Load(mOpenFile.FileName); if (xdoc.DocumentElement.Name == "GameData") { for (int i = 0; i < xdoc.DocumentElement.ChildNodes.Count; i++) { XmlNode node = xdoc.DocumentElement.ChildNodes.Item(i); if (node.Name == "LandscapeData") { for (int j = 0; j < node.ChildNodes.Count; j++) { XmlNode landscape = node.ChildNodes.Item(j); if (landscape.Name == "Square") { XmlAttributeCollection newxac = landscape.Attributes; for (int o = 0; o < newxac.Count; o++) { XmlNode newattribute = newxac.Item(o); //Split the string and basically manually parse the point string[] temp = newattribute.Value.Split(new char[] { ',' }); //Then look for the correct square to start parsing data from if (mCurrentPosition == new Point(int.Parse(temp[0]), int.Parse(temp[1]))) { for (int k = 0; k < landscape.ChildNodes.Count; k++) { XmlNode square = landscape.ChildNodes.Item(k); WorldObject worldObj = new WorldObject(); switch (square.Name) { case "Asteroid": worldObj.Texture = new Bitmap(@"C:\Users\Mark\Documents\Visual Studio 2008\Projects\SuperNova\SuperNova\Content\Textures\Asteroid.png"); for (int l = 0; l < square.ChildNodes.Count; l++) { XmlNode vector = square.ChildNodes.Item(l); string[] tempPoint; switch (vector.Name) { case "Position": tempPoint = vector.InnerText.Split(new char[] { ',' }); worldObj.Position = new Point(int.Parse(tempPoint[0]), int.Parse(tempPoint[1])); break; case "Name": worldObj.Name = vector.InnerText; break; } } //After we get out of this loop, we should have sufficent info to add it to the world mWorldObjects.Add(worldObj); break; case "SpaceStation": break; case "Enemy": worldObj.Texture = new Bitmap(@"C:\Users\Mark\Documents\Visual Studio 2008\Projects\SuperNova\SuperNova\Content\Textures\BadSpaceship.png"); for (int l = 0; l < square.ChildNodes.Count; l++) { XmlNode vector = square.ChildNodes.Item(l); string[] tempPoint; switch (vector.Name) { case "Position": tempPoint = vector.InnerText.Split(new char[] { ',' }); worldObj.Position = new Point(int.Parse(tempPoint[0]), int.Parse(tempPoint[1])); break; case "Name": worldObj.Name = vector.InnerText; break; } } //After we get out of this loop, we should have sufficent info to add it to the world mWorldObjects.Add(worldObj); break; } } return true; } } } } //Well.... then our square wasn't found we have to make a new one AddWorldData("Square", null); } } return true; } else return false; }