PlayerKam.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 = movementFlags & MovementFlags::FLYING; // reset all but flying
  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
  175. = movementFlags & MovementFlags::FLYING; // reset all but flying
  176. }
  177. else
  178. {
  179. if (!kameraControll && me.id != ME_RLeft && me.id != ME_RRight)
  180. {
  181. if (me.id == ME_PLeft) setControlEnabled(1);
  182. }
  183. else
  184. {
  185. if (kameraControll || me.id == ME_RLeft || me.id == ME_RRight)
  186. {
  187. if (me.id == ME_PLeft)
  188. {
  189. char action[2] = {1, 8};
  190. World::INSTANCE->zClient()->sendPlayerAction(action, 2);
  191. }
  192. if (me.id == ME_RLeft)
  193. {
  194. char action[2] = {0, 8};
  195. World::INSTANCE->zClient()->sendPlayerAction(action, 2);
  196. }
  197. if (me.id == ME_PRight)
  198. {
  199. char action[2] = {1, 9};
  200. World::INSTANCE->zClient()->sendPlayerAction(action, 2);
  201. }
  202. if (me.id == ME_RRight)
  203. {
  204. char action[2] = {0, 9};
  205. World::INSTANCE->zClient()->sendPlayerAction(action, 2);
  206. }
  207. }
  208. }
  209. me.processed = 1;
  210. }
  211. if (lastMovementFlags != movementFlags)
  212. {
  213. World::INSTANCE->zClient()->sendPlayerMovement(movementFlags);
  214. lastMovementFlags = movementFlags;
  215. }
  216. }
  217. bool PlayerKam::tick(double time)
  218. {
  219. if (!World::INSTANCE)
  220. {
  221. return 1;
  222. }
  223. __int64 style = 0;
  224. if (hasStyle(Style::Movable)) style |= Style::Movable;
  225. if (hasStyle(Style::Rotatable)) style |= Style::Rotatable;
  226. if (hasStyle(Style::Zoomable)) style |= Style::Zoomable;
  227. removeStyle(Style::Movable | Style::Rotatable | Style::Zoomable);
  228. bool result = Cam3D::tick(time);
  229. addStyle(style);
  230. if (kameraControll)
  231. {
  232. Point dir
  233. = window->getSize() / 2 - (getMousePos() - window->getPosition());
  234. setRotation(
  235. {min(max(getRotation().x - dir.y * (float)time * 0.2f, 0.4f), 3.1f),
  236. getRotation().y,
  237. getRotation().z - dir.x * (float)time * 0.2f});
  238. if (getRotation().z > 2 * PI)
  239. setRotation({getRotation().x,
  240. getRotation().y,
  241. getRotation().z - 2.f * (float)PI});
  242. if (getRotation().z < -2 * PI)
  243. setRotation({getRotation().x,
  244. getRotation().y,
  245. getRotation().z + 2.f * (float)PI});
  246. SetCursorPos(window->getPosition().x + window->getBodySize().x / 2,
  247. window->getPosition().y + window->getBodySize().y / 2);
  248. setShowCursor(false);
  249. setMousePos(window->getPosition() + window->getSize() / 2);
  250. }
  251. Vec3<float> direction = getDirection();
  252. if (direction != lastDirection)
  253. {
  254. World::INSTANCE->zClient()->sendPlayerFaceDirection(direction);
  255. lastDirection = direction;
  256. }
  257. return 1;
  258. }
  259. void PlayerKam::setEntityId(int id)
  260. {
  261. entityId = id;
  262. }
  263. void PlayerKam::setControlEnabled(bool enabled)
  264. {
  265. kameraControll = enabled;
  266. setShowCursor(!kameraControll);
  267. if (kameraControll)
  268. {
  269. SetCursorPos(window->getPosition().x + window->getBodySize().x / 2,
  270. window->getPosition().y + window->getBodySize().y / 2);
  271. }
  272. }
  273. int PlayerKam::getEntityId() const
  274. {
  275. return entityId;
  276. }
  277. Framework::Vec3<float> PlayerKam::getDirection() const
  278. {
  279. return getWorldDirection(getScreenPos() + getScreenSize() / 2);
  280. }