Game.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include "Game.h"
  2. #include <AsynchronCall.h>
  3. #include <Bildschirm.h>
  4. #include "Globals.h"
  5. #include "Initialisierung.h"
  6. #include "ItemBar.h"
  7. #include "StatusBars.h"
  8. Game::Game(Bildschirm* zScreen)
  9. : Menu(zScreen)
  10. {
  11. inventoryDragController = new DragController<InventoryDragSource, int>();
  12. logout = initKnopf(10, 10, 200, 20, Knopf::Style::Normal, "Verlassen");
  13. logout->setMausEreignis([this, zScreen](void* p, void* o, MausEreignis me) {
  14. if (me.id == ME_RLinks)
  15. {
  16. logout->removeStyle(Knopf::Style::Erlaubt);
  17. new AsynchronCall([this, zScreen]() {
  18. // TODO
  19. /*if (World::INSTANCE->zClient()->leaveGame())
  20. {
  21. World::INSTANCE->release();
  22. World::INSTANCE = 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,
  35. 40,
  36. 500,
  37. 250,
  38. TextFeld::Style::Text | TextFeld::Style::Mehrzeilig,
  39. "");
  40. elements.add(debug);
  41. guiView = new UIMLView("<v/>", uiFactory);
  42. guiView->addKnownElement(new ItemBarElement());
  43. guiView->addKnownElement(new StatusBarsElement());
  44. guiView->setStyle(UIMLView::Style::Sichtbar);
  45. guiView->setSize(window->zBildschirm()->getBackBufferSize());
  46. elements.add(guiView);
  47. targetUIMLView = new UIMLView("<v/>", uiFactory);
  48. targetUIMLView->setStyle(UIMLView::Style::Hintergrund
  49. | UIMLView::Style::HAlpha);
  50. targetUIMLView->setHintergrundFarbe(0xA0000000);
  51. elements.add(targetUIMLView);
  52. }
  53. Game::~Game()
  54. {
  55. inventoryDragController->release();
  56. }
  57. void Game::updatePosition(
  58. Vec3<float> position, bool target, Vec3<int> targetPos)
  59. {
  60. Text txt = "Position: (";
  61. txt.setPrecision(2);
  62. txt += position.x;
  63. txt += ", ";
  64. txt += position.y;
  65. txt += ", ";
  66. txt += position.z;
  67. txt += ")";
  68. if (target)
  69. {
  70. txt += "\nTarget: (";
  71. txt += targetPos.x;
  72. txt += ", ";
  73. txt += targetPos.y;
  74. txt += ", ";
  75. txt += targetPos.z;
  76. txt += ")\n";
  77. Block* b = World::INSTANCE->zBlockAt(targetPos);
  78. if (b)
  79. {
  80. txt += "TargetLight: \n";
  81. txt += b->printLightInfo();
  82. }
  83. }
  84. debug->setText(txt);
  85. }
  86. void Game::api(char* data)
  87. {
  88. switch (data[0])
  89. {
  90. case 0: // open dialog
  91. {
  92. bool exists = 0;
  93. short len = *(short*)(data + 1);
  94. char* dialogName = new char[len + 1];
  95. memcpy(dialogName, data + 3, len);
  96. dialogName[len] = 0;
  97. for (UIMLDialog* dialog : dialogs)
  98. {
  99. if (dialog->getName().istGleich(dialogName))
  100. {
  101. exists = 1;
  102. break;
  103. }
  104. }
  105. delete[] dialogName;
  106. if (!exists)
  107. {
  108. int uimlLen = *(int*)(data + 3 + len);
  109. char* uiml = new char[uimlLen + 1];
  110. memcpy(uiml, data + 7 + len, uimlLen);
  111. uiml[uimlLen] = 0;
  112. UIMLDialog* dialog
  113. = new UIMLDialog(uiml, [this](UIMLDialog* dialog) {
  114. logout->postAction([this, dialog]() {
  115. int index = 0;
  116. for (UIMLDialog* d : dialogs)
  117. {
  118. if (d == dialog)
  119. {
  120. dialogs.remove(index);
  121. window->zBildschirm()->removeMember(d);
  122. World::INSTANCE->zKamera()
  123. ->setControlEnabled(
  124. dialogs.getEintragAnzahl() == 0);
  125. break;
  126. }
  127. index++;
  128. }
  129. });
  130. });
  131. dialogs.add(dialog);
  132. World::INSTANCE->zKamera()->setControlEnabled(0);
  133. window->zBildschirm()->addMember(dialog);
  134. delete[] uiml;
  135. }
  136. break;
  137. }
  138. case 1:
  139. { // element message
  140. for (UIMLDialog* dialog : dialogs)
  141. {
  142. dialog->api(data + 1);
  143. }
  144. short idLen = *(short*)(data + 1);
  145. char* id = new char[idLen + 1];
  146. memcpy(id, data + 3, idLen);
  147. id[idLen] = 0;
  148. NetworkAPIProcessor* processor = dynamic_cast<NetworkAPIProcessor*>(
  149. guiView->zZeichnungById(id));
  150. if (processor) processor->api(data + 3 + idLen);
  151. delete[] id;
  152. break;
  153. }
  154. case 2:
  155. { // set gui
  156. int uimlLen = *(int*)(data + 1);
  157. char* uiml = new char[uimlLen + 1];
  158. memcpy(uiml, data + 5, uimlLen);
  159. uiml[uimlLen] = 0;
  160. guiView->setUIML(uiml);
  161. guiView->layout();
  162. delete[] uiml;
  163. }
  164. }
  165. }
  166. void Game::closeCurrentDialog()
  167. {
  168. if (dialogs.getEintragAnzahl() > 0)
  169. {
  170. UIMLDialog* d = dialogs.get(dialogs.getEintragAnzahl() - 1);
  171. window->zBildschirm()->removeMember(d);
  172. dialogs.remove(dialogs.getEintragAnzahl() - 1);
  173. World::INSTANCE->zKamera()->setControlEnabled(
  174. dialogs.getEintragAnzahl() == 0);
  175. }
  176. }
  177. DragController<InventoryDragSource, int>* Game::zInventoryDragController()
  178. {
  179. return inventoryDragController;
  180. }
  181. void Game::setTargetUIML(Framework::Text uiml)
  182. {
  183. if (uiml.getLength())
  184. {
  185. window->zBildschirm()->lock();
  186. targetUIMLView->setUIML(uiml);
  187. targetUIMLView->layout();
  188. window->zBildschirm()->unlock();
  189. targetUIMLView->setSize(targetUIMLView->calculateContentSize());
  190. targetUIMLView->setPosition(
  191. window->zBildschirm()->zGraphicsApi()->getBackBufferSize()
  192. - targetUIMLView->getSize());
  193. targetUIMLView->addStyle(UIMLView::Style::Sichtbar);
  194. }
  195. else
  196. {
  197. targetUIMLView->removeStyle(UIMLView::Style::Sichtbar);
  198. }
  199. }