Game.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include "Game.h"
  2. #include "Initialisierung.h"
  3. #include "Globals.h"
  4. #include "ItemBar.h"
  5. #include <AsynchronCall.h>
  6. #include <Bildschirm.h>
  7. Game::Game(Bildschirm* zScreen)
  8. : Menu(zScreen)
  9. {
  10. logout = initKnopf(10, 10, 200, 20, Knopf::Style::Normal, "Verlassen");
  11. logout->setMausEreignis([this, zScreen](void* p, void* o, MausEreignis me)
  12. {
  13. if (me.id == ME_RLinks)
  14. {
  15. logout->removeStyle(Knopf::Style::Erlaubt);
  16. new AsynchronCall([this, zScreen]()
  17. {
  18. if (network->leaveGame())
  19. {
  20. currentGame->release();
  21. currentGame = 0;
  22. zScreen->lock();
  23. hide();
  24. menuRegister->get("directConnect")->show();
  25. zScreen->unlock();
  26. }
  27. logout->addStyle(Knopf::Style::Erlaubt);
  28. });
  29. }
  30. return 1;
  31. });
  32. elements.add(logout);
  33. debug = initTextFeld(10, 40, 500, 40, TextFeld::Style::Text | TextFeld::Style::Mehrzeilig, "");
  34. elements.add(debug);
  35. guiView = new UIMLView("<v/>", uiFactory);
  36. guiView->addKnownElement(new ItemBarElement());
  37. guiView->setStyle(UIMLView::Style::Sichtbar);
  38. guiView->setSize(window->zBildschirm()->getBackBufferSize());
  39. elements.add(guiView);
  40. }
  41. Game::~Game()
  42. {}
  43. void Game::updatePosition(Vec3<float> position, bool target, Vec3<int> targetPos)
  44. {
  45. Text txt = "Position: (";
  46. txt.setPrecision(2);
  47. txt += position.x;
  48. txt += ", ";
  49. txt += position.y;
  50. txt += ", ";
  51. txt += position.z;
  52. txt += ")";
  53. if (target)
  54. {
  55. txt += "\nTarget: (";
  56. txt += targetPos.x;
  57. txt += ", ";
  58. txt += targetPos.y;
  59. txt += ", ";
  60. txt += targetPos.z;
  61. txt += ")";
  62. }
  63. debug->setText(txt);
  64. }
  65. void Game::api(char* data)
  66. {
  67. switch (data[0])
  68. {
  69. case 0: // open dialog
  70. {
  71. bool exists = 0;
  72. short len = *(short*)(data + 1);
  73. char* dialogName = new char[len + 1];
  74. memcpy(dialogName, data + 3, len);
  75. dialogName[len] = 0;
  76. for (UIMLDialog* dialog : dialogs)
  77. {
  78. if (dialog->getName().istGleich(dialogName))
  79. {
  80. exists = 1;
  81. break;
  82. }
  83. }
  84. delete[] dialogName;
  85. if (!exists)
  86. {
  87. int uimlLen = *(int*)(data + 3 + len);
  88. char* uiml = new char[uimlLen + 1];
  89. memcpy(uiml, data + 7 + len, uimlLen);
  90. uiml[uimlLen] = 0;
  91. UIMLDialog* dialog = new UIMLDialog(uiml, [this](UIMLDialog* dialog)
  92. {
  93. logout->postAction([this, dialog]()
  94. {
  95. int index = 0;
  96. for (UIMLDialog* d : dialogs)
  97. {
  98. if (d == dialog)
  99. {
  100. dialogs.remove(index);
  101. window->zBildschirm()->removeMember(d);
  102. currentGame->zKamera()->setControlEnabled(dialogs.getEintragAnzahl() == 0);
  103. break;
  104. }
  105. index++;
  106. }
  107. });
  108. });
  109. dialogs.add(dialog);
  110. currentGame->zKamera()->setControlEnabled(0);
  111. window->zBildschirm()->addMember(dialog);
  112. delete[]uiml;
  113. }
  114. break;
  115. }
  116. case 1:
  117. { // element message
  118. for (UIMLDialog* dialog : dialogs)
  119. {
  120. dialog->api(data + 1);
  121. }
  122. short idLen = *(short*)(data + 1);
  123. char* id = new char[idLen + 1];
  124. memcpy(id, data + 3, idLen);
  125. id[idLen] = 0;
  126. NetworkAPIProcessor* processor = dynamic_cast<NetworkAPIProcessor*>(guiView->zZeichnungById(id));
  127. if (processor)
  128. processor->api(data + 3 + idLen);
  129. delete[] id;
  130. break;
  131. }
  132. case 2:
  133. { // set gui
  134. int uimlLen = *(int*)(data + 1);
  135. char* uiml = new char[uimlLen + 1];
  136. memcpy(uiml, data + 5, uimlLen);
  137. uiml[uimlLen] = 0;
  138. guiView->setUIML(uiml);
  139. guiView->layout();
  140. delete[] uiml;
  141. }
  142. }
  143. }
  144. void Game::closeCurrentDialog()
  145. {
  146. if (dialogs.getEintragAnzahl() > 0)
  147. {
  148. UIMLDialog* d = dialogs.get(dialogs.getEintragAnzahl() - 1);
  149. window->zBildschirm()->removeMember(d);
  150. dialogs.remove(dialogs.getEintragAnzahl() - 1);
  151. currentGame->zKamera()->setControlEnabled(dialogs.getEintragAnzahl() == 0);
  152. }
  153. }