Game.cpp 12 KB

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