Ejemplo n.º 1
0
        protected override JobHandle OnUpdate(JobHandle inputDeps)
        {
            var boardChunks = this._boardGroup.CreateArchetypeChunkArray(Allocator.TempJob);

            for (var chunkIndex = 0; chunkIndex < boardChunks.Length; chunkIndex++)
            {
                var chunk         = boardChunks[chunkIndex];
                var boardEntities = chunk.GetNativeArray(this.GetArchetypeChunkEntityType());
                var boards        = chunk.GetNativeArray(this.GetArchetypeChunkComponentType <BoardComponent>());
                for (var i = 0; i < chunk.Count; i++)
                {
                    var board       = boards[i];
                    var boardEntity = boardEntities[i];
                    var random      = new Random(board.RandomSeed);

                    if (this.CurrentPhase == 0)
                    {
                        var roomCount = board.RoomCount;

                        this.Tiles          = new NativeArray <TileType>(board.Size.x * board.Size.y, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
                        this.RoomsQueue     = new NativeQueue <RoomComponent>(Allocator.TempJob);
                        this.CorridorsQueue = new NativeQueue <CorridorComponent>(Allocator.TempJob);
                        var firstCorridors = new NativeArray <CorridorComponent>(4, Allocator.TempJob);
                        for (var j = 0; j < this.Tiles.Length; j++)
                        {
                            this.Tiles[j] = TileType.Wall;
                        }

                        // setup fist room and corridors to all 4 directions
                        var firstRoom = CreateRoom(board, ref random);
                        this.RoomsQueue.Enqueue(firstRoom);
                        for (var c = 0; c < 4; c++)
                        {
                            firstCorridors[c] = this.CreateCorridor(firstRoom, board, ref random, c);
                            this.CorridorsQueue.Enqueue(firstCorridors[c]);
                        }

                        inputDeps = new CreateRoomsAndCorridorsJob
                        {
                            Board          = board,
                            Rooms          = this.RoomsQueue.AsParallelWriter(),
                            Corridors      = this.CorridorsQueue.AsParallelWriter(),
                            FirstCorridors = firstCorridors,
                            RoomCount      = roomCount / 4,
                            RandomSeed     = random.NextInt()
                        }.Schedule(4, 1, inputDeps);
                    }
                    else if (this.CurrentPhase == 1)
                    {
                        var roomsCount     = this.RoomsQueue.Count;
                        var corridorsCount = this.CorridorsQueue.Count;
                        var roomsArray     = new NativeArray <RoomComponent>(roomsCount, Allocator.TempJob);
                        var corridorsArray = new NativeArray <CorridorComponent>(corridorsCount, Allocator.TempJob);

                        var roomsQueueToArrayJobHandle = new RoomsQueueToArrayJob
                        {
                            RoomsQueue = this.RoomsQueue,
                            RoomsArray = roomsArray
                        }.Schedule(inputDeps);

                        var corridorsQueueToArrayJobHandle = new CorridorsQueueToArrayJob
                        {
                            CorridorsQueue = this.CorridorsQueue,
                            CorridorsArray = corridorsArray
                        }.Schedule(inputDeps);

                        var setRoomTilesJobHandle = new SetRoomTilesJob
                        {
                            Roms       = roomsArray,
                            Tiles      = Tiles,
                            TileStride = board.Size.x
                        }.Schedule(roomsCount, 1, roomsQueueToArrayJobHandle);

                        var setCorridorTilesJobHandle = new SetCorridorTilesJob
                        {
                            Corridors  = corridorsArray,
                            Tiles      = Tiles,
                            TileStride = board.Size.x
                        }.Schedule(corridorsCount, 1, corridorsQueueToArrayJobHandle);

                        var removeThinWallsJobHandle = new RemoveThinWallsJob
                        {
                            Board = board,
                            Tiles = Tiles
                        }.Schedule(JobHandle.CombineDependencies(setRoomTilesJobHandle, setCorridorTilesJobHandle));

                        inputDeps = new CloseBordersJob
                        {
                            Board = board,
                            Tiles = Tiles
                        }.Schedule(removeThinWallsJobHandle);
                    }
                    else if (this.CurrentPhase == 2)
                    {
                        this.CorridorsQueue.Dispose();
                        this.RoomsQueue.Dispose();

                        inputDeps = new TagBoardDoneJob
                        {
                            CommandBuffer  = this._endFrameBarrier.CreateCommandBuffer(),
                            BoardEntity    = boardEntity,
                            Tiles          = Tiles,
                            BoardComponent = board,
                            TileStride     = board.Size.x,
                            RandomSeed     = random.NextInt()
                        }.Schedule(inputDeps);
                        this._endFrameBarrier.AddJobHandleForProducer(inputDeps);
                    }
                }
            }
            this.CurrentPhase = (this.CurrentPhase + 1) % 3;
            var cleanUpJobHandle = new CleanUpJob
            {
                Chunks = boardChunks
            }.Schedule(inputDeps);

            return(cleanUpJobHandle);
        }
Ejemplo n.º 2
0
        protected override JobHandle OnUpdate(JobHandle inputDeps)
        {
            var boardChunks = this._boardGroup.CreateArchetypeChunkArray(Allocator.TempJob);

            for (var chunkIndex = 0; chunkIndex < boardChunks.Length; chunkIndex++)
            {
                var chunk         = boardChunks[chunkIndex];
                var boardEntities = chunk.GetNativeArray(this.GetArchetypeChunkEntityType());
                var boards        = chunk.GetNativeArray(this.GetArchetypeChunkComponentType <BoardComponent>());
                for (var i = 0; i < chunk.Count; i++)
                {
                    var board       = boards[i];
                    var boardEntity = boardEntities[i];
                    var random      = new Random(board.RandomSeed);
                    if (this.CurrentPhase == 0)
                    {
                        this.Tiles = new NativeArray <TileType>(board.Size.x * board.Size.y, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
                        for (var j = 0; j < this.Tiles.Length; j++)
                        {
                            this.Tiles[j] = TileType.Wall;
                        }

                        var bspTree = new BSPTree(board.Size.x, board.Size.y, board.MinRoomSize, ref random);
                        this.TreeArray      = bspTree.ToNativeArray();
                        this.CorridorsQueue = new NativeQueue <CorridorComponent>(Allocator.TempJob);
                        var concurrentQueue = this.CorridorsQueue.AsParallelWriter();

                        inputDeps = new CreateRoomsJob
                        {
                            TreeArray  = TreeArray,
                            RandomSeed = random.NextInt(),
                            Board      = board
                        }.Schedule(this.TreeArray.Length, 1, inputDeps);

                        for (var level = bspTree.Height - 1; level >= 0; level--)
                        {
                            var nodesAmount = (int)math.pow(2, level - 1);
                            inputDeps = new FindCorridorsForLevelJob
                            {
                                TreeArray  = TreeArray,
                                RandomSeed = random.NextInt(),
                                StartIndex = nodesAmount - 1,
                                Corridors  = concurrentQueue,
                            }.Schedule(nodesAmount, 1, inputDeps);
                        }
                    }
                    else if (this.CurrentPhase == 1)
                    {
                        var corridorsArray = new NativeArray <CorridorComponent>(this.CorridorsQueue.Count, Allocator.TempJob);

                        var corridorsQueueToArrayJobHandle = new CorridorsQueueToArrayJob
                        {
                            CorridorsArray = corridorsArray,
                            CorridorsQueue = this.CorridorsQueue
                        }.Schedule(inputDeps);

                        var createCorridorsJobHandle = new CreateCorridorsJob
                        {
                            Tiles      = Tiles,
                            Corridors  = corridorsArray,
                            TileStride = board.Size.x
                        }.Schedule(corridorsArray.Length, 1, corridorsQueueToArrayJobHandle);

                        var setRoomTilesJobHandle = new SetRoomTilesJob
                        {
                            TreeArray  = TreeArray,
                            Tiles      = Tiles,
                            TileStride = board.Size.x
                        }.Schedule(this.TreeArray.Length, 1, inputDeps);

                        inputDeps = new RemoveThinWallsJob
                        {
                            Board = board,
                            Tiles = Tiles
                        }.Schedule(JobHandle.CombineDependencies(setRoomTilesJobHandle, createCorridorsJobHandle));
                    }
                    else if (this.CurrentPhase == 2)
                    {
                        this.CorridorsQueue.Dispose();

                        inputDeps = new TagBoardDoneJob
                        {
                            CommandBuffer  = this._endFrameBarrier.CreateCommandBuffer(),
                            BoardEntity    = boardEntity,
                            BoardComponent = board,
                            TreeArray      = TreeArray,
                            Tiles          = Tiles,
                            RandomSeed     = random.NextInt()
                        }.Schedule(inputDeps);
                        this._endFrameBarrier.AddJobHandleForProducer(inputDeps);
                    }
                }
            }

            this.CurrentPhase = (this.CurrentPhase + 1) % 3;
            var cleanUpJobHandle = new CleanUpJob
            {
                Chunks = boardChunks
            }.Schedule(inputDeps);

            return(cleanUpJobHandle);
        }