GrowingPlant.cpp 5.3 KB

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