PlayerKam.cpp 5.9 KB

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