Entity.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "Entity.h"
  2. #include "Globals.h"
  3. ActionTarget::ActionTarget( Framework::Vec3<int> blockPos, Direction blockSide )
  4. : blockPos( blockPos ),
  5. targetBlockSide( blockSide ),
  6. entityId( -1 )
  7. {}
  8. ActionTarget::ActionTarget( int entityId )
  9. : entityId( entityId )
  10. {}
  11. ActionTarget* ActionTarget::load( Framework::StreamReader* zReader )
  12. {
  13. char b;
  14. zReader->lese( &b, 1 );
  15. if( b == 1 )
  16. {
  17. int id;
  18. zReader->lese( (char*)&id, 4 );
  19. return new ActionTarget( id );
  20. }
  21. else if( b == 2 )
  22. {
  23. Framework::Vec3<int> pos;
  24. Direction side;
  25. zReader->lese( (char*)&pos.x, 4 );
  26. zReader->lese( (char*)&pos.y, 4 );
  27. zReader->lese( (char*)&pos.z, 4 );
  28. zReader->lese( (char*)&side, 4 );
  29. return new ActionTarget( pos, side );
  30. }
  31. return 0;
  32. }
  33. bool ActionTarget::isBlock() const
  34. {
  35. return entityId == -1;
  36. }
  37. bool ActionTarget::isEntity() const
  38. {
  39. return entityId >= 0;
  40. }
  41. int ActionTarget::getEntityId() const
  42. {
  43. return entityId;
  44. }
  45. Framework::Vec3<int> ActionTarget::getBlockPos() const
  46. {
  47. return blockPos;
  48. }
  49. Direction ActionTarget::getBlockSide() const
  50. {
  51. return targetBlockSide;
  52. }
  53. Framework::Either<Block*, Entity*> ActionTarget::zTarget( int dimension ) const
  54. {
  55. if( entityId >= 0 )
  56. return currentGame->zEntity( entityId );
  57. else
  58. return currentGame->zBlockAt( blockPos, dimension );
  59. }
  60. Framework::Either<Block*, Entity*> ActionTarget::getTarget( int dimension ) const
  61. {
  62. if( entityId >= 0 )
  63. return currentGame->getEntity( entityId );
  64. else
  65. return currentGame->getBlockAt( blockPos, dimension );
  66. }
  67. Entity::Entity( const EntityType* zType, bool hasInventory )
  68. : Model3D(), Inventory( { 0.f, 0.f, 0.f }, hasInventory ), zEntityType( zType ), target( 0 )
  69. {}
  70. Entity::~Entity()
  71. {
  72. if( target )
  73. delete target;
  74. }
  75. bool Entity::tick( double time )
  76. {
  77. setPosition( location );
  78. // TODO: calculate rotation based on faceDir
  79. return Model3D::tick( time );
  80. }
  81. int Entity::getId() const
  82. {
  83. return id;
  84. }
  85. const EntityType* Entity::zType() const
  86. {
  87. return zEntityType;
  88. }
  89. int Entity::getCurrentDimension() const
  90. {
  91. return currentDimensionId;
  92. }
  93. void Entity::lock()
  94. {
  95. cs.lock();
  96. }
  97. void Entity::unlock()
  98. {
  99. cs.unlock();
  100. }