BlockType.cpp 3.9 KB

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