BlockType.cpp 4.0 KB

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