Exemplo n.º 1
0
    IEnumerator GenerateDrawTileChunk()
    {
        for (int x = chunkPosition.x - chunkIteration; x <= chunkPosition.x + chunkIteration; x++)
        {
            for (int y = chunkPosition.y - chunkIteration; y <= chunkPosition.y + chunkIteration; y++)
            {
                Vector2Int currentChunkPosition = new Vector2Int(x, y) * chunkSize;
                Vector3Int currentBoundPosition = new Vector3Int(currentChunkPosition.x, currentChunkPosition.y, 0);

                if (visitedChunk.Exists(c => c.bound.position == currentBoundPosition))
                {
                    continue;
                }

                NativeArray <int>  tileIndexes   = new NativeArray <int>(arraySize, Allocator.TempJob);
                NativeArray <int2> tilePositions = new NativeArray <int2>(arraySize, Allocator.TempJob);

                BoundsInt currentBound = new BoundsInt(currentBoundPosition, new Vector3Int(chunkSize.x, chunkSize.y, 1));

                var chunkBlockJob = new ChunkBlockJob
                {
                    colors    = nativeMapColorArray,
                    tiles     = tileIndexes,
                    pos       = tilePositions,
                    bound     = currentBound,
                    mapSize   = new int2(mapSize.x, mapSize.y),
                    oreColors = nativeTileColorArray
                };

                jobHandle = chunkBlockJob.Schedule(arraySize, 32);
                jobHandle.Complete();

                tileIndexes.CopyTo(indexesBuffer);
                tilePositions.CopyTo(positionBuffer);

                tileIndexes.Dispose();
                tilePositions.Dispose();


                for (int i = 0; i < arraySize; i++)
                {
                    tileBases[i] = tiles[indexesBuffer[i]];
                    positions[i] = new Vector3Int(positionBuffer[i].x, positionBuffer[i].y, 0);
                }

                TileChunk tileChunk = new TileChunk(currentBound, tileBases, tilemap);
                tileChunk.Draw();

                drawChunkQueue.Enqueue(tileChunk);
                visitedChunk.Add(tileChunk);
                yield return(null);
            }
        }
    }
Exemplo n.º 2
0
    IEnumerator ProcessDrawQueue()
    {
        while (true)
        {
            while (drawChunkQueue.Count == 0)
            {
                yield return(null);
            }

            TileChunk chunk = drawChunkQueue.Dequeue();

            chunk.Draw();
            yield return(null);
        }
    }