Entity.cpp 3.3 KB

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