GrowingPlant.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. #include "GrowingPlant.h"
  2. #include "Dimension.h"
  3. #include "Game.h"
  4. GrowthState::GrowthState(float percentage, ModelInfo* model)
  5. : ReferenceCounter(),
  6. percentage(percentage),
  7. model(model)
  8. {}
  9. GrowthState::~GrowthState()
  10. {
  11. model->release();
  12. }
  13. float GrowthState::getPercentage() const
  14. {
  15. return percentage;
  16. }
  17. ModelInfo* GrowthState::zModel() const
  18. {
  19. return model;
  20. }
  21. GrowingPlantBlock::GrowingPlantBlock(int typeId,
  22. Framework::Vec3<int> pos,
  23. int dimensionId,
  24. int maxTicks,
  25. Framework::Text name,
  26. int blockTypeAfterGrowth)
  27. : Block(typeId, pos, dimensionId, 0),
  28. seblingTicks(0),
  29. seblingTicksMax(maxTicks),
  30. name(name),
  31. states(),
  32. blockTypeAfterGrowth(blockTypeAfterGrowth),
  33. plantSpawned(0),
  34. lastSendState(-1)
  35. {}
  36. bool GrowingPlantBlock::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
  37. {
  38. float beforePercentage = seblingTicks / (float)seblingTicksMax;
  39. seblingTicks += (float)numTicks;
  40. if ((int)(seblingTicks / (float)seblingTicksMax * 100.f)
  41. != (int)(beforePercentage * 100.f))
  42. {
  43. Game::INSTANCE->blockTargetChanged(this);
  44. }
  45. int index = 0;
  46. int currentIndex = 0;
  47. for (GrowthState* state : states)
  48. {
  49. if (state->getPercentage() <= seblingTicks / (float)seblingTicksMax)
  50. {
  51. currentIndex = index;
  52. }
  53. else
  54. {
  55. break;
  56. }
  57. index++;
  58. }
  59. if (lastSendState != currentIndex)
  60. {
  61. updateModel(states.z(currentIndex)->zModel());
  62. lastSendState = currentIndex;
  63. }
  64. return 1;
  65. }
  66. void GrowingPlantBlock::onPostTick()
  67. {
  68. if (seblingTicks >= (float)seblingTicksMax && !plantSpawned)
  69. {
  70. plantSpawned = 1;
  71. Game::INSTANCE->doLater([this]() {
  72. Game::INSTANCE->zDimension(getDimensionId())
  73. ->placeBlock(getPos(), blockTypeAfterGrowth);
  74. });
  75. }
  76. }
  77. void GrowingPlantBlock::sendModelInfo(NetworkMessage* zMessage)
  78. {
  79. GrowthState* current = 0;
  80. for (GrowthState* state : states)
  81. {
  82. if (state->getPercentage() <= seblingTicks / (float)seblingTicksMax)
  83. {
  84. current = state;
  85. }
  86. }
  87. if (current)
  88. {
  89. zMessage->addressBlock(this);
  90. Framework::InMemoryBuffer buffer;
  91. current->zModel()->writeTo(&buffer);
  92. char* msg = new char[(int)buffer.getSize() + 1];
  93. msg[0] = 1; // hmodel change
  94. buffer.lese(msg + 1, (int)buffer.getSize());
  95. zMessage->setMessage(msg, (int)buffer.getSize() + 1);
  96. }
  97. }
  98. TickSourceType GrowingPlantBlock::isTickSource() const
  99. {
  100. return TickSourceType::EACH_TICK;
  101. }
  102. Framework::Text GrowingPlantBlock::getTargetUIML()
  103. {
  104. return Framework::Text("<targetInfo><text width=\"auto\" height=\"auto\">")
  105. + name + "\n" + "Growth: "
  106. + Framework::Text((int)(seblingTicks / (float)seblingTicksMax * 100.f))
  107. + "%</text></targetInfo>";
  108. }
  109. GrowingPlantBlock* GrowingPlantBlock::addGrowthState(GrowthState* state)
  110. {
  111. int index = 0;
  112. for (GrowthState* s : states)
  113. {
  114. if (s->getPercentage() > state->getPercentage())
  115. {
  116. states.add(state, index);
  117. return this;
  118. }
  119. index++;
  120. }
  121. states.add(state);
  122. return this;
  123. }
  124. GrowingPlantBlockType::GrowingPlantBlockType()
  125. : BlockType(),
  126. transparent(1),
  127. passable(1),
  128. speedModifier(0.3f),
  129. interactable(1),
  130. ticksNeeded(0)
  131. {}
  132. void GrowingPlantBlockType::createSuperBlock(Block* zBlock, Item* zItem) const
  133. {
  134. GrowingPlantBlock* block = dynamic_cast<GrowingPlantBlock*>(zBlock);
  135. block->transparent = transparent;
  136. block->passable = passable;
  137. block->hardness = getHardness();
  138. block->speedModifier = speedModifier;
  139. block->interactable = interactable;
  140. BlockType::createSuperBlock(zBlock, zItem);
  141. }
  142. void GrowingPlantBlockType::loadSuperBlock(
  143. Block* zBlock, Framework::StreamReader* zReader, int dimensionId) const
  144. {
  145. BlockType::loadSuperBlock(zBlock, zReader, dimensionId);
  146. GrowingPlantBlock* block = dynamic_cast<GrowingPlantBlock*>(zBlock);
  147. zReader->lese((char*)&block->seblingTicks, 4);
  148. }
  149. void GrowingPlantBlockType::saveSuperBlock(
  150. Block* zBlock, Framework::StreamWriter* zWriter) const
  151. {
  152. BlockType::saveSuperBlock(zBlock, zWriter);
  153. GrowingPlantBlock* block = dynamic_cast<GrowingPlantBlock*>(zBlock);
  154. zWriter->schreibe((char*)&block->seblingTicks, 4);
  155. }
  156. Item* GrowingPlantBlockType::createItem() const
  157. {
  158. return 0;
  159. }
  160. Block* GrowingPlantBlockType::createBlock(
  161. Framework::Vec3<int> position, int dimensionId) const
  162. {
  163. GrowingPlantBlock* block = new GrowingPlantBlock(getId(),
  164. position,
  165. dimensionId,
  166. ticksNeeded,
  167. readableName,
  168. blockTypeIdAfterGrowth);
  169. for (GrowthState* state : states)
  170. {
  171. block->addGrowthState(dynamic_cast<GrowthState*>(state->getThis()));
  172. }
  173. return block;
  174. }
  175. GrowingPlantBlockType* GrowingPlantBlockType::addGrowthState(
  176. float growthPercentage, ModelInfo* model)
  177. {
  178. states.add(new GrowthState(growthPercentage, model));
  179. return this;
  180. }
  181. Framework::Text GrowingPlantBlockType::getBlockTypeNameAfterGrowth() const
  182. {
  183. return blockTypeNameAfterGrowth;
  184. }
  185. void GrowingPlantBlockType::setReadableName(Framework::Text readableName)
  186. {
  187. this->readableName = readableName;
  188. }
  189. Framework::Text GrowingPlantBlockType::getReadableName() const
  190. {
  191. return readableName;
  192. }
  193. void GrowingPlantBlockType::setTicksNeeded(int ticksNeeded)
  194. {
  195. this->ticksNeeded = ticksNeeded;
  196. }
  197. int GrowingPlantBlockType::getTicksNeeded() const
  198. {
  199. return ticksNeeded;
  200. }
  201. void GrowingPlantBlockType::setTransparent(bool transparent)
  202. {
  203. this->transparent = transparent;
  204. }
  205. bool GrowingPlantBlockType::isTransparent() const
  206. {
  207. return transparent;
  208. }
  209. void GrowingPlantBlockType::setPassable(bool passable)
  210. {
  211. this->passable = passable;
  212. }
  213. bool GrowingPlantBlockType::isPassable() const
  214. {
  215. return passable;
  216. }
  217. void GrowingPlantBlockType::setSpeedModifier(float speedModifier)
  218. {
  219. this->speedModifier = speedModifier;
  220. }
  221. float GrowingPlantBlockType::getSpeedModifier() const
  222. {
  223. return speedModifier;
  224. }
  225. void GrowingPlantBlockType::setInteractable(bool interactable)
  226. {
  227. this->interactable = interactable;
  228. }
  229. bool GrowingPlantBlockType::isInteractable() const
  230. {
  231. return interactable;
  232. }
  233. const Framework::RCArray<GrowthState>& GrowingPlantBlockType::getStates() const
  234. {
  235. return states;
  236. }
  237. void GrowingPlantBlockType::setBlockTypeNameAfterGrowth(
  238. Framework::Text blockTypeIdAfterGrowth)
  239. {}
  240. ItemType* GrowingPlantBlockType::createItemType() const
  241. {
  242. return 0;
  243. }
  244. GrowingPlantBlockTypeFactory::GrowingPlantBlockTypeFactory()
  245. : BlockTypeFactoryBase()
  246. {}
  247. GrowingPlantBlockType* GrowingPlantBlockTypeFactory::createValue(
  248. Framework::JSON::JSONObject* zJson) const
  249. {
  250. return new GrowingPlantBlockType();
  251. }
  252. void GrowingPlantBlockTypeFactory::fromJson(
  253. GrowingPlantBlockType* zResult, Framework::JSON::JSONObject* zJson) const
  254. {
  255. zResult->setBlockTypeNameAfterGrowth(
  256. zJson->zValue("blockTypeAfterGrowth")->asString()->getString());
  257. zResult->setReadableName(
  258. zJson->zValue("readableName")->asString()->getString());
  259. zResult->setTicksNeeded(
  260. (int)zJson->zValue("ticksNeeded")->asNumber()->getNumber());
  261. zResult->setTransparent(zJson->zValue("transparent")->asBool()->getBool());
  262. zResult->setPassable(zJson->zValue("passable")->asBool()->getBool());
  263. zResult->setSpeedModifier(
  264. (float)zJson->zValue("speedModifier")->asNumber()->getNumber());
  265. zResult->setInteractable(
  266. zJson->zValue("interactable")->asBool()->getBool());
  267. for (Framework::JSON::JSONValue* state :
  268. *zJson->zValue("states")->asArray())
  269. {
  270. zResult->addGrowthState((float)state->asObject()
  271. ->zValue("percentage")
  272. ->asNumber()
  273. ->getNumber(),
  274. Game::INSTANCE->zTypeRegistry()->fromJson<ModelInfo>(
  275. state->asObject()->zValue("model")));
  276. }
  277. BlockTypeFactoryBase::fromJson(zResult, zJson);
  278. }
  279. void GrowingPlantBlockTypeFactory::toJson(
  280. GrowingPlantBlockType* zObject, Framework::JSON::JSONObject* zResult) const
  281. {
  282. zResult->addValue("readableName",
  283. new Framework::JSON::JSONString(zObject->getReadableName()));
  284. zResult->addValue(
  285. "model", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zModel()));
  286. zResult->addValue(
  287. "name", new Framework::JSON::JSONString(zObject->getName()));
  288. zResult->addValue(
  289. "hardness", new Framework::JSON::JSONNumber(zObject->getHardness()));
  290. zResult->addValue(
  291. "mapColor", new Framework::JSON::JSONString(zObject->getMapColor()));
  292. zResult->addValue("blockTypeAfterGrowth",
  293. new Framework::JSON::JSONString(
  294. zObject->getBlockTypeNameAfterGrowth()));
  295. zResult->addValue("ticksNeeded",
  296. new Framework::JSON::JSONNumber((double)zObject->getTicksNeeded()));
  297. Framework::JSON::JSONArray* states = new Framework::JSON::JSONArray();
  298. for (GrowthState* state : zObject->getStates())
  299. {
  300. Framework::JSON::JSONObject* stateObj
  301. = new Framework::JSON::JSONObject();
  302. stateObj->addValue(
  303. "model", Game::INSTANCE->zTypeRegistry()->toJson(state->zModel()));
  304. stateObj->addValue("percentage",
  305. new Framework::JSON::JSONNumber(state->getPercentage()));
  306. states->addValue(stateObj);
  307. }
  308. zResult->addValue("states", states);
  309. Framework::JSON::JSONArray* groupNames = new Framework::JSON::JSONArray();
  310. for (Framework::Text* groupName : zObject->getGroupNames())
  311. {
  312. groupNames->addValue(new Framework::JSON::JSONString(*groupName));
  313. }
  314. zResult->addValue("groupNames", groupNames);
  315. }
  316. JSONObjectValidationBuilder* GrowingPlantBlockTypeFactory::addToValidator(
  317. JSONObjectValidationBuilder* builder) const
  318. {
  319. return BlockTypeFactoryBase::addToValidator(
  320. builder->withRequiredString("readableName")
  321. ->finishString()
  322. ->withRequiredNumber("ticksNeeded")
  323. ->finishNumber()
  324. ->withRequiredString("blockTypeAfterGrowth")
  325. ->finishString()
  326. ->withRequiredArray("states")
  327. ->addAcceptedObjectInArray()
  328. ->withRequiredNumber("percentage")
  329. ->whichIsGreaterOrEqual(0.0)
  330. ->whichIsLessOrEqual(1.0)
  331. ->finishNumber()
  332. ->withRequiredAttribute("model",
  333. Game::INSTANCE->zTypeRegistry()->getValidator<ModelInfo>())
  334. ->finishObject()
  335. ->finishArray()
  336. ->withRequiredBool("transparent")
  337. ->withDefault(true)
  338. ->finishBool()
  339. ->withRequiredBool("passable")
  340. ->withDefault(true)
  341. ->finishBool()
  342. ->withRequiredNumber("speedModifier")
  343. ->withDefault(0.5)
  344. ->finishNumber()
  345. ->withRequiredBool("interactable")
  346. ->withDefault(true)
  347. ->finishBool());
  348. }
  349. Framework::Text GrowingPlantBlockTypeFactory::getTypeToken() const
  350. {
  351. return "growingPlant";
  352. }