Game.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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, 250, 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 += ")\n";
  65. Block* b = currentGame->zBlockAt(targetPos);
  66. if (b)
  67. {
  68. txt += "TargetLight: \n";
  69. txt += b->printLightInfo();
  70. }
  71. }
  72. debug->setText(txt);
  73. }
  74. void Game::api(char* data)
  75. {
  76. switch (data[0])
  77. {
  78. case 0: // open dialog
  79. {
  80. bool exists = 0;
  81. short len = *(short*)(data + 1);
  82. char* dialogName = new char[len + 1];
  83. memcpy(dialogName, data + 3, len);
  84. dialogName[len] = 0;
  85. for (UIMLDialog* dialog : dialogs)
  86. {
  87. if (dialog->getName().istGleich(dialogName))
  88. {
  89. exists = 1;
  90. break;
  91. }
  92. }
  93. delete[] dialogName;
  94. if (!exists)
  95. {
  96. int uimlLen = *(int*)(data + 3 + len);
  97. char* uiml = new char[uimlLen + 1];
  98. memcpy(uiml, data + 7 + len, uimlLen);
  99. uiml[uimlLen] = 0;
  100. UIMLDialog* dialog = new UIMLDialog(uiml, [this](UIMLDialog* dialog)
  101. {
  102. logout->postAction([this, dialog]()
  103. {
  104. int index = 0;
  105. for (UIMLDialog* d : dialogs)
  106. {
  107. if (d == dialog)
  108. {
  109. dialogs.remove(index);
  110. window->zBildschirm()->removeMember(d);
  111. currentGame->zKamera()->setControlEnabled(dialogs.getEintragAnzahl() == 0);
  112. break;
  113. }
  114. index++;
  115. }
  116. });
  117. });
  118. dialogs.add(dialog);
  119. currentGame->zKamera()->setControlEnabled(0);
  120. window->zBildschirm()->addMember(dialog);
  121. delete[]uiml;
  122. }
  123. break;
  124. }
  125. case 1:
  126. { // element message
  127. for (UIMLDialog* dialog : dialogs)
  128. {
  129. dialog->api(data + 1);
  130. }
  131. short idLen = *(short*)(data + 1);
  132. char* id = new char[idLen + 1];
  133. memcpy(id, data + 3, idLen);
  134. id[idLen] = 0;
  135. NetworkAPIProcessor* processor = dynamic_cast<NetworkAPIProcessor*>(guiView->zZeichnungById(id));
  136. if (processor)
  137. processor->api(data + 3 + idLen);
  138. delete[] id;
  139. break;
  140. }
  141. case 2:
  142. { // set gui
  143. int uimlLen = *(int*)(data + 1);
  144. char* uiml = new char[uimlLen + 1];
  145. memcpy(uiml, data + 5, uimlLen);
  146. uiml[uimlLen] = 0;
  147. guiView->setUIML(uiml);
  148. guiView->layout();
  149. delete[] uiml;
  150. }
  151. }
  152. }
  153. void Game::closeCurrentDialog()
  154. {
  155. if (dialogs.getEintragAnzahl() > 0)
  156. {
  157. UIMLDialog* d = dialogs.get(dialogs.getEintragAnzahl() - 1);
  158. window->zBildschirm()->removeMember(d);
  159. dialogs.remove(dialogs.getEintragAnzahl() - 1);
  160. currentGame->zKamera()->setControlEnabled(dialogs.getEintragAnzahl() == 0);
  161. }
  162. }
  163. DragController<InventoryDragSource, int>* Game::zInventoryDragController()
  164. {
  165. return inventoryDragController;
  166. }