ItemEntity.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include "ItemEntity.h"
  2. #include "Game.h"
  3. #include "EntityChangedUpdate.h"
  4. ItemEntity::ItemEntity( Framework::Vec3<float> location, int dimensionId, int entityId )
  5. : Entity( ItemEntityType::INSTANCE, location, dimensionId, entityId )
  6. {
  7. slot = new ItemSlot( __INT32_MAX__, 0, 0, 0, ANY_DIRECTION, 0 );
  8. addSlot( slot );
  9. faceOffset = { 0.f, 0.f, 0.f };
  10. maxHP = 10;
  11. currentHP = 10;
  12. stamina = 10;
  13. maxStamina = 10;
  14. hunger = 10;
  15. maxHunger = 10;
  16. thirst = 10;
  17. maxThirst = 10;
  18. targetDistanceLimit = 4;
  19. }
  20. void ItemEntity::tick( const Dimension* zDimension )
  21. {
  22. // add speed to next entity with free inventory
  23. Entity* zOther = Game::INSTANCE->zNearestEntity( currentDimensionId, location, [this]( Entity* zOther ) {
  24. return zOther != this && zOther->numberOfAddableItems( slot->zStack(), NO_DIRECTION );
  25. } );
  26. bool found = 1;
  27. if( zOther )
  28. {
  29. float d = location.abstand( zOther->getPosition() );
  30. if( d < 0.5f )
  31. {
  32. // add items of this entity to the other entity
  33. zOther->interactWith( this, NO_DIRECTION ).pullItems( slot->getNumberOfItems(), 0 );
  34. if( slot->getNumberOfItems() == 0 )
  35. onDeath();
  36. }
  37. else if( d < 3.f )
  38. {
  39. // accelerate towards of the other entity
  40. speed += (zOther->getPosition() - location).normalize() * (20 / (d + 0.5f)) / 30.f;
  41. }
  42. else
  43. found = 0;
  44. }
  45. else
  46. found = 0;
  47. if( !found )
  48. {
  49. speed -= speed / 30.f;
  50. if( speed.getLength() < 0.2f )
  51. speed = { 0.f, 0.f, 0.f };
  52. }
  53. Entity::tick( zDimension );
  54. }
  55. void ItemEntity::api( Framework::StreamReader* zRequest, NetworkResponse* zResponse )
  56. {
  57. }
  58. void ItemEntity::onFall( float collisionSpeed )
  59. {
  60. if( collisionSpeed >= 50.f )
  61. this->currentHP = 0;
  62. }
  63. ItemEntityType::ItemEntityType()
  64. : EntityType( ID )
  65. {}
  66. Entity* ItemEntityType::createEntity( Framework::Vec3<float> position, int dimensionId, int entityId ) const
  67. {
  68. return new ItemEntity( position, dimensionId, entityId );
  69. }