Entity.cpp 21 KB

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