Guest User

Chunk.cpp

a guest
Dec 6th, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.89 KB | None | 0 0
  1. #include "chunk.hpp"
  2. #include <ctime>
  3. #include <cstdlib>
  4.  
  5. Chunk::Chunk ( int w, int h, int d, float block_size, std::vector <Block *> blocks,  ISceneManager * smgr, int id ): ISceneNode(0,smgr,id)
  6. {
  7.     if (w <= 0 || h <= 0 || d <= 0)
  8.         throw "Chunk: tamanho de chunk invalido\n";
  9.  
  10.     if (smgr == 0)
  11.         throw "Chunk: SceneManager é nulo!\n";
  12.  
  13.     this->block_size = block_size;
  14.  
  15.     width = w;
  16.     height = h;
  17.     depth = d;
  18.    
  19.    
  20.     int i, j, k;
  21.    
  22.     tile = new int**[width];
  23.     for (i = 0; i < width; i++)
  24.     {
  25.         tile[i] = new int*[height];
  26.         for (j = 0; j < height; j++)
  27.         {
  28.             tile[i][j] = new int[depth];
  29.         }
  30.     }
  31.  
  32.     setParent(smgr->getRootSceneNode());
  33.  
  34.     vector3df p;
  35.  
  36.     srand(time(0));
  37.     for (i = 0; i < width; i++)
  38.     {
  39.         for (j = 0; j < height; j++)
  40.         {
  41.             for (k = 0; k < depth; k++)
  42.             {
  43.                 tile[i][j][k] = 0;
  44.                 if (rand() % 2)
  45.                 {
  46.                     tile[i][j][k] = 1;
  47.                     printf("Chunk: usando um bloco como 1\n");
  48.                 }
  49.             }
  50.         }
  51.     }
  52.    
  53.     block = blocks;
  54.     // NOTA não pode ser chilkd do chunk pois os blocos são compartilhados
  55.     //for (size_t i = 0; i < block.size(); i++)
  56.     //  addChild(block[i]);
  57.    
  58.     setSurface();
  59.    
  60.     p = vector3df(0,0,0);
  61.     p += vector3df(-block_size/2.0, -block_size/2.0, -block_size/2.0);
  62.     box.MinEdge = p;
  63.    
  64.     p = vector3df((width - 1)*block_size, (height - 1)*block_size, (depth - 1)*block_size);
  65.     p += vector3df(block_size/2.0, block_size/2.0, block_size/2.0);
  66.     box.MaxEdge = p;
  67. }
  68.  
  69. Chunk::~Chunk (  )
  70. {
  71.     destroyTile();
  72.     destroyBlock();
  73. }
  74.  
  75.  
  76. int Chunk::getWidth (  )
  77. {
  78.     return width;
  79. }
  80.  
  81. int Chunk::getHeight (  )
  82. {
  83.     return height;
  84. }
  85.  
  86. int Chunk::getDepth (  )
  87. {
  88.     return depth;
  89. }
  90. /*
  91. void Chunk::addBlock ( int x, int y, int z, int type, int id )
  92. {
  93. }
  94. */
  95.  
  96. std::vector <Block *> Chunk::getBlock (  )
  97. {
  98.     return block;
  99. }
  100.  
  101. void Chunk::destroyTile (  )
  102. {
  103.     for (int i = 0; i < width; i++)
  104.     {
  105.         for (int j = 0; j < height; j++)
  106.         {
  107.             delete[] tile[i][j];
  108.         }
  109.        
  110.         delete[] tile[i];
  111.     }
  112.  
  113.     delete[] tile;
  114.    
  115.     tile = 0;
  116. }
  117.  
  118. void Chunk::destroyBlock (  )
  119. {
  120.     if (!block.size())
  121.         return;
  122.    
  123.     // não se pode deletar os blocos aqui
  124.     /*
  125.     for (size_t i = 0; i < block.size(); i++)
  126.     {
  127.         delete block[i];
  128.     }
  129.     */
  130.    
  131.     block.clear();
  132. }
  133.  
  134. void Chunk::setSurface (  )
  135. {
  136.    
  137. }
  138.  
  139. bool Chunk::resize ( int w, int h, int d, bool keep )
  140. {
  141.     int i, j, k;
  142.    
  143.     if (w <= 0 || h <= 0 || d <= 0)
  144.         return false;
  145.    
  146.     int ***p;
  147.    
  148.     p = new int**[w];
  149.     for (i = 0; i < w; i++)
  150.     {
  151.         p[i] = new int*[h];
  152.         for (j = 0; j < h; j++)
  153.         {
  154.             p[i][j] = new int[d];
  155.         }
  156.     }
  157.    
  158.     if (keep == false)
  159.     {
  160.         destroyTile();
  161.         destroyBlock();
  162.        
  163.         return true;
  164.     }
  165.    
  166.     int nw, nh, nd;
  167.    
  168.     if (width > w)
  169.         nw = w;
  170.     else
  171.         nw = width;
  172.    
  173.     if (height > h)
  174.         nh = h;
  175.     else
  176.         nh = height;
  177.        
  178.     if (depth > d)
  179.         nd = d;
  180.     else
  181.         nd = depth;
  182.    
  183.     // copia conteudo de block para p
  184.     for (i = 0; i < nw; i++)
  185.         for (j = 0; j < nh; j++)
  186.             for (k = 0; k < nd; k++)
  187.             {
  188.                 p[i][j][k] = tile[i][j][k];
  189.             }
  190.    
  191.     // limpa tile
  192.     destroyTile();
  193.     //guarda o ponteiro
  194.     tile = p;
  195.     p = 0;
  196.     // redimensiona
  197.     width = w;
  198.     height = h;
  199.     depth = d;
  200.    
  201.     return true;
  202. }
  203.  
  204. void Chunk::OnRegisterSceneNode (  )
  205. {
  206.     if (IsVisible)
  207.         SceneManager->registerNodeForRendering(this);
  208.  
  209.     ISceneNode::OnRegisterSceneNode();
  210. }
  211.  
  212. void Chunk::render (  )
  213. {
  214.     int i, j, k;
  215.     size_t l;
  216.     vector3df pos;
  217.     vector3df posBase = getAbsolutePosition();
  218.     for (i = 0; i < width; i++)
  219.         for (j = 0; j < height; j++)
  220.             for (k = 0; k < depth; k++)
  221.             {
  222.                 if (tile[i][j][k] == 0)
  223.                     continue;
  224.                 //printf("PRE-Renderizando\n");
  225.                 pos.X = i * block_size + posBase.X;
  226.                 pos.Y = j * block_size + posBase.Y;
  227.                 pos.Z = k * block_size + posBase.Z;
  228.                 for (l = 0; l < block.size(); l++)
  229.                     if (block[l]->getType() == tile[i][j][k])
  230.                     {
  231.                         block[l]->setPosition(pos);
  232.                         block[l]->render();
  233.                     }
  234.             }
  235. }
  236.  
  237. const aabbox3d<f32> & Chunk::getBoundingBox (  ) const
  238. {
  239.     return box;
  240. }
Advertisement
Add Comment
Please, Sign In to add comment