public void SetBlock(Vector3Int worldPosition, IBlock block) { Vector3Int chunkIndex = WorldChunksData.WorldPostionToChunkIndex(worldPosition); Vector3Int chunkIndexRUF = WorldChunksData.WorldPostionToChunkIndex(worldPosition + Vector3Int.one); Vector3Int chunkIndexLDB = WorldChunksData.WorldPostionToChunkIndex(worldPosition - Vector3Int.one); Dictionary <Vector3Int, ChunkLoadType> chunksToRefresh = new Dictionary <Vector3Int, ChunkLoadType>() { { chunkIndex, ChunkLoadType.dynamicEnabled } }; if (chunkIndex.x < chunkIndexRUF.x) { RefreshChunkIfNeighbourBlockNotEmpty(new Vector3Int(1, 0, 0)); } if (chunkIndex.y < chunkIndexRUF.y) { RefreshChunkIfNeighbourBlockNotEmpty(new Vector3Int(0, 1, 0)); } if (chunkIndex.z < chunkIndexRUF.z) { RefreshChunkIfNeighbourBlockNotEmpty(new Vector3Int(0, 0, 1)); } if (chunkIndex.x > chunkIndexLDB.x) { RefreshChunkIfNeighbourBlockNotEmpty(new Vector3Int(-1, 0, 0)); } if (chunkIndex.y > chunkIndexLDB.y) { RefreshChunkIfNeighbourBlockNotEmpty(new Vector3Int(0, -1, 0)); } if (chunkIndex.z > chunkIndexLDB.z) { RefreshChunkIfNeighbourBlockNotEmpty(new Vector3Int(0, 0, -1)); } WorldChunksData.SetBlock(worldPosition, block); ActualizeDisplayedChunks(chunksToRefresh); void RefreshChunkIfNeighbourBlockNotEmpty(Vector3Int offet) { if (WorldChunksData.GetBlock(worldPosition + offet) != null) { chunksToRefresh.Add(chunkIndex + offet, ChunkLoadType.dynamicEnabled); } } }
public static void WorldChunksDataTest() { Debug.Log("Test of: " + nameof(WorldChunksData.WorldPostionToChunkIndex)); Debug.Log(WorldChunksData.WorldPostionToChunkIndex(new Vector3Int(-12, -11, -10)) == -Vector3Int.one); Debug.Log(WorldChunksData.WorldPostionToChunkIndex(new Vector3Int(0, 0, 0)) == Vector3Int.zero); Debug.Log(WorldChunksData.WorldPostionToChunkIndex(new Vector3Int(15, 12, 10)) == Vector3Int.zero); Debug.Log(WorldChunksData.WorldPostionToChunkIndex(new Vector3Int(15, 16, 10)) == Vector3Int.up); Debug.Log(WorldChunksData.WorldPostionToChunkIndex(new Vector3Int(15, -16, 10)) == Vector3Int.down); Debug.Log(WorldChunksData.WorldPostionToChunkIndex(new Vector3Int(15, -17, 10)) == Vector3Int.down * 2); Debug.Log("Test of: " + nameof(WorldChunksData.WorldToLocalPostion)); Vector3Int chunkIndex = new Vector3Int(-10, 15, 0); Vector3Int position = WorldChunksData.WorldToLocalPostion(new Vector3Int(15, -17, 10), ref chunkIndex); Debug.Log(chunkIndex == new Vector3Int(-10, 13, 0)); Debug.Log(position == new Vector3Int(15, 15, 10)); Debug.Log("Test of: " + nameof(WorldChunksData.SetBlock) + " & " + nameof(WorldChunksData.GetBlock)); WorldChunksData.SetBlock(Vector3Int.zero, null); WorldChunksData.SetBlock(Vector3Int.zero, new StoneBlock()); Debug.Log(WorldChunksData.GetBlock(Vector3Int.zero) is StoneBlock); }