ItemEntity.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. maxMovementSpeed = 1;
  19. }
  20. void ItemEntity::prepareTick(const Dimension* zDimension)
  21. {
  22. if (slot->zStack() == 0 && !removed)
  23. throw "Illegal State exception";
  24. if (movements.getEintragAnzahl() <= 1)
  25. {
  26. Entity* zOther = Game::INSTANCE->zNearestEntity(currentDimensionId, location, [this](Entity* zOther)
  27. {
  28. return zOther != this && zOther->numberOfAddableItems(slot->zStack(), NO_DIRECTION) && (!this->slot->isFull() || zOther->zType()->getId() != ItemEntityType::ID);
  29. });
  30. if (zOther)
  31. {
  32. MovementFrame frame;
  33. frame.direction = zOther->getPosition() - getPosition();
  34. frame.duration = 0.25;
  35. frame.movementFlags = 0x1; // TODO: torn on flight mode
  36. frame.targetPosition = getPosition() + frame.direction * (0.5f * maxMovementSpeed);
  37. addMovementFrame(frame);
  38. }
  39. }
  40. Entity::prepareTick(zDimension);
  41. }
  42. void ItemEntity::tick(const Dimension* zDimension)
  43. {
  44. Entity* zOther = Game::INSTANCE->zNearestEntity(currentDimensionId, location, [this](Entity* zOther)
  45. {
  46. return zOther != this && zOther->numberOfAddableItems(slot->zStack(), NO_DIRECTION) && (!this->slot->isFull() || zOther->zType()->getId() != ItemEntityType::ID);
  47. });
  48. if (zOther)
  49. {
  50. float d = location.abstand(zOther->getPosition());
  51. if (d < 0.5f)
  52. {
  53. // add items of this entity to the other entity
  54. zOther->interactWith(this, NO_DIRECTION).pullItems(slot->getNumberOfItems(), 0);
  55. if (slot->getNumberOfItems() == 0)
  56. onDeath();
  57. }
  58. }
  59. Entity::tick(zDimension);
  60. }
  61. void ItemEntity::onFall(float collisionSpeed)
  62. {
  63. if (collisionSpeed >= 50.f)
  64. this->currentHP = 0;
  65. }
  66. bool ItemEntity::hasDefaultModel() const
  67. {
  68. return 0;
  69. }
  70. ModelInfo ItemEntity::getSpecialModel() const
  71. {
  72. const ItemType* zItemType = 0;
  73. if (!slot->isEmpty())
  74. zItemType = slot->zStack()->zItem()->zItemType();
  75. return !zItemType ? ModelInfo("", "", 0) : zItemType->getModel();
  76. }
  77. ItemEntityType::ItemEntityType()
  78. : EntityType(ID, ModelInfo("", "", 0))
  79. {}
  80. Entity* ItemEntityType::createEntity(Framework::Vec3<float> position, int dimensionId, int entityId) const
  81. {
  82. return new ItemEntity(position, dimensionId, entityId);
  83. }