ItemEntity.cpp 3.0 KB

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