Chunk.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace KairoEngine.Chunks
  6. {
  7. [System.Serializable]
  8. public struct Chunk<TChunkBlock>
  9. {
  10. private bool initialized;
  11. public bool isInitialized() => initialized;
  12. private Vector3Int size;
  13. public Vector3Int position { get; private set; }
  14. public TChunkBlock[ , , ] blocks;
  15. public Chunk(Vector3Int size, Vector3Int position, Func<Chunk<TChunkBlock>, Vector3Int, TChunkBlock> createChunkBlock,
  16. bool debug = false)
  17. {
  18. this.size = size;
  19. this.position = position;
  20. this.blocks = new TChunkBlock[size.x, size.y, size.z];
  21. this.initialized = true;
  22. for (int x = 0; x < size.x; x++)
  23. {
  24. for (int y = 0; y < size.y; y++)
  25. {
  26. for (int z = 0; z < size.z; z++)
  27. {
  28. blocks[x, y, z] = createChunkBlock(this, new Vector3Int(x, y, z));
  29. }
  30. }
  31. }
  32. }
  33. public TChunkBlock GetBlock(Vector3Int pos)
  34. {
  35. if(pos.x > size.x || pos.x < 0) return default(TChunkBlock);
  36. if(pos.y > size.y || pos.y < 0) return default(TChunkBlock);
  37. if(pos.z > size.z || pos.z < 0) return default(TChunkBlock);
  38. return blocks[pos.x, pos.y, pos.z];
  39. }
  40. public void SetBlock(Vector3Int pos, TChunkBlock data)
  41. {
  42. if(pos.x > size.x || pos.x < 0) return;
  43. if(pos.y > size.y || pos.y < 0) return;
  44. if(pos.z > size.z || pos.z < 0) return;
  45. blocks[pos.x, pos.y, pos.z] = data;
  46. }
  47. }
  48. }