Chunk.cpp 1.7 KB

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