TreeSeblingBlock.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. #include "TreeSeblingBlock.h"
  2. #include "BasicBlocks.h"
  3. #include "Dimension.h"
  4. #include "Game.h"
  5. #include "NoBlock.h"
  6. #include "RandNoise.h"
  7. #include "TreeTemplate.h"
  8. #include "WorldGenerator.h"
  9. TreeSeblingBlock::TreeSeblingBlock(int typeId,
  10. Framework::Vec3<int> pos,
  11. int dimensionId,
  12. const BlockType* wood,
  13. const BlockType* leaves)
  14. : Block(typeId, pos, dimensionId, 0),
  15. seblingTicks(0),
  16. seblingTicksMax(10000),
  17. wood(wood),
  18. leaves(leaves)
  19. {}
  20. bool TreeSeblingBlock::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
  21. {
  22. int lastPercentage = (int)(seblingTicks / (float)seblingTicksMax * 100.f);
  23. seblingTicks += 1;
  24. if ((int)(seblingTicks / (float)seblingTicksMax * 100.f) != lastPercentage)
  25. {
  26. Game::INSTANCE->blockTargetChanged(this);
  27. }
  28. return 0;
  29. }
  30. void TreeSeblingBlock::onPostTick()
  31. {
  32. if ((int)seblingTicks >= seblingTicksMax)
  33. {
  34. Game::INSTANCE->doLater([wood = wood,
  35. leaves = leaves,
  36. pos = getPos(),
  37. dim = getDimensionId()]() {
  38. // the tree sebling object will be deleted during this operation
  39. RandNoise noise((int)time(0));
  40. if (!Game::INSTANCE->zGenerator()->spawnStructure(pos,
  41. dim,
  42. [wood = wood, leaves = leaves](GeneratorTemplate* tmpl) {
  43. TreeTemplate* tree = dynamic_cast<TreeTemplate*>(tmpl);
  44. return tree && tree->zWoodType() == wood
  45. && tree->zLeavesType() == leaves;
  46. }))
  47. {
  48. Game::INSTANCE->zDimension(dim)->placeBlock(
  49. pos, BlockTypeEnum::AIR);
  50. }
  51. });
  52. }
  53. }
  54. TickSourceType TreeSeblingBlock::isTickSource() const
  55. {
  56. return TickSourceType::EACH_TICK;
  57. }
  58. Framework::Text TreeSeblingBlock::getTargetUIML()
  59. {
  60. return Framework::Text("<targetInfo><text width=\"auto\" height=\"auto\">")
  61. + Game::INSTANCE->zBlockType(typeId)->getName() + "\n" + "Growth: "
  62. + Framework::Text((int)(seblingTicks / (float)seblingTicksMax * 100.f))
  63. + "%</text></targetInfo>";
  64. }
  65. TreeSeblingBlockType::TreeSeblingBlockType()
  66. : BlockType(),
  67. transparent(true),
  68. passable(true),
  69. speedModifier(0.5f),
  70. interactable(1)
  71. {}
  72. bool TreeSeblingBlockType::initialize(Game* zGame)
  73. {
  74. if (itemTypeName.getLength())
  75. {
  76. itemTypeId = zGame->getItemTypeId(itemTypeName);
  77. }
  78. else
  79. {
  80. itemTypeId = 0;
  81. }
  82. woodTypeId = zGame->getBlockTypeId(woodTypeName);
  83. leavesTypeId = zGame->getBlockTypeId(leavesTypeName);
  84. return itemTypeId >= 0 && BlockType::initialize(zGame);
  85. }
  86. void TreeSeblingBlockType::setItemTypeName(Framework::Text itemTypeName)
  87. {
  88. this->itemTypeName = itemTypeName;
  89. }
  90. Framework::Text TreeSeblingBlockType::getItemTypeName() const
  91. {
  92. return itemTypeName;
  93. }
  94. void TreeSeblingBlockType::setWoodTypeName(Framework::Text woodTypeName)
  95. {
  96. this->woodTypeName = woodTypeName;
  97. }
  98. Framework::Text TreeSeblingBlockType::getWoodTypeName() const
  99. {
  100. return woodTypeName;
  101. }
  102. void TreeSeblingBlockType::setLeavesTypeName(Framework::Text leavesTypeName)
  103. {
  104. this->leavesTypeName = leavesTypeName;
  105. }
  106. Framework::Text TreeSeblingBlockType::getLeavesTypeName() const
  107. {
  108. return leavesTypeName;
  109. }
  110. void TreeSeblingBlockType::setTransparent(bool transparent)
  111. {
  112. this->transparent = transparent;
  113. }
  114. bool TreeSeblingBlockType::isTransparent() const
  115. {
  116. return transparent;
  117. }
  118. void TreeSeblingBlockType::setPassable(bool passable)
  119. {
  120. this->passable = passable;
  121. }
  122. bool TreeSeblingBlockType::isPassable() const
  123. {
  124. return passable;
  125. }
  126. void TreeSeblingBlockType::setSpeedModifier(float speedModifier)
  127. {
  128. this->speedModifier = speedModifier;
  129. }
  130. float TreeSeblingBlockType::getSpeedModifier() const
  131. {
  132. return speedModifier;
  133. }
  134. void TreeSeblingBlockType::setInteractable(bool interactable)
  135. {
  136. this->interactable = interactable;
  137. }
  138. bool TreeSeblingBlockType::isInteractable() const
  139. {
  140. return interactable;
  141. }
  142. ItemType* TreeSeblingBlockType::createItemType() const
  143. {
  144. return new BasicBlockItemType(getItemTypeName(),
  145. new ModelInfo(zModel()->getModelPath(),
  146. zModel()->getTexturePaths(),
  147. zModel()->isTransparent(),
  148. zModel()->getSize() / 2.f),
  149. transparent,
  150. passable,
  151. getHardness(),
  152. speedModifier,
  153. getName(),
  154. 0,
  155. 50,
  156. getGroupNames());
  157. }
  158. void TreeSeblingBlockType::createSuperBlock(Block* zBlock, Item* zItem) const
  159. {
  160. TreeSeblingBlock* block = dynamic_cast<TreeSeblingBlock*>(zBlock);
  161. block->transparent = transparent;
  162. block->passable = passable;
  163. block->hp = (float)getInitialMaxHP();
  164. block->maxHP = (float)getInitialMaxHP();
  165. block->hardness = getHardness();
  166. block->speedModifier = speedModifier;
  167. block->interactable = interactable;
  168. BlockType::createSuperBlock(zBlock, zItem);
  169. }
  170. void TreeSeblingBlockType::loadSuperBlock(
  171. Block* zBlock, Framework::StreamReader* zReader, int dimensionId) const
  172. {
  173. TreeSeblingBlock* block = dynamic_cast<TreeSeblingBlock*>(zBlock);
  174. zReader->lese((char*)&block->seblingTicks, 4);
  175. zReader->lese((char*)&block->seblingTicksMax, 4);
  176. int id;
  177. zReader->lese((char*)&id, 4);
  178. block->wood = Game::INSTANCE->zBlockType(id);
  179. zReader->lese((char*)&id, 4);
  180. block->leaves = Game::INSTANCE->zBlockType(id);
  181. BlockType::loadSuperBlock(zBlock, zReader, dimensionId);
  182. }
  183. void TreeSeblingBlockType::saveSuperBlock(
  184. Block* zBlock, Framework::StreamWriter* zWriter) const
  185. {
  186. TreeSeblingBlock* block = dynamic_cast<TreeSeblingBlock*>(zBlock);
  187. zWriter->schreibe((char*)&block->seblingTicks, 4);
  188. zWriter->schreibe((char*)&block->seblingTicksMax, 4);
  189. int id = block->wood->getId();
  190. zWriter->schreibe((char*)&id, 4);
  191. id = block->leaves->getId();
  192. zWriter->schreibe((char*)&id, 4);
  193. BlockType::saveSuperBlock(zBlock, zWriter);
  194. }
  195. Item* TreeSeblingBlockType::createItem() const
  196. {
  197. return Game::INSTANCE->zItemType(itemTypeId)->createItem();
  198. }
  199. Block* TreeSeblingBlockType::createBlock(
  200. Framework::Vec3<int> position, int dimensionId) const
  201. {
  202. return new TreeSeblingBlock(getId(),
  203. position,
  204. dimensionId,
  205. Game::INSTANCE->zBlockType(woodTypeId),
  206. Game::INSTANCE->zBlockType(leavesTypeId));
  207. }
  208. TreeSeblingBlockTypeFactory::TreeSeblingBlockTypeFactory()
  209. : BlockTypeFactoryBase()
  210. {}
  211. TreeSeblingBlockType* TreeSeblingBlockTypeFactory::createValue(
  212. Framework::JSON::JSONObject* zJson) const
  213. {
  214. return new TreeSeblingBlockType();
  215. }
  216. void TreeSeblingBlockTypeFactory::fromJson(
  217. TreeSeblingBlockType* zResult, Framework::JSON::JSONObject* zJson) const
  218. {
  219. zResult->setItemTypeName(
  220. zJson->zValue("itemType")->asString()->getString());
  221. zResult->setWoodTypeName(
  222. zJson->zValue("woodType")->asString()->getString());
  223. zResult->setLeavesTypeName(
  224. zJson->zValue("leavesType")->asString()->getString());
  225. zResult->setTransparent(zJson->zValue("transparent")->asBool()->getBool());
  226. zResult->setPassable(zJson->zValue("passable")->asBool()->getBool());
  227. zResult->setSpeedModifier(
  228. (float)zJson->zValue("speedModifier")->asNumber()->getNumber());
  229. zResult->setInteractable(
  230. zJson->zValue("interactable")->asBool()->getBool());
  231. BlockTypeFactoryBase::fromJson(zResult, zJson);
  232. }
  233. void TreeSeblingBlockTypeFactory::toJson(
  234. TreeSeblingBlockType* zObject, Framework::JSON::JSONObject* zResult) const
  235. {
  236. zResult->addValue("itemType",
  237. new Framework::JSON::JSONString(zObject->getItemTypeName()));
  238. zResult->addValue("woodType",
  239. new Framework::JSON::JSONString(zObject->getWoodTypeName()));
  240. zResult->addValue("leavesType",
  241. new Framework::JSON::JSONString(zObject->getLeavesTypeName()));
  242. zResult->addValue("transparent",
  243. new Framework::JSON::JSONNumber(zObject->isTransparent()));
  244. zResult->addValue(
  245. "passable", new Framework::JSON::JSONNumber(zObject->isPassable()));
  246. zResult->addValue("speedModifier",
  247. new Framework::JSON::JSONNumber(zObject->getSpeedModifier()));
  248. zResult->addValue("interactable",
  249. new Framework::JSON::JSONNumber(zObject->isInteractable()));
  250. BlockTypeFactoryBase::toJson(zObject, zResult);
  251. }
  252. JSONObjectValidationBuilder* TreeSeblingBlockTypeFactory::addToValidator(
  253. JSONObjectValidationBuilder* builder) const
  254. {
  255. return BlockTypeFactoryBase::addToValidator(
  256. builder->withRequiredString("itemType")
  257. ->finishString()
  258. ->withRequiredString("woodType")
  259. ->finishString()
  260. ->withRequiredString("leavesType")
  261. ->finishString()
  262. ->withRequiredBool("transparent")
  263. ->withDefault(true)
  264. ->finishBool()
  265. ->withRequiredBool("passable")
  266. ->withDefault(true)
  267. ->finishBool()
  268. ->withRequiredNumber("speedModifier")
  269. ->withDefault(0.5)
  270. ->finishNumber()
  271. ->withRequiredBool("interactable")
  272. ->withDefault(true)
  273. ->finishBool());
  274. }
  275. Framework::Text TreeSeblingBlockTypeFactory::getTypeToken() const
  276. {
  277. return "treeSapling";
  278. }