BasicBlocks.cpp 11 KB

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