GrowingPlant.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include "GrowingPlant.h"
  2. #include "Game.h"
  3. GrowthState::GrowthState(float percentage, ModelInfo model)
  4. : percentage(percentage),
  5. model(model)
  6. {}
  7. GrowthState::GrowthState()
  8. : percentage(1),
  9. model("", "", 0)
  10. {}
  11. GrowthState& GrowthState::operator=(const GrowthState& right)
  12. {
  13. percentage = right.percentage;
  14. model = ModelInfo(right.model);
  15. return *this;
  16. }
  17. GrowingPlantBlock::GrowingPlantBlock(int typeId,
  18. const ItemType* zTool,
  19. Framework::Vec3<int> pos,
  20. int maxTicks,
  21. const char* name,
  22. int blockTypeAfterGrowth)
  23. : Block(typeId, zTool, pos, 0),
  24. seblingTicks(0),
  25. seblingTicksMax(maxTicks),
  26. name(name),
  27. states(),
  28. blockTypeAfterGrowth(blockTypeAfterGrowth),
  29. plantSpawned(0),
  30. modelUpdated(0)
  31. {
  32. tickSource = 1;
  33. }
  34. bool GrowingPlantBlock::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
  35. {
  36. float beforePercentage = seblingTicks / (float)seblingTicksMax;
  37. seblingTicks += (float)numTicks;
  38. if ((int)(seblingTicks / (float)seblingTicksMax * 100.f)
  39. != (int)(beforePercentage * 100.f))
  40. {
  41. Game::INSTANCE->blockTargetChanged(this);
  42. }
  43. for (const GrowthState& state : states)
  44. {
  45. if ((state.percentage > beforePercentage || !modelUpdated)
  46. && state.percentage <= seblingTicks / (float)seblingTicksMax)
  47. {
  48. updateModel(state.model);
  49. modelUpdated = 1;
  50. }
  51. }
  52. return 1;
  53. }
  54. void GrowingPlantBlock::onPostTick()
  55. {
  56. if (seblingTicks >= (float)seblingTicksMax && !plantSpawned)
  57. {
  58. plantSpawned = 1;
  59. Game::INSTANCE->doLater([this]() {
  60. Game::INSTANCE->zDimension(getDimensionId())
  61. ->placeBlock(getPos(), blockTypeAfterGrowth);
  62. });
  63. }
  64. }
  65. Framework::Text GrowingPlantBlock::getTargetUIML()
  66. {
  67. return Text("<targetInfo><text width=\"auto\" height=\"auto\">") + name
  68. + "\n" + "Growth: "
  69. + Text((int)(seblingTicks / (float)seblingTicksMax * 100.f))
  70. + "%</text></targetInfo>";
  71. }
  72. GrowingPlantBlock* GrowingPlantBlock::addGrowthState(GrowthState state)
  73. {
  74. int index = 0;
  75. for (const GrowthState& s : states)
  76. {
  77. if (s.percentage > state.percentage)
  78. {
  79. states.add(state, index);
  80. return this;
  81. }
  82. index++;
  83. }
  84. states.add(state);
  85. return this;
  86. }
  87. GrowingPlantlockType::GrowingPlantlockType(int typeId,
  88. ModelInfo model,
  89. const char* name,
  90. int blockTypeAfterGrowth,
  91. const char* readableName,
  92. int ticksNeeded)
  93. : BlockType(typeId, 0, model, 1, 10.f, 0, name),
  94. transparent(1),
  95. passable(1),
  96. hardness(0.1f),
  97. zTool(0),
  98. speedModifier(0.3f),
  99. interactable(1),
  100. states(),
  101. blockTypeAfterGrowth(blockTypeAfterGrowth),
  102. readableName(readableName),
  103. ticksNeeded(ticksNeeded)
  104. {}
  105. GrowingPlantlockType* GrowingPlantlockType::setHardness(float hardness)
  106. {
  107. this->hardness = hardness;
  108. return this;
  109. }
  110. GrowingPlantlockType* GrowingPlantlockType::addGrowthState(
  111. float growthPercentage, ModelInfo model)
  112. {
  113. states.add(GrowthState(growthPercentage, model));
  114. return this;
  115. }
  116. void GrowingPlantlockType::createSuperBlock(Block* zBlock, Item* zItem) const
  117. {
  118. GrowingPlantlockType* block = dynamic_cast<GrowingPlantlockType*>(zBlock);
  119. if (!block)
  120. throw "TreeSeblingBlockType::createSuperBlock was called with a block "
  121. "witch is not an instance of TreeSeblingBlock";
  122. block->transparent = transparent;
  123. block->passable = passable;
  124. block->hardness = hardness;
  125. block->zTool = zTool;
  126. block->speedModifier = speedModifier;
  127. block->interactable = interactable;
  128. BlockType::createSuperBlock(zBlock, zItem);
  129. }
  130. void GrowingPlantlockType::loadSuperBlock(
  131. Block* zBlock, Framework::StreamReader* zReader, int dimensionId) const
  132. {
  133. BlockType::loadSuperBlock(zBlock, zReader, dimensionId);
  134. GrowingPlantBlock* block = dynamic_cast<GrowingPlantBlock*>(zBlock);
  135. zReader->lese((char*)&block->seblingTicks, 4);
  136. }
  137. void GrowingPlantlockType::saveSuperBlock(
  138. Block* zBlock, Framework::StreamWriter* zWriter) const
  139. {
  140. BlockType::saveSuperBlock(zBlock, zWriter);
  141. GrowingPlantBlock* block = dynamic_cast<GrowingPlantBlock*>(zBlock);
  142. zWriter->schreibe((char*)&block->seblingTicks, 4);
  143. }
  144. Item* GrowingPlantlockType::createItem() const
  145. {
  146. return 0;
  147. }
  148. Block* GrowingPlantlockType::createBlock(Framework::Vec3<int> position) const
  149. {
  150. GrowingPlantBlock* block = new GrowingPlantBlock(getId(),
  151. zTool,
  152. position,
  153. ticksNeeded,
  154. readableName,
  155. blockTypeAfterGrowth);
  156. for (const GrowthState& state : states)
  157. {
  158. block->addGrowthState(state);
  159. }
  160. return block;
  161. }