Chunk.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. //maxY = 10;
  16. if(maxY > HEIGHT)
  17. {
  18. maxY = HEIGHT;
  19. }
  20. for(int y = 0; y < maxY; y++)
  21. {
  22. blocks[y][x][z] = 1;
  23. }
  24. for(int y = maxY; y < HEIGHT; y++)
  25. {
  26. blocks[y][x][z] = 0;
  27. }
  28. }
  29. }
  30. }
  31. Chunk::~Chunk()
  32. {
  33. }
  34. void Chunk::setBlock(int x, int y, int z, unsigned short block)
  35. {
  36. blocks[y & BITMASK_HEIGHT][x & BITMASK_WIDTH][z & BITMASK_DEPTH] = block;
  37. }
  38. unsigned short Chunk::getBlock(int x, int y, int z)
  39. {
  40. return blocks[y & BITMASK_HEIGHT][x & BITMASK_WIDTH][z & BITMASK_DEPTH];
  41. }
  42. int Chunk::getChunkX() const
  43. {
  44. return chunkX;
  45. }
  46. int Chunk::getChunkZ() const
  47. {
  48. return chunkZ;
  49. }
  50. bool Chunk::isDirty() const
  51. {
  52. for(int i = 0; i < HEIGHT_PARTIONS; i++)
  53. {
  54. if(dirty[i])
  55. {
  56. return true;
  57. }
  58. }
  59. return false;
  60. }
  61. bool Chunk::isDirty(int index) const
  62. {
  63. if(index >= 0 && index < HEIGHT_PARTIONS)
  64. {
  65. return dirty[index];
  66. }
  67. return false;
  68. }
  69. void Chunk::clearDirtyFlag(int index)
  70. {
  71. if(index >= 0 && index < HEIGHT_PARTIONS)
  72. {
  73. dirty[index] = false;
  74. }
  75. }