Example #1
0
    public IEnumerator GenerateChunk()
    {
        Stack <GenericBlockData> blocks = m_Pool.GetObjectData();

        m_CaveGenerator.ResetGeneration();
        yield return(StartCoroutine(m_CaveGenerator.CaveCourotine()));

        while (!m_CaveGenerator.done)
        {
            yield return(new WaitForEndOfFrame());
        }
        byte[,] caves = m_CaveGenerator.ReturnData();

        Debug.Log("Caves Done");

        //byte[,] dirt = m_CaveGenerator.GenerateCave();

        m_CaveGenerator.SetMaxOres(40);
        yield return(StartCoroutine(m_CaveGenerator.OresCourotine()));

        while (!m_CaveGenerator.done)
        {
            yield return(new WaitForEndOfFrame());
        }
        byte[,] diamons = m_CaveGenerator.ReturnData();

        Debug.Log("Diamonds Done");

        m_CaveGenerator.SetMaxOres(85);
        yield return(StartCoroutine(m_CaveGenerator.OresCourotine()));

        while (!m_CaveGenerator.done)
        {
            yield return(new WaitForEndOfFrame());
        }
        byte[,] emeralds = m_CaveGenerator.ReturnData();

        Debug.Log("Emeralds Done");

        m_CaveGenerator.SetMaxOres(50);
        yield return(StartCoroutine(m_CaveGenerator.OresCourotine()));

        while (!m_CaveGenerator.done)
        {
            yield return(new WaitForEndOfFrame());
        }
        byte[,] gold = m_CaveGenerator.ReturnData();

        Debug.Log("Gold Done");

        m_CaveGenerator.SetMaxOres(110);
        yield return(StartCoroutine(m_CaveGenerator.OresCourotine()));

        while (!m_CaveGenerator.done)
        {
            yield return(new WaitForEndOfFrame());
        }
        byte[,] iron = m_CaveGenerator.ReturnData();

        Debug.Log("Iron Done");

        m_CaveGenerator.SetMaxOres(75);
        yield return(StartCoroutine(m_CaveGenerator.OresCourotine()));

        while (!m_CaveGenerator.done)
        {
            yield return(new WaitForEndOfFrame());
        }
        byte[,] ruby = m_CaveGenerator.ReturnData();

        Debug.Log("Ruby Done");

        m_CaveGenerator.SetMaxOres(75);
        yield return(StartCoroutine(m_CaveGenerator.OresCourotine()));

        while (!m_CaveGenerator.done)
        {
            yield return(new WaitForEndOfFrame());
        }
        byte[,] sapphire = m_CaveGenerator.ReturnData();

        Debug.Log("Sapphire Done");


        int Xmax = caves.GetLength(0);
        int Ymax = caves.GetLength(1);

        for (int x = 0; x < m_Size; x++)
        {
            for (int y = 0; y < m_Size; y++)
            {
                if (caves[x, y] < 16)
                {
                    Vector3  position = transform.position + new Vector3(x, y) * 1.5f;
                    BigBlock block    = Instantiate(m_Prefab, position, Quaternion.identity).GetComponent <BigBlock>();

                    //if (dirt[x, y]>16)
                    //{
                    //    block.Initialize(BlockServiceProvider.instance.GetBlock(1));
                    //}

                    BigBlock ore = null;

                    //SpawnOre
                    if (iron[x, y] > 15)
                    {
                        ore = Instantiate(m_Prefab, position, Quaternion.identity).GetComponent <BigBlock>();
                        ore.Initialize(BlockServiceProvider.instance.GetOre(4));
                        ore.gameObject.name = "Iron";
                    }
                    else if (emeralds[x, y] > 15)
                    {
                        ore = Instantiate(m_Prefab, position, Quaternion.identity).GetComponent <BigBlock>();
                        ore.Initialize(BlockServiceProvider.instance.GetOre(2));
                        ore.gameObject.name = "Emeralds";
                    }
                    else if (ruby[x, y] > 15)
                    {
                        ore = Instantiate(m_Prefab, position, Quaternion.identity).GetComponent <BigBlock>();
                        ore.Initialize(BlockServiceProvider.instance.GetOre(5));
                        ore.gameObject.name = "Ruby";
                    }
                    else if (sapphire[x, y] > 15)
                    {
                        ore = Instantiate(m_Prefab, position, Quaternion.identity).GetComponent <BigBlock>();
                        ore.Initialize(BlockServiceProvider.instance.GetOre(6));
                        ore.gameObject.name = "Sapphire";
                    }
                    else if (gold[x, y] > 15)
                    {
                        ore = Instantiate(m_Prefab, position, Quaternion.identity).GetComponent <BigBlock>();
                        ore.Initialize(BlockServiceProvider.instance.GetOre(3));
                        ore.gameObject.name = "Gold";
                    }

                    else if (diamons[x, y] > 15)
                    {
                        ore = Instantiate(m_Prefab, position, Quaternion.identity).GetComponent <BigBlock>();
                        ore.Initialize(BlockServiceProvider.instance.GetOre(1));
                        ore.gameObject.name = "Diamond";
                    }

                    //LeftTop
                    if (x - 1 > 0 && y + 1 < Ymax)
                    {
                        if (caves[x - 1, y + 1] > 16)
                        {
                            block.SetBlockState(0, BlockState.Mined);
                            if (ore != null)
                            {
                                ore.SetBlockState(0, BlockState.Mined);
                            }
                        }
                    }
                    //Left
                    if (x - 1 > 0)
                    {
                        if (caves[x - 1, y] > 16)
                        {
                            block.SetBlockState(0, BlockState.Mined);
                            block.SetBlockState(2, BlockState.Mined);
                            if (ore != null)
                            {
                                ore.SetBlockState(0, BlockState.Mined);
                                ore.SetBlockState(2, BlockState.Mined);
                            }
                        }
                    }
                    //LeftBottom
                    if (x - 1 > 0 && y - 1 > 0)
                    {
                        if (caves[x - 1, y - 1] > 16)
                        {
                            block.SetBlockState(2, BlockState.Mined);
                            if (ore != null)
                            {
                                ore.SetBlockState(2, BlockState.Mined);
                            }
                        }
                    }
                    //Top
                    if (y + 1 < Ymax)
                    {
                        if (caves[x, y + 1] > 16)
                        {
                            block.SetBlockState(0, BlockState.Mined);
                            block.SetBlockState(1, BlockState.Mined);
                            if (ore != null)
                            {
                                ore.SetBlockState(0, BlockState.Mined);
                                ore.SetBlockState(1, BlockState.Mined);
                            }
                        }
                    }
                    //Bottom
                    if (y - 1 > 0)
                    {
                        if (caves[x, y - 1] > 16)
                        {
                            block.SetBlockState(2, BlockState.Mined);
                            block.SetBlockState(3, BlockState.Mined);
                            if (ore != null)
                            {
                                ore.SetBlockState(2, BlockState.Mined);
                                ore.SetBlockState(3, BlockState.Mined);
                            }
                        }
                    }
                    //RightTop
                    if (x + 1 < Xmax && y + 1 < Ymax)
                    {
                        if (caves[x + 1, y + 1] > 16)
                        {
                            block.SetBlockState(1, BlockState.Mined);
                            if (ore != null)
                            {
                                ore.SetBlockState(1, BlockState.Mined);
                            }
                        }
                    }
                    //Right
                    if (x + 1 < Xmax)
                    {
                        if (caves[x + 1, y] > 16)
                        {
                            block.SetBlockState(1, BlockState.Mined);
                            block.SetBlockState(3, BlockState.Mined);
                            if (ore != null)
                            {
                                ore.SetBlockState(1, BlockState.Mined);
                                ore.SetBlockState(3, BlockState.Mined);
                            }
                        }
                    }
                    //RightBottom
                    if (x + 1 < Xmax && y - 1 > 0)
                    {
                        if (caves[x + 1, y - 1] > 16)
                        {
                            block.SetBlockState(3, BlockState.Mined);
                            if (ore != null)
                            {
                                ore.SetBlockState(3, BlockState.Mined);
                            }
                        }
                    }

                    block.UpdateBlock();
                    block.transform.parent = this.transform;
                    if (ore != null)
                    {
                        ore.UpdateBlock();
                        ore.transform.parent = this.transform;
                        ore.gameObject.GetComponent <SpriteRenderer>().sortingOrder = 1;
                    }

                    //yield return new WaitForEndOfFrame();
                }
            }
        }
        m_Done = true;
        yield return(StartCoroutine(PopulateCaves(caves)));
    }