PlayerKam.cpp 8.6 KB

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