BasicBlock.cpp 4.4 KB

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