BasicBlock.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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(int typeId, int itemTypeId, ModelInfo model)
  57. : BasicBlockType(
  58. typeId, itemTypeId, model, [typeId](Framework::Vec3<int> pos) {
  59. return new BasicBlock(typeId, 0, pos);
  60. })
  61. {}
  62. BasicBlockType::BasicBlockType(int typeId,
  63. int itemTypeId,
  64. ModelInfo model,
  65. std::function<Block*(Framework::Vec3<int>)> creatBlockCustom)
  66. : BlockType(typeId, 0, model, 1, 100, 0),
  67. itemType(itemTypeId),
  68. transparent(0),
  69. passable(0),
  70. hardness(1.f),
  71. zTool(0),
  72. speedModifier(1.f),
  73. interactable(1),
  74. creatBlockCustom(creatBlockCustom)
  75. {}
  76. void BasicBlockType::createSuperBlock(Block* zBlock, Item* zItem) const
  77. {
  78. BasicBlock* block = dynamic_cast<BasicBlock*>(zBlock);
  79. if (!block)
  80. throw "DirtBlockType::createSuperBlock was called with a block witch "
  81. "is not an instance of BasicBlock";
  82. block->transparent = transparent;
  83. block->passable = passable;
  84. block->hp = (float)getInitialMaxHP();
  85. block->maxHP = (float)getInitialMaxHP();
  86. block->hardness = hardness;
  87. block->zTool = zTool;
  88. block->speedModifier = speedModifier;
  89. block->interactable = interactable;
  90. BlockType::createSuperBlock(zBlock, zItem);
  91. }
  92. Block* BasicBlockType::createBlock(Framework::Vec3<int> position) const
  93. {
  94. return creatBlockCustom(position);
  95. }
  96. Item* BasicBlockType::createItem() const
  97. {
  98. return StaticRegistry<ItemType>::INSTANCE.zElement(itemType)->createItem();
  99. }
  100. BasicBlockType* BasicBlockType::setHardness(float hardness)
  101. {
  102. this->hardness = hardness;
  103. return this;
  104. }