Entity.cpp 3.6 KB

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