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