Chunk.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include <cstdlib>
  2. #include <cmath>
  3. #include <iostream>
  4. #include "common/world/Chunk.h"
  5. #include "common/block/Blocks.h"
  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 - 1;
  22. }
  23. for(int y = 0; y < maxY - 3; y++)
  24. {
  25. setBlock(x, y, z, Blocks::STONE);
  26. }
  27. for(int y = maxY - 3; y < maxY - 1; y++)
  28. {
  29. setBlock(x, y, z, Blocks::DIRT);
  30. }
  31. setBlock(x, maxY - 1, z, Blocks::GRASS);
  32. for(int y = maxY; y < HEIGHT; y++)
  33. {
  34. setBlock(x, y, z, Blocks::AIR);
  35. }
  36. }
  37. }
  38. }
  39. Chunk::~Chunk()
  40. {
  41. }
  42. void Chunk::setBlock(int x, int y, int z, const Block& block)
  43. {
  44. blocks[y & BITMASK_HEIGHT][x & BITMASK_WIDTH][z & BITMASK_DEPTH] = block.getId();
  45. }
  46. const Block& Chunk::getBlock(int x, int y, int z)
  47. {
  48. return BlockRegistry::getBlockFromId(blocks[y & BITMASK_HEIGHT][x & BITMASK_WIDTH][z & BITMASK_DEPTH]);
  49. }
  50. int Chunk::getChunkX() const
  51. {
  52. return chunkX;
  53. }
  54. int Chunk::getChunkZ() const
  55. {
  56. return chunkZ;
  57. }
  58. bool Chunk::isDirty() const
  59. {
  60. for(int i = 0; i < HEIGHT_PARTIONS; i++)
  61. {
  62. if(dirty[i])
  63. {
  64. return true;
  65. }
  66. }
  67. return false;
  68. }
  69. bool Chunk::isDirty(int index) const
  70. {
  71. if(index >= 0 && index < HEIGHT_PARTIONS)
  72. {
  73. return dirty[index];
  74. }
  75. return false;
  76. }
  77. void Chunk::clearDirtyFlag(int index)
  78. {
  79. if(index >= 0 && index < HEIGHT_PARTIONS)
  80. {
  81. dirty[index] = false;
  82. }
  83. }