using UnityEngine;
using System.Collections;
using System.Collections.Generic;


// You know what, nevermind. Lets do this the easy way.

public class SpawnerGrid : MonoBehaviour {
	/*

	// The game area is composed of a number of "chunks." A chunk should correspond to a area of space that can contain the player.
	// The game will spawn obstacles inside of chunks at various intervals.
	// The Spawner Grid will store and handle all of the spawn locations.
	// (This is kind of an expensive way to do this, isn't it?)

	// This doesn't actually need in instance, but it helps for visualizations during OnGizmos.

	private static Vector3[,,] chunks3D;

	private static Vector3[] chunks;

	[SerializeField]
	private float chunkSize;
	[SerializeField]
	private float gameRadius;
	[SerializeField]
	private float gameHeight;
	
	static public void Setup (float gameRadius, float chunkSize)
	{
		// Sanity Check
		// Don't let <1 chunksizes exist.
		chunkSize = Mathf.Min (1, chunkSize);
		
		// Precompute number of chunks.
		int numChunks = Mathf.CeilToInt (gameRadius / chunkSize);
		
		// Prebuild array with numChunks^2 slots.
		List<Vector3> chunkLocations = new List<Vector3> (numChunks*numChunks);
		
		// Spawn chunks as squares.
		for (int x = 0; x < numChunks; x++) {
			for (int y = 0; y < numChunks; y++) {
				chunkLocations.Add (new Vector3((x*chunkSize)+(chunkSize*0.5f), 0, (y*chunkSize)+(chunkSize*0.5f)));
			}
		}
		chunks = chunkLocations.ToArray ();
	}

	static public void Setup3D (float gameRadius, float gameHeight, float chunkSize)
	{
		// Sanity Check
		// Don't let <1 chunksizes exist.
		chunkSize = Mathf.Min (1, chunkSize);
		// Precompute number of chunks.
		int numChunkWidth	= Mathf.CeilToInt (gameRadius / chunkSize);
		int numChunkHeight	= Mathf.CeilToInt (gameHeight / chunkSize);

		// Because we're using a 3D array, we won't bother with an intermediary list. 
		// We'll just assign directly.
		chunks3D = new Vector3[numChunkWidth,numChunkHeight,numChunkWidth];
		// Chunk positions will mirror unity positions.
		// Since this is a game about primary transit along the Z axis, we'll use the game radius for the X and Z dimensions.
		// The Y dimension corresponds to the game height.

		// Spawn Chunks as cubes

	}

//	static public Vector3 GetRandomChunk()
//	{
//		if (chunks && chunks.Length > 0)
//		{
//			return chunks[Random.Range(0,chunks.Length)];
//		}
//		return Vector3.zero;
//	}
	
	#region Gizmos
	#if UNITY_EDITOR
	[SerializeField]
	private Mesh cylinderMesh;

	void OnDrawGizmos()
	{
		chunkSize = Mathf.Max (1, chunkSize);
		
		// Precompute number of chunks.
		int numChunks = Mathf.CeilToInt (gameRadius / chunkSize);

		Gizmos.color = new Color (1,0,0,0.25f);

		Gizmos.DrawMesh (cylinderMesh,
		                 new Vector3(gameRadius*0.5f,-5,gameRadius*0.5f),
		                 Quaternion.identity,
		                 new Vector3(gameRadius,5,gameRadius));
		Gizmos.color = new Color (1,0,0,1f);
		Gizmos.DrawMesh (cylinderMesh,
		                 new Vector3(gameRadius*0.5f,0,gameRadius*0.5f),
		                 Quaternion.identity,
		                 new Vector3(gameRadius,0f,gameRadius));
		Gizmos.DrawMesh (cylinderMesh,
		                 new Vector3(gameRadius*0.5f,-10,gameRadius*0.5f),
		                 Quaternion.identity,
		                 new Vector3(gameRadius,0f,gameRadius));
		
		Gizmos.color = Color.green;
		Gizmos.DrawWireCube (new Vector3(gameRadius*0.5f,-5,gameRadius*0.5f),new Vector3(gameRadius,10,gameRadius));

		for (int x = 0; x < numChunks; x++) {
			for (int y = 0; y < numChunks; y++) {

				float xx = (x*chunkSize)+(chunkSize*0.5f);
				float zz = (y*chunkSize)+(chunkSize*0.5f);
				Gizmos.DrawWireCube(new Vector3(xx,0,zz),new Vector3(chunkSize,0,chunkSize));
			}
		}

		if (chunks == null) {
			return;
		}
		foreach (Vector3 v in chunks) {
			Gizmos.color = Color.red;
			Gizmos.DrawLine(v,v + (Vector3.down * 0.1f));
		}
	}
	#endif
	#endregion
*/
}