Entity.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include "Entity.h"
  2. #include <Globals.h>
  3. #include <math.h>
  4. #include "Game.h"
  5. #include "Globals.h"
  6. Entity::Entity(const EntityType* zType,
  7. Framework::Model3DData* model,
  8. Framework::Model3DTexture* texture,
  9. int id,
  10. Framework::Vec3<float> position,
  11. float size)
  12. : FactoryCraftModel(),
  13. id(id),
  14. zType(zType),
  15. playerControlled(0),
  16. frameLength(0.f)
  17. {
  18. pos = position;
  19. setModelData(model);
  20. setModelTextur(texture);
  21. currentFrame.duration = 0;
  22. rend = 1;
  23. setSize(size);
  24. }
  25. Entity::~Entity() {}
  26. void Entity::api(char* message)
  27. {
  28. switch (message[0])
  29. {
  30. case 0:
  31. { // add movement frame
  32. MovementFrame frame;
  33. frame.position.x = *(float*)(message += 1);
  34. frame.position.y = *(float*)(message += 4);
  35. frame.position.z = *(float*)(message += 4);
  36. frame.rotation = *(float*)(message += 4);
  37. frame.duration = *(float*)(message += 4);
  38. framesLock.lockWrite();
  39. frameLength += frame.duration;
  40. frames.add(frame);
  41. framesLock.unlockWrite();
  42. break;
  43. }
  44. }
  45. }
  46. bool Entity::tick(double time)
  47. {
  48. double totalTime = time;
  49. if (frameLength > 1.f)
  50. {
  51. totalTime *= 1.5f;
  52. }
  53. framesLock.lockWrite();
  54. frameLength -= totalTime;
  55. framesLock.unlockWrite();
  56. framesLock.lockRead();
  57. if (frameLength < 0.f)
  58. {
  59. frameLength = 0.f;
  60. }
  61. while (totalTime > 0)
  62. {
  63. if (currentFrame.duration <= 0)
  64. {
  65. if (frames.getEntryCount() > 0)
  66. {
  67. currentFrame = frames.get(0);
  68. framesLock.lockWrite();
  69. frames.remove(0);
  70. framesLock.unlockWrite();
  71. }
  72. else
  73. {
  74. break;
  75. }
  76. }
  77. float t = min(currentFrame.duration, (float)totalTime);
  78. pos += (currentFrame.position - pos) * (t / currentFrame.duration);
  79. if (getZRotation() - currentFrame.rotation > (float)PI)
  80. {
  81. setRotationZ(getZRotation() - 2.f * (float)PI);
  82. }
  83. else if (currentFrame.rotation - getZRotation() > (float)PI)
  84. {
  85. setRotationZ(getZRotation() + 2.f * (float)PI);
  86. }
  87. setRotationZ(getZRotation()
  88. + (currentFrame.rotation - getZRotation())
  89. * (t / currentFrame.duration));
  90. currentFrame.duration -= t;
  91. totalTime -= t;
  92. if (currentFrame.duration <= 0)
  93. {
  94. pos = currentFrame.position;
  95. setRotationZ(currentFrame.rotation);
  96. }
  97. rend = 1;
  98. }
  99. framesLock.unlockRead();
  100. if (playerControlled)
  101. {
  102. World::INSTANCE->zKamera()->setPosition(
  103. pos + Vec3<float>(0.f, 0.f, 1.5f));
  104. Model3D* target = World::INSTANCE->getCurrentTarget();
  105. Block* b = target ? dynamic_cast<Block*>(target) : 0;
  106. ((Game*)(Menu*)menuRegister->get("game"))
  107. ->updatePosition(
  108. pos, b != 0, b ? b->getLocation() : Vec3<int>(0, 0, 0));
  109. if (target) target->release();
  110. }
  111. return Model3D::tick(time);
  112. }
  113. int Entity::getId() const
  114. {
  115. return id;
  116. }
  117. const EntityType* Entity::zEntityType() const
  118. {
  119. return zType;
  120. }
  121. void Entity::setPlayerControlled()
  122. {
  123. playerControlled = 1;
  124. World::INSTANCE->zKamera()->setPosition(pos + Vec3<float>(0.f, 0.f, 1.5f));
  125. }