Hoe.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. ItemTypeEnum::BROKEN_HOE,
  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. bool HoeToolSkill::use(Entity* zActor, Item* zUsedItem, Block* zTarget)
  55. {
  56. if (nextActionCounter == 0)
  57. {
  58. if (zActor->getStamina() > 0.5f / (float)level)
  59. {
  60. if (zTarget->zBlockType()->getId() == BlockTypeEnum::DIRT)
  61. {
  62. zActor->setStamina(
  63. zActor->getStamina() - 0.5f / (float)level);
  64. zUsedItem->setDurability(zUsedItem->getDurability() - 0.5f);
  65. Vec3<int> pos = zTarget->getPos();
  66. int dimension = zTarget->getDimensionId();
  67. xp += 0.5f;
  68. Game::INSTANCE->doLater([pos, dimension]() {
  69. Game::INSTANCE->zDimension(dimension)->placeBlock(
  70. pos, BlockTypeEnum::FARMLAND);
  71. });
  72. nextActionCounter = 20;
  73. }
  74. else
  75. {
  76. zUsedItem->setDurability(
  77. zUsedItem->getDurability() - 0.5f / (float)level);
  78. zActor->setStamina(zActor->getStamina() - 0.5f / (float)level);
  79. }
  80. return 1;
  81. }
  82. }
  83. else if (nextActionCounter > 0)
  84. nextActionCounter--;
  85. return 0;
  86. }
  87. bool HoeToolSkill::use(Entity* zActor, Item* zUsedItem, Entity* zTarget)
  88. {
  89. // a hoe can not be used on an entity
  90. return 0;
  91. }