BasicBlock.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include "AddEntityUpdate.h"
  2. #include "BasicBlocks.h"
  3. #include "Game.h"
  4. #include "ItemEntity.h"
  5. #include "TreeSeblingBlock.h"
  6. BasicBlock::BasicBlock(int typeId, ItemType* zTool, Framework::Vec3<int> pos)
  7. : Block(typeId, zTool, pos, false)
  8. {}
  9. bool BasicBlock::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
  10. {
  11. return 0;
  12. }
  13. void BasicBlock::onPostTick() {}
  14. AdditionalItemSpawningBlock::AdditionalItemSpawningBlock(
  15. int typeId, ItemType* zTool, Framework::Vec3<int> pos)
  16. : BasicBlock(typeId, zTool, pos)
  17. {}
  18. void AdditionalItemSpawningBlock::addSpawn(SpawnConfig config)
  19. {
  20. spawns.add(config);
  21. }
  22. void AdditionalItemSpawningBlock::onDestroy()
  23. {
  24. for (const SpawnConfig& config : spawns)
  25. {
  26. if ((double)rand() / RAND_MAX < config.chance)
  27. {
  28. int amount = config.min
  29. + (int)((config.max - config.min)
  30. * ((double)rand() / RAND_MAX));
  31. if (amount > 0)
  32. {
  33. ItemStack* spawnedItems = StaticRegistry<ItemType>::INSTANCE
  34. .zElement(config.itemType)
  35. ->createItemStack(amount);
  36. if (spawnedItems)
  37. {
  38. ItemEntity* itemEntity
  39. = (ItemEntity*)StaticRegistry<EntityType>::INSTANCE
  40. .zElement(EntityTypeEnum::ITEM)
  41. ->createEntity(location
  42. + Framework::Vec3<float>(
  43. 0.5f, 0.5f, 0.5f),
  44. getDimensionId(),
  45. Game::INSTANCE->getNextEntityId());
  46. itemEntity->unsaveAddItem(spawnedItems, NO_DIRECTION);
  47. spawnedItems->release();
  48. Game::INSTANCE->requestWorldUpdate(
  49. new AddEntityUpdate(itemEntity, getDimensionId()));
  50. }
  51. }
  52. }
  53. }
  54. BasicBlock::onDestroy();
  55. }
  56. BasicBlockType::BasicBlockType(
  57. int typeId, int itemTypeId, ModelInfo model, const char* name)
  58. : BasicBlockType(
  59. typeId,
  60. itemTypeId,
  61. model,
  62. [typeId](Framework::Vec3<int> pos) {
  63. return new BasicBlock(typeId, 0, pos);
  64. },
  65. name)
  66. {}
  67. BasicBlockType::BasicBlockType(int typeId,
  68. int itemTypeId,
  69. ModelInfo model,
  70. std::function<Block*(Framework::Vec3<int>)> creatBlockCustom,
  71. const char* name)
  72. : BlockType(typeId, 0, model, 1, 100, 0, name, false),
  73. itemType(itemTypeId),
  74. transparent(0),
  75. passable(0),
  76. hardness(1.f),
  77. zTool(0),
  78. speedModifier(1.f),
  79. interactable(1),
  80. creatBlockCustom(creatBlockCustom)
  81. {}
  82. void BasicBlockType::createSuperBlock(Block* zBlock, Item* zItem) const
  83. {
  84. BasicBlock* block = dynamic_cast<BasicBlock*>(zBlock);
  85. block->transparent = transparent;
  86. block->passable = passable;
  87. block->hp = (float)getInitialMaxHP();
  88. block->maxHP = (float)getInitialMaxHP();
  89. block->hardness = hardness;
  90. block->zTool = zTool;
  91. block->speedModifier = speedModifier;
  92. block->interactable = interactable;
  93. BlockType::createSuperBlock(zBlock, zItem);
  94. }
  95. Block* BasicBlockType::createBlock(Framework::Vec3<int> position) const
  96. {
  97. return creatBlockCustom(position);
  98. }
  99. Item* BasicBlockType::createItem() const
  100. {
  101. return StaticRegistry<ItemType>::INSTANCE.zElement(itemType)->createItem();
  102. }
  103. BasicBlockType* BasicBlockType::setHardness(float hardness)
  104. {
  105. this->hardness = hardness;
  106. return this;
  107. }
  108. BasicBlockType* BasicBlockType::setTransparent(bool transparent) {
  109. this->transparent = transparent;
  110. return this;
  111. }