BlockType.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. BasicBlockItem* item = dynamic_cast<BasicBlockItem*>(zItem);
  56. if (!item)
  57. {
  58. throw "BlockType::createSuperBlock was called with an item witch was not an instance of BasicBlockItem";
  59. }
  60. zBlock->transparent = item->transparent;
  61. zBlock->passable = item->passable;
  62. zBlock->hp = item->hp;
  63. zBlock->maxHP = item->maxHP;
  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. void BlockType::createSuperItem(Block* zBlock, Item* zItem) const
  70. {
  71. BasicBlockItem* item = dynamic_cast<BasicBlockItem*>(zItem);
  72. if (!item)
  73. {
  74. throw "BlockType::createSuperItem was called with an item witch was not an instance of BasicBlockItem";
  75. }
  76. item->transparent = zBlock->transparent;
  77. item->passable = zBlock->passable;
  78. item->hp = zBlock->maxHP; // reset hp
  79. item->maxHP = zBlock->maxHP;
  80. item->hardness = zBlock->hardness;
  81. item->speedModifier = zBlock->speedModifier;
  82. item->toolId = zBlock->zTool ? zBlock->zTool->getId() : -1;
  83. item->interactable = zBlock->interactable;
  84. }
  85. Block* BlockType::loadBlock(Framework::Vec3<int> position, Framework::StreamReader* zReader) const
  86. {
  87. Block* result = createBlock(position);
  88. loadSuperBlock(result, zReader);
  89. return result;
  90. }
  91. void BlockType::saveBlock(Block* zBlock, Framework::StreamWriter* zWriter) const
  92. {
  93. saveSuperBlock(zBlock, zWriter);
  94. }
  95. Item* BlockType::getItemFromBlock(Block* zBlock) const
  96. {
  97. Item* result = createItem();
  98. createSuperItem(zBlock, result);
  99. return result;
  100. }
  101. Block* BlockType::createBlockAt(Framework::Vec3<int> position, Item* zUsedItem) const
  102. {
  103. Block* result = createBlock(position);
  104. createSuperBlock(result, zUsedItem);
  105. return result;
  106. }
  107. bool BlockType::doesNeedClientInstance() const
  108. {
  109. return needsClientInstance;
  110. }
  111. const ModelInfo& BlockType::getModel() const
  112. {
  113. return model;
  114. }
  115. int BlockType::getId() const
  116. {
  117. return id;
  118. }
  119. const Block* BlockType::zDefault() const
  120. {
  121. return defaultBlock;
  122. }
  123. int BlockType::getInitialMaxHP() const
  124. {
  125. return initialMaxHP;
  126. }
  127. bool BlockType::isLightSource() const
  128. {
  129. return lightSource;
  130. }
  131. const Block* getDefaultBlock(Either<Block*, int> b)
  132. {
  133. if (b.isA())
  134. return b;
  135. else
  136. return StaticRegistry<BlockType>::INSTANCE.zElement(b)->zDefault();
  137. }