ItemEntity.cpp 2.2 KB

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