BlockRegistry.cpp 819 B

12345678910111213141516171819202122232425262728293031
  1. #include "common/block/BlockRegistry.h"
  2. BlockRegistry::BlockRegistry() : registryToId("air", 0) {
  3. add("air", BlockBuilder());
  4. add("stone", BlockBuilder());
  5. add("dirt", BlockBuilder());
  6. add("grass", BlockBuilder());
  7. }
  8. void BlockRegistry::forEach(void(*f)(const Block&)) const {
  9. for(const Block& b : blocks) {
  10. f(b);
  11. }
  12. }
  13. void BlockRegistry::add(const char* registry, const BlockBuilder& builder) {
  14. u16 id = blocks.getLength();
  15. blocks.add(Block(id, registry, builder));
  16. registryToId.add(registry, id);
  17. }
  18. const Block& BlockRegistry::getBlock(const HashedString& registry) const {
  19. return blocks[registryToId.search(registry)];
  20. }
  21. const Block& BlockRegistry::getBlock(u16 id) const {
  22. if(id < blocks.getLength()) {
  23. return blocks[id];
  24. }
  25. return blocks[0];
  26. }