Game.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. break;
  103. }
  104. index++;
  105. }
  106. });
  107. });
  108. dialogs.add(dialog);
  109. window->zBildschirm()->addMember(dialog);
  110. delete[]uiml;
  111. }
  112. break;
  113. }
  114. case 1:
  115. { // element message
  116. for (UIMLDialog* dialog : dialogs)
  117. {
  118. dialog->api(data + 1);
  119. }
  120. short idLen = *(short*)(data + 1);
  121. char* id = new char[idLen + 1];
  122. memcpy(id, data + 3, idLen);
  123. id[idLen] = 0;
  124. NetworkAPIProcessor* processor = dynamic_cast<NetworkAPIProcessor*>(guiView->zZeichnungById(id));
  125. if (processor)
  126. processor->api(data + 3 + idLen);
  127. delete[] id;
  128. break;
  129. }
  130. case 2:
  131. { // set gui
  132. int uimlLen = *(int*)(data + 1);
  133. char* uiml = new char[uimlLen + 1];
  134. memcpy(uiml, data + 5, uimlLen);
  135. uiml[uimlLen] = 0;
  136. guiView->setUIML(uiml);
  137. guiView->layout();
  138. delete[] uiml;
  139. }
  140. }
  141. }