Entity.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #include <Globals.h>
  2. #include "Entity.h"
  3. #include "Globals.h"
  4. #include "Game.h"
  5. Entity::Entity(const EntityType* zType, Framework::Model3DData* model, Framework::Model3DTextur* texture, int id, Framework::Vec3<float> position, float maxMovementSpeed)
  6. : Model3D(), id(id), zType(zType),
  7. playerControlled(0),
  8. maxMovementSpeed(maxMovementSpeed),
  9. lastFlags(0),
  10. timeSinceSync(0),
  11. speed(0, 0, 0)
  12. {
  13. pos = position;
  14. setModelDaten(model);
  15. setModelTextur(texture);
  16. lastDirection = currentGame->zKamera()->getDirection();
  17. currentFrame.duration = 0;
  18. rend = 1;
  19. }
  20. Entity::~Entity()
  21. {}
  22. void Entity::api(char* message)
  23. {
  24. switch (message[0])
  25. {
  26. case 0:
  27. { // add movement frame
  28. if (!playerControlled)
  29. {
  30. MovementFrame frame;
  31. frame.direction.x = *(float*)(message += 1);
  32. frame.direction.y = *(float*)(message += 4);
  33. frame.direction.z = *(float*)(message += 4);
  34. frame.targetPosition.x = *(float*)(message += 4);
  35. frame.targetPosition.y = *(float*)(message += 4);
  36. frame.targetPosition.z = *(float*)(message += 4);
  37. frame.movementFlags = *(int*)(message += 4);
  38. frame.duration = *(double*)(message += 4);
  39. cs.lock();
  40. movements.add(frame);
  41. cs.unlock();
  42. }
  43. break;
  44. }
  45. case 1:
  46. { // position correction
  47. if (playerControlled)
  48. {
  49. timeSinceSync = 0;
  50. pos.x = *(float*)(message += 1);
  51. pos.y = *(float*)(message += 4);
  52. pos.z = *(float*)(message += 4);
  53. lastDirection = currentGame->zKamera()->getDirection();
  54. lastFlags = 0;
  55. }
  56. break;
  57. }
  58. }
  59. }
  60. bool Entity::tick(double time)
  61. {
  62. if (playerControlled && GetForegroundWindow() == window->getFensterHandle())
  63. {
  64. Vec3<float> direction = currentGame->zKamera()->getDirection();
  65. Vec3<float> lastPos = pos;
  66. int flags = 0;
  67. speed = { 0, 0, speed.z };
  68. if (GetKeyState('w') & 0x8000 || GetKeyState('W') & 0x8000)
  69. {
  70. flags |= 1;
  71. speed += {direction.x, direction.y, 0};
  72. }
  73. if (GetKeyState('a') & 0x8000 || GetKeyState('A') & 0x8000)
  74. {
  75. flags |= 2;
  76. Vec2<float> norm = { direction.x, direction.y };
  77. norm.CCW90().normalize();
  78. speed += {norm.x, norm.y, 0};
  79. }
  80. if (GetKeyState('s') & 0x8000 || GetKeyState('S') & 0x8000)
  81. {
  82. flags |= 4;
  83. speed += {-direction.x, -direction.y, 0};
  84. }
  85. if (GetKeyState('d') & 0x8000 || GetKeyState('D') & 0x8000)
  86. {
  87. flags |= 8;
  88. Vec2<float> norm = { direction.x, direction.y };
  89. norm.CW90().normalize();
  90. speed += {norm.x, norm.y, 0};
  91. }
  92. if (GetKeyState(T_Shift) & 0x8000)
  93. {
  94. flags |= 16;
  95. speed.z = -maxMovementSpeed;
  96. }
  97. else if (GetKeyState(T_Space) & 0x8000)
  98. {
  99. flags |= 32;
  100. speed.z = maxMovementSpeed;
  101. }
  102. else
  103. {
  104. speed.z = 0.f;
  105. }
  106. Vec2<float> norm = { speed.x, speed.y };
  107. if (norm.getLengthSq() != 0)
  108. {
  109. norm.normalize();
  110. speed.x = norm.x * maxMovementSpeed;
  111. speed.y = norm.y * maxMovementSpeed;
  112. }
  113. // TODO: collision check
  114. pos += speed * (float)time;
  115. currentGame->zKamera()->setPosition(pos + Vec3<float>(0.f, 0.f, 1.5f));
  116. ((Game*)(Menu*)menuRegister->get("game"))->updatePosition(pos, 0, { 0, 0, 0 });
  117. if (flags != lastFlags || direction != lastDirection || timeSinceSync >= 1)
  118. {
  119. if (timeSinceSync > 0)
  120. {
  121. MovementFrame frame;
  122. frame.direction = lastDirection;
  123. frame.targetPosition = lastPos;
  124. frame.movementFlags = lastFlags;
  125. frame.duration = timeSinceSync;
  126. network->zFactoryClient()->sendPlayerMovement(frame);
  127. }
  128. lastFlags = flags;
  129. lastDirection = direction;
  130. timeSinceSync = 0;
  131. }
  132. timeSinceSync += time;
  133. rend = 1;
  134. }
  135. else
  136. {
  137. double totalTime = time;
  138. while (totalTime > 0)
  139. {
  140. if (currentFrame.duration <= 0)
  141. {
  142. if (movements.getEintragAnzahl() > 0)
  143. {
  144. currentFrame = movements.get(0);
  145. cs.lock();
  146. movements.remove(0);
  147. cs.unlock();
  148. }
  149. else
  150. {
  151. break;
  152. }
  153. }
  154. double t = MIN(totalTime, currentFrame.duration);
  155. speed = { 0, 0, speed.z };
  156. if ((currentFrame.movementFlags | 1) == currentFrame.movementFlags)
  157. {
  158. speed += {currentFrame.direction.x, currentFrame.direction.y, 0};
  159. }
  160. if ((currentFrame.movementFlags | 2) == currentFrame.movementFlags)
  161. {
  162. Vec2<float> norm = { currentFrame.direction.x, currentFrame.direction.y };
  163. norm.CCW90().normalize();
  164. speed += {norm.x, norm.y, 0};
  165. }
  166. if ((currentFrame.movementFlags | 4) == currentFrame.movementFlags)
  167. {
  168. speed += {-currentFrame.direction.x, -currentFrame.direction.y, 0};
  169. }
  170. if ((currentFrame.movementFlags | 8) == currentFrame.movementFlags)
  171. {
  172. Vec2<float> norm = { currentFrame.direction.x, currentFrame.direction.y };
  173. norm.CW90().normalize();
  174. speed += {norm.x, norm.y, 0};
  175. }
  176. if ((currentFrame.movementFlags | 16) == currentFrame.movementFlags)
  177. {
  178. speed.z = -maxMovementSpeed;
  179. }
  180. else if ((currentFrame.movementFlags | 32) == currentFrame.movementFlags)
  181. {
  182. speed.z = maxMovementSpeed;
  183. }
  184. else
  185. {
  186. speed.z = 0.f;
  187. }
  188. Vec2<float> norm = { speed.x, speed.y };
  189. if (norm.getLengthSq() != 0)
  190. {
  191. norm.normalize();
  192. speed.x = norm.x * maxMovementSpeed;
  193. speed.y = norm.y * maxMovementSpeed;
  194. }
  195. // TODO: collision check
  196. pos += speed * (float)t;
  197. currentFrame.duration -= t;
  198. totalTime -= t;
  199. if (currentFrame.duration <= 0)
  200. {
  201. pos = currentFrame.targetPosition;
  202. }
  203. rend = 1;
  204. }
  205. }
  206. return Model3D::tick(time);
  207. }
  208. int Entity::getId() const
  209. {
  210. return id;
  211. }
  212. const EntityType* Entity::zEntityType() const
  213. {
  214. return zType;
  215. }
  216. void Entity::lock()
  217. {
  218. cs.lock();
  219. }
  220. void Entity::unlock()
  221. {
  222. cs.unlock();
  223. }
  224. void Entity::setPlayerControlled()
  225. {
  226. playerControlled = 1;
  227. currentGame->zKamera()->setPosition(pos + Vec3<float>(0.f, 0.f, 1.5f));
  228. }