ItemEntity.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include "ItemEntity.h"
  2. #include "Game.h"
  3. ItemEntity::ItemEntity(
  4. Framework::Vec3<float> location, int dimensionId, int entityId)
  5. : Entity(EntityTypeEnum::ITEM, 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. maxMovementSpeed = 1;
  20. }
  21. void ItemEntity::prepareTick(const Dimension* zDimension)
  22. {
  23. if (slot->zStack() == 0 && !removed) throw "Illegal State exception";
  24. if (movements.getEintragAnzahl() <= 1)
  25. {
  26. Entity* zOther = Game::INSTANCE->zNearestEntity(
  27. currentDimensionId, location, [this](Entity* zOther) {
  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
  41. = getPosition() + frame.direction * (0.5f * maxMovementSpeed);
  42. addMovementFrame(frame);
  43. }
  44. }
  45. Entity::prepareTick(zDimension);
  46. }
  47. void ItemEntity::tick(const Dimension* zDimension)
  48. {
  49. Entity* zOther = Game::INSTANCE->zNearestEntity(
  50. currentDimensionId, location, [this](Entity* zOther) {
  51. return zOther != this
  52. && zOther->numberOfAddableItems(slot->zStack(), NO_DIRECTION)
  53. && (!this->slot->isFull()
  54. || zOther->zType()->getId() != EntityTypeEnum::ITEM);
  55. });
  56. if (zOther)
  57. {
  58. float d = location.abstand(zOther->getPosition());
  59. if (d < 0.5f)
  60. {
  61. // add items of this entity to the other entity
  62. zOther->interactWith(this, NO_DIRECTION)
  63. .pullItems(slot->getNumberOfItems(), 0);
  64. if (slot->getNumberOfItems() == 0) onDeath();
  65. }
  66. }
  67. Entity::tick(zDimension);
  68. }
  69. void ItemEntity::onFall(float collisionSpeed)
  70. {
  71. if (collisionSpeed >= 50.f) 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()) zItemType = slot->zStack()->zItem()->zItemType();
  81. return !zItemType ? ModelInfo("", "", 0) : zItemType->getModel();
  82. }
  83. ItemEntityType::ItemEntityType()
  84. : EntityType(EntityTypeEnum::ITEM, ModelInfo("", "", 0))
  85. {}
  86. Entity* ItemEntityType::createEntity(
  87. Framework::Vec3<float> position, int dimensionId, int entityId) const
  88. {
  89. return new ItemEntity(position, dimensionId, entityId);
  90. }