ItemEntity.cpp 2.7 KB

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