public IEnumerable <BlocksCollection> GetMergedBlocksFrom(Tilemap tilemap, Func <BlockData, BlockData, bool> mergeRule) { var blocks = TraverseBlocks(tilemap); HashSet <BlockData> visitedBlocks = new HashSet <BlockData>(); Queue <BlockData> blocksToVisit = new Queue <BlockData>(32); foreach (var startBlock in blocks) { var t = visitedBlocks; if (visitedBlocks.Contains(startBlock)) { continue; } var merged = new BlocksCollection(); blocksToVisit.Clear(); blocksToVisit.Enqueue(startBlock); while (blocksToVisit.Count > 0) { var block = blocksToVisit.Dequeue(); if (visitedBlocks.Contains(block)) { continue; } visitedBlocks.Add(block); merged.Set(block); GetNeighborsFrom(block, tilemap) .Where(next => next != BlockData.Null) .Where(next => !visitedBlocks.Contains(next)) .Where(next => mergeRule(block, next)) .ForEach(next => blocksToVisit.Enqueue(next)); } yield return(merged); } }
BlocksCollection MergeBlocks(BlockData startBlock, HashSet <BlockData> visitedBlocks) { var mergedBlocks = new BlocksCollection(); Queue <BlockData> blocksToVisit = new Queue <BlockData>(32); blocksToVisit.Enqueue(startBlock); while (blocksToVisit.Count > 0) { var x = blocksToVisit.Reverse().Take(100).ToArray(); var block = blocksToVisit.Dequeue(); if (visitedBlocks.Contains(block)) { continue; } visitedBlocks.Add(block); mergedBlocks.Set(block); GetNeighbors(block) .Where(next => next != BlockData.Null) .Where(next => !visitedBlocks.Contains(next)) .Where(next => next.BlockType == block.BlockType) .ForEach(next => blocksToVisit.Enqueue(next)); } return(mergedBlocks); }