Game.cpp 4.2 KB

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