Entity.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. #include "Entity.h"
  2. #include <Globals.h>
  3. #include "Game.h"
  4. #include "Globals.h"
  5. Entity::Entity(const EntityType* zType,
  6. Framework::Model3DData* model,
  7. Framework::Model3DTextur* texture,
  8. int id,
  9. Framework::Vec3<float> position,
  10. float maxMovementSpeed)
  11. : Model3D(),
  12. id(id),
  13. zType(zType),
  14. playerControlled(0),
  15. maxMovementSpeed(maxMovementSpeed),
  16. lastFlags(0),
  17. timeSinceSync(0),
  18. speed(0, 0, 0)
  19. {
  20. pos = position;
  21. setModelDaten(model);
  22. setModelTextur(texture);
  23. lastDirection = World::INSTANCE->zKamera()->getDirection();
  24. currentFrame.duration = 0;
  25. rend = 1;
  26. }
  27. Entity::~Entity() {}
  28. void Entity::api(char* message)
  29. {
  30. switch (message[0])
  31. {
  32. case 0:
  33. { // add movement frame
  34. if (!playerControlled)
  35. {
  36. MovementFrame frame;
  37. frame.direction.x = *(float*)(message += 1);
  38. frame.direction.y = *(float*)(message += 4);
  39. frame.direction.z = *(float*)(message += 4);
  40. frame.targetPosition.x = *(float*)(message += 4);
  41. frame.targetPosition.y = *(float*)(message += 4);
  42. frame.targetPosition.z = *(float*)(message += 4);
  43. frame.movementFlags = *(int*)(message += 4);
  44. frame.duration = *(double*)(message += 4);
  45. cs.lock();
  46. movements.add(frame);
  47. cs.unlock();
  48. }
  49. break;
  50. }
  51. case 1:
  52. { // position correction
  53. if (playerControlled)
  54. {
  55. timeSinceSync = 0;
  56. pos.x = *(float*)(message += 1);
  57. pos.y = *(float*)(message += 4);
  58. pos.z = *(float*)(message += 4);
  59. lastDirection = World::INSTANCE->zKamera()->getDirection();
  60. lastFlags = 0;
  61. }
  62. break;
  63. }
  64. }
  65. }
  66. bool Entity::tick(double time)
  67. {
  68. if (playerControlled && GetForegroundWindow() == window->getFensterHandle())
  69. {
  70. Vec3<float> direction = World::INSTANCE->zKamera()->getDirection();
  71. Vec3<float> lastPos = pos;
  72. int flags = 0;
  73. speed = {0, 0, speed.z};
  74. if (GetKeyState('w') & 0x8000 || GetKeyState('W') & 0x8000)
  75. {
  76. flags |= 1;
  77. speed += {direction.x, direction.y, 0};
  78. }
  79. if (GetKeyState('a') & 0x8000 || GetKeyState('A') & 0x8000)
  80. {
  81. flags |= 2;
  82. Vec2<float> norm = {direction.x, direction.y};
  83. norm.CCW90().normalize();
  84. speed += {norm.x, norm.y, 0};
  85. }
  86. if (GetKeyState('s') & 0x8000 || GetKeyState('S') & 0x8000)
  87. {
  88. flags |= 4;
  89. speed += {-direction.x, -direction.y, 0};
  90. }
  91. if (GetKeyState('d') & 0x8000 || GetKeyState('D') & 0x8000)
  92. {
  93. flags |= 8;
  94. Vec2<float> norm = {direction.x, direction.y};
  95. norm.CW90().normalize();
  96. speed += {norm.x, norm.y, 0};
  97. }
  98. if (GetKeyState(T_Shift) & 0x8000)
  99. {
  100. flags |= 16;
  101. speed.z = -maxMovementSpeed;
  102. }
  103. else if (GetKeyState(T_Space) & 0x8000)
  104. {
  105. flags |= 32;
  106. speed.z = maxMovementSpeed;
  107. }
  108. else
  109. {
  110. speed.z = 0.f;
  111. }
  112. Vec2<float> norm = {speed.x, speed.y};
  113. if (norm.getLengthSq() != 0)
  114. {
  115. norm.normalize();
  116. speed.x = norm.x * maxMovementSpeed;
  117. speed.y = norm.y * maxMovementSpeed;
  118. }
  119. // collision checking
  120. Vec3<float> minP = model->getMinPos();
  121. Vec3<float> maxP = model->getMaxPos();
  122. Vec3<float> worldBoundingBox[8];
  123. worldBoundingBox[0] = applyWorldTransformation(minP);
  124. worldBoundingBox[1]
  125. = applyWorldTransformation({minP.x, minP.y, maxP.z});
  126. worldBoundingBox[2]
  127. = applyWorldTransformation({minP.x, maxP.y, minP.z});
  128. worldBoundingBox[3]
  129. = applyWorldTransformation({maxP.x, minP.y, minP.z});
  130. worldBoundingBox[4]
  131. = applyWorldTransformation({maxP.x, minP.y, maxP.z});
  132. worldBoundingBox[5]
  133. = applyWorldTransformation({maxP.x, maxP.y, minP.z});
  134. worldBoundingBox[6]
  135. = applyWorldTransformation({minP.x, maxP.y, maxP.z});
  136. worldBoundingBox[7] = applyWorldTransformation(maxP);
  137. Vec3<float> worldBoundingBoxFloor[8];
  138. for (int i = 0; i < 8; i++)
  139. {
  140. worldBoundingBoxFloor[i] = Vec3<float>(floor(worldBoundingBox[i].x),
  141. floor(worldBoundingBox[i].y),
  142. floor(worldBoundingBox[i].z));
  143. }
  144. Vec3<float> frameSpeed = speed * (float)time;
  145. bool hasCollided = 0;
  146. while (true)
  147. {
  148. float tf = 1.f;
  149. int collType = 0;
  150. int updateType = 0;
  151. int updateI = 0;
  152. if (frameSpeed.x > 0)
  153. {
  154. for (int i = 0; i < 8; i++)
  155. {
  156. if (abs(frameSpeed.x) >= abs(worldBoundingBoxFloor[i].x
  157. + 1.f - worldBoundingBox[i].x))
  158. {
  159. float xt = (worldBoundingBoxFloor[i].x + 1.f
  160. - worldBoundingBox[i].x)
  161. / frameSpeed.x;
  162. Vec3<float> tmp = worldBoundingBox[i] + frameSpeed * xt;
  163. if (tmp.y >= worldBoundingBoxFloor[i].y
  164. && tmp.y < worldBoundingBoxFloor[i].y + 1.f
  165. && tmp.z >= worldBoundingBoxFloor[i].z
  166. && tmp.z < worldBoundingBoxFloor[i].z + 1.f)
  167. {
  168. Block* b = World::INSTANCE->zBlockAt(
  169. Vec3<int>{(int)worldBoundingBoxFloor[i].x + 1,
  170. (int)worldBoundingBoxFloor[i].y,
  171. (int)worldBoundingBoxFloor[i].z});
  172. if (b) // TODO: ignore passable blocks
  173. {
  174. if (xt < tf)
  175. {
  176. tf = xt;
  177. collType = 1;
  178. updateType = 0;
  179. }
  180. hasCollided = 1;
  181. }
  182. else
  183. {
  184. if (xt < tf)
  185. {
  186. tf = xt;
  187. collType = 0;
  188. updateType = 1;
  189. updateI = i;
  190. }
  191. }
  192. }
  193. }
  194. }
  195. }
  196. if (frameSpeed.x < 0)
  197. {
  198. for (int i = 0; i < 8; i++)
  199. {
  200. if (abs(frameSpeed.x) >= abs(
  201. worldBoundingBoxFloor[i].x - worldBoundingBox[i].x))
  202. {
  203. float xt = (worldBoundingBoxFloor[i].x
  204. - worldBoundingBox[i].x)
  205. / frameSpeed.x;
  206. Vec3<float> tmp = worldBoundingBox[i] + frameSpeed * xt;
  207. if (tmp.y >= worldBoundingBoxFloor[i].y
  208. && tmp.y < worldBoundingBoxFloor[i].y + 1.f
  209. && tmp.z >= worldBoundingBoxFloor[i].z
  210. && tmp.z < worldBoundingBoxFloor[i].z + 1.f)
  211. {
  212. Block* b = World::INSTANCE->zBlockAt(
  213. Vec3<int>{(int)worldBoundingBoxFloor[i].x - 1,
  214. (int)worldBoundingBoxFloor[i].y,
  215. (int)worldBoundingBoxFloor[i].z});
  216. if (b) // TODO: ignore passable blocks
  217. {
  218. if (xt < tf)
  219. {
  220. tf = xt;
  221. collType = 1;
  222. updateType = 0;
  223. }
  224. hasCollided = 1;
  225. }
  226. else
  227. {
  228. if (xt < tf)
  229. {
  230. tf = xt;
  231. collType = 0;
  232. updateType = 1;
  233. updateI = i;
  234. }
  235. }
  236. }
  237. }
  238. }
  239. }
  240. if (frameSpeed.y > 0)
  241. {
  242. for (int i = 0; i < 8; i++)
  243. {
  244. if (abs(frameSpeed.y) >= abs(worldBoundingBoxFloor[i].y
  245. + 1.f - worldBoundingBox[i].y))
  246. {
  247. float yt = (worldBoundingBoxFloor[i].y + 1.f
  248. - worldBoundingBox[i].y)
  249. / frameSpeed.y;
  250. Vec3<float> tmp = worldBoundingBox[i] + frameSpeed * yt;
  251. if (tmp.x >= worldBoundingBoxFloor[i].x
  252. && tmp.x < worldBoundingBoxFloor[i].x + 1.f
  253. && tmp.z >= worldBoundingBoxFloor[i].z
  254. && tmp.z < worldBoundingBoxFloor[i].z + 1.f)
  255. {
  256. Block* b = World::INSTANCE->zBlockAt(
  257. Vec3<int>{(int)worldBoundingBoxFloor[i].x,
  258. (int)worldBoundingBoxFloor[i].y + 1,
  259. (int)worldBoundingBoxFloor[i].z});
  260. if (b) // TODO: ignore passable blocks
  261. {
  262. if (yt < tf)
  263. {
  264. tf = yt;
  265. collType = 2;
  266. updateType = 0;
  267. }
  268. hasCollided = 1;
  269. }
  270. else
  271. {
  272. if (yt < tf)
  273. {
  274. tf = yt;
  275. collType = 0;
  276. updateType = 2;
  277. updateI = i;
  278. }
  279. }
  280. }
  281. }
  282. }
  283. }
  284. if (frameSpeed.y < 0)
  285. {
  286. for (int i = 0; i < 8; i++)
  287. {
  288. if (abs(frameSpeed.y) >= abs(
  289. worldBoundingBoxFloor[i].y - worldBoundingBox[i].y))
  290. {
  291. float yt = (worldBoundingBoxFloor[i].y
  292. - worldBoundingBox[i].y)
  293. / frameSpeed.y;
  294. Vec3<float> tmp = worldBoundingBox[i] + frameSpeed * yt;
  295. if (tmp.x >= worldBoundingBoxFloor[i].x
  296. && tmp.x < worldBoundingBoxFloor[i].x + 1.f
  297. && tmp.z >= worldBoundingBoxFloor[i].z
  298. && tmp.z < worldBoundingBoxFloor[i].z + 1.f)
  299. {
  300. Block* b = World::INSTANCE->zBlockAt(
  301. Vec3<int>{(int)worldBoundingBoxFloor[i].x,
  302. (int)worldBoundingBoxFloor[i].y - 1,
  303. (int)worldBoundingBoxFloor[i].z});
  304. if (b) // TODO: ignore passable blocks
  305. {
  306. if (yt < tf)
  307. {
  308. tf = yt;
  309. collType = 2;
  310. updateType = 0;
  311. }
  312. hasCollided = 1;
  313. }
  314. else
  315. {
  316. if (yt < tf)
  317. {
  318. tf = yt;
  319. collType = 0;
  320. updateType = 2;
  321. updateI = i;
  322. }
  323. }
  324. }
  325. }
  326. }
  327. }
  328. if (frameSpeed.z > 0)
  329. {
  330. for (int i = 0; i < 8; i++)
  331. {
  332. if (abs(frameSpeed.z) >= abs(worldBoundingBoxFloor[i].z
  333. + 1.f - worldBoundingBox[i].z))
  334. {
  335. float zt = (worldBoundingBoxFloor[i].z + 1.f
  336. - worldBoundingBox[i].z)
  337. / frameSpeed.z;
  338. Vec3<float> tmp = worldBoundingBox[i] + frameSpeed * zt;
  339. if (zt <= 1.f && tmp.x >= worldBoundingBoxFloor[i].x
  340. && tmp.x < worldBoundingBoxFloor[i].x + 1.f
  341. && tmp.y >= worldBoundingBoxFloor[i].y
  342. && tmp.y < worldBoundingBoxFloor[i].y + 1.f)
  343. {
  344. Block* b = World::INSTANCE->zBlockAt(
  345. Vec3<int>{(int)worldBoundingBoxFloor[i].x,
  346. (int)worldBoundingBoxFloor[i].y,
  347. (int)worldBoundingBoxFloor[i].z + 1});
  348. if (b) // TODO: ignore passable blocks
  349. {
  350. if (zt < tf)
  351. {
  352. tf = zt;
  353. collType = 3;
  354. }
  355. hasCollided = 1;
  356. }
  357. else
  358. {
  359. if (zt < tf)
  360. {
  361. tf = zt;
  362. collType = 0;
  363. updateType = 3;
  364. updateI = i;
  365. }
  366. }
  367. }
  368. }
  369. }
  370. }
  371. if (frameSpeed.z < 0)
  372. {
  373. for (int i = 0; i < 8; i++)
  374. {
  375. if (abs(frameSpeed.z) >= abs(
  376. worldBoundingBoxFloor[i].z - worldBoundingBox[i].z))
  377. {
  378. float zt = (worldBoundingBoxFloor[i].z
  379. - worldBoundingBox[i].z)
  380. / frameSpeed.z;
  381. Vec3<float> tmp = worldBoundingBox[i] + frameSpeed * zt;
  382. if (tmp.x >= worldBoundingBoxFloor[i].x
  383. && tmp.x < worldBoundingBoxFloor[i].x + 1.f
  384. && tmp.y >= worldBoundingBoxFloor[i].y
  385. && tmp.y < worldBoundingBoxFloor[i].y + 1)
  386. {
  387. Block* b = World::INSTANCE->zBlockAt(
  388. Vec3<int>{(int)worldBoundingBoxFloor[i].x,
  389. (int)worldBoundingBoxFloor[i].y,
  390. (int)worldBoundingBoxFloor[i].z - 1});
  391. if (b) // TODO: ignore passable blocks
  392. {
  393. if (zt < tf)
  394. {
  395. tf = zt;
  396. collType = 3;
  397. updateType = 0;
  398. }
  399. hasCollided = 1;
  400. }
  401. else
  402. {
  403. if (zt < tf)
  404. {
  405. tf = zt;
  406. collType = 0;
  407. updateType = 3;
  408. updateI = i;
  409. }
  410. }
  411. }
  412. }
  413. }
  414. }
  415. if (collType == 1)
  416. frameSpeed.x = tf > 0.1f ? frameSpeed.x * (tf - 0.1f) : 0.f;
  417. if (collType == 2)
  418. frameSpeed.y = tf > 0.1f ? frameSpeed.y * (tf - 0.1f) : 0.f;
  419. if (collType == 3)
  420. frameSpeed.z = tf > 0.1f ? frameSpeed.z * (tf - 0.1f) : 0.f;
  421. if (updateType == 1)
  422. {
  423. if ((int)worldBoundingBoxFloor[updateI].x
  424. < (int)floor(worldBoundingBox[updateI].x + frameSpeed.x))
  425. worldBoundingBoxFloor[updateI].x++;
  426. if ((int)worldBoundingBoxFloor[updateI].x
  427. > (int)floor(worldBoundingBox[updateI].x + frameSpeed.x))
  428. worldBoundingBoxFloor[updateI].x--;
  429. }
  430. if (updateType == 2)
  431. {
  432. if ((int)worldBoundingBoxFloor[updateI].y
  433. < (int)floor(worldBoundingBox[updateI].y + frameSpeed.y))
  434. worldBoundingBoxFloor[updateI].y++;
  435. if ((int)worldBoundingBoxFloor[updateI].y
  436. > (int)floor(worldBoundingBox[updateI].y + frameSpeed.y))
  437. worldBoundingBoxFloor[updateI].y--;
  438. }
  439. if (updateType == 3)
  440. {
  441. if ((int)worldBoundingBoxFloor[updateI].z
  442. < (int)floor(worldBoundingBox[updateI].z + frameSpeed.z))
  443. worldBoundingBoxFloor[updateI].z++;
  444. if ((int)worldBoundingBoxFloor[updateI].z
  445. > (int)floor(worldBoundingBox[updateI].z + frameSpeed.z))
  446. worldBoundingBoxFloor[updateI].z--;
  447. }
  448. if (updateType || collType) continue;
  449. break;
  450. }
  451. pos += frameSpeed;
  452. World::INSTANCE->zKamera()->setPosition(
  453. pos + Vec3<float>(0.f, 0.f, 1.5f));
  454. Model3D* target = World::INSTANCE->getCurrentTarget();
  455. Block* b = target ? dynamic_cast<Block*>(target) : 0;
  456. ((Game*)(Menu*)menuRegister->get("game"))
  457. ->updatePosition(
  458. pos, b != 0, b ? b->getLocation() : Vec3<int>(0, 0, 0));
  459. if (target) target->release();
  460. if (flags != lastFlags || direction != lastDirection
  461. || timeSinceSync >= 1 || hasCollided)
  462. {
  463. if (timeSinceSync > 0)
  464. {
  465. MovementFrame frame;
  466. frame.direction = lastDirection;
  467. frame.targetPosition = lastPos;
  468. frame.movementFlags = lastFlags;
  469. frame.duration = timeSinceSync;
  470. World::INSTANCE->zClient()->sendPlayerMovement(frame);
  471. }
  472. lastFlags = flags;
  473. lastDirection = direction;
  474. timeSinceSync = 0;
  475. }
  476. timeSinceSync += time;
  477. rend = 1;
  478. }
  479. else
  480. {
  481. double totalTime = time;
  482. while (totalTime > 0)
  483. {
  484. if (currentFrame.duration <= 0)
  485. {
  486. if (movements.getEintragAnzahl() > 0)
  487. {
  488. currentFrame = movements.get(0);
  489. cs.lock();
  490. movements.remove(0);
  491. cs.unlock();
  492. }
  493. else
  494. {
  495. break;
  496. }
  497. }
  498. double t = min(currentFrame.duration, totalTime);
  499. pos += (currentFrame.targetPosition - pos)
  500. * (float)(t / currentFrame.duration);
  501. currentFrame.duration -= t;
  502. totalTime -= t;
  503. if (currentFrame.duration <= 0)
  504. {
  505. pos = currentFrame.targetPosition;
  506. }
  507. rend = 1;
  508. }
  509. }
  510. return Model3D::tick(time);
  511. }
  512. int Entity::getId() const
  513. {
  514. return id;
  515. }
  516. const EntityType* Entity::zEntityType() const
  517. {
  518. return zType;
  519. }
  520. void Entity::lock()
  521. {
  522. cs.lock();
  523. }
  524. void Entity::unlock()
  525. {
  526. cs.unlock();
  527. }
  528. void Entity::setPlayerControlled()
  529. {
  530. playerControlled = 1;
  531. World::INSTANCE->zKamera()->setPosition(pos + Vec3<float>(0.f, 0.f, 1.5f));
  532. }