Hoe.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include "Hoe.h"
  2. #include "Game.h"
  3. HoeToolItemType::HoeToolItemType() // TODO: add broken hoe
  4. : BasicToolItemType(ItemTypeEnum::HOE,
  5. "Hoe",
  6. new HoeToolLevelUpRule(),
  7. 0,
  8. ModelInfo("tools.m3/hoe", "tools.ltdb/stonehoe.png", 1))
  9. {}
  10. Item* HoeToolItemType::createItem() const
  11. {
  12. return new BasicToolItem(ItemTypeEnum::HOE, "Hoe");
  13. }
  14. ItemSkill* HoeToolItemType::createDefaultItemSkill() const
  15. {
  16. return new HoeToolSkill();
  17. }
  18. void HoeToolItemType::loadSuperItemSkill(
  19. ItemSkill* zSkill, Framework::StreamReader* zReader) const
  20. {
  21. HoeToolSkill* skill = dynamic_cast<HoeToolSkill*>(zSkill);
  22. zReader->lese((char*)&skill->level, 4);
  23. zReader->lese((char*)&skill->xp, 4);
  24. zReader->lese((char*)&skill->maxXP, 4);
  25. }
  26. void HoeToolItemType::saveSuperItemSkill(
  27. const ItemSkill* zSkill, Framework::StreamWriter* zWriter) const
  28. {
  29. const HoeToolSkill* skill = dynamic_cast<const HoeToolSkill*>(zSkill);
  30. zWriter->schreibe((char*)&skill->level, 4);
  31. zWriter->schreibe((char*)&skill->xp, 4);
  32. zWriter->schreibe((char*)&skill->maxXP, 4);
  33. }
  34. HoeToolLevelUpRule::HoeToolLevelUpRule()
  35. : ItemSkillLevelUpRule()
  36. {}
  37. void HoeToolLevelUpRule::applyOn(ItemSkill* zSkill)
  38. {
  39. HoeToolSkill* skill = dynamic_cast<HoeToolSkill*>(zSkill);
  40. if (skill->xp >= skill->maxXP)
  41. {
  42. skill->level++;
  43. skill->xp = 0;
  44. skill->maxXP = skill->maxXP * 2;
  45. }
  46. }
  47. HoeToolSkill::HoeToolSkill()
  48. : ItemSkill(ItemTypeEnum::HOE),
  49. level(1),
  50. xp(0.f),
  51. maxXP(10.f),
  52. nextActionCounter(0)
  53. {}
  54. void HoeToolSkill::use(Entity* zActor, Item* zUsedItem, Block* zTarget)
  55. {
  56. if (nextActionCounter == 0)
  57. {
  58. if (zTarget->zBlockType()->getId() == BlockTypeEnum::DIRT)
  59. {
  60. if (zActor->getStamina() > 0.5f / (float)level)
  61. {
  62. zActor->setStamina(zActor->getStamina() - 0.5f / (float)level);
  63. zUsedItem->setDurability(zUsedItem->getDurability() - 0.5f);
  64. Vec3<int> pos = zTarget->getPos();
  65. int dimension = zTarget->getDimensionId();
  66. xp += 0.5f;
  67. Game::INSTANCE->doLater([pos, dimension]() {
  68. Game::INSTANCE->zDimension(dimension)->placeBlock(
  69. pos, BlockTypeEnum::FARMLAND);
  70. });
  71. nextActionCounter = 20;
  72. }
  73. }
  74. }
  75. else if (nextActionCounter > 0)
  76. nextActionCounter--;
  77. }
  78. void HoeToolSkill::use(Entity* zActor, Item* zUsedItem, Entity* zTarget)
  79. {
  80. // a hoe can not be used on an entity
  81. }