Game.cpp 4.0 KB

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