Exemplo n.º 1
0
    /// <summary>
    /// Adds blocks in range to loadedBlocks, creating new blocks if they don't exist yet, and calling Generate() on blocks that should be visable
    /// </summary>
    /// <param name="range"></param>
    private void LoadBlocks(IntRect range, bool wrap = false)
    {
        //Initialize all blocks, with a one-wide buffer edge between null blocks and generated geometry
        for (int x = range.Left; x <= range.Right; x++)
        {
            for (int y = range.Bottom; y <= range.Top; y++)
            {
                if (cityBlocks.BlockInRange(x, y, wrap))
                {
                    BlockGenerator currentBlock = cityBlocks.GetBlock(x, y, wrap);

                    if (currentBlock == null)
                    {
                        currentBlock = CreateBlock(new Int2(x, y));
                    }
                    else
                    {
                        currentBlock.WakeUp();
                    }

                    loadedBlocks.Add(currentBlock);
                }
            }
        }

        //Generate geometry inside buffer edge
        IntRect generatedArea = range.Shrink(new Int2(1, 1));

        for (int x = generatedArea.Left; x <= generatedArea.Right; x++)
        {
            for (int y = generatedArea.Bottom; y <= generatedArea.Top; y++)
            {
                if (cityBlocks.BlockInRange(x, y, wrap) && cityBlocks.GetBlock(x, y, wrap) != null)
                {
                    BlockGenerator[,] surroundingBlocks = cityBlocks.GetAdjacentBlocks(x, y, wrap);
                    List <Vector3> surroundingPoints = new List <Vector3>();
                    for (int i = 0; i < 3; i++) //Loop through surroundingBlocks
                    {
                        for (int j = 0; j < 3; j++)
                        {
                            if (!(i == 1 && j == 1))                                                              //Skip middle block because it is the active one
                            {
                                if (surroundingBlocks[i, j] != null && surroundingBlocks[i, j].pointsSet == true) //If the block exists and has been initialized
                                {
                                    //Debug.Log("Block at " + cityBlocks.GetBlock(x, y).indexPosition + " is surrounded by " + surroundingBlocks[i, j].indexPosition);

                                    //Offset each point in pointlist by the position of the surrounding block relative to the center
                                    Vector3[]      pointList       = surroundingBlocks[i, j].pointList;
                                    List <Vector3> offsetPointlist = new List <Vector3>();
                                    for (int point = 0; point < pointList.Length; point++)
                                    {
                                        Vector3 offset = new Vector3((i - 1) * blockSize, 0f, (j - 1) * blockSize);
                                        offsetPointlist.Add(pointList[point] + offset);
                                    }
                                    surroundingPoints.AddRange(offsetPointlist);
                                }
                            }
                        }
                    }
                    cityBlocks.GetBlock(x, y, wrap).Generate(surroundingPoints.ToArray());
                }
            }
        }
    }