Game.cpp 6.5 KB

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