Game.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include "Game.h"
  2. #include "Initialisierung.h"
  3. #include "Globals.h"
  4. #include <AsynchronCall.h>
  5. #include <Bildschirm.h>
  6. Game::Game(Bildschirm* zScreen)
  7. : Menu(zScreen)
  8. {
  9. logout = initKnopf(10, 10, 200, 20, Knopf::Style::Normal, "Verlassen");
  10. logout->setMausEreignis([this, zScreen](void* p, void* o, MausEreignis me)
  11. {
  12. if (me.id == ME_RLinks)
  13. {
  14. logout->removeStyle(Knopf::Style::Erlaubt);
  15. new AsynchronCall([this, zScreen]()
  16. {
  17. if (network->leaveGame())
  18. {
  19. currentGame->release();
  20. currentGame = 0;
  21. zScreen->lock();
  22. hide();
  23. menuRegister->get("directConnect")->show();
  24. zScreen->unlock();
  25. }
  26. logout->addStyle(Knopf::Style::Erlaubt);
  27. });
  28. }
  29. return 1;
  30. });
  31. elements.add(logout);
  32. debug = initTextFeld(10, 40, 500, 40, TextFeld::Style::Text | TextFeld::Style::Mehrzeilig, "");
  33. elements.add(debug);
  34. invB = new Bild();
  35. invB->neuBild(zScreen->getBackBufferSize().x - 20, 50, 0);
  36. invB->setAlpha3D(1);
  37. inventory = initBildZ(10, zScreen->getBackBufferSize().y - 60, zScreen->getBackBufferSize().x - 20, 50, BildZ::Style::Sichtbar | BildZ::Style::Alpha, invB);
  38. elements.add(inventory);
  39. }
  40. Game::~Game()
  41. {}
  42. void Game::updatePosition(Vec3<float> position, bool target, Vec3<int> targetPos)
  43. {
  44. Text txt = "Position: (";
  45. txt.setPrecision(2);
  46. txt += position.x;
  47. txt += ", ";
  48. txt += position.y;
  49. txt += ", ";
  50. txt += position.z;
  51. txt += ")";
  52. if (target)
  53. {
  54. txt += "\nTarget: (";
  55. txt += targetPos.x;
  56. txt += ", ";
  57. txt += targetPos.y;
  58. txt += ", ";
  59. txt += targetPos.z;
  60. txt += ")";
  61. }
  62. debug->setText(txt);
  63. }
  64. void Game::updateInventory(Framework::Array<ItemSlot*>& itemBar, int pos)
  65. {
  66. invB->fillRegion(0, 0, invB->getBreite(), invB->getHeight(), 0);
  67. TextRenderer tr;
  68. tr.setSchriftZ(dynamic_cast<Schrift*>(uiFactory.initParam.schrift->getThis()));
  69. tr.setSchriftSize(12);
  70. int index = 0;
  71. for (auto slot : itemBar)
  72. {
  73. if (pos == index)
  74. {
  75. invB->fillRegion(50 * index, 0, 50, 50, 0x7F202020);
  76. }
  77. if (slot && slot->zStack() && slot->zStack()->zItem())
  78. {
  79. Bild* icon = itemIcons->z(slot->zStack()->zItem()->zItemType()->getId());
  80. invB->alphaBild(50 * index, 0, 50, 50, *icon);
  81. tr.renderText(5 + index * 50, 5, slot->zStack()->zItem()->getName(), *invB, 0xFFFFFFFF);
  82. int size = slot->zStack()->getSize();
  83. const char* units[] = { "", "K", "M", "G", "T", "P" };
  84. int i = 0;
  85. for (; i < 6 && size > 1024; i++)
  86. size = size / 1024;
  87. Text count = size;
  88. count += units[i];
  89. tr.renderText(45 - tr.getTextBreite(count) + index * 50, 45 - tr.getTextHeight(count), count, *invB, 0xFFFFFFFF);
  90. }
  91. index++;
  92. }
  93. inventory->setRender();
  94. }
  95. void Game::api(char* data)
  96. {
  97. switch (data[0])
  98. {
  99. case 0: // open dialog
  100. {
  101. bool exists = 0;
  102. short len = *(short*)(data + 1);
  103. char* dialogName = new char[len + 1];
  104. memcpy(dialogName, data + 3, len);
  105. dialogName[len] = 0;
  106. for (UIMLDialog* dialog : dialogs)
  107. {
  108. if (dialog->getName().istGleich(dialogName))
  109. {
  110. exists = 1;
  111. break;
  112. }
  113. }
  114. delete[] dialogName;
  115. if (!exists)
  116. {
  117. int uimlLen = *(int*)(data + 4 + len);
  118. char* uiml = new char[uimlLen + 1];
  119. memcpy(uiml, data + 8 + len, uimlLen);
  120. uiml[uimlLen] = 0;
  121. UIMLDialog* dialog = new UIMLDialog(uiml, [this](UIMLDialog* dialog)
  122. {
  123. logout->postAction([this, dialog]()
  124. {
  125. int index = 0;
  126. for (UIMLDialog* d : dialogs)
  127. {
  128. if (d == dialog)
  129. {
  130. dialogs.remove(index);
  131. window->zBildschirm()->removeMember(d);
  132. break;
  133. }
  134. index++;
  135. }
  136. });
  137. });
  138. dialogs.add(dialog);
  139. window->zBildschirm()->addMember(dialog);
  140. delete[]uiml;
  141. }
  142. break;
  143. }
  144. case 1:
  145. { // element message
  146. for (UIMLDialog* dialog : dialogs)
  147. {
  148. dialog->api(data + 1);
  149. }
  150. break;
  151. }
  152. }
  153. }