Entity.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. Entity::Entity( const EntityType* zType, bool hasInventory )
  61. : Model3D(), Inventory( { 0.f, 0.f, 0.f }, hasInventory ), zEntityType( zType ), target( 0 )
  62. {}
  63. Entity::~Entity()
  64. {
  65. if( target )
  66. delete target;
  67. }
  68. bool Entity::tick( double time )
  69. {
  70. setPosition( location );
  71. // TODO: calculate rotation based on faceDir
  72. return Model3D::tick( time );
  73. }
  74. int Entity::getId() const
  75. {
  76. return id;
  77. }
  78. const EntityType* Entity::zType() const
  79. {
  80. return zEntityType;
  81. }
  82. int Entity::getCurrentDimension() const
  83. {
  84. return currentDimensionId;
  85. }