PlayerKam.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #include <Globals.h>
  2. #include "PlayerKam.h"
  3. #include "Globals.h"
  4. #include "Game.h"
  5. PlayerKam::PlayerKam(Framework::Bildschirm3D* zScreen)
  6. : Kam3D()
  7. {
  8. kameraControll = 0;
  9. setBildschirmPosition(0, 0);
  10. setBildschirmSize(zScreen->getBackBufferSize());
  11. setStyle(Kam3D::Style::Tick | Kam3D::Style::Movable | Kam3D::Style::Rotatable);
  12. setRotation({ (float)PI / 2.f, 0, 0 });
  13. entityId = -1;
  14. transmittedDirection = getWorldDirection(getScreenPos() + getScreenSize() / 2);
  15. timeSiceTransmision = 0;
  16. }
  17. void PlayerKam::setDirection(Framework::Vec3<float> direction)
  18. {
  19. if (direction.getLengthSq() > 0)
  20. {
  21. float rotZ = std::atan2(direction.y, direction.x) + (float)PI / 2;
  22. setRotation({ getRotation().x, getRotation().y, rotZ });
  23. }
  24. }
  25. void PlayerKam::doTastaturEreignis(Framework::TastaturEreignis& te)
  26. {
  27. char action[2];
  28. if (te.id == TE_Press)
  29. {
  30. action[0] = 1;
  31. if (te.taste == 'w' || te.taste == 'W')
  32. {
  33. action[1] = 0;
  34. network->zFactoryClient()->sendPlayerAction(action, 2);
  35. }
  36. if (te.taste == 'a' || te.taste == 'A')
  37. {
  38. action[1] = 1;
  39. network->zFactoryClient()->sendPlayerAction(action, 2);
  40. }
  41. if (te.taste == 's' || te.taste == 'S')
  42. {
  43. action[1] = 2;
  44. network->zFactoryClient()->sendPlayerAction(action, 2);
  45. }
  46. if (te.taste == 'd' || te.taste == 'D')
  47. {
  48. action[1] = 3;
  49. network->zFactoryClient()->sendPlayerAction(action, 2);
  50. }
  51. if (te.taste == T_Shift)
  52. {
  53. action[1] = 4;
  54. network->zFactoryClient()->sendPlayerAction(action, 2);
  55. }
  56. if (te.taste == 'q' || te.taste == 'Q')
  57. {
  58. action[1] = 5;
  59. network->zFactoryClient()->sendPlayerAction(action, 2);
  60. }
  61. if (te.taste == 'e' || te.taste == 'E')
  62. {
  63. action[1] = 6;
  64. network->zFactoryClient()->sendPlayerAction(action, 2);
  65. }
  66. if (te.taste == T_Space)
  67. {
  68. action[1] = 7;
  69. network->zFactoryClient()->sendPlayerAction(action, 2);
  70. }
  71. if (te.taste >= '0' && te.taste <= '9')
  72. {
  73. char action[5];
  74. action[0] = 3;
  75. *(int*)(action + 1) = te.taste - '1';
  76. if (*(int*)(action + 1) < 0)
  77. *(int*)(action + 1) = 9;
  78. network->zFactoryClient()->sendPlayerAction(action, 5);
  79. }
  80. }
  81. if (te.id == TE_Release)
  82. {
  83. action[0] = 0;
  84. if (te.taste == 'w' || te.taste == 'W')
  85. {
  86. action[1] = 0;
  87. network->zFactoryClient()->sendPlayerAction(action, 2);
  88. }
  89. if (te.taste == 'a' || te.taste == 'A')
  90. {
  91. action[1] = 1;
  92. network->zFactoryClient()->sendPlayerAction(action, 2);
  93. }
  94. if (te.taste == 's' || te.taste == 'S')
  95. {
  96. action[1] = 2;
  97. network->zFactoryClient()->sendPlayerAction(action, 2);
  98. }
  99. if (te.taste == 'd' || te.taste == 'D')
  100. {
  101. action[1] = 3;
  102. network->zFactoryClient()->sendPlayerAction(action, 2);
  103. }
  104. if (te.taste == T_Shift)
  105. {
  106. action[1] = 4;
  107. network->zFactoryClient()->sendPlayerAction(action, 2);
  108. }
  109. if (te.taste == 'q' || te.taste == 'Q')
  110. {
  111. action[1] = 5;
  112. network->zFactoryClient()->sendPlayerAction(action, 2);
  113. }
  114. if (te.taste == 'e' || te.taste == 'E')
  115. {
  116. action[1] = 6;
  117. network->zFactoryClient()->sendPlayerAction(action, 2);
  118. }
  119. if (te.taste == T_Space)
  120. {
  121. action[1] = 7;
  122. network->zFactoryClient()->sendPlayerAction(action, 2);
  123. }
  124. if (te.taste == T_Esc)
  125. {
  126. bool oldControl = kameraControll;
  127. kameraControll = 0;
  128. setShowCursor(true);
  129. if (!oldControl)
  130. ((Game*)(Menu*)menuRegister->get("game"))->closeCurrentDialog();
  131. }
  132. if (te.taste == T_Tab)
  133. {
  134. action[0] = 4;
  135. network->zFactoryClient()->sendPlayerAction(action, 1);
  136. }
  137. }
  138. }
  139. void PlayerKam::doMausEreignis(Framework::MausEreignis& me)
  140. {
  141. if (me.verarbeitet)
  142. {
  143. kameraControll = 0;
  144. setShowCursor(true);
  145. }
  146. else
  147. {
  148. if (!kameraControll)
  149. {
  150. if (me.id == ME_PLinks)
  151. setControlEnabled(1);
  152. }
  153. else
  154. {
  155. if (kameraControll)
  156. {
  157. if (me.id == ME_PLinks)
  158. {
  159. char action[2] = { 1, 8 };
  160. network->zFactoryClient()->sendPlayerAction(action, 2);
  161. }
  162. if (me.id == ME_RLinks)
  163. {
  164. char action[2] = { 0, 8 };
  165. network->zFactoryClient()->sendPlayerAction(action, 2);
  166. }
  167. if (me.id == ME_PRechts)
  168. {
  169. char action[2] = { 1, 9 };
  170. network->zFactoryClient()->sendPlayerAction(action, 2);
  171. }
  172. if (me.id == ME_RRechts)
  173. {
  174. char action[2] = { 0, 9 };
  175. network->zFactoryClient()->sendPlayerAction(action, 2);
  176. }
  177. }
  178. }
  179. me.verarbeitet = 1;
  180. }
  181. }
  182. bool PlayerKam::tick(double time)
  183. {
  184. __int64 style = 0;
  185. if (hatStyle(Style::Movable))
  186. style |= Style::Movable;
  187. if (hatStyle(Style::Rotatable))
  188. style |= Style::Rotatable;
  189. if (hatStyle(Style::Zoomable))
  190. style |= Style::Zoomable;
  191. removeStyle(Style::Movable | Style::Rotatable | Style::Zoomable);
  192. bool result = Kam3D::tick(time);
  193. addStyle(style);
  194. if (entityId >= 0)
  195. {
  196. Entity* entity = currentGame->zEntity(entityId);
  197. if (entity)
  198. {
  199. // update camera position
  200. setPosition(entity->getPos() + Vec3<float>(0.f, 0.f, 1.5f));
  201. ((Game*)(Menu*)menuRegister->get("game"))->updatePosition(entity->getPos(), 0, { 0, 0, 0 });
  202. }
  203. }
  204. if (kameraControll)
  205. {
  206. Punkt dir = window->getGröße() / 2 - (getMausPos() - window->getPosition());
  207. setRotation({ min(max(getRotation().x - dir.y * (float)time * 0.2f, 0.1f), 2.5f), getRotation().y, getRotation().z - dir.x * (float)time * 0.2f });
  208. if (getRotation().z > 2 * PI)
  209. setRotation({ getRotation().x, getRotation().y, getRotation().z - 2.f * (float)PI });
  210. if (getRotation().z < -2 * PI)
  211. setRotation({ getRotation().x, getRotation().y, getRotation().z + 2.f * (float)PI });
  212. SetCursorPos(window->getPosition().x + window->getKörperGröße().x / 2, window->getPosition().y + window->getKörperGröße().y / 2);
  213. setShowCursor(false);
  214. setMausPos(window->getPosition() + window->getGröße() / 2);
  215. }
  216. timeSiceTransmision += time;
  217. if (timeSiceTransmision > 0.5 && transmittedDirection != getWorldDirection(getScreenPos() + getScreenSize() / 2))
  218. {
  219. transmittedDirection = getWorldDirection(getScreenPos() + getScreenSize() / 2);
  220. char action[13];
  221. action[0] = 2;
  222. *(float*)(action + 1) = transmittedDirection.x;
  223. *(float*)(action + 5) = transmittedDirection.y;
  224. *(float*)(action + 9) = transmittedDirection.z;
  225. network->zFactoryClient()->sendPlayerAction(action, 13);
  226. timeSiceTransmision = 0;
  227. }
  228. return result;
  229. }
  230. void PlayerKam::setEntityId(int id)
  231. {
  232. entityId = id;
  233. }
  234. void PlayerKam::setControlEnabled(bool enabled)
  235. {
  236. kameraControll = enabled;
  237. setShowCursor(!kameraControll);
  238. if (kameraControll)
  239. {
  240. SetCursorPos(window->getPosition().x + window->getKörperGröße().x / 2, window->getPosition().y + window->getKörperGröße().y / 2);
  241. }
  242. }