Entity.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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 = World::INSTANCE->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 = World::INSTANCE->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 = World::INSTANCE->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. // collision checking
  114. Vec3<float> minP = model->getMinPos();
  115. Vec3<float> maxP = model->getMaxPos();
  116. Vec3<float> worldBoundingBox[8];
  117. worldBoundingBox[0] = applyWorldTransformation(minP);
  118. worldBoundingBox[1] = applyWorldTransformation({ minP.x, minP.y, maxP.z });
  119. worldBoundingBox[2] = applyWorldTransformation({ minP.x, maxP.y, minP.z });
  120. worldBoundingBox[3] = applyWorldTransformation({ maxP.x, minP.y, minP.z });
  121. worldBoundingBox[4] = applyWorldTransformation({ maxP.x, minP.y, maxP.z });
  122. worldBoundingBox[5] = applyWorldTransformation({ maxP.x, maxP.y, minP.z });
  123. worldBoundingBox[6] = applyWorldTransformation({ minP.x, maxP.y, maxP.z });
  124. worldBoundingBox[7] = applyWorldTransformation(maxP);
  125. Vec3<float> worldBoundingBoxFloor[8];
  126. for (int i = 0; i < 8; i++)
  127. {
  128. worldBoundingBoxFloor[i] = Vec3<float>(floor(worldBoundingBox[i].x), floor(worldBoundingBox[i].y), floor(worldBoundingBox[i].z));
  129. }
  130. Vec3<float> frameSpeed = speed * (float)time;
  131. bool hasCollided = 0;
  132. while (true)
  133. {
  134. float tf = 1.f;
  135. int collType = 0;
  136. int updateType = 0;
  137. int updateI = 0;
  138. if (frameSpeed.x > 0)
  139. {
  140. for (int i = 0; i < 8; i++)
  141. {
  142. if (abs(frameSpeed.x) >= abs(worldBoundingBoxFloor[i].x + 1.f - worldBoundingBox[i].x))
  143. {
  144. float xt = (worldBoundingBoxFloor[i].x + 1.f - worldBoundingBox[i].x) / frameSpeed.x;
  145. Vec3<float> tmp = worldBoundingBox[i] + frameSpeed * xt;
  146. if (tmp.y >= worldBoundingBoxFloor[i].y && tmp.y < worldBoundingBoxFloor[i].y + 1.f && tmp.z >= worldBoundingBoxFloor[i].z && tmp.z < worldBoundingBoxFloor[i].z + 1.f)
  147. {
  148. Block* b = World::INSTANCE->zBlockAt(Vec3<int>{ (int)worldBoundingBoxFloor[i].x + 1, (int)worldBoundingBoxFloor[i].y, (int)worldBoundingBoxFloor[i].z });
  149. if (b) // TODO: ignore passable blocks
  150. {
  151. if (xt < tf)
  152. {
  153. tf = xt;
  154. collType = 1;
  155. updateType = 0;
  156. }
  157. hasCollided = 1;
  158. }
  159. else
  160. {
  161. if (xt < tf)
  162. {
  163. tf = xt;
  164. collType = 0;
  165. updateType = 1;
  166. updateI = i;
  167. }
  168. }
  169. }
  170. }
  171. }
  172. }
  173. if (frameSpeed.x < 0)
  174. {
  175. for (int i = 0; i < 8; i++)
  176. {
  177. if (abs(frameSpeed.x) >= abs(worldBoundingBoxFloor[i].x - worldBoundingBox[i].x))
  178. {
  179. float xt = (worldBoundingBoxFloor[i].x - worldBoundingBox[i].x) / frameSpeed.x;
  180. Vec3<float> tmp = worldBoundingBox[i] + frameSpeed * xt;
  181. if (tmp.y >= worldBoundingBoxFloor[i].y && tmp.y < worldBoundingBoxFloor[i].y + 1.f && tmp.z >= worldBoundingBoxFloor[i].z && tmp.z < worldBoundingBoxFloor[i].z + 1.f)
  182. {
  183. Block* b = World::INSTANCE->zBlockAt(Vec3<int>{ (int)worldBoundingBoxFloor[i].x - 1, (int)worldBoundingBoxFloor[i].y, (int)worldBoundingBoxFloor[i].z });
  184. if (b) // TODO: ignore passable blocks
  185. {
  186. if (xt < tf)
  187. {
  188. tf = xt;
  189. collType = 1;
  190. updateType = 0;
  191. }
  192. hasCollided = 1;
  193. }
  194. else
  195. {
  196. if (xt < tf)
  197. {
  198. tf = xt;
  199. collType = 0;
  200. updateType = 1;
  201. updateI = i;
  202. }
  203. }
  204. }
  205. }
  206. }
  207. }
  208. if (frameSpeed.y > 0)
  209. {
  210. for (int i = 0; i < 8; i++)
  211. {
  212. if (abs(frameSpeed.y) >= abs(worldBoundingBoxFloor[i].y + 1.f - worldBoundingBox[i].y))
  213. {
  214. float yt = (worldBoundingBoxFloor[i].y + 1.f - worldBoundingBox[i].y) / frameSpeed.y;
  215. Vec3<float> tmp = worldBoundingBox[i] + frameSpeed * yt;
  216. if (tmp.x >= worldBoundingBoxFloor[i].x && tmp.x < worldBoundingBoxFloor[i].x + 1.f && tmp.z >= worldBoundingBoxFloor[i].z && tmp.z < worldBoundingBoxFloor[i].z + 1.f)
  217. {
  218. Block* b = World::INSTANCE->zBlockAt(Vec3<int>{ (int)worldBoundingBoxFloor[i].x, (int)worldBoundingBoxFloor[i].y + 1, (int)worldBoundingBoxFloor[i].z });
  219. if (b) // TODO: ignore passable blocks
  220. {
  221. if (yt < tf)
  222. {
  223. tf = yt;
  224. collType = 2;
  225. updateType = 0;
  226. }
  227. hasCollided = 1;
  228. }
  229. else
  230. {
  231. if (yt < tf)
  232. {
  233. tf = yt;
  234. collType = 0;
  235. updateType = 2;
  236. updateI = i;
  237. }
  238. }
  239. }
  240. }
  241. }
  242. }
  243. if (frameSpeed.y < 0)
  244. {
  245. for (int i = 0; i < 8; i++)
  246. {
  247. if (abs(frameSpeed.y) >= abs(worldBoundingBoxFloor[i].y - worldBoundingBox[i].y))
  248. {
  249. float yt = (worldBoundingBoxFloor[i].y - worldBoundingBox[i].y) / frameSpeed.y;
  250. Vec3<float> tmp = worldBoundingBox[i] + frameSpeed * yt;
  251. if (tmp.x >= worldBoundingBoxFloor[i].x && tmp.x < worldBoundingBoxFloor[i].x + 1.f && tmp.z >= worldBoundingBoxFloor[i].z && tmp.z < worldBoundingBoxFloor[i].z + 1.f)
  252. {
  253. Block* b = World::INSTANCE->zBlockAt(Vec3<int>{ (int)worldBoundingBoxFloor[i].x, (int)worldBoundingBoxFloor[i].y - 1, (int)worldBoundingBoxFloor[i].z });
  254. if (b) // TODO: ignore passable blocks
  255. {
  256. if (yt < tf)
  257. {
  258. tf = yt;
  259. collType = 2;
  260. updateType = 0;
  261. }
  262. hasCollided = 1;
  263. }
  264. else
  265. {
  266. if (yt < tf)
  267. {
  268. tf = yt;
  269. collType = 0;
  270. updateType = 2;
  271. updateI = i;
  272. }
  273. }
  274. }
  275. }
  276. }
  277. }
  278. if (frameSpeed.z > 0)
  279. {
  280. for (int i = 0; i < 8; i++)
  281. {
  282. if (abs(frameSpeed.z) >= abs(worldBoundingBoxFloor[i].z + 1.f - worldBoundingBox[i].z))
  283. {
  284. float zt = (worldBoundingBoxFloor[i].z + 1.f - worldBoundingBox[i].z) / frameSpeed.z;
  285. Vec3<float> tmp = worldBoundingBox[i] + frameSpeed * zt;
  286. if (zt <= 1.f && tmp.x >= worldBoundingBoxFloor[i].x && tmp.x < worldBoundingBoxFloor[i].x + 1.f && tmp.y >= worldBoundingBoxFloor[i].y && tmp.y < worldBoundingBoxFloor[i].y + 1.f)
  287. {
  288. Block* b = World::INSTANCE->zBlockAt(Vec3<int>{ (int)worldBoundingBoxFloor[i].x, (int)worldBoundingBoxFloor[i].y, (int)worldBoundingBoxFloor[i].z + 1 });
  289. if (b) // TODO: ignore passable blocks
  290. {
  291. if (zt < tf)
  292. {
  293. tf = zt;
  294. collType = 3;
  295. }
  296. hasCollided = 1;
  297. }
  298. else
  299. {
  300. if (zt < tf)
  301. {
  302. tf = zt;
  303. collType = 0;
  304. updateType = 3;
  305. updateI = i;
  306. }
  307. }
  308. }
  309. }
  310. }
  311. }
  312. if (frameSpeed.z < 0)
  313. {
  314. for (int i = 0; i < 8; i++)
  315. {
  316. if (abs(frameSpeed.z) >= abs(worldBoundingBoxFloor[i].z - worldBoundingBox[i].z))
  317. {
  318. float zt = (worldBoundingBoxFloor[i].z - worldBoundingBox[i].z) / frameSpeed.z;
  319. Vec3<float> tmp = worldBoundingBox[i] + frameSpeed * zt;
  320. if (tmp.x >= worldBoundingBoxFloor[i].x && tmp.x < worldBoundingBoxFloor[i].x + 1.f && tmp.y >= worldBoundingBoxFloor[i].y && tmp.y < worldBoundingBoxFloor[i].y + 1)
  321. {
  322. Block* b = World::INSTANCE->zBlockAt(Vec3<int>{ (int)worldBoundingBoxFloor[i].x, (int)worldBoundingBoxFloor[i].y, (int)worldBoundingBoxFloor[i].z - 1 });
  323. if (b) // TODO: ignore passable blocks
  324. {
  325. if (zt < tf)
  326. {
  327. tf = zt;
  328. collType = 3;
  329. updateType = 0;
  330. }
  331. hasCollided = 1;
  332. }
  333. else
  334. {
  335. if (zt < tf)
  336. {
  337. tf = zt;
  338. collType = 0;
  339. updateType = 3;
  340. updateI = i;
  341. }
  342. }
  343. }
  344. }
  345. }
  346. }
  347. if (collType == 1)
  348. frameSpeed.x = tf > 0.1f ? frameSpeed.x * (tf - 0.1f) : 0.f;
  349. if (collType == 2)
  350. frameSpeed.y = tf > 0.1f ? frameSpeed.y * (tf - 0.1f) : 0.f;
  351. if (collType == 3)
  352. frameSpeed.z = tf > 0.1f ? frameSpeed.z * (tf - 0.1f) : 0.f;
  353. if (updateType == 1)
  354. {
  355. if ((int)worldBoundingBoxFloor[updateI].x < (int)floor(worldBoundingBox[updateI].x + frameSpeed.x))
  356. worldBoundingBoxFloor[updateI].x++;
  357. if ((int)worldBoundingBoxFloor[updateI].x > (int)floor(worldBoundingBox[updateI].x + frameSpeed.x))
  358. worldBoundingBoxFloor[updateI].x--;
  359. }
  360. if (updateType == 2)
  361. {
  362. if ((int)worldBoundingBoxFloor[updateI].y < (int)floor(worldBoundingBox[updateI].y + frameSpeed.y))
  363. worldBoundingBoxFloor[updateI].y++;
  364. if ((int)worldBoundingBoxFloor[updateI].y > (int)floor(worldBoundingBox[updateI].y + frameSpeed.y))
  365. worldBoundingBoxFloor[updateI].y--;
  366. }
  367. if (updateType == 3)
  368. {
  369. if ((int)worldBoundingBoxFloor[updateI].z < (int)floor(worldBoundingBox[updateI].z + frameSpeed.z))
  370. worldBoundingBoxFloor[updateI].z++;
  371. if ((int)worldBoundingBoxFloor[updateI].z > (int)floor(worldBoundingBox[updateI].z + frameSpeed.z))
  372. worldBoundingBoxFloor[updateI].z--;
  373. }
  374. if (updateType || collType)
  375. continue;
  376. break;
  377. }
  378. pos += frameSpeed;
  379. World::INSTANCE->zKamera()->setPosition(pos + Vec3<float>(0.f, 0.f, 1.5f));
  380. Model3D* target = World::INSTANCE->getCurrentTarget();
  381. Block* b = target ? dynamic_cast<Block*>(target) : 0;
  382. ((Game*)(Menu*)menuRegister->get("game"))->updatePosition(pos, b != 0, b ? b->getLocation() : Vec3<int>(0, 0, 0));
  383. if(target)
  384. target->release();
  385. if (flags != lastFlags || direction != lastDirection || timeSinceSync >= 1 || hasCollided)
  386. {
  387. if (timeSinceSync > 0)
  388. {
  389. MovementFrame frame;
  390. frame.direction = lastDirection;
  391. frame.targetPosition = lastPos;
  392. frame.movementFlags = lastFlags;
  393. frame.duration = timeSinceSync;
  394. World::INSTANCE->zClient()->sendPlayerMovement(frame);
  395. }
  396. lastFlags = flags;
  397. lastDirection = direction;
  398. timeSinceSync = 0;
  399. }
  400. timeSinceSync += time;
  401. rend = 1;
  402. }
  403. else
  404. {
  405. double totalTime = time;
  406. while (totalTime > 0)
  407. {
  408. if (currentFrame.duration <= 0)
  409. {
  410. if (movements.getEintragAnzahl() > 0)
  411. {
  412. currentFrame = movements.get(0);
  413. cs.lock();
  414. movements.remove(0);
  415. cs.unlock();
  416. }
  417. else
  418. {
  419. break;
  420. }
  421. }
  422. double t = min(currentFrame.duration, totalTime);
  423. pos += (currentFrame.targetPosition - pos) * (float)(t / currentFrame.duration);
  424. currentFrame.duration -= t;
  425. totalTime -= t;
  426. if (currentFrame.duration <= 0)
  427. {
  428. pos = currentFrame.targetPosition;
  429. }
  430. rend = 1;
  431. }
  432. }
  433. return Model3D::tick(time);
  434. }
  435. int Entity::getId() const
  436. {
  437. return id;
  438. }
  439. const EntityType* Entity::zEntityType() const
  440. {
  441. return zType;
  442. }
  443. void Entity::lock()
  444. {
  445. cs.lock();
  446. }
  447. void Entity::unlock()
  448. {
  449. cs.unlock();
  450. }
  451. void Entity::setPlayerControlled()
  452. {
  453. playerControlled = 1;
  454. World::INSTANCE->zKamera()->setPosition(pos + Vec3<float>(0.f, 0.f, 1.5f));
  455. }