Click here to Skip to main content
15,891,951 members
Articles / Programming Languages / C#

Mars Mission (3) : structure interior editor

Rate me:
Please Sign up or sign in to vote.
4.94/5 (18 votes)
21 Mar 2011CPOL27 min read 46.6K   2.2K   14  
strategy/action game defending the solar system : collision detection between a polygon object moving inside a polygon container
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.IO;
using System.Windows.Forms;
using System.Xml;


namespace Mars_Mission
{
	public class classBase
	{
		public static classBase[] cBases = new classBase[0];
		static int intUniqueIDCounter = 0;
		public int intMyUniqueID;
		public classCollisionDetectionObject cCDO = new classCollisionDetectionObject();
		public int intMyIndex;
		public classObjectContainer cObjContainer;
		
		public TreeNode tNode = new TreeNode();
		public TreeNode tNode_Astronauts = new TreeNode();
		public TreeNode tNode_Resources = new TreeNode();
		public TreeNode tNode_Ships = new TreeNode();
		classBase cMyReference;
		public classStructure[] cStructures = new classStructure[0];

		private classBase()
		{
			cMyReference = this;
			object objMyObjectReference = (object)cMyReference;
			cObjContainer = new classObjectContainer(ref objMyObjectReference, enuTypeObject.Base);
			intMyUniqueID = intUniqueIDCounter++;
			Name = "Base #" + intMyUniqueID.ToString();
			Array.Resize<classBase>(ref cBases, cBases.Length + 1);
			cBases[cBases.Length - 1] = this;

			cCDO.cIAmBase = this;
			cCDO.eTypeObject = enuTypeObject.Base;

			tNode.Text = Name; tNode.ForeColor = formMarsMission.clrNodeDull;
			tNode.Nodes.Add(tNode_Ships); tNode_Ships.Text = "Ships"; tNode_Ships.ForeColor = formMarsMission.clrNodeDull;
			tNode.Nodes.Add(tNode_Resources); tNode_Resources.Text = "Resources"; tNode_Resources.ForeColor = formMarsMission.clrNodeDull;
			tNode.Nodes.Add(tNode_Astronauts); tNode_Astronauts.Text = "Astronauts"; tNode_Astronauts.ForeColor = formMarsMission.clrNodeDull;
		}

		public XmlNode getSaveGameNode(ref XmlDocument xDoc)
		{
			XmlNode xRetVal = xDoc.CreateElement(classSaveGame.conXmlField_Base);

			XmlNode xName = xDoc.CreateElement(classSaveGame.conXmlField_Base_Name);
			xName.InnerText = Name;
			xRetVal.AppendChild(xName);

			xRetVal.AppendChild(cCDO.getSaveGameNode(ref xDoc));
			xRetVal.AppendChild(cObjContainer.getSaveGameNode(ref xDoc));

			XmlNode xWallLocation = xDoc.CreateElement(classSaveGame.conXmlField_Base_WallLocation);
			xWallLocation.InnerText = ((int)eCaveWallLocation).ToString();
			xRetVal.AppendChild(xWallLocation);

			XmlNode xStructureArray = xDoc.CreateElement(classSaveGame.conXmlField_Base_StructureArray);
			if (cStructures != null)
				for (int intStructureCounter = 0; intStructureCounter < cStructures.Length; intStructureCounter++)
					xStructureArray.AppendChild(cStructures[intStructureCounter].getSaveGameNode(ref xDoc));

			xRetVal.AppendChild(xStructureArray);

			return xRetVal;
		}

		public static classBase fromXmlNode(ref XmlNode xNode)
		{
			classBase cRetVal = new classBase();

			XmlNode xName = xNode.FirstChild;
			cRetVal.Name = xName.InnerText;

			XmlNode xCDO = xName.NextSibling;
			cRetVal.cCDO = classCollisionDetectionObject.fromXmlNode(ref xCDO);
			cRetVal.cCDO.cIAmBase = cRetVal;
			cRetVal.cCDO.eTypeObject = enuTypeObject.Base;

			XmlNode xObjContainer = xCDO.NextSibling;
			object objBase = (object)cRetVal;
			cRetVal.cObjContainer = classObjectContainer.fromXmlNode(ref xObjContainer, ref objBase, enuTypeObject.Base);

			for (int intShipCounter = 0; intShipCounter < cRetVal.cObjContainer.cShips.Length; intShipCounter++)
				cRetVal.cObjContainer.cShips[intShipCounter].cBase = cRetVal;

			XmlNode xWallLocation = xObjContainer.NextSibling;
			cRetVal.eCaveWallLocation = (enuTypeCaveCellWall)Convert.ToInt16(xWallLocation.InnerText);

			XmlNode xStructureArray = xWallLocation.NextSibling;
			cRetVal.cStructures = new classStructure[xStructureArray.ChildNodes.Count];
			for (int intStructureCounter = 0; intStructureCounter < cRetVal.cStructures.Length; intStructureCounter++)
			{
				XmlNode xStructure = xStructureArray.ChildNodes[intStructureCounter];
				cRetVal.cStructures[intStructureCounter] = classStructure.fromXmlNode(ref xStructure);
			}

			return cRetVal;
		}

		public enuTypeCaveCellWall eCaveWallLocation = enuTypeCaveCellWall.bottom;

		public void BuildStructure(enuStructureModels eStructureModel)
		{
			Array.Resize<classStructure>(ref cStructures, cStructures.Length + 1);
			cStructures[cStructures.Length - 1] = new classStructure(eStructureModel);
			cStructures[cStructures.Length - 1].cBase = this;
		}

		public static classBase createBase()
		{
			classBase cNewBase = new classBase();
			return cNewBase;
		}

		string _Name;
		public string Name
		{
			get { return _Name; }
			set 
			{
				_Name = value;
				tNode.Text = _Name;
			}
		}

		public void RemoveFromSolarObject()
		{
			if (cCDO.cSolarObject == null)
				MessageBox.Show("error : Remove Ship From cSolarObject -> Ship's cSolarObject == null");
			cCDO.cSolarObject.cObjContainer.removeBase(ref cMyReference);
			cCDO.cSolarObject.tNode_Bases.Nodes.Remove(tNode);
		}

		public static void clear()
		{
			cBases = new classBase[0];
		}

		public void AddToSolarObject(ref classSolarObject cNewSolarObject)
		{
			cCDO.cSolarObject = cNewSolarObject;
			cCDO.cSolarObject.cObjContainer.addBase(ref cMyReference);
			cCDO.cSolarObject.tNode_Bases.Nodes.Add(tNode);
		}

		public static classBase getBaseByName(string strName)
		{
			for (int intBaseCounter = 0; intBaseCounter < cBases.Length && cBases[intBaseCounter] != null; intBaseCounter++)
				if (cBases[intBaseCounter].Name == strName)
				{
					return cBases[intBaseCounter];
				}

			return null;
		}

		public classShip BuildShip(string strName, enuShipModels eModel)
		{
			classShip cNewShip = classShip.createShip();
			cNewShip.eModel = eModel;
			cNewShip.cCDO.Landed = true;
			cNewShip.Name = strName;
			cNewShip.AddToBase(ref cMyReference);
			cNewShip.cCDO.cSolarObject = cCDO.cSolarObject;
			cNewShip.cCDO.dblAngle = Math.PI * 1.5;

			cNewShip.cCDO.eLoc = enuLocation.Base;
			if (cCDO.eLoc == enuLocation.Surface)
			{
				cNewShip.cCDO.dptPos
					= new classMath.classDoubleCartesian(cCDO.dptPos.X + (classLandscape.intLandscapeCellWidth - cNewShip.cCDO.radius * 2),
														 classMath.findCenterOfLine(cNewShip.cCDO.cCell.dptLeft, cNewShip.cCDO.cCell.dptRight).Y - cNewShip.cCDO.radius);
			}
			else
			{
				switch (eCaveWallLocation)
				{
					case enuTypeCaveCellWall.left:
						classMath.classDoubleCartesian dptCenterOfLine_Left = classMath.findCenterOfLine(cNewShip.cCDO.cCell.cLeftWall.dptMine, cNewShip.cCDO.cCell.cLeftWall.dptNext);
						cNewShip.cCDO.dptPos = new classMath.classDoubleCartesian(dptCenterOfLine_Left.X,
																				  dptCenterOfLine_Left.Y - cNewShip.cCDO.radius);
						break;

					case enuTypeCaveCellWall.right:
						classMath.classDoubleCartesian dptCenterOfLine_Right = classMath.findCenterOfLine(cNewShip.cCDO.cCell.cRightWall.dptMine, cNewShip.cCDO.cCell.cRightWall.dptNext);
						cNewShip.cCDO.dptPos = new classMath.classDoubleCartesian(dptCenterOfLine_Right.X,
																				  dptCenterOfLine_Right.Y - cNewShip.cCDO.radius);
						break;

					case enuTypeCaveCellWall.bottom:
						classMath.classDoubleCartesian dptCenterOfLine_Bottom = classMath.findCenterOfLine(cNewShip.cCDO.cCell.cBottomWall.dptMine, cNewShip.cCDO.cCell.cBottomWall.dptNext);
						cNewShip.cCDO.dptPos = new classMath.classDoubleCartesian(dptCenterOfLine_Bottom.X,
																				  dptCenterOfLine_Bottom.Y - cNewShip.cCDO.radius);
						break;
				}
			}
			cNewShip.cCDO.cSolarObject = classSolarObject.cSolarObjects[(int)enuSolarObjects.Earth];
			cNewShip.setAtmosphericPressure();
			cNewShip.AddToSolarObject(ref cCDO.cSolarObject);
			return cNewShip;
		}

		public classAstronaut PromoteAstronaut(string strName, int intRating_Pilot, int intRating_Engineer, int intRating_Chemist, int intRating_Geologist)
		{
			classAstronaut cNewAstronaut = classAstronaut.CreateAstronaut();
			cNewAstronaut.AddToBase(ref cMyReference);
			cNewAstronaut.Name = strName;
			cNewAstronaut.RatingPilot = intRating_Pilot;
			cNewAstronaut.RatingEngineer = intRating_Engineer;
			cNewAstronaut.RatingChemist = intRating_Chemist;
			cNewAstronaut.RatingGeologist = intRating_Geologist;
			cCDO.cSolarObject.cObjContainer.addAstronaut(ref cNewAstronaut);
			return cNewAstronaut;
		}
	}

	
	public class classBaseData
	{
		public static GroupBox grbBaseData = new GroupBox();
		static Label lblLongitude_Name = new Label(), lblPosition_Name;
		static Label lblLongitude_Value = new Label(), lblPosition_Value;
		static Label lblAltitude_Name = new Label();
		static Label lblAltitude_Value = new Label();
		static Label lblLocation_Name = new Label();
		static Label lblLocation_Value = new Label();
		
		static classBase cBase;
		static public classBase Base
		{
			get
			{
				return cBase;
			}
			set
			{
				cBase = value;
				if (grbBaseData.Text != cBase.Name)
				{
					if (cBase.cCDO.cCell.cLandscape != null)
					grbBaseData.Text = cBase.Name + "(" + cBase.cCDO.cCell.cLandscape.cSolarObject.eSolarObject.ToString() + ")";
					//switch (cBase.cCDO.eLoc)
					//{
					//    case enuLocation.Cave:
					//        grbBaseData.Text = cBase.Name + "(" + cBase.cCDO.cCell.cCave.LandscapeCell.cLandscape.cSolarObject.eSolarObject.ToString() + ")";
					//        break;

					//    default:
					//        grbBaseData.Text = cBase.Name + "(" + cBase.cCDO.cLandscapeCell.cLandscape.cSolarObject.eSolarObject.ToString() + ")";
					//        break;
					//}
				}
			}
		}

		static public void setLabelNames()
		{
			if (cBase != null)
			{
				switch (cBase.cCDO.eLoc)
				{
					case enuLocation.Surface:
					case enuLocation.Cave:
					case enuLocation.Base:
						lblLongitude_Name.Text = "Longitude";
						lblAltitude_Name.Visible
							= lblAltitude_Value.Visible
							= true;
						grbBaseData.Text = cBase.Name + " (" + cBase.cCDO.cSolarObject.eSolarObject.ToString() + ")";
						break;
				}
			}
		}

		static public void DisplayBaseInfo()
		{
			if (cBase != null)
			{
				switch (cBase.cCDO.eLoc)
				{
					case enuLocation.Surface:
					case enuLocation.Cave:
						if (cBase.cCDO.eLoc == enuLocation.Surface)
						{ /// prime-meridian at crease
							double dblRealWidth = cBase.cCDO.cCell.cLandscape.dblRealWidth;

							double dblX = cBase.cCDO.dptPos.X;
							while (dblX >= dblRealWidth)
								dblX -= dblRealWidth;
							while (dblX < 0)
								dblX += dblRealWidth;
							//grbBaseData.Text = dblX.ToString();
							double dblRWBy2 = dblRealWidth / 2;
							if (dblX > dblRWBy2)
							{ /// 180 e/w at RW/2 -> west longitudinal values decreasing towards limit of X
								double dblDifference = dblRWBy2 - cBase.cCDO.dptPos.X;
								double dblDegreesAwayFromPrimeMeridian = 180.0 - Math.Abs(dblDifference / dblRWBy2) * 180.0;
								lblLongitude_Value.Text = classStringHandler.F2( dblDegreesAwayFromPrimeMeridian) + " West";
							}
							else
							{ /// east longitudinal values increasing towards limit of RW/2
								double dblDegreesAwayFromPrimeMeridian = Math.Abs(dblX / dblRWBy2) * 180.0;
								lblLongitude_Value.Text =classStringHandler.F2( dblDegreesAwayFromPrimeMeridian) + " East";
							}
						}

						if (cBase.cCDO.eLoc == enuLocation.Surface)
							lblAltitude_Value.Text = classStringHandler.F2(cBase.cCDO.cCell.cLandscape.intAveAlt - cBase.cCDO.dptPos.Y);

						if (cBase.cCDO.eLoc == enuLocation.Cave && cBase.cCDO.cCell != null)
						{
							lblLocation_Value.Text = "Cave (" + cBase.cCDO.cCell.intMyIndex.ToString() + "/" + cBase.cCDO.cCell.cCave.cCells.Length.ToString() + ")";
						}
						else
							lblLocation_Value.Text = cBase.cCDO.eLoc.ToString();
						break;
				}
			}
		}

		public static void initGrbBaseData()
		{
			grbBaseData.ForeColor = Color.Yellow;

			lblPosition_Name = lblLongitude_Name;
			lblPosition_Value = lblLongitude_Value;

			grbBaseData.Controls.Add(lblAltitude_Name);
			grbBaseData.Controls.Add(lblAltitude_Value);
			grbBaseData.Controls.Add(lblLocation_Name);
			grbBaseData.Controls.Add(lblLocation_Value);
			grbBaseData.Controls.Add(lblLongitude_Name);
			grbBaseData.Controls.Add(lblLongitude_Value);
			lblLongitude_Name.AutoSize
				= lblLongitude_Value.AutoSize
				= lblAltitude_Name.AutoSize
				= lblAltitude_Value.AutoSize
				= lblLocation_Name.AutoSize
				= lblLocation_Value.AutoSize
				= true;
			lblLongitude_Name.Visible
				= lblLongitude_Value.Visible
				= lblAltitude_Name.Visible
				= lblAltitude_Value.Visible
				= lblLocation_Name.Visible
				= lblLocation_Value.Visible
				= true;

			lblAltitude_Value.ForeColor
				= lblLocation_Value.ForeColor
				= lblLongitude_Value.ForeColor
				= Color.White;

			int intValueLabelsLeft = 219;
			int intVerticalGapBetweenLabels = 19;
			lblLongitude_Value.Left
				= lblAltitude_Value.Left
				= lblLocation_Value.Left
				= intValueLabelsLeft;

			lblLongitude_Value.Top
				= lblLongitude_Name.Top
				= 9;
			lblAltitude_Name.Top
				= lblAltitude_Value.Top
				= lblLongitude_Name.Top + intVerticalGapBetweenLabels;
			lblLocation_Name.Top
				= lblLocation_Value.Top
				= lblAltitude_Value.Top + intVerticalGapBetweenLabels;

			lblLongitude_Name.Text = "Longitude";
			lblLongitude_Name.Left = intValueLabelsLeft - 5 - lblLongitude_Name.Width;

			lblAltitude_Name.Text = "Altitude";
			lblAltitude_Name.Left = intValueLabelsLeft - 5 - lblAltitude_Name.Width;

			lblLocation_Name.Text = "Location";
			lblLocation_Name.Left = intValueLabelsLeft - 5 - lblLocation_Name.Width;

			grbBaseData.Height = lblLocation_Name.Top + lblLocation_Name.Height + 5;
			grbBaseData.Width = intValueLabelsLeft + 200;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO unemployable
Canada Canada
Christ Kennedy grew up in the suburbs of Montreal and is a bilingual Quebecois with a bachelor’s degree in computer engineering from McGill University. He is unemployable and currently living in Moncton, N.B. writing his next novel.

Comments and Discussions