ItemEntity.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "ItemEntity.h"
  2. #include "Game.h"
  3. #include "ItemSlot.h"
  4. #include "ItemStack.h"
  5. #include "ItemType.h"
  6. #ifdef _WINDOWS
  7. # define __INT32_MAX__ 0x7FFFFFFF
  8. #endif
  9. ItemEntity::ItemEntity(
  10. Framework::Vec3<float> location, int dimensionId, int entityId)
  11. : Entity(EntityTypeEnum::ITEM, location, dimensionId, entityId)
  12. {
  13. slot = new ItemSlot("Inventory", __INT32_MAX__, 0, 0, 0, ANY_DIRECTION, 0);
  14. addSlot(slot);
  15. faceOffset = {0.f, 0.f, 0.f};
  16. maxHP = 10;
  17. maxStamina = 10;
  18. maxHunger = 10;
  19. maxThirst = 10;
  20. setHP(0, 0, 0, 10.f);
  21. setStamina(10);
  22. setHunger(10);
  23. setThirst(10);
  24. targetDistanceLimit = 4;
  25. maxMovementSpeed = 1;
  26. }
  27. void ItemEntity::prepareTick(const Dimension* zDimension)
  28. {
  29. if (slot->zStack() == 0 && !removed) throw "Illegal State exception";
  30. if (movements.getEintragAnzahl() <= 1)
  31. {
  32. Entity* zOther = Game::INSTANCE->zNearestEntity(
  33. dimensionId, location, [this](Entity* zOther) {
  34. return zOther != this
  35. && zOther->numberOfAddableItems(
  36. slot->zStack()->zItem(), NO_DIRECTION)
  37. && (!this->slot->isFull()
  38. || zOther->zType()->getId() != EntityTypeEnum::ITEM);
  39. });
  40. if (zOther)
  41. {
  42. MovementFrame frame;
  43. frame.direction = zOther->getPosition() - getPosition();
  44. frame.duration = 0.25;
  45. frame.movementFlags = 0x1; // TODO: torn on flight mode
  46. frame.targetPosition
  47. = getPosition() + frame.direction * (0.5f * maxMovementSpeed);
  48. addMovementFrame(frame);
  49. }
  50. }
  51. Entity::prepareTick(zDimension);
  52. }
  53. void ItemEntity::tick(const Dimension* zDimension)
  54. {
  55. Entity* zOther = Game::INSTANCE->zNearestEntity(
  56. dimensionId, location, [this](Entity* zOther) {
  57. return zOther != this
  58. && zOther->numberOfAddableItems(
  59. slot->zStack()->zItem(), NO_DIRECTION)
  60. && (!this->slot->isFull()
  61. || zOther->zType()->getId() != EntityTypeEnum::ITEM);
  62. });
  63. if (zOther)
  64. {
  65. float d = location.abstand(zOther->getPosition());
  66. if (d < 0.5f)
  67. {
  68. // add items of this entity to the other entity
  69. zOther->interactWith(this, NO_DIRECTION)
  70. .pullItems(slot->getNumberOfItems(), 0);
  71. if (slot->getNumberOfItems() == 0) onDeath(0, 0, 0);
  72. }
  73. }
  74. Entity::tick(zDimension);
  75. }
  76. void ItemEntity::onFall(float collisionSpeed)
  77. {
  78. if (collisionSpeed >= 50.f) this->setHP(0, 0, 0, 0.f);
  79. }
  80. bool ItemEntity::hasDefaultModel() const
  81. {
  82. return 0;
  83. }
  84. ModelInfo* ItemEntity::zSpecialModel() const
  85. {
  86. const ItemType* zItemType = 0;
  87. if (!slot->isEmpty()) zItemType = slot->zStack()->zItem()->zItemType();
  88. return !zItemType ? 0 : zItemType->zModel();
  89. }
  90. ItemEntityType::ItemEntityType()
  91. : EntityType()
  92. {
  93. setName("Item");
  94. }
  95. Entity* ItemEntityType::createEntity(
  96. Framework::Vec3<float> position, int dimensionId, int entityId) const
  97. {
  98. return new ItemEntity(position, dimensionId, entityId);
  99. }