Game.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. }
  35. Game::~Game()
  36. {}
  37. void Game::updatePosition(Vec3<float> position, bool target, Vec3<int> targetPos)
  38. {
  39. Text txt = "Position: (";
  40. txt.setPrecision(2);
  41. txt += position.x;
  42. txt += ", ";
  43. txt += position.y;
  44. txt += ", ";
  45. txt += position.z;
  46. txt += ")";
  47. if (target)
  48. {
  49. txt += "\nTarget: (";
  50. txt += targetPos.x;
  51. txt += ", ";
  52. txt += targetPos.y;
  53. txt += ", ";
  54. txt += targetPos.z;
  55. txt += ")";
  56. }
  57. debug->setText(txt);
  58. }
  59. void Game::api(char* data)
  60. {
  61. switch (data[0])
  62. {
  63. case 0: // open dialog
  64. {
  65. bool exists = 0;
  66. short len = *(short*)(data + 1);
  67. char* dialogName = new char[len + 1];
  68. memcpy(dialogName, data + 3, len);
  69. dialogName[len] = 0;
  70. for (UIMLDialog* dialog : dialogs)
  71. {
  72. if (dialog->getName().istGleich(dialogName))
  73. {
  74. exists = 1;
  75. break;
  76. }
  77. }
  78. delete[] dialogName;
  79. if (!exists)
  80. {
  81. int uimlLen = *(int*)(data + 4 + len);
  82. char* uiml = new char[uimlLen + 1];
  83. memcpy(uiml, data + 8 + len, uimlLen);
  84. uiml[uimlLen] = 0;
  85. UIMLDialog* dialog = new UIMLDialog(uiml, [this](UIMLDialog* dialog)
  86. {
  87. logout->postAction([this, dialog]()
  88. {
  89. int index = 0;
  90. for (UIMLDialog* d : dialogs)
  91. {
  92. if (d == dialog)
  93. {
  94. dialogs.remove(index);
  95. window->zBildschirm()->removeMember(d);
  96. break;
  97. }
  98. index++;
  99. }
  100. });
  101. });
  102. dialogs.add(dialog);
  103. window->zBildschirm()->addMember(dialog);
  104. delete[]uiml;
  105. }
  106. break;
  107. }
  108. case 1:
  109. { // element message
  110. for (UIMLDialog* dialog : dialogs)
  111. {
  112. dialog->api(data + 1);
  113. }
  114. break;
  115. }
  116. }
  117. }