Grass.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "Grass.h"
  2. #include "Game.h"
  3. GrassBlock::GrassBlock(
  4. int typeId, const ItemType* zTool, Framework::Vec3<int> pos)
  5. : Block(typeId, zTool, pos, 0)
  6. {
  7. tickSource = 1;
  8. }
  9. bool GrassBlock::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
  10. {
  11. // TODO: spread to neighbor blocks if light level is hight enought
  12. return 0;
  13. }
  14. void GrassBlock::onPostTick() {}
  15. void GrassBlock::onDestroy()
  16. {
  17. if (!deadAndRemoved)
  18. {
  19. for (int i = 0; i < 6; i++)
  20. {
  21. if (neighbourTypes[i] == BlockTypeEnum::NO_BLOCK)
  22. {
  23. Framework::Vec3<int> pos
  24. = getPos() + getDirection(getDirectionFromIndex(i));
  25. Game::INSTANCE->zDimension(getDimensionId())
  26. ->placeBlock(pos,
  27. Game::INSTANCE->zGenerator()->generateSingleBlock(
  28. pos, getDimensionId()));
  29. }
  30. }
  31. // TODO: calculate chance for dropping wheat seeds
  32. for (MultiblockStructure* structure : structures)
  33. structure->onBlockRemoved(this);
  34. Game::INSTANCE->zDimension(getDimensionId())
  35. ->placeBlock(
  36. getPos(), BlockTypeEnum::AIR); // this will be deleted here
  37. }
  38. }
  39. GrassBlockType::GrassBlockType(
  40. int typeId, int itemTypeId, ModelInfo model, const char* name)
  41. : BlockType(typeId, 0, model, 1, 10, 0, name),
  42. itemType(itemTypeId),
  43. transparent(true),
  44. passable(true),
  45. hardness(0.1f),
  46. zTool(0),
  47. speedModifier(0.5f),
  48. interactable(1)
  49. {}
  50. void GrassBlockType::createSuperBlock(Block* zBlock, Item* zItem) const
  51. {
  52. GrassBlock* block = dynamic_cast<GrassBlock*>(zBlock);
  53. if (!block)
  54. throw "GrassBlockType::createSuperBlock was called with a block "
  55. "witch is not an instance of GrassBlock";
  56. block->transparent = transparent;
  57. block->passable = passable;
  58. block->hp = (float)getInitialMaxHP();
  59. block->maxHP = (float)getInitialMaxHP();
  60. block->hardness = hardness;
  61. block->zTool = zTool;
  62. block->speedModifier = speedModifier;
  63. block->interactable = interactable;
  64. BlockType::createSuperBlock(zBlock, zItem);
  65. }
  66. void GrassBlockType::loadSuperBlock(
  67. Block* zBlock, Framework::StreamReader* zReader, int dimensionId) const
  68. {
  69. BlockType::loadSuperBlock(zBlock, zReader, dimensionId);
  70. }
  71. void GrassBlockType::saveSuperBlock(
  72. Block* zBlock, Framework::StreamWriter* zWriter) const
  73. {
  74. BlockType::saveSuperBlock(zBlock, zWriter);
  75. }
  76. Item* GrassBlockType::createItem() const
  77. {
  78. return 0;
  79. }
  80. Block* GrassBlockType::createBlock(Framework::Vec3<int> position) const
  81. {
  82. return new GrassBlock(getId(), zTool, position);
  83. }