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

Mars Mission (4) : Astronauts

Rate me:
Please Sign up or sign in to vote.
4.96/5 (26 votes)
5 Jul 2011CPOL55 min read 43.6K   6.5K   29  
strategy/action game defending the solar system : incorporating astronauts, resources, and actions inside and outside the ships
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 string strUniqueIDFilename = "UniqueID_Bases.xml";
		public static cLibString.classBase26Variable b26UniqueIDCounter = cLibString.classBase26Variable.fromXmlFile(classRotatedImage.strWorkingDirectory + strUniqueIDFilename);
		public cLibString.classBase26Variable b26MyUniqueID;
		
		public classCollisionDetectionObject cCDO = new classCollisionDetectionObject();
		public classObjectContainer cObjContainer;
		
		classBase cMyReference;
		public classStructure cStructure;


		static public classBase createBase(enuStructureModels StructureModel)
		{
			classBase cRetVal = new classBase(StructureModel);
			//Array.Resize<classBase>(ref cBases, cBases.Length + 1);

			//cBases[cBases.Length - 1] = cRetVal;
			return cRetVal;
		}

		private classBase(enuStructureModels StructureModel)
		{
			cStructure = new classStructure(StructureModel);
			initBase();
		}

		private classBase(ref classStructure Structure)
		{
			cStructure = Structure;
			initBase();
		}

		private void initBase()
		{
			cMyReference = this;
			object objMyObjectReference = (object)cMyReference;
			cObjContainer = new classObjectContainer(ref objMyObjectReference, enuTypeObject.Base);
			b26MyUniqueID = b26UniqueIDCounter.increment();
			b26UniqueIDCounter.saveToXml(classRotatedImage.strWorkingDirectory + strUniqueIDFilename);
			Name = "Base UID:" + b26MyUniqueID.Value;
			Array.Resize<classBase>(ref cBases, cBases.Length + 1);
			cBases[cBases.Length - 1] = this;

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

		}

		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);

			XmlNode xUID = xDoc.CreateElement(classSaveGame.conXmlField_Base_UID);
			xUID.InnerText = b26MyUniqueID.Value;
			xRetVal.AppendChild(xUID);

			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);
			xStructureArray.AppendChild(cStructure.getSaveGameNode(ref xDoc));

			xRetVal.AppendChild(xStructureArray);

			return xRetVal;
		}

		public static classBase fromXmlNode(ref XmlNode xNode)
		{
			XmlNode xName = xNode.FirstChild;
			XmlNode xUID = xName.NextSibling;
			XmlNode xCDO = xUID.NextSibling;
			XmlNode xObjContainer = xCDO.NextSibling;
			XmlNode xWallLocation = xObjContainer.NextSibling;
			XmlNode xStructure = xWallLocation.NextSibling;

			classStructure cStructure = classStructure.fromXmlNode(ref xStructure);

			classBase cRetVal = new classBase(cStructure.eModel);
			cRetVal.Name = xName.InnerText;
			cRetVal.cCDO = classCollisionDetectionObject.fromXmlNode(ref xCDO);
		
			
			cRetVal.b26MyUniqueID.Value = xUID.InnerText;
			cRetVal.cCDO.cIAmBase = cRetVal;
			cRetVal.cCDO.eTypeObject = enuTypeObject.Base;
			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].cCDO.Base = cRetVal;

			cRetVal.eCaveWallLocation = (enuTypeCaveCellWall)Convert.ToInt16(xWallLocation.InnerText);
			return cRetVal;
		}

		public enuTypeCaveCellWall eCaveWallLocation = enuTypeCaveCellWall.bottom;

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

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

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

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

		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 static classBase getBaseByUID(string strUID)
		{
			for (int intBaseCounter = 0; intBaseCounter < cBases.Length && cBases[intBaseCounter] != null; intBaseCounter++)
				if (cBases[intBaseCounter].b26MyUniqueID.Value == strUID)
				{
					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.addToCell(ref cCDO.cCell);
			cNewShip.cCDO.cSolarObject = cCDO.cSolarObject;
			switch (eModel)
			{
				//case enuShipModels.shuttleCraft:
				//    cNewShip.cCDO.dblAngle = 0;
				//    break;

				default:
					cNewShip.cCDO.dblAngle = Math.PI * 1.5;
					break;
			}

			cNewShip.cCDO.eLoc = cCDO.eLoc;
			if (cCDO.eLoc == enuLocation.Surface)
			{
				classMath.classRadialCoor cRad = new classMath.classRadialCoor(classMath.arcTan(cCDO.cCell.dptLeft, cCDO.cCell.dptRight), 10 * cNewShip.cCDO.radius);
				classMath.classDoubleCartesian dptGround = classMath.convertRadToDCar(cCDO.dptPos, cRad);
				cNewShip.cCDO.dptPos
					= new classMath.classDoubleCartesian(dptGround.X, dptGround.Y - cNewShip.cCDO.radius + cStructure.dblVerticalDrawShift );
			}
			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.setAtmosphericPressure();
			//cNewShip.AddToSolarObject(ref cCDO.cSolarObject);
			return cNewShip;
		}

		public string descriptor()
		{
			return Name
					+ (cCDO.eLoc == enuLocation.Surface
								? " on the surface of "
								: " beneath the surface of ")
					+ cCDO.cSolarObject.descriptor();
		}

		public classAstronaut PromoteAstronaut(string strName, double dblRating_Pilot, double dblRating_Engineer, double dblRating_Chemist, double dblRating_Geologist)
		{
			classAstronaut cNewAstronaut = classAstronaut.CreateAstronaut();
			cNewAstronaut.AddToBase(ref cMyReference);
			cNewAstronaut.Name = strName;
			cNewAstronaut.RatingPilot = dblRating_Pilot;
			cNewAstronaut.RatingEngineer = dblRating_Engineer;
			cNewAstronaut.RatingChemist = dblRating_Chemist;
			cNewAstronaut.RatingGeologist = dblRating_Geologist;
			cNewAstronaut.cCDO.Base = this;
			cCDO.cSolarObject.cObjContainer.addAstronaut(ref cNewAstronaut);
			return cNewAstronaut;
		}
	}
	
	public class groupboxBaseData : CK_Controls.groupboxCollapsible
	{
		Label lblLongitude_Name = new Label(), lblPosition_Name;
		Label lblLongitude_Value = new Label(), lblPosition_Value;
		Label lblAltitude_Name = new Label();
		Label lblAltitude_Value = new Label();
		Label lblLocation_Name = new Label();
		Label lblLocation_Value = new Label();
		
		classBase cBase;
		public classBase Base
		{
			get
			{
				return cBase;
			}
			set
			{
				cBase = value;
				if (grb.Text != cBase.Name)
				{
					grb.ForeColor = Color.Blue;
					if (cBase.cCDO.cCell.cLandscape != null)
						grb.Text = "base " + cBase.Name + "(" + cBase.cCDO.cCell.cLandscape.cSolarObject.eSolarObject.ToString() + ")";
				}
			}
		}

		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;

						grb.Text = "base " + cBase.Name + "(" + cBase.cCDO.cCell.cLandscape.cSolarObject.eSolarObject.ToString() + ")";
						break;
				}
			}
		}

		public void ShowData()
		{
			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;
							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();
						if (Parent != null)
							Width = Parent.Width - 10;
						break;
				}
			}
		}

		public void initGrbBaseData()
		{
			ForeColor 
				= grb.ForeColor 
				= Color.Blue;

			lblPosition_Name = lblLongitude_Name;
			lblPosition_Value = lblLongitude_Value;

			grb.Controls.Add(lblAltitude_Name);
			grb.Controls.Add(lblAltitude_Value);
			grb.Controls.Add(lblLocation_Name);
			grb.Controls.Add(lblLocation_Value);
			grb.Controls.Add(lblLongitude_Name);
			grb.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
				= 10;
			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;

			grb.Height = lblLocation_Name.Top + lblLocation_Name.Height + 5;
			if (Parent != null)
				Width = Parent.Width - 10; 
		}
	}

}

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