ItemEntity.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include "ItemEntity.h"
  2. #include "Game.h"
  3. ItemEntity::ItemEntity(Framework::Vec3<float> location, int dimensionId, int entityId)
  4. : Entity(ItemEntityType::INSTANCE, location, dimensionId, entityId)
  5. {
  6. slot = new ItemSlot("Inventory", __INT32_MAX__, 0, 0, 0, ANY_DIRECTION, 0);
  7. addSlot(slot);
  8. faceOffset = { 0.f, 0.f, 0.f };
  9. maxHP = 10;
  10. currentHP = 10;
  11. stamina = 10;
  12. maxStamina = 10;
  13. hunger = 10;
  14. maxHunger = 10;
  15. thirst = 10;
  16. maxThirst = 10;
  17. targetDistanceLimit = 4;
  18. }
  19. void ItemEntity::tick(const Dimension* zDimension)
  20. {
  21. if (slot->zStack() == 0 && !removed)
  22. throw "Illegal State exception";
  23. // add speed to next entity with free inventory
  24. Entity* zOther = Game::INSTANCE->zNearestEntity(currentDimensionId, location, [this](Entity* zOther)
  25. {
  26. return zOther != this && zOther->numberOfAddableItems(slot->zStack(), NO_DIRECTION);
  27. });
  28. bool found = 1;
  29. if (zOther)
  30. {
  31. float d = location.abstand(zOther->getPosition());
  32. if (d < 0.5f)
  33. {
  34. // add items of this entity to the other entity
  35. zOther->interactWith(this, NO_DIRECTION).pullItems(slot->getNumberOfItems(), 0);
  36. if (slot->getNumberOfItems() == 0)
  37. onDeath();
  38. }
  39. else if (d < 3.f)
  40. {
  41. // accelerate towards of the other entity
  42. speed += (zOther->getPosition() - location).normalize() * (20 / (d + 0.5f)) / 30.f;
  43. }
  44. else
  45. found = 0;
  46. }
  47. else
  48. found = 0;
  49. if (!found)
  50. {
  51. speed -= speed / 30.f;
  52. if (speed.getLength() < 0.2f)
  53. speed = { 0.f, 0.f, 0.f };
  54. }
  55. Entity::tick(zDimension);
  56. }
  57. void ItemEntity::onFall(float collisionSpeed)
  58. {
  59. if (collisionSpeed >= 50.f)
  60. this->currentHP = 0;
  61. }
  62. bool ItemEntity::hasDefaultModel() const
  63. {
  64. return 0;
  65. }
  66. ModelInfo ItemEntity::getSpecialModel() const
  67. {
  68. const ItemType* zItemType = 0;
  69. if (!slot->isEmpty())
  70. zItemType = slot->zStack()->zItem()->zItemType();
  71. return !zItemType ? ModelInfo("", "", 0) : zItemType->getModel();
  72. }
  73. ItemEntityType::ItemEntityType()
  74. : EntityType(ID, ModelInfo("", "", 0))
  75. {}
  76. Entity* ItemEntityType::createEntity(Framework::Vec3<float> position, int dimensionId, int entityId) const
  77. {
  78. return new ItemEntity(position, dimensionId, entityId);
  79. }