using UnityEngine;
using System.Collections;
using System.Xml;
public class GenerateMaze : MonoBehaviour 
{
	public GameObject wall;
	private int [,] worldMap = new int [,] 
	{
		{1,1,1,1,1,1,1,1,1,1},
		{1,2,1,0,0,0,0,0,0,1},
		{1,0,1,0,1,0,1,0,0,1},
		{1,0,1,0,0,0,0,0,0,1},
		{1,0,1,1,1,1,0,0,0,1},					
		{1,0,0,0,0,0,0,0,0,1},
		{1,0,1,0,1,0,1,1,1,1},
		{1,0,0,1,0,0,0,0,0,1},
		{1,0,1,0,0,0,0,0,0,1},
		{1,1,1,1,1,1,1,1,1,1},					
	};
	// Use this for initialization
	void Start()

	{
		
		TextAsset textAsset = (TextAsset) Resources.Load("scene");  
		XmlDocument doc = new XmlDocument ();
		doc.LoadXml ( textAsset.text );

		foreach (XmlNode level in doc.SelectNodes("game/level")) 
		{
			if (level.Attributes.GetNamedItem ("number").Value == "1") 
			{
				foreach (XmlNode gameObject in level.SelectNodes(".//object")) 
				{
					string name, location;
					name = gameObject.Attributes.GetNamedItem ("name").Value;
					location = gameObject.Attributes.GetNamedItem ("location").Value;
					Vector3 v = ConvertStringToVector (location);
					GameObject g = (GameObject)Instantiate (wall, v, Quaternion.identity);
					g.name = name;
				}
			}

		}

	}
		
	
	
	// Update is called once per frame
	void Update () {
	
	}
	void GenerateFromArray()
	{
		int i,j;
		for (i = 0; i < 10; i++)
		{
			for (j = 0; j < 10; j++)
			{
				GameObject t;
				if (worldMap [i,j] == 1) t = (GameObject)(Instantiate (wall, new Vector3 (50-i*10, 1.5f, 50-j*10), Quaternion.identity));

			} 
		}
	}
	void GenerateFromText()
	{
		TextAsset t1 = (TextAsset)Resources.Load("maze", typeof(TextAsset));
		string s = t1.text;
		int i, j;
		s = s.Replace("\n","");
		for (i = 0; i < s.Length; i++) 
		{
			if (s [i] == '1') 
			{
				int column, row;
				column = i%10;
				row = i / 10;
				GameObject t;
				t = (GameObject)(Instantiate (wall, new Vector3 (50 - column * 10, 1.5f, 50 - row * 10), Quaternion.identity));
			}
		}
	}

	Vector3 ConvertStringToVector(string s)
	{
		string [] newString;
		newString = s.Split (new char [] { ',' });
		float x, y, z;
		x = float.Parse (newString[0]);
		y = float.Parse (newString[1]);
		z = float.Parse (newString[2]);
		return new Vector3 (x, y, z);
	}

}
