Game.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. #include "Game.h"
  2. #include <AsynchronCall.h>
  3. #include <Bildschirm.h>
  4. #include <DateiSystem.h>
  5. #include "Globals.h"
  6. #include "Initialisierung.h"
  7. #include "ItemBar.h"
  8. #include "StatusBars.h"
  9. Game::Game(Bildschirm* zScreen)
  10. : Menu(zScreen),
  11. recipieVisible(0),
  12. itemListContainer(0)
  13. {
  14. LTDBDatei dat;
  15. dat.setDatei(new Text("data/images/gui_icons.ltdb"));
  16. dat.leseDaten(0);
  17. Bild* search = dat.laden(0, new Text("search.png"));
  18. searchIcon = new BildZ();
  19. searchIcon->setBildZ(search);
  20. searchIcon->setStyle(
  21. BildZ::Style::Sichtbar | BildZ::Style::Alpha | BildZ::Style::Rahmen);
  22. searchIcon->setSize(20, 20);
  23. searchIcon->setRahmenFarbe(0xFF6d6d6d);
  24. elements.add(new ScreenCenter());
  25. inventoryDragController = new DragController<InventoryDragSource, int>();
  26. logout = initKnopf(10, 10, 200, 20, Knopf::Style::Normal, "Verlassen");
  27. logout->setMausEreignis([this, zScreen](void* p, void* o, MausEreignis me) {
  28. if (me.id == ME_RLinks)
  29. {
  30. logout->removeStyle(Knopf::Style::Erlaubt);
  31. zScreen->postAction([this, zScreen]() {
  32. World::INSTANCE->zClient()->leaveGame();
  33. logout->addStyle(Knopf::Style::Erlaubt);
  34. });
  35. }
  36. return 1;
  37. });
  38. elements.add(logout);
  39. debug = initTextFeld(10,
  40. 40,
  41. 500,
  42. 250,
  43. TextFeld::Style::Text | TextFeld::Style::Mehrzeilig,
  44. "");
  45. elements.add(debug);
  46. guiView = new UIMLView("<v/>", uiFactory);
  47. guiView->addKnownElement(new ItemBarElement());
  48. guiView->addKnownElement(new StatusBarsElement());
  49. guiView->setStyle(UIMLView::Style::Sichtbar);
  50. guiView->setSize(window->zBildschirm()->getBackBufferSize());
  51. elements.add(guiView);
  52. targetUIMLView = new UIMLView("<v/>", uiFactory);
  53. targetUIMLView->setStyle(
  54. UIMLView::Style::Hintergrund | UIMLView::Style::HAlpha);
  55. targetUIMLView->setHintergrundFarbe(0xA0000000);
  56. elements.add(targetUIMLView);
  57. filter = initTextFeld(zScreen->getBackBufferSize().x / 2 - 200,
  58. zScreen->getBackBufferSize().y - 200,
  59. 400,
  60. 20,
  61. Framework::TextFeld::Style::TextFeld,
  62. "");
  63. searchIcon->setPosition(
  64. filter->getX() + filter->getBreite() - 20, filter->getY());
  65. chat = new Chat();
  66. elements.add(chat);
  67. LTDBDatei iconsDat;
  68. iconsDat.setDatei(new Text("data/images/gui_icons.ltdb"));
  69. iconsDat.leseDaten(0);
  70. chatButton = uiFactory.createKnopf(uiFactory.initParam);
  71. chatButton->setToolTipText("Chat", zScreen, uiFactory.initParam.schrift);
  72. chatButton->setAlphaFeldFarbe(0x5F337AB7);
  73. chatButton->setSize(40, 40);
  74. chatButton->setPosition(5, zScreen->getBackBufferSize().y - 45);
  75. chatButton->addStyle(Framework::Knopf::Style::HBild
  76. | Framework::Knopf::Style::HAlpha
  77. | Framework::Knopf::Style::Hintergrund);
  78. chatButton->setHintergrundBildZ(iconsDat.laden(0, new Text("chat.png")));
  79. chatButton->setMausEreignis(
  80. [this](void* p, void* o, Framework::MausEreignis me) {
  81. if (me.id == ME_RLinks)
  82. {
  83. chat->addStyle(Fenster::Style::Sichtbar);
  84. chatButton->removeStyle(Knopf::Style::Sichtbar);
  85. }
  86. return 1;
  87. });
  88. elements.add(chatButton);
  89. mapWindow = new MapWindow();
  90. elements.add(mapWindow);
  91. }
  92. Game::~Game()
  93. {
  94. inventoryDragController->release();
  95. filter->release();
  96. searchIcon->release();
  97. if (itemListContainer) itemListContainer->release();
  98. }
  99. void Game::updatePosition(
  100. Vec3<float> position, bool target, Vec3<int> targetPos)
  101. {
  102. Text txt = "Position: (";
  103. txt.setPrecision(2);
  104. txt += position.x;
  105. txt += ", ";
  106. txt += position.y;
  107. txt += ", ";
  108. txt += position.z;
  109. txt += ")";
  110. if (target)
  111. {
  112. txt += "\nTarget: (";
  113. txt += targetPos.x;
  114. txt += ", ";
  115. txt += targetPos.y;
  116. txt += ", ";
  117. txt += targetPos.z;
  118. txt += ")\n";
  119. Block* b = World::INSTANCE->zBlockAt(targetPos);
  120. if (b)
  121. {
  122. txt += "TargetLight: \n";
  123. txt += b->printLightInfo();
  124. }
  125. txt += "Target Chunk: ";
  126. txt += World::INSTANCE->getChunkCenter(targetPos.x, targetPos.y).x;
  127. txt += ", ";
  128. txt += World::INSTANCE->getChunkCenter(targetPos.x, targetPos.y).y;
  129. }
  130. debug->setText(txt);
  131. }
  132. void Game::api(char* data)
  133. {
  134. switch (data[0])
  135. {
  136. case 0: // open dialog
  137. {
  138. bool exists = 0;
  139. short len = *(short*)(data + 1);
  140. char* dialogName = new char[len + 1];
  141. memcpy(dialogName, data + 3, len);
  142. dialogName[len] = 0;
  143. for (UIMLDialog* dialog : dialogs)
  144. {
  145. if (dialog->getName().istGleich(dialogName))
  146. {
  147. exists = 1;
  148. break;
  149. }
  150. }
  151. delete[] dialogName;
  152. if (!exists)
  153. {
  154. int uimlLen = *(int*)(data + 3 + len);
  155. char* uiml = new char[uimlLen + 1];
  156. memcpy(uiml, data + 7 + len, uimlLen);
  157. uiml[uimlLen] = 0;
  158. UIMLDialog* dialog
  159. = new UIMLDialog(uiml, [this](UIMLDialog* dialog) {
  160. window->zBildschirm()->postAction([this, dialog]() {
  161. int index = 0;
  162. for (UIMLDialog* d : dialogs)
  163. {
  164. if (d == dialog)
  165. {
  166. window->zBildschirm()->removeMember(d);
  167. dialogs.remove(index);
  168. World::INSTANCE->zKamera()
  169. ->setControlEnabled(
  170. dialogs.getEintragAnzahl() == 0);
  171. updateRecipieVisibility();
  172. break;
  173. }
  174. index++;
  175. }
  176. });
  177. });
  178. dialogs.add(dialog);
  179. updateRecipieVisibility();
  180. World::INSTANCE->zKamera()->setControlEnabled(0);
  181. window->zBildschirm()->addMember(dialog);
  182. delete[] uiml;
  183. }
  184. break;
  185. }
  186. case 1:
  187. { // element message
  188. for (UIMLDialog* dialog : dialogs)
  189. {
  190. dialog->api(data + 1);
  191. }
  192. short idLen = *(short*)(data + 1);
  193. char* id = new char[idLen + 1];
  194. memcpy(id, data + 3, idLen);
  195. id[idLen] = 0;
  196. NetworkAPIProcessor* processor = dynamic_cast<NetworkAPIProcessor*>(
  197. guiView->zZeichnungById(id));
  198. int processorId = *(int*)(data + 3 + idLen);
  199. if (processor
  200. && (processorId == -1 || processorId == processor->getId()))
  201. {
  202. processor->api(data + 7 + idLen);
  203. }
  204. delete[] id;
  205. break;
  206. }
  207. case 2:
  208. { // set gui
  209. int uimlLen = *(int*)(data + 1);
  210. char* uiml = new char[uimlLen + 1];
  211. memcpy(uiml, data + 5, uimlLen);
  212. uiml[uimlLen] = 0;
  213. guiView->setUIML(uiml);
  214. guiView->layout();
  215. delete[] uiml;
  216. }
  217. case 3:
  218. { // update dialog content
  219. short dialogNameLen = *(short*)(data + 1);
  220. char* dialogName = new char[dialogNameLen + 1];
  221. memcpy(dialogName, data + 3, dialogNameLen);
  222. dialogName[dialogNameLen] = 0;
  223. for (UIMLDialog* dialog : dialogs)
  224. {
  225. if (dialog->getName().istGleich(dialogName))
  226. {
  227. int uimlLen = *(int*)(data + 3 + dialogNameLen);
  228. char* uiml = new char[uimlLen + 1];
  229. memcpy(uiml, data + 7 + dialogNameLen, uimlLen);
  230. uiml[uimlLen] = 0;
  231. dialog->updateUIML(uiml);
  232. delete[] uiml;
  233. break;
  234. }
  235. }
  236. }
  237. }
  238. }
  239. void Game::closeCurrentDialog()
  240. {
  241. if (dialogs.getEintragAnzahl() > 0)
  242. {
  243. UIMLDialog* d = dialogs.get(dialogs.getEintragAnzahl() - 1);
  244. d->close();
  245. }
  246. }
  247. DragController<InventoryDragSource, int>* Game::zInventoryDragController()
  248. {
  249. return inventoryDragController;
  250. }
  251. void Game::setTargetUIML(Framework::Text uiml)
  252. {
  253. if (uiml.getLength())
  254. {
  255. window->zBildschirm()->lock();
  256. targetUIMLView->setUIML(uiml);
  257. targetUIMLView->layout();
  258. window->zBildschirm()->unlock();
  259. targetUIMLView->setSize(targetUIMLView->calculateContentSize());
  260. targetUIMLView->setPosition(
  261. window->zBildschirm()->zGraphicsApi()->getBackBufferSize()
  262. - targetUIMLView->getSize());
  263. targetUIMLView->addStyle(UIMLView::Style::Sichtbar);
  264. }
  265. else
  266. {
  267. targetUIMLView->removeStyle(UIMLView::Style::Sichtbar);
  268. }
  269. }
  270. void Game::updateRecipieVisibility()
  271. {
  272. if (!recipieVisible)
  273. {
  274. if (dialogs.getEintragAnzahl() > 0)
  275. {
  276. recipieVisible = 1;
  277. window->zBildschirm()->addMember(
  278. dynamic_cast<Zeichnung*>(filter->getThis()));
  279. window->zBildschirm()->addMember(
  280. dynamic_cast<Zeichnung*>(searchIcon->getThis()));
  281. }
  282. }
  283. else
  284. {
  285. if (dialogs.getEintragAnzahl() == 0)
  286. {
  287. recipieVisible = 0;
  288. window->zBildschirm()->removeMember(filter);
  289. window->zBildschirm()->removeMember(searchIcon);
  290. if (itemListContainer)
  291. itemListContainer->removeStyle(Fenster::Style::Sichtbar);
  292. }
  293. }
  294. }
  295. void Game::showItemList()
  296. {
  297. if (!itemListContainer)
  298. {
  299. itemListContainer = new ItemListContainer();
  300. window->zBildschirm()->addMember(
  301. dynamic_cast<Zeichnung*>(itemListContainer->getThis()));
  302. }
  303. itemListContainer->addStyle(Fenster::Style::Sichtbar);
  304. }
  305. bool Game::isItemListVisible()
  306. {
  307. return itemListContainer
  308. && itemListContainer->hatStyle(Fenster::Style::Sichtbar);
  309. }
  310. const Text* Game::zFilterText()
  311. {
  312. return filter->zText();
  313. }
  314. void Game::makeChatButtonVisible()
  315. {
  316. chatButton->addStyle(Knopf::Style::Sichtbar);
  317. }
  318. Chat* Game::zChat() const
  319. {
  320. return chat;
  321. }
  322. MapWindow* Game::zMap() const
  323. {
  324. return mapWindow;
  325. }
  326. void Game::hide()
  327. {
  328. Menu::hide();
  329. window->zBildschirm()->postAction([this]() {
  330. if (itemListContainer)
  331. {
  332. window->zBildschirm()->removeMember(itemListContainer);
  333. itemListContainer->release();
  334. itemListContainer = 0;
  335. }
  336. });
  337. }
  338. ScreenCenter::ScreenCenter()
  339. : Zeichnung()
  340. {
  341. setPosition(0, 0);
  342. setSize(uiFactory.initParam.bildschirm->getBackBufferSize());
  343. }
  344. void ScreenCenter::render(Bild& zRObj)
  345. {
  346. zRObj.drawLinieH(gr.x / 2 - 5, gr.y / 2, 10, 0xFFFFFFFF);
  347. zRObj.drawLinieV(gr.x / 2, gr.y / 2 - 5, 10, 0xFFFFFFFF);
  348. }