GrowingPlant.cpp 5.2 KB

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