BlockType.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "BlockType.h"
  2. #include "ItemType.h"
  3. #include "BasicBlocks.h"
  4. #include "Block.h"
  5. using namespace Framework;
  6. BlockType::BlockType(int id, Block* defaultBlock,
  7. ModelInfo model, bool needsClientInstance, int initialMaxHP, bool lightSource)
  8. : ReferenceCounter(),
  9. id(id),
  10. model(model),
  11. initialMaxHP(initialMaxHP),
  12. needsClientInstance(needsClientInstance),
  13. lightSource(lightSource),
  14. defaultBlock(defaultBlock)
  15. {
  16. StaticRegistry<BlockType>::INSTANCE.registerT(this, id);
  17. }
  18. BlockType::~BlockType()
  19. {
  20. if (defaultBlock)
  21. defaultBlock->release();
  22. }
  23. void BlockType::loadSuperBlock(Block* zBlock, Framework::StreamReader* zReader) const
  24. {
  25. zBlock->loadInventory(zReader);
  26. zReader->lese((char*)&zBlock->transparent, 1);
  27. zReader->lese((char*)&zBlock->passable, 1);
  28. zReader->lese((char*)&zBlock->hp, 4);
  29. zReader->lese((char*)&zBlock->maxHP, 4);
  30. zReader->lese((char*)&zBlock->hardness, 4);
  31. zReader->lese((char*)&zBlock->speedModifier, 4);
  32. int effectiveToolId;
  33. zReader->lese((char*)&effectiveToolId, 4);
  34. if (effectiveToolId >= 0)
  35. zBlock->zTool = StaticRegistry<ItemType>::INSTANCE.zElement(effectiveToolId);
  36. else
  37. zBlock->zTool = 0;
  38. zReader->lese((char*)&zBlock->interactable, 1);
  39. }
  40. void BlockType::saveSuperBlock(Block* zBlock, Framework::StreamWriter* zWriter) const
  41. {
  42. zBlock->saveInventory(zWriter);
  43. zWriter->schreibe((char*)&zBlock->transparent, 1);
  44. zWriter->schreibe((char*)&zBlock->passable, 1);
  45. zWriter->schreibe((char*)&zBlock->hp, 4);
  46. zWriter->schreibe((char*)&zBlock->maxHP, 4);
  47. zWriter->schreibe((char*)&zBlock->hardness, 4);
  48. zWriter->schreibe((char*)&zBlock->speedModifier, 4);
  49. int effectiveToolId = zBlock->zTool ? zBlock->zTool->getId() : -1;
  50. zWriter->schreibe((char*)&effectiveToolId, 4);
  51. zWriter->schreibe((char*)&zBlock->interactable, 1);
  52. }
  53. void BlockType::createSuperBlock(Block* zBlock, Item* zItem) const
  54. {
  55. if (zItem)
  56. {
  57. BasicBlockItem* item = dynamic_cast<BasicBlockItem*>(zItem);
  58. if (!item)
  59. {
  60. throw "BlockType::createSuperBlock was called with an item witch was not an instance of BasicBlockItem";
  61. }
  62. zBlock->transparent = item->transparent;
  63. zBlock->passable = item->passable;
  64. zBlock->hardness = item->hardness;
  65. zBlock->speedModifier = item->speedModifier;
  66. zBlock->zTool = item->toolId >= 0 ? StaticRegistry<ItemType>::INSTANCE.zElement(item->toolId) : 0;
  67. zBlock->interactable = item->interactable;
  68. }
  69. }
  70. void BlockType::createSuperItem(Block* zBlock, Item* zItem) const
  71. {
  72. BasicBlockItem* item = dynamic_cast<BasicBlockItem*>(zItem);
  73. if (!item)
  74. {
  75. throw "BlockType::createSuperItem was called with an item witch was not an instance of BasicBlockItem";
  76. }
  77. item->transparent = zBlock->transparent;
  78. item->passable = zBlock->passable;
  79. item->hardness = zBlock->hardness;
  80. item->speedModifier = zBlock->speedModifier;
  81. item->toolId = zBlock->zTool ? zBlock->zTool->getId() : -1;
  82. item->interactable = zBlock->interactable;
  83. }
  84. Block* BlockType::loadBlock(Framework::Vec3<int> position, Framework::StreamReader* zReader) const
  85. {
  86. Block* result = createBlock(position);
  87. loadSuperBlock(result, zReader);
  88. return result;
  89. }
  90. void BlockType::saveBlock(Block* zBlock, Framework::StreamWriter* zWriter) const
  91. {
  92. saveSuperBlock(zBlock, zWriter);
  93. }
  94. Item* BlockType::getItemFromBlock(Block* zBlock) const
  95. {
  96. Item* result = createItem();
  97. createSuperItem(zBlock, result);
  98. return result;
  99. }
  100. Block* BlockType::createBlockAt(Framework::Vec3<int> position, Item* zUsedItem) const
  101. {
  102. Block* result = createBlock(position);
  103. createSuperBlock(result, zUsedItem);
  104. return result;
  105. }
  106. bool BlockType::doesNeedClientInstance() const
  107. {
  108. return needsClientInstance;
  109. }
  110. const ModelInfo& BlockType::getModel() const
  111. {
  112. return model;
  113. }
  114. int BlockType::getId() const
  115. {
  116. return id;
  117. }
  118. const Block* BlockType::zDefault() const
  119. {
  120. return defaultBlock;
  121. }
  122. int BlockType::getInitialMaxHP() const
  123. {
  124. return initialMaxHP;
  125. }
  126. bool BlockType::isLightSource() const
  127. {
  128. return lightSource;
  129. }
  130. const Block* getDefaultBlock(Either<Block*, int> b)
  131. {
  132. if (b.isA())
  133. return b;
  134. else
  135. return StaticRegistry<BlockType>::INSTANCE.zElement(b)->zDefault();
  136. }