PlayerKam.cpp 5.5 KB

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