ItemEntity.cpp 2.3 KB

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