TreeSeblingBlock.cpp 5.0 KB

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