Game.cpp 7.5 KB

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