Blocks.cpp 783 B

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