Chunk.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include "Chunk.h"
  2. #include <cstdlib>
  3. #include <cmath>
  4. Chunk::Chunk(int chunkX, int chunkZ) : chunkX(chunkX), chunkZ(chunkZ)
  5. {
  6. for(int i = 0; i < HEIGHT_PARTIONS; i++)
  7. {
  8. dirty[i] = true;
  9. }
  10. for(int z = 0; z < DEPTH; z++)
  11. {
  12. for(int x = 0; x < WIDTH; x++)
  13. {
  14. int maxY = (int) (sinf((x + chunkX * WIDTH) * 0.3) * 20 + 22) + (sinf((z + chunkZ * DEPTH) * 0.3) * 20 + 22);
  15. if(maxY > HEIGHT)
  16. {
  17. maxY = HEIGHT;
  18. }
  19. for(int y = 0; y < maxY; y++)
  20. {
  21. blocks[y][x][z] = 1;
  22. }
  23. for(int y = maxY; y < HEIGHT; y++)
  24. {
  25. blocks[y][x][z] = 0;
  26. }
  27. }
  28. }
  29. }
  30. Chunk::~Chunk()
  31. {
  32. }
  33. void Chunk::setBlock(int x, int y, int z, unsigned short block)
  34. {
  35. blocks[y & BITMASK_HEIGHT][x & BITMASK_WIDTH][z & BITMASK_DEPTH] = block;
  36. }
  37. unsigned short Chunk::getBlock(int x, int y, int z)
  38. {
  39. return blocks[y & BITMASK_HEIGHT][x & BITMASK_WIDTH][z & BITMASK_DEPTH];
  40. }
  41. int Chunk::getChunkX() const
  42. {
  43. return chunkX;
  44. }
  45. int Chunk::getChunkZ() const
  46. {
  47. return chunkZ;
  48. }
  49. bool Chunk::isDirty() const
  50. {
  51. for(int i = 0; i < HEIGHT_PARTIONS; i++)
  52. {
  53. if(dirty[i])
  54. {
  55. return true;
  56. }
  57. }
  58. return false;
  59. }
  60. bool Chunk::isDirty(int index) const
  61. {
  62. if(index >= 0 && index < HEIGHT_PARTIONS)
  63. {
  64. return dirty[index];
  65. }
  66. return false;
  67. }
  68. void Chunk::clearDirtyFlag(int index)
  69. {
  70. if(index >= 0 && index < HEIGHT_PARTIONS)
  71. {
  72. dirty[index] = false;
  73. }
  74. }