Entity.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. void Entity::api(char* message)
  76. {
  77. // TODO: implement api
  78. }
  79. bool Entity::tick(double time)
  80. {
  81. setPosition(location);
  82. // TODO: calculate rotation based on faceDir
  83. return Model3D::tick(time);
  84. }
  85. int Entity::getId() const
  86. {
  87. return id;
  88. }
  89. const EntityType* Entity::zType() const
  90. {
  91. return zEntityType;
  92. }
  93. int Entity::getCurrentDimension() const
  94. {
  95. return currentDimensionId;
  96. }
  97. void Entity::lock()
  98. {
  99. cs.lock();
  100. }
  101. void Entity::unlock()
  102. {
  103. cs.unlock();
  104. }