Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "chunk.hpp"
- #include <ctime>
- #include <cstdlib>
- Chunk::Chunk ( int w, int h, int d, float block_size, std::vector <Block *> blocks, ISceneManager * smgr, int id ): ISceneNode(0,smgr,id)
- {
- if (w <= 0 || h <= 0 || d <= 0)
- throw "Chunk: tamanho de chunk invalido\n";
- if (smgr == 0)
- throw "Chunk: SceneManager é nulo!\n";
- this->block_size = block_size;
- width = w;
- height = h;
- depth = d;
- int i, j, k;
- tile = new int**[width];
- for (i = 0; i < width; i++)
- {
- tile[i] = new int*[height];
- for (j = 0; j < height; j++)
- {
- tile[i][j] = new int[depth];
- }
- }
- setParent(smgr->getRootSceneNode());
- vector3df p;
- srand(time(0));
- for (i = 0; i < width; i++)
- {
- for (j = 0; j < height; j++)
- {
- for (k = 0; k < depth; k++)
- {
- tile[i][j][k] = 0;
- if (rand() % 2)
- {
- tile[i][j][k] = 1;
- printf("Chunk: usando um bloco como 1\n");
- }
- }
- }
- }
- block = blocks;
- // NOTA não pode ser chilkd do chunk pois os blocos são compartilhados
- //for (size_t i = 0; i < block.size(); i++)
- // addChild(block[i]);
- setSurface();
- p = vector3df(0,0,0);
- p += vector3df(-block_size/2.0, -block_size/2.0, -block_size/2.0);
- box.MinEdge = p;
- p = vector3df((width - 1)*block_size, (height - 1)*block_size, (depth - 1)*block_size);
- p += vector3df(block_size/2.0, block_size/2.0, block_size/2.0);
- box.MaxEdge = p;
- }
- Chunk::~Chunk ( )
- {
- destroyTile();
- destroyBlock();
- }
- int Chunk::getWidth ( )
- {
- return width;
- }
- int Chunk::getHeight ( )
- {
- return height;
- }
- int Chunk::getDepth ( )
- {
- return depth;
- }
- /*
- void Chunk::addBlock ( int x, int y, int z, int type, int id )
- {
- }
- */
- std::vector <Block *> Chunk::getBlock ( )
- {
- return block;
- }
- void Chunk::destroyTile ( )
- {
- for (int i = 0; i < width; i++)
- {
- for (int j = 0; j < height; j++)
- {
- delete[] tile[i][j];
- }
- delete[] tile[i];
- }
- delete[] tile;
- tile = 0;
- }
- void Chunk::destroyBlock ( )
- {
- if (!block.size())
- return;
- // não se pode deletar os blocos aqui
- /*
- for (size_t i = 0; i < block.size(); i++)
- {
- delete block[i];
- }
- */
- block.clear();
- }
- void Chunk::setSurface ( )
- {
- }
- bool Chunk::resize ( int w, int h, int d, bool keep )
- {
- int i, j, k;
- if (w <= 0 || h <= 0 || d <= 0)
- return false;
- int ***p;
- p = new int**[w];
- for (i = 0; i < w; i++)
- {
- p[i] = new int*[h];
- for (j = 0; j < h; j++)
- {
- p[i][j] = new int[d];
- }
- }
- if (keep == false)
- {
- destroyTile();
- destroyBlock();
- return true;
- }
- int nw, nh, nd;
- if (width > w)
- nw = w;
- else
- nw = width;
- if (height > h)
- nh = h;
- else
- nh = height;
- if (depth > d)
- nd = d;
- else
- nd = depth;
- // copia conteudo de block para p
- for (i = 0; i < nw; i++)
- for (j = 0; j < nh; j++)
- for (k = 0; k < nd; k++)
- {
- p[i][j][k] = tile[i][j][k];
- }
- // limpa tile
- destroyTile();
- //guarda o ponteiro
- tile = p;
- p = 0;
- // redimensiona
- width = w;
- height = h;
- depth = d;
- return true;
- }
- void Chunk::OnRegisterSceneNode ( )
- {
- if (IsVisible)
- SceneManager->registerNodeForRendering(this);
- ISceneNode::OnRegisterSceneNode();
- }
- void Chunk::render ( )
- {
- int i, j, k;
- size_t l;
- vector3df pos;
- vector3df posBase = getAbsolutePosition();
- for (i = 0; i < width; i++)
- for (j = 0; j < height; j++)
- for (k = 0; k < depth; k++)
- {
- if (tile[i][j][k] == 0)
- continue;
- //printf("PRE-Renderizando\n");
- pos.X = i * block_size + posBase.X;
- pos.Y = j * block_size + posBase.Y;
- pos.Z = k * block_size + posBase.Z;
- for (l = 0; l < block.size(); l++)
- if (block[l]->getType() == tile[i][j][k])
- {
- block[l]->setPosition(pos);
- block[l]->render();
- }
- }
- }
- const aabbox3d<f32> & Chunk::getBoundingBox ( ) const
- {
- return box;
- }
Advertisement
Add Comment
Please, Sign In to add comment