PlayerKam.cpp 6.1 KB

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