BasicBlocks.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. BasicBlockType* BasicBlockTypeFactory::fromJson(
  129. Framework::JSON::JSONObject* zJson) const
  130. {
  131. BasicBlockType* result = BlockTypeFactoryBase::fromJson(zJson);
  132. result->setItemTypeName(zJson->zValue("itemType")->asString()->getString());
  133. result->setTransparent(zJson->zValue("transparent")->asBool()->getBool());
  134. result->setPassable(zJson->zValue("passable")->asBool()->getBool());
  135. result->setSpeedModifier(
  136. (float)zJson->zValue("speedModifier")->asNumber()->getNumber());
  137. result->setInteractable(
  138. (float)zJson->zValue("interactable")->asBool()->getBool());
  139. return result;
  140. }
  141. Framework::JSON::JSONObject* BasicBlockTypeFactory::toJsonObject(
  142. BasicBlockType* zObject) const
  143. {
  144. Framework::JSON::JSONObject* result
  145. = BlockTypeFactoryBase::toJsonObject(zObject);
  146. result->addValue("itemType",
  147. new Framework::JSON::JSONString(zObject->getItemTypeName()));
  148. result->addValue(
  149. "transparent", new Framework::JSON::JSONBool(zObject->isTransparent()));
  150. result->addValue(
  151. "passable", new Framework::JSON::JSONBool(zObject->isPassable()));
  152. result->addValue("speedModifier",
  153. new Framework::JSON::JSONNumber(zObject->getSpeedModifier()));
  154. result->addValue("interactable",
  155. new Framework::JSON::JSONBool(zObject->isInteractable()));
  156. return result;
  157. }
  158. JSONObjectValidationBuilder* BasicBlockTypeFactory::addToValidator(
  159. JSONObjectValidationBuilder* builder) const
  160. {
  161. return BlockTypeFactoryBase::addToValidator(
  162. builder->withRequiredString("itemType")
  163. ->withDefault("")
  164. ->finishString()
  165. ->withRequiredBool("transparent")
  166. ->withDefault(false)
  167. ->finishBool()
  168. ->withRequiredBool("passable")
  169. ->withDefault(false)
  170. ->finishBool()
  171. ->withRequiredNumber("speedModifier")
  172. ->withDefault(1.0)
  173. ->finishNumber()
  174. ->withRequiredBool("interactable")
  175. ->withDefault(true)
  176. ->finishBool());
  177. }
  178. const char* BasicBlockTypeFactory::getTypeToken() const
  179. {
  180. return "basicBlock";
  181. }
  182. AdditionalItemSpawningBlock::AdditionalItemSpawningBlock(
  183. int typeId, Framework::Vec3<int> pos, int dimensionId)
  184. : BasicBlock(typeId, pos, dimensionId)
  185. {}
  186. void AdditionalItemSpawningBlock::addSpawn(SpawnConfig config)
  187. {
  188. spawns.add(config);
  189. }
  190. void AdditionalItemSpawningBlock::onDestroy()
  191. {
  192. for (const SpawnConfig& config : spawns)
  193. {
  194. if ((double)rand() / RAND_MAX < config.chance)
  195. {
  196. int amount = config.min
  197. + (int)((config.max - config.min)
  198. * ((double)rand() / RAND_MAX));
  199. if (amount > 0)
  200. {
  201. ItemStack* spawnedItems
  202. = Game::INSTANCE->zItemType(config.typeId)
  203. ->createItemStack(amount);
  204. if (spawnedItems)
  205. {
  206. Game::INSTANCE->spawnItem(
  207. location + Framework::Vec3<float>(0.5f, 0.5f, 0.5f),
  208. getDimensionId(),
  209. spawnedItems);
  210. }
  211. }
  212. }
  213. }
  214. BasicBlock::onDestroy();
  215. }
  216. AdditionalItemSpawningBlockType::AdditionalItemSpawningBlockType()
  217. : BasicBlockType(),
  218. spawns()
  219. {}
  220. void AdditionalItemSpawningBlockType::createSuperBlock(
  221. Block* zBlock, Item* zItem) const
  222. {
  223. AdditionalItemSpawningBlock* block
  224. = dynamic_cast<AdditionalItemSpawningBlock*>(zBlock);
  225. if (block)
  226. {
  227. for (const SpawnConfig& config : spawns)
  228. {
  229. block->addSpawn(config);
  230. }
  231. }
  232. BasicBlockType::createSuperBlock(zBlock, zItem);
  233. }
  234. bool AdditionalItemSpawningBlockType::initialize(Game* zGame)
  235. {
  236. for (auto iterator = spawns.begin(); iterator; iterator++)
  237. {
  238. int itemTypeId
  239. = Game::INSTANCE->getItemTypeId(iterator.val().itemTypeName);
  240. if (itemTypeId < 0)
  241. {
  242. return false;
  243. }
  244. iterator.set({iterator.val().min,
  245. iterator.val().max,
  246. iterator.val().chance,
  247. iterator.val().itemTypeName,
  248. itemTypeId});
  249. }
  250. return BasicBlockType::initialize(zGame);
  251. }
  252. Block* AdditionalItemSpawningBlockType::createBlock(
  253. Framework::Vec3<int> position, int dimensionId) const
  254. {
  255. AdditionalItemSpawningBlock* block
  256. = new AdditionalItemSpawningBlock(getId(), position, dimensionId);
  257. return block;
  258. }
  259. void AdditionalItemSpawningBlockType::addSpawn(SpawnConfig config)
  260. {
  261. spawns.add(config);
  262. }
  263. const Framework::Array<SpawnConfig>&
  264. AdditionalItemSpawningBlockType::getSpawns() const
  265. {
  266. return spawns;
  267. }
  268. AdditionalItemSpawningBlockTypeFactory::AdditionalItemSpawningBlockTypeFactory()
  269. : BasicBlockTypeFactory()
  270. {}
  271. BasicBlockType* AdditionalItemSpawningBlockTypeFactory::createValue(
  272. Framework::JSON::JSONObject* zJson) const
  273. {
  274. return new AdditionalItemSpawningBlockType();
  275. }
  276. BasicBlockType* AdditionalItemSpawningBlockTypeFactory::fromJson(
  277. Framework::JSON::JSONObject* zJson) const
  278. {
  279. AdditionalItemSpawningBlockType* result
  280. = dynamic_cast<AdditionalItemSpawningBlockType*>(
  281. BasicBlockTypeFactory::fromJson(zJson));
  282. Framework::JSON::JSONArray* spawnsJson = zJson->zValue("spawns")->asArray();
  283. for (int i = 0; i < spawnsJson->getLength(); i++)
  284. {
  285. Framework::JSON::JSONObject* spawnJson
  286. = spawnsJson->zValue(i)->asObject();
  287. result->addSpawn(SpawnConfig{
  288. (int)spawnJson->zValue("min")->asNumber()->getNumber(),
  289. (int)spawnJson->zValue("max")->asNumber()->getNumber(),
  290. (float)spawnJson->zValue("chance")->asNumber()->getNumber(),
  291. spawnJson->zValue("itemType")->asString()->getString(),
  292. 0,
  293. });
  294. }
  295. return result;
  296. }
  297. Framework::JSON::JSONObject*
  298. AdditionalItemSpawningBlockTypeFactory::toJsonObject(
  299. BasicBlockType* zObject) const
  300. {
  301. AdditionalItemSpawningBlockType* zType
  302. = dynamic_cast<AdditionalItemSpawningBlockType*>(zObject);
  303. Framework::JSON::JSONObject* result
  304. = BasicBlockTypeFactory::toJsonObject(zObject);
  305. Framework::JSON::JSONArray* spawns = new Framework::JSON::JSONArray();
  306. for (const SpawnConfig& config : zType->getSpawns())
  307. {
  308. Framework::JSON::JSONObject* spawn = new Framework::JSON::JSONObject();
  309. spawn->addValue(
  310. "itemType", new Framework::JSON::JSONString(config.itemTypeName));
  311. spawn->addValue(
  312. "chance", new Framework::JSON::JSONNumber(config.chance));
  313. spawn->addValue("min", new Framework::JSON::JSONNumber(config.min));
  314. spawn->addValue("max", new Framework::JSON::JSONNumber(config.max));
  315. spawns->addValue(spawn);
  316. }
  317. result->addValue("spawns", spawns);
  318. return result;
  319. }
  320. JSONObjectValidationBuilder*
  321. AdditionalItemSpawningBlockTypeFactory::addToValidator(
  322. JSONObjectValidationBuilder* builder) const
  323. {
  324. return BasicBlockTypeFactory::addToValidator(
  325. builder->withRequiredAttribute("spawns",
  326. Framework::Validator::DataValidator::buildForArray()
  327. ->addAcceptedObjectInArray()
  328. ->withRequiredString("itemType")
  329. ->finishString()
  330. ->withRequiredNumber("chance")
  331. ->finishNumber()
  332. ->withRequiredNumber("min")
  333. ->finishNumber()
  334. ->withRequiredNumber("max")
  335. ->finishNumber()
  336. ->finishObject()
  337. ->finishArray()));
  338. }
  339. const char* AdditionalItemSpawningBlockTypeFactory::getTypeToken() const
  340. {
  341. return "additionalItemsBlockType";
  342. }
  343. const char* AdditionalItemSpawningBlockTypeFactory::getTypeName() const
  344. {
  345. return typeid(AdditionalItemSpawningBlockType).name();
  346. }