ItemEntity.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. Framework::Vec3<float> pos = location;
  23. // add speed to next entity with free inventory
  24. Entity* zOther = Game::INSTANCE->zNearestEntity( currentDimensionId, location, [this]( Entity* zOther ) {
  25. return zOther != this && zOther->numberOfAddableItems( slot->zStack(), NO_DIRECTION );
  26. } );
  27. bool found = 1;
  28. if( zOther )
  29. {
  30. float d = location.abstand( zOther->getPosition() );
  31. if( d < 0.5f )
  32. {
  33. // add items of this entity to the other entity
  34. zOther->interactWith( this, NO_DIRECTION ).pullItems( slot->getNumberOfItems(), 0 );
  35. if( slot->getNumberOfItems() == 0 )
  36. onDeath();
  37. }
  38. else if( d < 3.f )
  39. {
  40. // accelerate towards of the other entity
  41. speed += (zOther->getPosition() - location).normalize() * (20 / (d + 0.5f)) / 30.f;
  42. }
  43. else
  44. found = 0;
  45. }
  46. else
  47. found = 0;
  48. if( !found )
  49. {
  50. speed -= speed / 30.f;
  51. if( speed.getLength() < 0.2f )
  52. speed = { 0.f, 0.f, 0.f };
  53. }
  54. Entity::tick( zDimension );
  55. if( pos != location )
  56. {
  57. Game::INSTANCE->requestWorldUpdate( new EntityChangedUpdate( id, (Framework::Vec3<int>)location, currentDimensionId ) );
  58. }
  59. }
  60. void ItemEntity::api( Framework::StreamReader* zRequest, NetworkResponse* zResponse )
  61. {
  62. }
  63. void ItemEntity::onFall( float collisionSpeed )
  64. {
  65. if( collisionSpeed >= 50.f )
  66. this->currentHP = 0;
  67. }
  68. ItemEntityType::ItemEntityType()
  69. : EntityType( ID )
  70. {}
  71. Entity* ItemEntityType::createEntity( Framework::Vec3<float> position, int dimensionId, int entityId ) const
  72. {
  73. return new ItemEntity( position, dimensionId, entityId );
  74. }