PlayerKam.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include "PlayerKam.h"
  2. #include <Globals.h>
  3. #include "Game.h"
  4. #include "Globals.h"
  5. PlayerKam::PlayerKam(Framework::Bildschirm3D* zScreen)
  6. : Kam3D()
  7. {
  8. kameraControll = 0;
  9. setBildschirmPosition(0, 0);
  10. setBildschirmSize(zScreen->getBackBufferSize());
  11. setStyle(
  12. Kam3D::Style::Tick | Kam3D::Style::Movable | Kam3D::Style::Rotatable);
  13. setRotation({(float)PI / 2.f, 0, 0});
  14. entityId = -1;
  15. }
  16. void PlayerKam::setDirection(Framework::Vec3<float> direction)
  17. {
  18. if (direction.getLengthSq() > 0)
  19. {
  20. float rotZ = std::atan2(direction.y, direction.x) + (float)PI / 2;
  21. setRotation({getRotation().x, getRotation().y, rotZ});
  22. }
  23. }
  24. void PlayerKam::doTastaturEreignis(Framework::TastaturEreignis& te)
  25. {
  26. if (te.id == TE_Press)
  27. {
  28. if (te.taste[0] >= '0' && te.taste[0] <= '9')
  29. {
  30. char action[5];
  31. action[0] = 3;
  32. *(int*)(action + 1) = te.taste[0] - '1';
  33. if (*(int*)(action + 1) < 0) *(int*)(action + 1) = 9;
  34. World::INSTANCE->zClient()->sendPlayerAction(action, 5);
  35. }
  36. }
  37. if (te.id == TE_Release)
  38. {
  39. if (te.virtualKey == T_Esc)
  40. {
  41. bool oldControl = kameraControll;
  42. kameraControll = 0;
  43. setShowCursor(true);
  44. if (!oldControl)
  45. ((Game*)(Menu*)menuRegister->get("game"))->closeCurrentDialog();
  46. }
  47. if (te.virtualKey == T_Tab)
  48. {
  49. char action = 4;
  50. World::INSTANCE->zClient()->sendPlayerAction(&action, 1);
  51. }
  52. }
  53. }
  54. void PlayerKam::doMausEreignis(Framework::MausEreignis& me)
  55. {
  56. if (me.verarbeitet)
  57. {
  58. kameraControll = 0;
  59. setShowCursor(true);
  60. }
  61. else
  62. {
  63. if (!kameraControll)
  64. {
  65. if (me.id == ME_PLinks) setControlEnabled(1);
  66. }
  67. else
  68. {
  69. if (kameraControll)
  70. {
  71. if (me.id == ME_PLinks)
  72. {
  73. char action[2] = {1, 8};
  74. World::INSTANCE->zClient()->sendPlayerAction(action, 2);
  75. }
  76. if (me.id == ME_RLinks)
  77. {
  78. char action[2] = {0, 8};
  79. World::INSTANCE->zClient()->sendPlayerAction(action, 2);
  80. }
  81. if (me.id == ME_PRechts)
  82. {
  83. char action[2] = {1, 9};
  84. World::INSTANCE->zClient()->sendPlayerAction(action, 2);
  85. }
  86. if (me.id == ME_RRechts)
  87. {
  88. char action[2] = {0, 9};
  89. World::INSTANCE->zClient()->sendPlayerAction(action, 2);
  90. }
  91. }
  92. }
  93. me.verarbeitet = 1;
  94. }
  95. }
  96. bool PlayerKam::tick(double time)
  97. {
  98. __int64 style = 0;
  99. if (hatStyle(Style::Movable)) style |= Style::Movable;
  100. if (hatStyle(Style::Rotatable)) style |= Style::Rotatable;
  101. if (hatStyle(Style::Zoomable)) style |= Style::Zoomable;
  102. removeStyle(Style::Movable | Style::Rotatable | Style::Zoomable);
  103. bool result = Kam3D::tick(time);
  104. addStyle(style);
  105. if (kameraControll)
  106. {
  107. Punkt dir
  108. = window->getGröße() / 2 - (getMausPos() - window->getPosition());
  109. setRotation(
  110. {min(max(getRotation().x - dir.y * (float)time * 0.2f, 0.1f), 2.5f),
  111. getRotation().y,
  112. getRotation().z - dir.x * (float)time * 0.2f});
  113. if (getRotation().z > 2 * PI)
  114. setRotation({getRotation().x,
  115. getRotation().y,
  116. getRotation().z - 2.f * (float)PI});
  117. if (getRotation().z < -2 * PI)
  118. setRotation({getRotation().x,
  119. getRotation().y,
  120. getRotation().z + 2.f * (float)PI});
  121. SetCursorPos(window->getPosition().x + window->getKörperGröße().x / 2,
  122. window->getPosition().y + window->getKörperGröße().y / 2);
  123. setShowCursor(false);
  124. setMausPos(window->getPosition() + window->getGröße() / 2);
  125. }
  126. return result;
  127. }
  128. void PlayerKam::setEntityId(int id)
  129. {
  130. entityId = id;
  131. }
  132. void PlayerKam::setControlEnabled(bool enabled)
  133. {
  134. kameraControll = enabled;
  135. setShowCursor(!kameraControll);
  136. if (kameraControll)
  137. {
  138. SetCursorPos(window->getPosition().x + window->getKörperGröße().x / 2,
  139. window->getPosition().y + window->getKörperGröße().y / 2);
  140. }
  141. }
  142. int PlayerKam::getEntityId() const
  143. {
  144. return entityId;
  145. }
  146. Framework::Vec3<float> PlayerKam::getDirection() const
  147. {
  148. return getWorldDirection(getScreenPos() + getScreenSize() / 2);
  149. }