PlayerKam.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #include "PlayerKam.h"
  2. #include <Globals.h>
  3. #include "Game.h"
  4. #include "Globals.h"
  5. class MovementFlags
  6. {
  7. public:
  8. static const int SNEAKING = 0x00001;
  9. static const int SPRINTING = 0x00002;
  10. static const int FLYING = 0x00004;
  11. static const int JUMPING = 0x00008;
  12. static const int WALK_FORWARD = 0x00010;
  13. static const int WALK_BACKWARD = 0x00020;
  14. static const int WALK_LEFT = 0x00040;
  15. static const int WALK_RIGHT = 0x00080;
  16. static const int ROTATE_LEFT = 0x00100;
  17. static const int ROTATE_RIGHT = 0x00200;
  18. static const int GROUND_CONTACT = 0x01000;
  19. };
  20. PlayerKam::PlayerKam(Framework::Bildschirm3D* zScreen)
  21. : Kam3D()
  22. {
  23. kameraControll = 0;
  24. setBildschirmPosition(0, 0);
  25. setBildschirmSize(zScreen->getBackBufferSize());
  26. setStyle(
  27. Kam3D::Style::Tick | Kam3D::Style::Movable | Kam3D::Style::Rotatable);
  28. setRotation({(float)PI / 2.f, 0, 0});
  29. lastDirection = {0, 0, 0};
  30. entityId = -1;
  31. movementFlags = 0;
  32. lastMovementFlags = 0;
  33. }
  34. void PlayerKam::setDirection(Framework::Vec3<float> direction)
  35. {
  36. if (direction.getLengthSq() > 0)
  37. {
  38. float rotZ = std::atan2(direction.y, direction.x) + (float)PI / 2;
  39. setRotation({getRotation().x, getRotation().y, rotZ});
  40. }
  41. }
  42. void PlayerKam::doTastaturEreignis(Framework::TastaturEreignis& te)
  43. {
  44. if (!World::INSTANCE)
  45. {
  46. return;
  47. }
  48. if (te.id == TE_Press)
  49. {
  50. if (te.taste[0] >= '0' && te.taste[0] <= '9')
  51. {
  52. char action[5];
  53. action[0] = 3;
  54. *(int*)(action + 1) = te.taste[0] - '1';
  55. if (*(int*)(action + 1) < 0) *(int*)(action + 1) = 9;
  56. World::INSTANCE->zClient()->sendPlayerAction(action, 5);
  57. }
  58. if (kameraControll)
  59. {
  60. if (te.virtualKey == 'W')
  61. {
  62. movementFlags |= MovementFlags::WALK_FORWARD;
  63. }
  64. if (te.virtualKey == 'S')
  65. {
  66. movementFlags |= MovementFlags::WALK_BACKWARD;
  67. }
  68. if (te.virtualKey == 'A')
  69. {
  70. movementFlags |= MovementFlags::WALK_LEFT;
  71. }
  72. if (te.virtualKey == 'D')
  73. {
  74. movementFlags |= MovementFlags::WALK_RIGHT;
  75. }
  76. if (te.virtualKey == VK_SPACE)
  77. {
  78. movementFlags |= MovementFlags::JUMPING;
  79. }
  80. if (te.virtualKey == T_Shift)
  81. {
  82. movementFlags |= MovementFlags::SPRINTING;
  83. }
  84. if (te.virtualKey == T_Strg)
  85. {
  86. movementFlags |= MovementFlags::SNEAKING;
  87. }
  88. }
  89. }
  90. if (te.id == TE_Release)
  91. {
  92. if (te.virtualKey == T_Esc)
  93. {
  94. bool oldControl = kameraControll;
  95. kameraControll = 0;
  96. movementFlags = 0;
  97. setShowCursor(true);
  98. if (!oldControl)
  99. ((Game*)(Menu*)menuRegister->get("game"))->closeCurrentDialog();
  100. }
  101. if (te.virtualKey == T_Tab)
  102. {
  103. char action = 4; // open inventory
  104. World::INSTANCE->zClient()->sendPlayerAction(&action, 1);
  105. }
  106. if (te.taste[0] == 'q')
  107. {
  108. char action = 9; // open quest dialog
  109. World::INSTANCE->zClient()->sendPlayerAction(&action, 1);
  110. }
  111. if (te.taste[0] == 'm')
  112. {
  113. ((Game*)(Menu*)menuRegister->get("game"))->zMap()->setVisibility(1);
  114. }
  115. if (te.virtualKey == 'W')
  116. {
  117. movementFlags &= ~MovementFlags::WALK_FORWARD;
  118. }
  119. if (te.virtualKey == 'S')
  120. {
  121. movementFlags &= ~MovementFlags::WALK_BACKWARD;
  122. }
  123. if (te.virtualKey == 'A')
  124. {
  125. movementFlags &= ~MovementFlags::WALK_LEFT;
  126. }
  127. if (te.virtualKey == 'D')
  128. {
  129. movementFlags &= ~MovementFlags::WALK_RIGHT;
  130. }
  131. if (te.virtualKey == VK_SPACE)
  132. {
  133. movementFlags &= ~MovementFlags::JUMPING;
  134. }
  135. if (te.virtualKey == T_Shift)
  136. {
  137. movementFlags &= ~MovementFlags::SPRINTING;
  138. }
  139. if (te.virtualKey == T_Strg)
  140. {
  141. movementFlags &= ~MovementFlags::SNEAKING;
  142. }
  143. if (te.taste[0] == 'F')
  144. {
  145. if (movementFlags & MovementFlags::FLYING)
  146. {
  147. movementFlags &= ~MovementFlags::FLYING;
  148. }
  149. else
  150. {
  151. movementFlags |= MovementFlags::FLYING;
  152. }
  153. }
  154. }
  155. if (lastMovementFlags != movementFlags)
  156. {
  157. World::INSTANCE->zClient()->sendPlayerMovement(movementFlags);
  158. lastMovementFlags = movementFlags;
  159. }
  160. }
  161. void PlayerKam::doMausEreignis(Framework::MausEreignis& me)
  162. {
  163. if (!World::INSTANCE)
  164. {
  165. return;
  166. }
  167. if (me.verarbeitet && me.id != ME_RLinks && me.id != ME_RRechts)
  168. {
  169. kameraControll = 0;
  170. setShowCursor(true);
  171. movementFlags = 0;
  172. }
  173. else
  174. {
  175. if (!kameraControll && me.id != ME_RLinks && me.id != ME_RRechts)
  176. {
  177. if (me.id == ME_PLinks) setControlEnabled(1);
  178. }
  179. else
  180. {
  181. if (kameraControll || me.id == ME_RLinks || me.id == ME_RRechts)
  182. {
  183. if (me.id == ME_PLinks)
  184. {
  185. char action[2] = {1, 8};
  186. World::INSTANCE->zClient()->sendPlayerAction(action, 2);
  187. }
  188. if (me.id == ME_RLinks)
  189. {
  190. char action[2] = {0, 8};
  191. World::INSTANCE->zClient()->sendPlayerAction(action, 2);
  192. }
  193. if (me.id == ME_PRechts)
  194. {
  195. char action[2] = {1, 9};
  196. World::INSTANCE->zClient()->sendPlayerAction(action, 2);
  197. }
  198. if (me.id == ME_RRechts)
  199. {
  200. char action[2] = {0, 9};
  201. World::INSTANCE->zClient()->sendPlayerAction(action, 2);
  202. }
  203. }
  204. }
  205. me.verarbeitet = 1;
  206. }
  207. if (lastMovementFlags != movementFlags)
  208. {
  209. World::INSTANCE->zClient()->sendPlayerMovement(movementFlags);
  210. lastMovementFlags = movementFlags;
  211. }
  212. }
  213. bool PlayerKam::tick(double time)
  214. {
  215. if (!World::INSTANCE)
  216. {
  217. return 1;
  218. }
  219. __int64 style = 0;
  220. if (hatStyle(Style::Movable)) style |= Style::Movable;
  221. if (hatStyle(Style::Rotatable)) style |= Style::Rotatable;
  222. if (hatStyle(Style::Zoomable)) style |= Style::Zoomable;
  223. removeStyle(Style::Movable | Style::Rotatable | Style::Zoomable);
  224. bool result = Kam3D::tick(time);
  225. addStyle(style);
  226. if (kameraControll)
  227. {
  228. Punkt dir
  229. = window->getGröße() / 2 - (getMausPos() - window->getPosition());
  230. setRotation(
  231. {min(max(getRotation().x - dir.y * (float)time * 0.2f, 0.4f), 3.1f),
  232. getRotation().y,
  233. getRotation().z - dir.x * (float)time * 0.2f});
  234. if (getRotation().z > 2 * PI)
  235. setRotation({getRotation().x,
  236. getRotation().y,
  237. getRotation().z - 2.f * (float)PI});
  238. if (getRotation().z < -2 * PI)
  239. setRotation({getRotation().x,
  240. getRotation().y,
  241. getRotation().z + 2.f * (float)PI});
  242. SetCursorPos(window->getPosition().x + window->getKörperGröße().x / 2,
  243. window->getPosition().y + window->getKörperGröße().y / 2);
  244. setShowCursor(false);
  245. setMausPos(window->getPosition() + window->getGröße() / 2);
  246. }
  247. Vec3<float> direction = getDirection();
  248. if (direction != lastDirection)
  249. {
  250. World::INSTANCE->zClient()->sendPlayerFaceDirection(direction);
  251. lastDirection = direction;
  252. }
  253. return 1;
  254. }
  255. void PlayerKam::setEntityId(int id)
  256. {
  257. entityId = id;
  258. }
  259. void PlayerKam::setControlEnabled(bool enabled)
  260. {
  261. kameraControll = enabled;
  262. setShowCursor(!kameraControll);
  263. if (kameraControll)
  264. {
  265. SetCursorPos(window->getPosition().x + window->getKörperGröße().x / 2,
  266. window->getPosition().y + window->getKörperGröße().y / 2);
  267. }
  268. }
  269. int PlayerKam::getEntityId() const
  270. {
  271. return entityId;
  272. }
  273. Framework::Vec3<float> PlayerKam::getDirection() const
  274. {
  275. return getWorldDirection(getScreenPos() + getScreenSize() / 2);
  276. }