ItemEntity.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. movementFlags |= MovementFlags::ROTATE_TO_FACE;
  27. }
  28. void ItemEntity::prepareTick(const Dimension* zDimension, double seconds)
  29. {
  30. if (slot->zStack() == 0 && !removed) throw "Illegal State exception";
  31. // TODO: implement movement towards other item entities
  32. /* if (movements.getEintragAnzahl() <= 1)
  33. {
  34. Entity* zOther = Game::INSTANCE->zNearestEntity(
  35. dimensionId, location, [this](Entity* zOther) {
  36. return zOther != this
  37. && zOther->numberOfAddableItems(
  38. slot->zStack()->zItem(), NO_DIRECTION)
  39. && (!this->slot->isFull()
  40. || zOther->zType()->getId() != EntityTypeEnum::ITEM);
  41. });
  42. if (zOther)
  43. {
  44. MovementFrame frame;
  45. frame.direction = zOther->getPosition() - getPosition();
  46. frame.duration = 0.25;
  47. frame.movementFlags = 0x1; // TODO: torn on flight mode
  48. frame.targetPosition
  49. = getPosition() + frame.direction * (0.5f * maxMovementSpeed);
  50. addMovementFrame(frame);
  51. }
  52. }*/
  53. Entity::prepareTick(zDimension, seconds);
  54. }
  55. void ItemEntity::tick(const Dimension* zDimension, double seconds)
  56. {
  57. Entity* zOther = Game::INSTANCE->zNearestEntity(
  58. dimensionId, location, [this](Entity* zOther) {
  59. return zOther != this
  60. && zOther->numberOfAddableItems(
  61. slot->zStack()->zItem(), NO_DIRECTION)
  62. && (!this->slot->isFull()
  63. || zOther->zType()->getId() != EntityTypeEnum::ITEM);
  64. });
  65. if (zOther)
  66. {
  67. float d = location.abstand(zOther->getPosition());
  68. if (d < 0.5f)
  69. {
  70. // add items of this entity to the other entity
  71. zOther->interactWith(this, NO_DIRECTION)
  72. .pullItems(slot->getNumberOfItems(), 0);
  73. if (slot->getNumberOfItems() == 0) onDeath(0, 0, 0);
  74. }
  75. }
  76. Entity::tick(zDimension, seconds);
  77. }
  78. void ItemEntity::onFall(float collisionSpeed)
  79. {
  80. if (collisionSpeed >= 50.f) this->setHP(0, 0, 0, 0.f);
  81. }
  82. bool ItemEntity::hasDefaultModel() const
  83. {
  84. return 0;
  85. }
  86. ModelInfo* ItemEntity::zSpecialModel() const
  87. {
  88. const ItemType* zItemType = 0;
  89. if (!slot->isEmpty()) zItemType = slot->zStack()->zItem()->zItemType();
  90. return !zItemType ? 0 : zItemType->zModel();
  91. }
  92. ItemEntityType::ItemEntityType()
  93. : EntityType()
  94. {
  95. setName("Item");
  96. }
  97. Entity* ItemEntityType::createEntity(
  98. Framework::Vec3<float> position, int dimensionId, int entityId) const
  99. {
  100. return new ItemEntity(position, dimensionId, entityId);
  101. }