public void TryUnite(ChunkWithGeometry child) { if (_needToUniteIntoParent.Add(child) && _needToUniteIntoParent.Count == 8) { Unite(); } }
protected void DestroyIfCan(ChunkWithGeometry chunk) { for (int i = 0; i < 27; i++) { if (i == 13) { continue; } int x = (i % 3) - 1; int y = ((i - x) / 3) % 3 - 1; int z = ((i - y) / 9) % 3 - 1; Vector3Int newPosition = chunk.Position + new Vector3Int(x, y, z) * Chunk.HierarchySize2WorldSize(HierarchySize); if (_children.TryGetValue(newPosition, out Chunk neighbourChunk) && neighbourChunk is ChunkWithChunks) { return; } } _children.Remove(chunk.Position); chunk.Hide(); if (!chunk.IsInitialized) { needToBeDestroyed.Add(chunk); return; } chunk.Destroy(); }
public void StartInitializing() { _children = new Dictionary <Vector3Int, Chunk>(); needToBeDestroyed = new List <ChunkWithGeometry>(); var chunk = new ChunkWithGeometry(Position, HierarchySize, this, ChunkTracker, GeometryManager); _children.Add(Position, chunk); chunk.Initialized += chunk.Instantiate; chunk.StartInitializing(); }
protected void TryDestroyThatNeedsToBeDestroyed() { Queue <ChunkWithGeometry> destroying = new Queue <ChunkWithGeometry>(); foreach (ChunkWithGeometry chunk in needToBeDestroyed) { if (chunk.IsInitialized) { destroying.Enqueue(chunk); } } for (int i = 0, size = destroying.Count; i < size; i++) { ChunkWithGeometry destroingChunk = destroying.Dequeue(); needToBeDestroyed.Remove(destroingChunk); destroingChunk.Destroy(); } }
protected void Unite() { foreach (ChunkWithGeometry chunk in _needToUniteIntoParent) { chunk.Hide(); } var newChunk = new ChunkWithGeometry(this); newChunk.Initialized += () => { foreach (ChunkWithGeometry chunk in _needToUniteIntoParent) { chunk.Destroy(); } IsInitialized = false; Parent.ReplaceChild(this, newChunk); newChunk.Instantiate(); }; newChunk.StartInitializing(); }
protected void OnChildUnited(ChunkWithGeometry chunk) { TryDestroyThatNeedsToBeDestroyed(); for (int i = 0; i < 27; i++) { if (i == 13) { continue; } int x = (i % 3) - 1; int y = ((i - x) / 3) % 3 - 1; int z = ((i - y) / 9) % 3 - 1; Vector3Int newPosition = chunk.Position + new Vector3Int(x, y, z) * Chunk.HierarchySize2WorldSize(HierarchySize); if (_children.TryGetValue(newPosition, out Chunk neighbourChunk) && neighbourChunk is ChunkWithGeometry geometryChunk) { DestroyIfCan(geometryChunk); } } DestroyIfCan(chunk); }
protected void OnChildDivided(ChunkWithChunks chunk) { TryDestroyThatNeedsToBeDestroyed(); for (int i = 0; i < 27; i++) { if (i == 13) { continue; } int x = (i % 3) - 1; int y = ((i - x) / 3) % 3 - 1; int z = ((i - y) / 9) % 3 - 1; Vector3Int newPosition = chunk.Position + new Vector3Int(x, y, z) * Chunk.HierarchySize2WorldSize(HierarchySize); if (!_children.ContainsKey(newPosition)) { var newChunk = new ChunkWithGeometry(newPosition, HierarchySize, this, ChunkTracker, GeometryManager); _children.Add(newPosition, newChunk); newChunk.Initialized += newChunk.Instantiate; newChunk.StartInitializing(); } } }
public void TryUnUnite(ChunkWithGeometry child) { }
public void TryUnUnite(ChunkWithGeometry child) => _needToUniteIntoParent.Remove(child);