TreeSeblingBlock.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include "TreeSeblingBlock.h"
  2. #include "BasicBlocks.h"
  3. #include "Game.h"
  4. #include "NoBlock.h"
  5. #include "RandNoise.h"
  6. #include "TreeTemplate.h"
  7. TreeSeblingBlock::TreeSeblingBlock(int typeId,
  8. const ItemType* zTool,
  9. Framework::Vec3<int> pos,
  10. const BlockType* wood,
  11. const BlockType* leaves)
  12. : Block(typeId, zTool, pos, 0),
  13. seblingTicks(0),
  14. seblingTicksMax(10000),
  15. wood(wood),
  16. leaves(leaves)
  17. {
  18. tickSource = 1;
  19. }
  20. bool TreeSeblingBlock::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
  21. {
  22. seblingTicks += 1;
  23. return 0;
  24. }
  25. void TreeSeblingBlock::onPostTick()
  26. {
  27. if ((int)seblingTicks >= seblingTicksMax)
  28. {
  29. Game::INSTANCE->doLater([wood = wood,
  30. leaves = leaves,
  31. pos = getPos(),
  32. dim = getDimensionId()]() {
  33. // the tree sebling object will be deleted during this operation
  34. RandNoise noise((int)time(0));
  35. if (!Game::INSTANCE->zGenerator()->spawnStructure(pos,
  36. dim,
  37. [wood = wood, leaves = leaves](GenerationTemplate* tmpl) {
  38. TreeTemplate* tree = dynamic_cast<TreeTemplate*>(tmpl);
  39. return tree && tree->getWoodType() == wood
  40. && tree->getLeavesType() == leaves;
  41. }))
  42. {
  43. Game::INSTANCE->zDimension(dim)->placeBlock(
  44. pos, BlockTypeEnum::AIR);
  45. }
  46. });
  47. }
  48. }
  49. Framework::Text TreeSeblingBlock::getTargetUIML()
  50. {
  51. return Text("<targetInfo><text width=\"auto\" height=\"auto\">")
  52. + StaticRegistry<BlockType>::INSTANCE.zElement(typeId)->getName()
  53. + "\n" + "Growth: " + Text((int)(seblingTicks / seblingTicksMax * 100))
  54. + "%</text></targetInfo>";
  55. }
  56. TreeSeblingBlockType::TreeSeblingBlockType(int typeId,
  57. int itemTypeId,
  58. ModelInfo model,
  59. int woodType,
  60. int leavesType,
  61. const char* name)
  62. : BlockType(typeId, 0, model, 1, 10, 0, name),
  63. itemType(itemTypeId),
  64. transparent(true),
  65. passable(true),
  66. hardness(0.1f),
  67. zTool(0),
  68. speedModifier(0.5f),
  69. interactable(1),
  70. woodType(woodType),
  71. leavesType(leavesType)
  72. {}
  73. void TreeSeblingBlockType::createSuperBlock(Block* zBlock, Item* zItem) const
  74. {
  75. TreeSeblingBlock* block = dynamic_cast<TreeSeblingBlock*>(zBlock);
  76. if (!block)
  77. throw "TreeSeblingBlockType::createSuperBlock was called with a block "
  78. "witch is not an instance of TreeSeblingBlock";
  79. block->transparent = transparent;
  80. block->passable = passable;
  81. block->hp = (float)getInitialMaxHP();
  82. block->maxHP = (float)getInitialMaxHP();
  83. block->hardness = hardness;
  84. block->zTool = zTool;
  85. block->speedModifier = speedModifier;
  86. block->interactable = interactable;
  87. BlockType::createSuperBlock(zBlock, zItem);
  88. }
  89. void TreeSeblingBlockType::loadSuperBlock(
  90. Block* zBlock, Framework::StreamReader* zReader, int dimensionId) const
  91. {
  92. TreeSeblingBlock* block = dynamic_cast<TreeSeblingBlock*>(zBlock);
  93. if (!block)
  94. throw "TreeSeblingBlockType::loadSuperBlock was called with a block "
  95. "witch is not an instance of TreeSeblingBlock";
  96. zReader->lese((char*)&block->seblingTicks, 4);
  97. zReader->lese((char*)&block->seblingTicksMax, 4);
  98. int id;
  99. zReader->lese((char*)&id, 4);
  100. block->wood = StaticRegistry<BlockType>::INSTANCE.zElement(id);
  101. zReader->lese((char*)&id, 4);
  102. block->leaves = StaticRegistry<BlockType>::INSTANCE.zElement(id);
  103. BlockType::loadSuperBlock(zBlock, zReader, dimensionId);
  104. }
  105. void TreeSeblingBlockType::saveSuperBlock(
  106. Block* zBlock, Framework::StreamWriter* zWriter) const
  107. {
  108. TreeSeblingBlock* block = dynamic_cast<TreeSeblingBlock*>(zBlock);
  109. if (!block)
  110. throw "TreeSeblingBlockType::saveSuperBlock was called with a block "
  111. "witch is not an instance of TreeSeblingBlock";
  112. zWriter->schreibe((char*)&block->seblingTicks, 4);
  113. zWriter->schreibe((char*)&block->seblingTicksMax, 4);
  114. int id = block->wood->getId();
  115. zWriter->schreibe((char*)&id, 4);
  116. id = block->leaves->getId();
  117. zWriter->schreibe((char*)&id, 4);
  118. BlockType::saveSuperBlock(zBlock, zWriter);
  119. }
  120. Item* TreeSeblingBlockType::createItem() const
  121. {
  122. return StaticRegistry<ItemType>::INSTANCE.zElement(itemType)->createItem();
  123. }
  124. Block* TreeSeblingBlockType::createBlock(Framework::Vec3<int> position) const
  125. {
  126. return new TreeSeblingBlock(getId(),
  127. zTool,
  128. position,
  129. StaticRegistry<BlockType>::INSTANCE.zElement(woodType),
  130. StaticRegistry<BlockType>::INSTANCE.zElement(leavesType));
  131. }
  132. TreeSeblingBlockType* TreeSeblingBlockType::setHardness(float hardness)
  133. {
  134. this->hardness = hardness;
  135. return this;
  136. }