Esempio n. 1
0
		public void ParseToXML (string levelIndex)
		{
			var allObjects = GameObject.FindGameObjectsWithTag("LevelSystem");
			List<GameObject> listOfAllObjects = allObjects.ToList();
			listOfAllObjects.Add(GameObject.FindGameObjectWithTag("Rocket"));

			foreach (var obj in listOfAllObjects)
			{
				var nameToObjectParser = new NameToObjectParse();
				ObjectTypes type = nameToObjectParser.ParseNameToType(obj);

				var gravityObject = new GravityObject();

				if (type == ObjectTypes.BigAsteroid || type == ObjectTypes.SmallAsteroid)
				{
					gravityObject = new GravityObject(obj.transform.position, obj.transform.rotation, type);
					var asteroidScript = obj.GetComponent<Asteroid>();
					gravityObject.AType = asteroidScript.AsteroidType;
					gravityObject.AClockWise = asteroidScript.RotateClockwise;
					gravityObject.ASpeed = asteroidScript.Speed;
					gravityObject.ALength = asteroidScript.PathLength;
					Level.Add(gravityObject);
				}
				else
				{
					gravityObject = new GravityObject(obj.transform.position, obj.transform.rotation, type);
					Level.Add(gravityObject);
				}
			}
			
			Debug.Log("Level parsed!");
			var xmlWriter = new XMLWriter();
			xmlWriter.Write(Level, levelIndex);
			Debug.Log("Level [" + levelIndex +"] written to XML");
		}
Esempio n. 2
0
		public List<GravityObject> Load (string levelIndex) 
		{
			List<GravityObject> Level = new List<GravityObject>();

			var doc = (TextAsset)Resources.Load("Gravity_" + levelIndex, typeof(TextAsset));

			var loadedFromXML = new XmlDocument();

			try 
			{
				loadedFromXML.LoadXml (doc.text);
			} 
			catch (Exception ex) 
			{
				throw new NullReferenceException("XMLDoc not found..: (doc is null) - " + ex);
			}



			foreach (XmlNode node in loadedFromXML.DocumentElement.ChildNodes)
			{
				Vector2 vec = Vector2.zero;
				Quaternion quat = new Quaternion (0,0,0,1);
				ObjectTypes ot = ObjectTypes.Undefined;

				int commaLoc;

//				Debug.Log(node.Name);

				// Parse Vec2
				var v = node.Attributes["V"].InnerText;
				v = v.Replace("(", "").Replace(")", "");
				commaLoc = v.IndexOf(",");
				vec.x = float.Parse(v.Remove(commaLoc));
				vec.y = float.Parse(v.Remove(0, commaLoc+1));

//				Debug.Log(vec);


				// Parse Quaternion
				var q = node.Attributes["Q"].InnerText;
				q = q.Replace("(", "").Replace(")", "");
				commaLoc = q.IndexOf(",");
				var quatx = q.Remove(commaLoc);
				quat.x = float.Parse(quatx);

				var quaty = q.TrimStart(quatx.ToCharArray()).TrimStart(',');
				commaLoc = quaty.IndexOf(",");
				quaty = quaty.Remove(commaLoc);
				quat.y = float.Parse(quaty);

				var quatz = q.TrimStart(quatx.ToCharArray()).TrimStart(',').TrimStart(quaty.ToCharArray()).TrimStart(',');
				var quatw = quatz;			// Borrowing this value here.
				commaLoc = quatz.IndexOf(",");
				quatz = quatz.Remove(commaLoc);
				quat.z = float.Parse(quatz);

				quatw = quatw.TrimStart(quatz.ToCharArray()).TrimStart(',');
				quat.w = float.Parse(quatw);

//				Debug.Log(quat);

				// Parse ObjectType
				var t = node.Attributes["T"].InnerText;

				if (t == "Rocket")
					ot = ObjectTypes.Rocket;
				else if (t == "BlackHole")
					ot = ObjectTypes.BlackHole;
				else if (t == "Rocky")
					ot = ObjectTypes.Rocky;
				else if (t == "Rocky2")
					ot = ObjectTypes.Rocky2;
				else if (t == "GasGiant")
					ot = ObjectTypes.GasGiant;
				else if (t == "EndPlanet")
					ot = ObjectTypes.EndPlanet;
				else if (t == "StartPlanet")
					ot = ObjectTypes.StartPlanet;	
				else if (t == "Beyonce")
					ot = ObjectTypes.Beyonce;
				else if (t == "ButtHole")
					ot = ObjectTypes.ButtHole;
				else if (t == "LonelyPlanet")
					ot = ObjectTypes.LonelyPlanet;
				else if (t == "SeaGod")
					ot = ObjectTypes.SeaGod;
				else if (t == "Son")
					ot = ObjectTypes.Son;
				else if (t == "WormIn")
					ot = ObjectTypes.WormIn;
				else if (t == "WormOut")
					ot = ObjectTypes.WormOut;
				else if (t == "BigAsteroid")
					ot = ObjectTypes.BigAsteroid;
				else if (t == "SmallAsteroid")
					ot = ObjectTypes.SmallAsteroid;
				else if (t == "Undefined")
					Debug.Log("Undefined Object type... Something went wrong.");

//				Debug.Log(ot);

				// Asteroid specific section
				if (ot == ObjectTypes.BigAsteroid || ot == ObjectTypes.SmallAsteroid)
				{
					AsteroidType atype = AsteroidType.Continuous;
					bool arot = false;
					float aspeed = 0f;
					float alength = 0f;

					// Type
					var at = node.Attributes["A1"].InnerText;

					if (at == "FauxOrbit")
						atype = AsteroidType.FauxOrbit;
					else if (at == "OrbitPivot")
						atype = AsteroidType.OrbitPivot;

					// Rotation
					var cw = node.Attributes["A2"].InnerText;

					if (cw == "False")
						arot = false;
					else
						arot = true;

					// Speed
					var sp = node.Attributes["A3"].InnerText;

					aspeed = float.Parse(sp);

					// Path length
					var pl = node.Attributes["A4"].InnerText;

					alength = float.Parse(pl);

					// Load up
					var obj = new GravityObject(vec, quat, ot);
					obj.AType = atype;
					obj.AClockWise = arot;
					obj.ASpeed = aspeed;
					obj.ALength = alength;

					Level.Add(obj);
				}
				else
				{
					var obj = new GravityObject(vec, quat, ot);
					Level.Add(obj);
				}
			}
			Resources.UnloadUnusedAssets();
			return Level;
		}