Game.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. #include "Game.h"
  2. #include <AsynchronCall.h>
  3. #include <FileSystem.h>
  4. #include <Screen.h>
  5. #include "Globals.h"
  6. #include "Initialization.h"
  7. #include "ItemBar.h"
  8. #include "StatusBars.h"
  9. Game::Game(Screen* zScreen)
  10. : Menu(zScreen),
  11. recipieVisible(0),
  12. itemListContainer(0)
  13. {
  14. LTDBFile dat;
  15. dat.setFile(new Text("data/images/gui_icons.ltdb"));
  16. dat.readData(0);
  17. Image* search = dat.load(0, new Text("search.png"));
  18. searchIcon = new ImageView();
  19. searchIcon->setImageZ(search);
  20. searchIcon->setStyle(ImageView::Style::Visible | ImageView::Style::Alpha
  21. | ImageView::Style::Border);
  22. searchIcon->setSize(20, 20);
  23. searchIcon->setBorderColor(0xFF6d6d6d);
  24. elements.add(new ::ScreenCenter());
  25. inventoryDragController = new DragController<InventoryDragSource, int>();
  26. logout = initButton(10, 10, 200, 20, Button::Style::Normal, "Verlassen");
  27. logout->setMouseEvent([this, zScreen](void* p, void* o, MouseEvent me) {
  28. if (me.id == ME_RLeft)
  29. {
  30. logout->removeStyle(Button::Style::Allowed);
  31. zScreen->postAction([this, zScreen]() {
  32. World::INSTANCE->zClient()->leaveGame();
  33. logout->addStyle(Button::Style::Allowed);
  34. });
  35. }
  36. return 1;
  37. });
  38. elements.add(logout);
  39. debug = initTextField(10,
  40. 40,
  41. 500,
  42. 250,
  43. TextField::Style::Text | TextField::Style::Multiline,
  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::Visible);
  50. guiView->setSize(window->zScreen()->getBackBufferSize());
  51. elements.add(guiView);
  52. targetUIMLView = new UIMLView("<v/>", uiFactory);
  53. targetUIMLView->setStyle(
  54. UIMLView::Style::Background | UIMLView::Style::BAlpha);
  55. targetUIMLView->setBackgroundColor(0xA0000000);
  56. elements.add(targetUIMLView);
  57. filter = initTextField(zScreen->getBackBufferSize().x / 2 - 200,
  58. zScreen->getBackBufferSize().y - 200,
  59. 400,
  60. 20,
  61. Framework::TextField::Style::TextField,
  62. "");
  63. searchIcon->setPosition(
  64. filter->getX() + filter->getWidth() - 20, filter->getY());
  65. chat = new Chat();
  66. elements.add(chat);
  67. LTDBFile iconsDat;
  68. iconsDat.setFile(new Text("data/images/gui_icons.ltdb"));
  69. iconsDat.readData(0);
  70. chatButton = uiFactory.createButton(uiFactory.initParam);
  71. chatButton->setToolTipText("Chat", zScreen, uiFactory.initParam.font);
  72. chatButton->setAlphaFieldColor(0x5F337AB7);
  73. chatButton->setSize(40, 40);
  74. chatButton->setPosition(5, zScreen->getBackBufferSize().y - 45);
  75. chatButton->addStyle(Framework::Button::Style::BImage
  76. | Framework::Button::Style::BAlpha
  77. | Framework::Button::Style::Background);
  78. chatButton->setBackgroundImageZ(iconsDat.load(0, new Text("chat.png")));
  79. chatButton->setMouseEvent(
  80. [this](void* p, void* o, Framework::MouseEvent me) {
  81. if (me.id == ME_RLeft)
  82. {
  83. chat->addStyle(Window::Style::Visible);
  84. chatButton->removeStyle(Button::Style::Visible);
  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, double tickTime)
  101. {
  102. fpsHistory.add(tickTime);
  103. double fpsSum = 0;
  104. for (double d : fpsHistory)
  105. {
  106. fpsSum += d;
  107. }
  108. while (fpsSum > 1)
  109. {
  110. fpsSum -= fpsHistory.get(0);
  111. fpsHistory.remove(0);
  112. }
  113. Text txt = "";
  114. txt.append() << "FPS: " << fpsHistory.getEntryCount() << "\n"
  115. << "Position: (";
  116. txt.setPrecision(2);
  117. txt += position.x;
  118. txt += ", ";
  119. txt += position.y;
  120. txt += ", ";
  121. txt += position.z;
  122. txt += ")";
  123. if (target)
  124. {
  125. txt += "\nTarget: (";
  126. txt += targetPos.x;
  127. txt += ", ";
  128. txt += targetPos.y;
  129. txt += ", ";
  130. txt += targetPos.z;
  131. txt += ")\n";
  132. Block* b = World::INSTANCE->zBlockAt(targetPos);
  133. if (b)
  134. {
  135. txt += "TargetLight: \n";
  136. txt += b->printLightInfo();
  137. }
  138. txt += "Target Chunk: ";
  139. txt += World::INSTANCE->getChunkCenter(targetPos.x, targetPos.y).x;
  140. txt += ", ";
  141. txt += World::INSTANCE->getChunkCenter(targetPos.x, targetPos.y).y;
  142. }
  143. debug->setText(txt);
  144. }
  145. void Game::api(char* data)
  146. {
  147. switch (data[0])
  148. {
  149. case 0: // open dialog
  150. {
  151. bool exists = 0;
  152. short len = *(short*)(data + 1);
  153. char* dialogName = new char[len + 1];
  154. memcpy(dialogName, data + 3, len);
  155. dialogName[len] = 0;
  156. dialogLock.lockRead();
  157. for (UIMLDialog* dialog : dialogs)
  158. {
  159. if (dialog->getName().isEqual(dialogName))
  160. {
  161. exists = 1;
  162. break;
  163. }
  164. }
  165. dialogLock.unlockRead();
  166. delete[] dialogName;
  167. if (!exists)
  168. {
  169. int uimlLen = *(int*)(data + 3 + len);
  170. char* uiml = new char[uimlLen + 1];
  171. memcpy(uiml, data + 7 + len, uimlLen);
  172. uiml[uimlLen] = 0;
  173. window->zScreen()->postAction([this, uiml]() {
  174. UIMLDialog* dialog
  175. = new UIMLDialog(uiml, [this](UIMLDialog* dialog) {
  176. window->zScreen()->postAction([this, dialog]() {
  177. int index = 0;
  178. dialogLock.lockWrite();
  179. for (UIMLDialog* d : dialogs)
  180. {
  181. if (d == dialog)
  182. {
  183. window->zScreen()->removeMember(d);
  184. dialogs.remove(index);
  185. World::INSTANCE->zKamera()
  186. ->setControlEnabled(
  187. dialogs.getEntryCount() == 0);
  188. updateRecipieVisibility();
  189. break;
  190. }
  191. index++;
  192. }
  193. dialogLock.unlockWrite();
  194. });
  195. });
  196. dialogLock.lockWrite();
  197. dialogs.add(dialog);
  198. dialogLock.unlockWrite();
  199. updateRecipieVisibility();
  200. World::INSTANCE->zKamera()->setControlEnabled(0);
  201. window->zScreen()->addMember(dialog);
  202. delete[] uiml;
  203. });
  204. }
  205. break;
  206. }
  207. case 1:
  208. { // element message
  209. dialogLock.lockRead();
  210. for (UIMLDialog* dialog : dialogs)
  211. {
  212. dialog->api(data + 1);
  213. }
  214. dialogLock.unlockRead();
  215. short idLen = *(short*)(data + 1);
  216. char* id = new char[idLen + 1];
  217. memcpy(id, data + 3, idLen);
  218. id[idLen] = 0;
  219. NetworkAPIProcessor* processor = dynamic_cast<NetworkAPIProcessor*>(
  220. guiView->zDrawableById(id));
  221. int processorId = *(int*)(data + 3 + idLen);
  222. if (processor
  223. && (processorId == -1 || processorId == processor->getId()))
  224. {
  225. processor->api(data + 7 + idLen);
  226. }
  227. delete[] id;
  228. break;
  229. }
  230. case 2:
  231. { // set gui
  232. int uimlLen = *(int*)(data + 1);
  233. char* uiml = new char[uimlLen + 1];
  234. memcpy(uiml, data + 5, uimlLen);
  235. uiml[uimlLen] = 0;
  236. guiView->setUIML(uiml);
  237. guiView->layout();
  238. delete[] uiml;
  239. }
  240. case 3:
  241. { // update dialog content
  242. short dialogNameLen = *(short*)(data + 1);
  243. char* dialogName = new char[dialogNameLen + 1];
  244. memcpy(dialogName, data + 3, dialogNameLen);
  245. dialogName[dialogNameLen] = 0;
  246. dialogLock.lockRead();
  247. for (UIMLDialog* dialog : dialogs)
  248. {
  249. if (dialog->getName().isEqual(dialogName))
  250. {
  251. int uimlLen = *(int*)(data + 3 + dialogNameLen);
  252. char* uiml = new char[uimlLen + 1];
  253. memcpy(uiml, data + 7 + dialogNameLen, uimlLen);
  254. uiml[uimlLen] = 0;
  255. dialog->updateUIML(uiml);
  256. delete[] uiml;
  257. break;
  258. }
  259. }
  260. dialogLock.unlockRead();
  261. }
  262. }
  263. }
  264. void Game::closeCurrentDialog()
  265. {
  266. dialogLock.lockRead();
  267. if (dialogs.getEntryCount() > 0)
  268. {
  269. UIMLDialog* d = dialogs.get(dialogs.getEntryCount() - 1);
  270. d->close();
  271. }
  272. dialogLock.unlockRead();
  273. }
  274. DragController<InventoryDragSource, int>* Game::zInventoryDragController()
  275. {
  276. return inventoryDragController;
  277. }
  278. void Game::setTargetUIML(Framework::Text uiml)
  279. {
  280. if (uiml.getLength())
  281. {
  282. targetUIMLView->setUIML(uiml);
  283. targetUIMLView->layout();
  284. targetUIMLView->setSize(targetUIMLView->calculateContentSize());
  285. targetUIMLView->setPosition(
  286. window->zScreen()->zGraphicsApi()->getBackBufferSize()
  287. - targetUIMLView->getSize());
  288. targetUIMLView->addStyle(UIMLView::Style::Visible);
  289. }
  290. else
  291. {
  292. targetUIMLView->removeStyle(UIMLView::Style::Visible);
  293. }
  294. }
  295. void Game::updateRecipieVisibility()
  296. {
  297. if (!recipieVisible)
  298. {
  299. if (dialogs.getEntryCount() > 0)
  300. {
  301. recipieVisible = 1;
  302. window->zScreen()->addMember(
  303. dynamic_cast<Drawable*>(filter->getThis()));
  304. window->zScreen()->addMember(
  305. dynamic_cast<Drawable*>(searchIcon->getThis()));
  306. }
  307. }
  308. else
  309. {
  310. if (dialogs.getEntryCount() == 0)
  311. {
  312. recipieVisible = 0;
  313. window->zScreen()->removeMember(filter);
  314. window->zScreen()->removeMember(searchIcon);
  315. if (itemListContainer)
  316. itemListContainer->removeStyle(Window::Style::Visible);
  317. }
  318. }
  319. }
  320. void Game::showItemList()
  321. {
  322. if (!itemListContainer)
  323. {
  324. itemListContainer = new ItemListContainer();
  325. window->zScreen()->postAction([this]() {
  326. window->zScreen()->addMember(
  327. dynamic_cast<Drawable*>(itemListContainer->getThis()));
  328. });
  329. }
  330. itemListContainer->addStyle(Window::Style::Visible);
  331. }
  332. bool Game::isItemListVisible()
  333. {
  334. return itemListContainer
  335. && itemListContainer->hasStyle(Window::Style::Visible);
  336. }
  337. const Text* Game::zFilterText()
  338. {
  339. return filter->zText();
  340. }
  341. void Game::makeChatButtonVisible()
  342. {
  343. chatButton->addStyle(Button::Style::Visible);
  344. }
  345. Chat* Game::zChat() const
  346. {
  347. return chat;
  348. }
  349. MapWindow* Game::zMap() const
  350. {
  351. return mapWindow;
  352. }
  353. void Game::hide()
  354. {
  355. Menu::hide();
  356. window->zScreen()->postAction([this]() {
  357. if (itemListContainer)
  358. {
  359. window->zScreen()->removeMember(itemListContainer);
  360. itemListContainer->release();
  361. itemListContainer = 0;
  362. }
  363. });
  364. }
  365. ScreenCenter::ScreenCenter()
  366. : Drawable()
  367. {
  368. setPosition(0, 0);
  369. setSize(uiFactory.initParam.bildschirm->getBackBufferSize());
  370. }
  371. void ScreenCenter::render(Image& zRObj)
  372. {
  373. zRObj.drawLineH(gr.x / 2 - 5, gr.y / 2, 10, 0xFFFFFFFF);
  374. zRObj.drawLineV(gr.x / 2, gr.y / 2 - 5, 10, 0xFFFFFFFF);
  375. }