ChunkSystem.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace KairoEngine.Chunks
  6. {
  7. [Serializable]
  8. public class ChunkSystem<TChunkBlock>
  9. {
  10. public Vector3Int chunkSize = new Vector3Int(16, 16, 16);
  11. public Vector3 voxelSize = new Vector3(1f, 1f, 1f);
  12. public Dictionary<Vector3Int, Chunk<TChunkBlock>> chunks;
  13. public List<Vector3> chunkList = new List<Vector3>();
  14. public ChunkSystem(Vector3Int chunkSize, Vector3 voxelSize, bool debug = false)
  15. {
  16. this.chunkSize = chunkSize;
  17. this.voxelSize = voxelSize;
  18. this.chunks = new Dictionary<Vector3Int, Chunk<TChunkBlock>>();
  19. }
  20. public void CreateChunk(Vector3 globalPosition, Vector3Int position, Func<Chunk<TChunkBlock>, Vector3Int, TChunkBlock> createChunkBlock)
  21. {
  22. Chunk<TChunkBlock> chunk = new Chunk<TChunkBlock>(chunkSize, globalPosition, position, createChunkBlock);
  23. chunks.Add(position, chunk);
  24. chunkList.Add(globalPosition);
  25. }
  26. public void DestroyChunk(Vector3 globalPosition)
  27. {
  28. Vector3Int chunkPos = GetChunkPositionFromGlobalPosition(globalPosition);
  29. chunks.Remove(chunkPos);
  30. chunkList.Remove(globalPosition);
  31. }
  32. public Vector3Int GetVoxelFromGlobalPosition(Vector3 position)
  33. {
  34. Vector3 voxelPos = new Vector3();
  35. Vector3 chunkPos = GetChunkGlobalPositionFromGlobalPosition(position);
  36. voxelPos = position - chunkPos;
  37. // voxelPos.x = position.x - (Mathf.Floor(position.x / chunkSize.x * voxelSize.x) * chunkSize.x * voxelSize.x);
  38. // voxelPos.y = position.y - (Mathf.Floor(position.y / chunkSize.y * voxelSize.y) * chunkSize.y * voxelSize.y);
  39. // voxelPos.z = position.z - (Mathf.Floor(position.z / chunkSize.z * voxelSize.z) * chunkSize.z * voxelSize.z);
  40. voxelPos = new Vector3(voxelPos.x / voxelSize.x, voxelPos.y / voxelSize.y, voxelPos.z / voxelSize.z);
  41. //voxelPos = new Vector3(voxelPos.x + 0.1f, voxelPos.y + 0.1f, voxelPos.z + 0.1f);
  42. Vector3Int pos = new Vector3Int(Mathf.FloorToInt(voxelPos.x), Mathf.FloorToInt(voxelPos.y), Mathf.FloorToInt(voxelPos.z));
  43. if(pos.x >= chunkSize.x || pos.y >= chunkSize.y || pos.z >= chunkSize.z)
  44. Debug.LogError($"Position is out of chunk bounds ({pos} | {position} | {chunkPos})");
  45. pos = new Vector3Int(Mathf.Clamp(pos.x, 0, chunkSize.x - 1), Mathf.Clamp(pos.y, 0, chunkSize.y - 1), Mathf.Clamp(pos.z, 0, chunkSize.z - 1));
  46. return pos;
  47. }
  48. public Vector3Int GetChunkPositionFromGlobalPosition(Vector3 position)
  49. {
  50. Vector3 pos = new Vector3(position.x / voxelSize.x, position.y / voxelSize.y, position.z / voxelSize.z);
  51. Vector3Int chunkPos = new Vector3Int();
  52. chunkPos.x = Mathf.FloorToInt(pos.x / chunkSize.x ) * chunkSize.x;
  53. chunkPos.y = Mathf.FloorToInt(pos.y / chunkSize.y ) * chunkSize.y;
  54. chunkPos.z = Mathf.FloorToInt(pos.z / chunkSize.z ) * chunkSize.z;
  55. return chunkPos;
  56. }
  57. public Vector3 GetChunkGlobalPositionFromGlobalPosition(Vector3 position)
  58. {
  59. Vector3Int chunkPos = GetChunkPositionFromGlobalPosition(position);
  60. Vector3 pos = new Vector3(chunkPos.x * voxelSize.x, chunkPos.y * voxelSize.y, chunkPos.z * voxelSize.z);
  61. return pos;
  62. }
  63. public TChunkBlock GetBlock(Vector3 position)
  64. {
  65. Chunk<TChunkBlock> chunk = GetChunk(position);
  66. if(!chunk.isInitialized()) return default(TChunkBlock);
  67. return chunk.GetBlock(GetVoxelFromGlobalPosition(position));
  68. }
  69. public void SetBlock(Vector3 position, TChunkBlock data)
  70. {
  71. Chunk<TChunkBlock> chunk = GetChunk(position);
  72. if(!chunk.isInitialized()) return;
  73. Vector3Int pos = GetVoxelFromGlobalPosition(position);
  74. if(pos.x >= chunkSize.x || pos.y >= chunkSize.y || pos.z >= chunkSize.z) return;
  75. chunk.SetBlock(pos, data);
  76. }
  77. public Chunk<TChunkBlock> GetChunk(Vector3 position)
  78. {
  79. Vector3Int chunkPos = GetChunkPositionFromGlobalPosition(position);
  80. Chunk<TChunkBlock> chunk;
  81. //if(!chunks.TryGetValue(chunkPos, out chunk)) Debug.LogWarning($"Chunk not found for position {chunkPos}");
  82. chunks.TryGetValue(chunkPos, out chunk);
  83. return chunk;
  84. }
  85. public bool ChunkExists(Vector3 position)
  86. {
  87. Vector3Int chunkPos = GetChunkPositionFromGlobalPosition(position);
  88. Chunk<TChunkBlock> chunk;
  89. bool value = chunks.TryGetValue(chunkPos, out chunk);
  90. return value;
  91. }
  92. }
  93. }