Blocks.cpp 916 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include "common/block/Blocks.h"
  2. const int BlockRegistry::BLOCK_AMOUNT = 4;
  3. const Block* BlockRegistry::BLOCKS[BLOCK_AMOUNT];
  4. const BlockAir Blocks::AIR;
  5. const Block Blocks::GRASS;
  6. const Block Blocks::DIRT;
  7. const Block Blocks::STONE;
  8. BlockRegistry::BlockRegistry()
  9. {
  10. }
  11. void BlockRegistry::registerBlock(const Block& block)
  12. {
  13. BlockId id = block.getId();
  14. if(id >= 0 && id < BLOCK_AMOUNT && BLOCKS[id] == nullptr)
  15. {
  16. BLOCKS[id] = &block;
  17. }
  18. }
  19. const Block& BlockRegistry::getBlockFromId(BlockId id)
  20. {
  21. return *BLOCKS[(id >= 0 && id < BLOCK_AMOUNT && BLOCKS[id] != nullptr) * id];
  22. }
  23. void BlockRegistry::init()
  24. {
  25. for(int i = 0; i < BLOCK_AMOUNT; i++)
  26. {
  27. BLOCKS[i] = nullptr;
  28. }
  29. }
  30. void BlockRegistry::registerBlocks()
  31. {
  32. init();
  33. registerBlock(Blocks::AIR);
  34. registerBlock(Blocks::GRASS);
  35. registerBlock(Blocks::DIRT);
  36. registerBlock(Blocks::STONE);
  37. }