PlayerKam.cpp 3.9 KB

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