BasicBlocks.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. #include "BasicBlocks.h"
  2. #include "Game.h"
  3. #include "ItemEntity.h"
  4. #include "ItemStack.h"
  5. #include "ModelInfo.h"
  6. #include "TreeSeblingBlock.h"
  7. BasicBlock::BasicBlock(int typeId, Framework::Vec3<int> pos, int dimensionId)
  8. : BasicBlock(typeId, pos, dimensionId, false)
  9. {}
  10. BasicBlock::BasicBlock(
  11. int typeId, Framework::Vec3<int> pos, int dimensionId, bool hasInventory)
  12. : Block(typeId, pos, dimensionId, hasInventory)
  13. {}
  14. bool BasicBlock::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
  15. {
  16. return 0;
  17. }
  18. void BasicBlock::onPostTick() {}
  19. BasicBlockType::BasicBlockType()
  20. : BlockType(),
  21. itemTypeName(),
  22. transparent(0),
  23. passable(0),
  24. speedModifier(1.f),
  25. interactable(1)
  26. {}
  27. bool BasicBlockType::initialize(Game* zGame)
  28. {
  29. if (itemTypeName.getLength())
  30. {
  31. itemTypeId = zGame->getItemTypeId(itemTypeName);
  32. }
  33. else
  34. {
  35. itemTypeId = 0;
  36. }
  37. return itemTypeId >= 0 && BlockType::initialize(zGame);
  38. }
  39. void BasicBlockType::createSuperBlock(Block* zBlock, Item* zItem) const
  40. {
  41. BasicBlock* block = dynamic_cast<BasicBlock*>(zBlock);
  42. block->transparent = transparent;
  43. block->passable = passable;
  44. block->hp = (float)getInitialMaxHP();
  45. block->maxHP = (float)getInitialMaxHP();
  46. block->hardness = getHardness();
  47. block->speedModifier = speedModifier;
  48. block->interactable = interactable;
  49. BlockType::createSuperBlock(zBlock, zItem);
  50. }
  51. Block* BasicBlockType::createBlock(
  52. Framework::Vec3<int> position, int dimensionId) const
  53. {
  54. return new BasicBlock(getId(), position, dimensionId);
  55. }
  56. Item* BasicBlockType::createItem() const
  57. {
  58. return Game::INSTANCE->zItemType(itemTypeId)->createItem();
  59. }
  60. Framework::Text BasicBlockType::getItemTypeName() const
  61. {
  62. return itemTypeName;
  63. }
  64. ItemType* BasicBlockType::createItemType() const
  65. {
  66. return new BasicBlockItemType(getItemTypeName(),
  67. new ModelInfo(zModel()->getModelPath(),
  68. zModel()->getTexturePaths(),
  69. zModel()->isTransparent(),
  70. zModel()->getSize() / 2.f),
  71. transparent,
  72. passable,
  73. getHardness(),
  74. speedModifier,
  75. getItemTypeName(),
  76. 0,
  77. 50,
  78. getGroupNames());
  79. }
  80. void BasicBlockType::setItemTypeName(Framework::Text itemTypeName)
  81. {
  82. this->itemTypeName = itemTypeName;
  83. }
  84. int BasicBlockType::getItemTypeId() const
  85. {
  86. return itemTypeId;
  87. }
  88. void BasicBlockType::setTransparent(bool transparent)
  89. {
  90. this->transparent = transparent;
  91. }
  92. bool BasicBlockType::isTransparent() const
  93. {
  94. return transparent;
  95. }
  96. void BasicBlockType::setPassable(bool passable)
  97. {
  98. this->passable = passable;
  99. }
  100. bool BasicBlockType::isPassable() const
  101. {
  102. return passable;
  103. }
  104. void BasicBlockType::setSpeedModifier(float speedModifier)
  105. {
  106. this->speedModifier = speedModifier;
  107. }
  108. float BasicBlockType::getSpeedModifier() const
  109. {
  110. return speedModifier;
  111. }
  112. void BasicBlockType::setInteractable(bool interactable)
  113. {
  114. this->interactable = interactable;
  115. }
  116. bool BasicBlockType::isInteractable() const
  117. {
  118. return interactable;
  119. }
  120. BasicBlockTypeFactory::BasicBlockTypeFactory()
  121. : BlockTypeFactoryBase()
  122. {}
  123. BasicBlockType* BasicBlockTypeFactory::createValue(
  124. Framework::JSON::JSONObject* zJson) const
  125. {
  126. return new BasicBlockType();
  127. }
  128. void BasicBlockTypeFactory::fromJson(
  129. BasicBlockType* zResult, Framework::JSON::JSONObject* zJson) const
  130. {
  131. zResult->setItemTypeName(
  132. zJson->zValue("itemType")->asString()->getString());
  133. zResult->setTransparent(zJson->zValue("transparent")->asBool()->getBool());
  134. zResult->setPassable(zJson->zValue("passable")->asBool()->getBool());
  135. zResult->setSpeedModifier(
  136. (float)zJson->zValue("speedModifier")->asNumber()->getNumber());
  137. zResult->setInteractable(
  138. (float)zJson->zValue("interactable")->asBool()->getBool());
  139. BlockTypeFactoryBase::fromJson(zResult, zJson);
  140. }
  141. void BasicBlockTypeFactory::toJson(
  142. BasicBlockType* zObject, Framework::JSON::JSONObject* zResult) const
  143. {
  144. zResult->addValue("itemType",
  145. new Framework::JSON::JSONString(zObject->getItemTypeName()));
  146. zResult->addValue(
  147. "transparent", new Framework::JSON::JSONBool(zObject->isTransparent()));
  148. zResult->addValue(
  149. "passable", new Framework::JSON::JSONBool(zObject->isPassable()));
  150. zResult->addValue("speedModifier",
  151. new Framework::JSON::JSONNumber(zObject->getSpeedModifier()));
  152. zResult->addValue("interactable",
  153. new Framework::JSON::JSONBool(zObject->isInteractable()));
  154. BlockTypeFactoryBase::toJson(zObject, zResult);
  155. }
  156. JSONObjectValidationBuilder* BasicBlockTypeFactory::addToValidator(
  157. JSONObjectValidationBuilder* builder) const
  158. {
  159. return BlockTypeFactoryBase::addToValidator(
  160. builder->withRequiredString("itemType")
  161. ->withDefault("")
  162. ->finishString()
  163. ->withRequiredBool("transparent")
  164. ->withDefault(false)
  165. ->finishBool()
  166. ->withRequiredBool("passable")
  167. ->withDefault(false)
  168. ->finishBool()
  169. ->withRequiredNumber("speedModifier")
  170. ->withDefault(1.0)
  171. ->finishNumber()
  172. ->withRequiredBool("interactable")
  173. ->withDefault(true)
  174. ->finishBool());
  175. }
  176. Framework::Text BasicBlockTypeFactory::getTypeToken() const
  177. {
  178. return "basicBlock";
  179. }
  180. AdditionalItemSpawningBlock::AdditionalItemSpawningBlock(
  181. int typeId, Framework::Vec3<int> pos, int dimensionId)
  182. : BasicBlock(typeId, pos, dimensionId)
  183. {}
  184. void AdditionalItemSpawningBlock::addSpawn(SpawnConfig config)
  185. {
  186. spawns.add(config);
  187. }
  188. void AdditionalItemSpawningBlock::onDestroy()
  189. {
  190. for (const SpawnConfig& config : spawns)
  191. {
  192. if ((double)rand() / RAND_MAX < config.chance)
  193. {
  194. int amount = config.min
  195. + (int)((config.max - config.min)
  196. * ((double)rand() / RAND_MAX));
  197. if (amount > 0)
  198. {
  199. ItemStack* spawnedItems
  200. = Game::INSTANCE->zItemType(config.typeId)
  201. ->createItemStack(amount);
  202. if (spawnedItems)
  203. {
  204. Game::INSTANCE->spawnItem(
  205. location + Framework::Vec3<float>(0.5f, 0.5f, 0.5f),
  206. getDimensionId(),
  207. spawnedItems);
  208. }
  209. }
  210. }
  211. }
  212. BasicBlock::onDestroy();
  213. }
  214. AdditionalItemSpawningBlockType::AdditionalItemSpawningBlockType()
  215. : BasicBlockType(),
  216. spawns()
  217. {}
  218. void AdditionalItemSpawningBlockType::createSuperBlock(
  219. Block* zBlock, Item* zItem) const
  220. {
  221. AdditionalItemSpawningBlock* block
  222. = dynamic_cast<AdditionalItemSpawningBlock*>(zBlock);
  223. if (block)
  224. {
  225. for (const SpawnConfig& config : spawns)
  226. {
  227. block->addSpawn(config);
  228. }
  229. }
  230. BasicBlockType::createSuperBlock(zBlock, zItem);
  231. }
  232. bool AdditionalItemSpawningBlockType::initialize(Game* zGame)
  233. {
  234. for (auto iterator = spawns.begin(); iterator; iterator++)
  235. {
  236. int itemTypeId
  237. = Game::INSTANCE->getItemTypeId(iterator.val().itemTypeName);
  238. if (itemTypeId < 0)
  239. {
  240. return false;
  241. }
  242. iterator.set({iterator.val().min,
  243. iterator.val().max,
  244. iterator.val().chance,
  245. iterator.val().itemTypeName,
  246. itemTypeId});
  247. }
  248. return BasicBlockType::initialize(zGame);
  249. }
  250. Block* AdditionalItemSpawningBlockType::createBlock(
  251. Framework::Vec3<int> position, int dimensionId) const
  252. {
  253. AdditionalItemSpawningBlock* block
  254. = new AdditionalItemSpawningBlock(getId(), position, dimensionId);
  255. return block;
  256. }
  257. void AdditionalItemSpawningBlockType::addSpawn(SpawnConfig config)
  258. {
  259. spawns.add(config);
  260. }
  261. const Framework::Array<SpawnConfig>&
  262. AdditionalItemSpawningBlockType::getSpawns() const
  263. {
  264. return spawns;
  265. }
  266. AdditionalItemSpawningBlockTypeFactory::AdditionalItemSpawningBlockTypeFactory()
  267. : BlockTypeFactoryBase()
  268. {}
  269. AdditionalItemSpawningBlockType*
  270. AdditionalItemSpawningBlockTypeFactory::createValue(
  271. Framework::JSON::JSONObject* zJson) const
  272. {
  273. return new AdditionalItemSpawningBlockType();
  274. }
  275. void AdditionalItemSpawningBlockTypeFactory::fromJson(
  276. AdditionalItemSpawningBlockType* zResult,
  277. Framework::JSON::JSONObject* zJson) const
  278. {
  279. Framework::JSON::JSONArray* spawnsJson = zJson->zValue("spawns")->asArray();
  280. for (int i = 0; i < spawnsJson->getLength(); i++)
  281. {
  282. Framework::JSON::JSONObject* spawnJson
  283. = spawnsJson->zValue(i)->asObject();
  284. zResult->addSpawn(SpawnConfig{
  285. (int)spawnJson->zValue("min")->asNumber()->getNumber(),
  286. (int)spawnJson->zValue("max")->asNumber()->getNumber(),
  287. (float)spawnJson->zValue("chance")->asNumber()->getNumber(),
  288. spawnJson->zValue("itemType")->asString()->getString(),
  289. 0,
  290. });
  291. }
  292. super.fromJson(zResult, zJson);
  293. }
  294. void AdditionalItemSpawningBlockTypeFactory::toJson(
  295. AdditionalItemSpawningBlockType* zObject,
  296. Framework::JSON::JSONObject* zResult) const
  297. {
  298. Framework::JSON::JSONArray* spawns = new Framework::JSON::JSONArray();
  299. for (const SpawnConfig& config : zObject->getSpawns())
  300. {
  301. Framework::JSON::JSONObject* spawn = new Framework::JSON::JSONObject();
  302. spawn->addValue(
  303. "itemType", new Framework::JSON::JSONString(config.itemTypeName));
  304. spawn->addValue(
  305. "chance", new Framework::JSON::JSONNumber(config.chance));
  306. spawn->addValue("min", new Framework::JSON::JSONNumber(config.min));
  307. spawn->addValue("max", new Framework::JSON::JSONNumber(config.max));
  308. spawns->addValue(spawn);
  309. }
  310. zResult->addValue("spawns", spawns);
  311. return super.toJson(zObject, zResult);
  312. }
  313. JSONObjectValidationBuilder*
  314. AdditionalItemSpawningBlockTypeFactory::addToValidator(
  315. JSONObjectValidationBuilder* builder) const
  316. {
  317. return super.addToValidator(builder->withRequiredAttribute("spawns",
  318. Framework::JSON::Validator::JSONValidator::buildForArray()
  319. ->addAcceptedObjectInArray()
  320. ->withRequiredString("itemType")
  321. ->finishString()
  322. ->withRequiredNumber("chance")
  323. ->finishNumber()
  324. ->withRequiredNumber("min")
  325. ->finishNumber()
  326. ->withRequiredNumber("max")
  327. ->finishNumber()
  328. ->finishObject()
  329. ->finishArray()));
  330. }
  331. Framework::Text AdditionalItemSpawningBlockTypeFactory::getTypeToken() const
  332. {
  333. return "additionalItemsBlockType";
  334. }