PlayerKam.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. if (te.taste[0] == 'm')
  53. {
  54. ((Game*)(Menu*)menuRegister->get("game"))->zMap()->setVisibility(1);
  55. }
  56. }
  57. }
  58. void PlayerKam::doMausEreignis(Framework::MausEreignis& me)
  59. {
  60. if (me.verarbeitet && me.id != ME_RLinks && me.id != ME_RRechts)
  61. {
  62. kameraControll = 0;
  63. setShowCursor(true);
  64. }
  65. else
  66. {
  67. if (!kameraControll && me.id != ME_RLinks && me.id != ME_RRechts)
  68. {
  69. if (me.id == ME_PLinks) setControlEnabled(1);
  70. }
  71. else
  72. {
  73. if (kameraControll || me.id == ME_RLinks || me.id == ME_RRechts)
  74. {
  75. if (me.id == ME_PLinks)
  76. {
  77. char action[2] = {1, 8};
  78. World::INSTANCE->zClient()->sendPlayerAction(action, 2);
  79. }
  80. if (me.id == ME_RLinks)
  81. {
  82. char action[2] = {0, 8};
  83. World::INSTANCE->zClient()->sendPlayerAction(action, 2);
  84. }
  85. if (me.id == ME_PRechts)
  86. {
  87. char action[2] = {1, 9};
  88. World::INSTANCE->zClient()->sendPlayerAction(action, 2);
  89. }
  90. if (me.id == ME_RRechts)
  91. {
  92. char action[2] = {0, 9};
  93. World::INSTANCE->zClient()->sendPlayerAction(action, 2);
  94. }
  95. }
  96. }
  97. me.verarbeitet = 1;
  98. }
  99. }
  100. bool PlayerKam::tick(double time)
  101. {
  102. __int64 style = 0;
  103. if (hatStyle(Style::Movable)) style |= Style::Movable;
  104. if (hatStyle(Style::Rotatable)) style |= Style::Rotatable;
  105. if (hatStyle(Style::Zoomable)) 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
  112. = window->getGröße() / 2 - (getMausPos() - window->getPosition());
  113. setRotation(
  114. {min(max(getRotation().x - dir.y * (float)time * 0.2f, 0.1f), 2.5f),
  115. getRotation().y,
  116. getRotation().z - dir.x * (float)time * 0.2f});
  117. if (getRotation().z > 2 * PI)
  118. setRotation({getRotation().x,
  119. getRotation().y,
  120. getRotation().z - 2.f * (float)PI});
  121. if (getRotation().z < -2 * PI)
  122. setRotation({getRotation().x,
  123. getRotation().y,
  124. getRotation().z + 2.f * (float)PI});
  125. SetCursorPos(window->getPosition().x + window->getKörperGröße().x / 2,
  126. window->getPosition().y + window->getKörperGröße().y / 2);
  127. setShowCursor(false);
  128. setMausPos(window->getPosition() + window->getGröße() / 2);
  129. }
  130. return 1;
  131. }
  132. void PlayerKam::setEntityId(int id)
  133. {
  134. entityId = id;
  135. }
  136. void PlayerKam::setControlEnabled(bool enabled)
  137. {
  138. kameraControll = enabled;
  139. setShowCursor(!kameraControll);
  140. if (kameraControll)
  141. {
  142. SetCursorPos(window->getPosition().x + window->getKörperGröße().x / 2,
  143. window->getPosition().y + window->getKörperGröße().y / 2);
  144. }
  145. }
  146. int PlayerKam::getEntityId() const
  147. {
  148. return entityId;
  149. }
  150. Framework::Vec3<float> PlayerKam::getDirection() const
  151. {
  152. return getWorldDirection(getScreenPos() + getScreenSize() / 2);
  153. }