Game.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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(
  50. UIMLView::Style::Hintergrund | 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. itemListContainer = new ItemListContainer();
  60. window->zBildschirm()->addMember(
  61. dynamic_cast<Zeichnung*>(itemListContainer->getThis()));
  62. }
  63. Game::~Game()
  64. {
  65. inventoryDragController->release();
  66. filter->release();
  67. itemListContainer->release();
  68. }
  69. void Game::updatePosition(
  70. Vec3<float> position, bool target, Vec3<int> targetPos)
  71. {
  72. Text txt = "Position: (";
  73. txt.setPrecision(2);
  74. txt += position.x;
  75. txt += ", ";
  76. txt += position.y;
  77. txt += ", ";
  78. txt += position.z;
  79. txt += ")";
  80. if (target)
  81. {
  82. txt += "\nTarget: (";
  83. txt += targetPos.x;
  84. txt += ", ";
  85. txt += targetPos.y;
  86. txt += ", ";
  87. txt += targetPos.z;
  88. txt += ")\n";
  89. Block* b = World::INSTANCE->zBlockAt(targetPos);
  90. if (b)
  91. {
  92. txt += "TargetLight: \n";
  93. txt += b->printLightInfo();
  94. }
  95. }
  96. debug->setText(txt);
  97. }
  98. void Game::api(char* data)
  99. {
  100. switch (data[0])
  101. {
  102. case 0: // open dialog
  103. {
  104. bool exists = 0;
  105. short len = *(short*)(data + 1);
  106. char* dialogName = new char[len + 1];
  107. memcpy(dialogName, data + 3, len);
  108. dialogName[len] = 0;
  109. for (UIMLDialog* dialog : dialogs)
  110. {
  111. if (dialog->getName().istGleich(dialogName))
  112. {
  113. exists = 1;
  114. break;
  115. }
  116. }
  117. delete[] dialogName;
  118. if (!exists)
  119. {
  120. int uimlLen = *(int*)(data + 3 + len);
  121. char* uiml = new char[uimlLen + 1];
  122. memcpy(uiml, data + 7 + len, uimlLen);
  123. uiml[uimlLen] = 0;
  124. UIMLDialog* dialog
  125. = new UIMLDialog(uiml, [this](UIMLDialog* dialog) {
  126. window->zBildschirm()->postAction([this, dialog]() {
  127. int index = 0;
  128. for (UIMLDialog* d : dialogs)
  129. {
  130. if (d == dialog)
  131. {
  132. window->zBildschirm()->removeMember(d);
  133. dialogs.remove(index);
  134. World::INSTANCE->zKamera()
  135. ->setControlEnabled(
  136. dialogs.getEintragAnzahl() == 0);
  137. updateRecipieVisibility();
  138. break;
  139. }
  140. index++;
  141. }
  142. });
  143. });
  144. dialogs.add(dialog);
  145. updateRecipieVisibility();
  146. World::INSTANCE->zKamera()->setControlEnabled(0);
  147. window->zBildschirm()->addMember(dialog);
  148. delete[] uiml;
  149. }
  150. break;
  151. }
  152. case 1:
  153. { // element message
  154. for (UIMLDialog* dialog : dialogs)
  155. {
  156. dialog->api(data + 1);
  157. }
  158. short idLen = *(short*)(data + 1);
  159. char* id = new char[idLen + 1];
  160. memcpy(id, data + 3, idLen);
  161. id[idLen] = 0;
  162. NetworkAPIProcessor* processor = dynamic_cast<NetworkAPIProcessor*>(
  163. guiView->zZeichnungById(id));
  164. if (processor) processor->api(data + 3 + idLen);
  165. delete[] id;
  166. break;
  167. }
  168. case 2:
  169. { // set gui
  170. int uimlLen = *(int*)(data + 1);
  171. char* uiml = new char[uimlLen + 1];
  172. memcpy(uiml, data + 5, uimlLen);
  173. uiml[uimlLen] = 0;
  174. guiView->setUIML(uiml);
  175. guiView->layout();
  176. delete[] uiml;
  177. }
  178. }
  179. }
  180. void Game::closeCurrentDialog()
  181. {
  182. if (dialogs.getEintragAnzahl() > 0)
  183. {
  184. UIMLDialog* d = dialogs.get(dialogs.getEintragAnzahl() - 1);
  185. window->zBildschirm()->removeMember(d);
  186. dialogs.remove(dialogs.getEintragAnzahl() - 1);
  187. World::INSTANCE->zKamera()->setControlEnabled(
  188. dialogs.getEintragAnzahl() == 0);
  189. updateRecipieVisibility();
  190. }
  191. }
  192. DragController<InventoryDragSource, int>* Game::zInventoryDragController()
  193. {
  194. return inventoryDragController;
  195. }
  196. void Game::setTargetUIML(Framework::Text uiml)
  197. {
  198. if (uiml.getLength())
  199. {
  200. window->zBildschirm()->lock();
  201. targetUIMLView->setUIML(uiml);
  202. targetUIMLView->layout();
  203. window->zBildschirm()->unlock();
  204. targetUIMLView->setSize(targetUIMLView->calculateContentSize());
  205. targetUIMLView->setPosition(
  206. window->zBildschirm()->zGraphicsApi()->getBackBufferSize()
  207. - targetUIMLView->getSize());
  208. targetUIMLView->addStyle(UIMLView::Style::Sichtbar);
  209. }
  210. else
  211. {
  212. targetUIMLView->removeStyle(UIMLView::Style::Sichtbar);
  213. }
  214. }
  215. void Game::updateRecipieVisibility()
  216. {
  217. if (!recipieVisible)
  218. {
  219. if (dialogs.getEintragAnzahl() > 0)
  220. {
  221. recipieVisible = 1;
  222. window->zBildschirm()->addMember(
  223. dynamic_cast<Zeichnung*>(filter->getThis()));
  224. }
  225. }
  226. else
  227. {
  228. if (dialogs.getEintragAnzahl() == 0)
  229. {
  230. recipieVisible = 0;
  231. window->zBildschirm()->removeMember(filter);
  232. itemListContainer->removeStyle(Fenster::Style::Sichtbar);
  233. }
  234. }
  235. }
  236. void Game::showItemList()
  237. {
  238. itemListContainer->addStyle(Fenster::Style::Sichtbar);
  239. }
  240. bool Game::isItemListVisible()
  241. {
  242. return itemListContainer->hatStyle(Fenster::Style::Sichtbar);
  243. }
  244. const Text* Game::zFilterText()
  245. {
  246. return filter->zText();
  247. }