Chunk.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 Vector3 globalPosition { get; private set; }
  15. public TChunkBlock[ , , ] blocks;
  16. public Chunk(Vector3Int size, Vector3 globalPosition, Vector3Int position, Func<Chunk<TChunkBlock>, Vector3Int, TChunkBlock> createChunkBlock,
  17. bool debug = false)
  18. {
  19. this.size = size;
  20. this.position = position;
  21. this.globalPosition = globalPosition;
  22. this.blocks = new TChunkBlock[size.x, size.y, size.z];
  23. this.initialized = true;
  24. for (int x = 0; x < size.x; x++)
  25. {
  26. for (int y = 0; y < size.y; y++)
  27. {
  28. for (int z = 0; z < size.z; z++)
  29. {
  30. blocks[x, y, z] = createChunkBlock(this, new Vector3Int(x, y, z));
  31. }
  32. }
  33. }
  34. }
  35. public TChunkBlock GetBlock(Vector3Int pos)
  36. {
  37. if(pos.x > size.x || pos.x < 0) return default(TChunkBlock);
  38. if(pos.y > size.y || pos.y < 0) return default(TChunkBlock);
  39. if(pos.z > size.z || pos.z < 0) return default(TChunkBlock);
  40. return blocks[pos.x, pos.y, pos.z];
  41. }
  42. public void SetBlock(Vector3Int pos, TChunkBlock data)
  43. {
  44. if(pos.x > size.x || pos.x < 0) return;
  45. if(pos.y > size.y || pos.y < 0) return;
  46. if(pos.z > size.z || pos.z < 0) return;
  47. blocks[pos.x, pos.y, pos.z] = data;
  48. }
  49. }
  50. }