PlayerKam.cpp 8.5 KB

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