Hoe.cpp 2.5 KB

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