using UnityEngine;
using System.Collections;

public class Mg_World : MonoBehaviour 
{
    [Header("Group 1")]
    public float amountType1;
    public float radius1;
    public float size1;
    public Material mat1;

    [Header("Group 2")]
    public float amountType2;
    public float radius2;
    public float size2;
    public Material mat2;

    [Header("Group 3")]
    public float amountType3;
    public float radius3;
    public float size3;
    public Material mat3;

	void Start () {

        for (int i = 0; i < amountType1; i++)
            CreatePlane(mat1, 1, size1, radius1);
        for (int i = 0; i < amountType2; i++)
            CreatePlane(mat2, 2, size2, radius2);
        for (int i = 0; i < amountType3; i++)
            CreatePlane(mat3, 3, size3, radius3);
	}

    public void CreatePlane(Material skin, float depth, float size, float radius)
    {
        Mesh mesh = new Mesh();

        Vector3[] verts = new Vector3[9];
        Vector2[] uvs = new Vector2[9];
        int[] tris = new int[8*3] { 0, 1, 2, 2, 1, 3, 1,4,3, 3,4,5 ,2,3,6 ,6,3,7, 3,5,7, 7,5,8};

        // Vertex layout
        // 0 ------1 4
        // |     / |
        // |   /   |		
        // | /     |
        // 2 ------ 3  5      
        // 6        7  8
        // 

        verts[0] = new Vector3(Random.Range(-0.1f, -0.5f), Random.Range(-0.1f, -0.5f), 0);
        verts[1] = new Vector3(Random.Range(0.1f, 0.5f), Random.Range(-0.1f, -0.5f), 0);
        verts[2] = new Vector3(Random.Range(-0.1f, -0.5f), Random.Range(0.1f, 0.5f), 0);
        verts[3] = new Vector3(Random.Range(0.1f, 0.5f), Random.Range(0.1f, 0.5f), 0);
        verts[4] = new Vector3(Random.Range(1f, 1.5f), Random.Range(-0.1f, -1.0f), 0);
        verts[5] = new Vector3(Random.Range(1f, 2f), Random.Range(0.1f, 2.0f), 0);

        verts[6] = new Vector3(Random.Range(-0.1f, -0.5f), Random.Range(1.1f, 1.5f), 0);
        verts[7] = new Vector3(Random.Range(0.1f, 0.5f), Random.Range(1.1f, 1.5f), 0);
        verts[8] = new Vector3(Random.Range(1f, 2f), Random.Range(1.1f, 2.0f), 0);

        uvs[0] = new Vector2(0.0f, 1.0f);
        uvs[1] = new Vector2(1.0f, 1.0f);
        uvs[2] = new Vector2(0.0f, 0.0f);
        uvs[3] = new Vector2(1.0f, 0.0f);

        mesh.vertices = verts;
        mesh.triangles = tris;
        mesh.uv = uvs;

        mesh.RecalculateNormals();
        mesh.RecalculateBounds();

        GameObject newMeshObject = new GameObject();
        newMeshObject.AddComponent<MeshFilter>().mesh = mesh;
        newMeshObject.AddComponent<MeshRenderer>();

        newMeshObject.gameObject.renderer.material = skin;
        newMeshObject.transform.parent = gameObject.transform;

        float w = radius * Mathf.Sqrt(Random.Range(0.05f, 1f));
        float t = 2 * Mathf.PI * Random.Range(0f, 1f);
        float x = w * Mathf.Cos(t);
        float y = w * Mathf.Sin(t);

        newMeshObject.transform.position = new Vector3(x, y, depth);
        newMeshObject.transform.localScale *= Random.Range(5f, size);
        newMeshObject.transform.localEulerAngles = new Vector3(0, 0, Random.Range(0f, 360f));
    }
}
