Grass.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "Grass.h"
  2. #include "AddEntityUpdate.h"
  3. #include "Game.h"
  4. #include "ItemEntity.h"
  5. GrassBlock::GrassBlock(
  6. int typeId, const ItemType* zTool, Framework::Vec3<int> pos)
  7. : Block(typeId, zTool, pos, 0)
  8. {
  9. tickSource = 1;
  10. }
  11. bool GrassBlock::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
  12. {
  13. // TODO: spread to neighbor blocks if light level is hight enought
  14. return 0;
  15. }
  16. void GrassBlock::onPostTick() {}
  17. void GrassBlock::onDestroy()
  18. {
  19. if (!deadAndRemoved)
  20. {
  21. for (int i = 0; i < 6; i++)
  22. {
  23. if (neighbourTypes[i] == BlockTypeEnum::NO_BLOCK)
  24. {
  25. Framework::Vec3<int> pos
  26. = getPos() + getDirection(getDirectionFromIndex(i));
  27. Game::INSTANCE->zDimension(getDimensionId())
  28. ->placeBlock(pos,
  29. Game::INSTANCE->zGenerator()->generateSingleBlock(
  30. pos, getDimensionId()));
  31. }
  32. }
  33. if ((double)rand() / RAND_MAX < 0.1)
  34. {
  35. ItemStack* spawnedItems = StaticRegistry<ItemType>::INSTANCE
  36. .zElement(ItemTypeEnum::WHEAT_SEED)
  37. ->createItemStack(1);
  38. if (spawnedItems)
  39. {
  40. ItemEntity* itemEntity
  41. = (ItemEntity*)StaticRegistry<EntityType>::INSTANCE
  42. .zElement(EntityTypeEnum::ITEM)
  43. ->createEntity(
  44. location
  45. + Framework::Vec3<float>(0.5f, 0.5f, 0.5f),
  46. getDimensionId(),
  47. Game::INSTANCE->getNextEntityId());
  48. itemEntity->unsaveAddItem(spawnedItems, NO_DIRECTION, 0);
  49. spawnedItems->release();
  50. Game::INSTANCE->requestWorldUpdate(
  51. new AddEntityUpdate(itemEntity, getDimensionId()));
  52. }
  53. }
  54. for (MultiblockStructure* structure : structures)
  55. structure->onBlockRemoved(this);
  56. Game::INSTANCE->zDimension(getDimensionId())
  57. ->placeBlock(
  58. getPos(), BlockTypeEnum::AIR); // this will be deleted here
  59. }
  60. }
  61. GrassBlockType::GrassBlockType(
  62. int typeId, int itemTypeId, ModelInfo model, const char* name, int mapColor)
  63. : BlockType(typeId, 0, model, 1, 10, 0, name, false, mapColor),
  64. itemType(itemTypeId),
  65. transparent(true),
  66. passable(true),
  67. hardness(0.1f),
  68. zTool(0),
  69. speedModifier(0.5f),
  70. interactable(1)
  71. {}
  72. void GrassBlockType::createSuperBlock(Block* zBlock, Item* zItem) const
  73. {
  74. GrassBlock* block = dynamic_cast<GrassBlock*>(zBlock);
  75. block->transparent = transparent;
  76. block->passable = passable;
  77. block->hp = (float)getInitialMaxHP();
  78. block->maxHP = (float)getInitialMaxHP();
  79. block->hardness = hardness;
  80. block->zTool = zTool;
  81. block->speedModifier = speedModifier;
  82. block->interactable = interactable;
  83. BlockType::createSuperBlock(zBlock, zItem);
  84. }
  85. void GrassBlockType::loadSuperBlock(
  86. Block* zBlock, Framework::StreamReader* zReader, int dimensionId) const
  87. {
  88. BlockType::loadSuperBlock(zBlock, zReader, dimensionId);
  89. }
  90. void GrassBlockType::saveSuperBlock(
  91. Block* zBlock, Framework::StreamWriter* zWriter) const
  92. {
  93. BlockType::saveSuperBlock(zBlock, zWriter);
  94. }
  95. Item* GrassBlockType::createItem() const
  96. {
  97. return 0;
  98. }
  99. Block* GrassBlockType::createBlock(Framework::Vec3<int> position) const
  100. {
  101. return new GrassBlock(getId(), zTool, position);
  102. }