ItemEntity.cpp 3.0 KB

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