Entity.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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::Model3DTextur* 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. setModelDaten(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. cs.lock();
  39. frameLength += frame.duration;
  40. frames.add(frame);
  41. cs.unlock();
  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. cs.lock();
  54. frameLength -= totalTime;
  55. if (frameLength < 0.f)
  56. {
  57. frameLength = 0.f;
  58. }
  59. while (totalTime > 0)
  60. {
  61. if (currentFrame.duration <= 0)
  62. {
  63. if (frames.getEintragAnzahl() > 0)
  64. {
  65. currentFrame = frames.get(0);
  66. frames.remove(0);
  67. }
  68. else
  69. {
  70. break;
  71. }
  72. }
  73. float t = min(currentFrame.duration, (float)totalTime);
  74. pos += (currentFrame.position - pos) * (t / currentFrame.duration);
  75. if (getZDrehung() - currentFrame.rotation > (float)PI)
  76. {
  77. setDrehungZ(getZDrehung() - 2.f * (float)PI);
  78. }
  79. else if (currentFrame.rotation - getZDrehung() > (float)PI)
  80. {
  81. setDrehungZ(getZDrehung() + 2.f * (float)PI);
  82. }
  83. setDrehungZ(getZDrehung()
  84. + (currentFrame.rotation - getZDrehung())
  85. * (t / currentFrame.duration));
  86. currentFrame.duration -= t;
  87. totalTime -= t;
  88. if (currentFrame.duration <= 0)
  89. {
  90. pos = currentFrame.position;
  91. setDrehungZ(currentFrame.rotation);
  92. }
  93. rend = 1;
  94. }
  95. cs.unlock();
  96. if (playerControlled)
  97. {
  98. World::INSTANCE->zKamera()->setPosition(
  99. pos + Vec3<float>(0.f, 0.f, 1.5f));
  100. Model3D* target = World::INSTANCE->getCurrentTarget();
  101. Block* b = target ? dynamic_cast<Block*>(target) : 0;
  102. ((Game*)(Menu*)menuRegister->get("game"))
  103. ->updatePosition(
  104. pos, b != 0, b ? b->getLocation() : Vec3<int>(0, 0, 0));
  105. if (target) target->release();
  106. }
  107. return Model3D::tick(time);
  108. }
  109. int Entity::getId() const
  110. {
  111. return id;
  112. }
  113. const EntityType* Entity::zEntityType() const
  114. {
  115. return zType;
  116. }
  117. void Entity::lock()
  118. {
  119. cs.lock();
  120. }
  121. void Entity::unlock()
  122. {
  123. cs.unlock();
  124. }
  125. void Entity::setPlayerControlled()
  126. {
  127. playerControlled = 1;
  128. World::INSTANCE->zKamera()->setPosition(pos + Vec3<float>(0.f, 0.f, 1.5f));
  129. }