ItemEntity.cpp 1.9 KB

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