Entity.cpp 21 KB

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