Minigames.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. #include "MiniGames.h"
  2. #include <Punkt.h>
  3. #include <Rahmen.h>
  4. #include "../../Global/Variablen.h"
  5. #include <Datei.h>
  6. #include "../../Global/Initialisierung.h"
  7. #include <InitDatei.h>
  8. #include <KSGTDatei.h>
  9. #include <Globals.h>
  10. typedef MiniGameV* (*GetMiniGame)();
  11. // Inhalt der MGLaden Klasse aus MiniGames.h
  12. // Konstruktor
  13. MGSuchen::MGSuchen(MiniGames* mGames)
  14. {
  15. this->mGames = mGames;
  16. start();
  17. }
  18. // Destruktor
  19. MGSuchen::~MGSuchen()
  20. {
  21. mGames->release();
  22. }
  23. // nicht constant
  24. void MGSuchen::thread()
  25. {
  26. KSGTDatei* dgt = new KSGTDatei("data/dg.ksgt");
  27. dgt->laden();
  28. bool ak = 0;
  29. int dgId = infoClient->getDateiGruppeIdVonPfad("data/Minigames");
  30. for (int i = 0; i < dgt->getZeilenAnzahl(); i++)
  31. {
  32. if (dgt->zFeld(i, 0) && TextZuInt(dgt->zFeld(i, 0)->getText(), 10) == dgId)
  33. {
  34. int lv = dgt->zFeld(i, 2) ? TextZuInt(dgt->zFeld(i, 2)->getText(), 10) : 0;
  35. int ov = infoClient->getDateiGruppeVersion(dgId);
  36. if (lv == ov)
  37. ak = 1;
  38. break;
  39. }
  40. }
  41. dgt->release();
  42. if (!ak)
  43. {
  44. mGames->setAktuell(0, dgId);
  45. delete this;
  46. return;
  47. }
  48. Datei* d = new Datei();
  49. d->setDatei("data/Minigames");
  50. if (!d->existiert())
  51. DateiPfadErstellen("data/MiniGames/");
  52. RCArray< Text >* list = d->getDateiListe();
  53. if (list)
  54. {
  55. for (int i = 0; i < list->getEintragAnzahl(); i++)
  56. {
  57. MiniGame* mg = new MiniGame(list->z(i)->getText());
  58. if (!mg->istOk())
  59. {
  60. mg->release();
  61. continue;
  62. }
  63. mGames->addMiniGame(mg);
  64. }
  65. list->release();
  66. }
  67. d->release();
  68. delete this;
  69. }
  70. // Inhalt der MGLaden Klasse aus MiniGameV.h
  71. // Konstruktor
  72. MGLaden::MGLaden(const char* name)
  73. {
  74. this->name = new Text(name);
  75. game = 0;
  76. start();
  77. }
  78. // Destruktor
  79. MGLaden::~MGLaden()
  80. {
  81. if (game)
  82. {
  83. game->release();
  84. Framework::getDLLRegister()->releaseDLL(name->getText());
  85. }
  86. name->release();
  87. }
  88. // nicht constant
  89. void MGLaden::thread()
  90. {
  91. Text* pfad = new Text("data/Minigames/");
  92. pfad->append(name->getText());
  93. if (!DateiExistiert(pfad->getText()))
  94. {
  95. pfad->release();
  96. run = 0;
  97. return;
  98. }
  99. pfad->append("/mg.ini");
  100. if (!DateiExistiert(pfad->getText()))
  101. {
  102. pfad->release();
  103. run = 0;
  104. return;
  105. }
  106. InitDatei* mgIni = new InitDatei(pfad);
  107. if (!mgIni->laden())
  108. {
  109. mgIni->release();
  110. run = 0;
  111. return;
  112. }
  113. if (!mgIni->wertExistiert("DllPfad"))
  114. {
  115. mgIni->release();
  116. run = 0;
  117. return;
  118. }
  119. Text* dllPfad = new Text("data/Minigames/");
  120. dllPfad->append(name->getText());
  121. dllPfad->append("/");
  122. dllPfad->append(mgIni->zWert("DllPfad")->getText());
  123. mgIni->release();
  124. if (!DateiExistiert(dllPfad->getText()))
  125. {
  126. dllPfad->release();
  127. run = 0;
  128. return;
  129. }
  130. HMODULE dll = Framework::getDLLRegister()->ladeDLL(name->getText(), dllPfad->getText());
  131. dllPfad->release();
  132. if (!dll)
  133. {
  134. run = 0;
  135. return;
  136. }
  137. GetMiniGame getMiniGame = (GetMiniGame)GetProcAddress(dll, "GetMiniGame");
  138. if (!getMiniGame)
  139. {
  140. Framework::getDLLRegister()->releaseDLL(name->getText());
  141. run = 0;
  142. return;
  143. }
  144. game = getMiniGame();
  145. if (!game)
  146. {
  147. Framework::getDLLRegister()->releaseDLL(name->getText());
  148. run = 0;
  149. return;
  150. }
  151. if (!minigameClient)
  152. minigameClient = mainClient->createMinigameServerClient();
  153. if (minigameClient)
  154. game->setMinigameClientZ(dynamic_cast<KSGClient::MinigameServerClient*>(minigameClient->getThis()));
  155. if (!game->laden())
  156. {
  157. game = (MiniGameV*)game->release();
  158. Framework::getDLLRegister()->releaseDLL(name->getText());
  159. }
  160. run = 0;
  161. }
  162. // constant
  163. bool MGLaden::fertig() const
  164. {
  165. return !isRunning();
  166. }
  167. MiniGameV* MGLaden::zGame() const
  168. {
  169. return game;
  170. }
  171. // Inhalt der MiniGames Klasse aus MiniGames.h
  172. // Konstruktor
  173. MiniGames::MiniGames(Fenster* zNachLoginFenster, int x)
  174. : Zeichnung()
  175. {
  176. bildschirmGröße = uiFactory.initParam.bildschirm->getBackBufferSize();
  177. pos = Punkt(x, 67);
  178. gr = Punkt(102, 32);
  179. rahmen = new LRahmen();
  180. rahmen->setFarbe(0xFFFFFFFF);
  181. rahmen->setSize(102, 32);
  182. alpha = 0;
  183. alpha2 = 0;
  184. animation = 0;
  185. sichtbar = 0;
  186. tickVal = 0;
  187. prozent1 = 0;
  188. prozent2 = 0;
  189. begPos = Punkt(0, 0);
  190. begGröße = Punkt(0, 0);
  191. größe1 = Punkt(102, 32);
  192. pos1 = Punkt(x, 67);
  193. größe2 = Punkt(800, 500);
  194. pos2 = bildschirmGröße / 2 - größe2 / 2;
  195. laden = (Animation2D*)ladeAnimation->dublizieren();
  196. laden->setSichtbar(0);
  197. laden->setPosition(375, 225);
  198. games = new RCArray< MiniGame >();
  199. suchFilter = initTextFeld(10, 10, 100, 20, TextFeld::Style::Text | TextFeld::Style::VCenter, "Suchfilter:");
  200. TextRenderer tr(dynamic_cast<Schrift*>(uiFactory.initParam.schrift->getThis()));
  201. tr.setSchriftSize(12);
  202. suchFilter->setSize(tr.getTextBreite(suchFilter->zText()->getText()), 20);
  203. suchName = initTextFeld(20 + suchFilter->getBreite(), 10, 200, 20, TextFeld::Style::TextFeld, "");
  204. suchen = initKnopf(230 + suchFilter->getBreite(), 10, 100, 20, Knopf::Style::Sichtbar, "Suchen");
  205. gefiltert = 0;
  206. getThis();
  207. zNachLoginFenster->addMember(this);
  208. dg = 0;
  209. mgl = 0;
  210. mgInitialized = 0;
  211. fullscreen = 0;
  212. slo = 0;
  213. minAlpha = 0;
  214. getThis();
  215. new MGSuchen(this);
  216. }
  217. // Destruktor
  218. MiniGames::~MiniGames()
  219. {
  220. rahmen->release();
  221. suchName->release();
  222. suchFilter->release();
  223. suchen->release();
  224. laden->release();
  225. games->release();
  226. if (mgl)
  227. mgl->release();
  228. }
  229. // nicht constant
  230. void MiniGames::setSichtbar(bool sicht)
  231. {
  232. begPos = pos;
  233. begGröße = gr;
  234. animation |= (sicht ? 0x1 : 0x2);
  235. rend = 1;
  236. }
  237. void MiniGames::addMiniGame(MiniGame* mg)
  238. {
  239. games->add(mg);
  240. if (gefiltert)
  241. filter();
  242. else
  243. {
  244. int i = games->getEintragAnzahl() - 1;
  245. games->z(i)->setPosition(10 + 10 * (i % 3) + 250 * (i % 3), 50 + 10 * (i / 3) + 100 * (i / 3));
  246. }
  247. }
  248. void MiniGames::setAktuell(bool aktuell, int dg)
  249. {
  250. this->aktuell = aktuell;
  251. if (aktuell)
  252. new MGSuchen(dynamic_cast<MiniGames*>(getThis()));
  253. if (!this->dg)
  254. this->dg = dg;
  255. if (!aktuell && !updateH->hat(dg))
  256. {
  257. nachLogin->zNachrichtenListe()->addNachricht(new SpielUpdateNachricht(new Text("Update"), new Text("Die minigames müssen aktualisiert werden."), dg,
  258. []()
  259. {
  260. if (nachLogin && nachLogin->zMGFenster())
  261. nachLogin->zMGFenster()->setAktuell(1);
  262. }));
  263. }
  264. }
  265. void MiniGames::setFullScreenMode(bool enabled)
  266. {
  267. fullscreen = enabled;
  268. if (fullscreen)
  269. nachLogin->hideBars();
  270. else
  271. nachLogin->showBars();
  272. }
  273. void MiniGames::showLoadingOverlay(unsigned char minAlpha)
  274. {
  275. slo++;
  276. this->minAlpha = minAlpha;
  277. if (slo)
  278. laden->setSichtbar(1);
  279. }
  280. void MiniGames::hideLoadingOverlay()
  281. {
  282. slo--;
  283. if (!slo)
  284. laden->setSichtbar(0);
  285. }
  286. Bild* MiniGames::loadBild(const char* path)
  287. {
  288. return bilder->get(path);
  289. }
  290. void MiniGames::filter()
  291. {
  292. Text filter = suchName->zText()->getText();
  293. bool notF = 0;
  294. if (!filter.getLength())
  295. notF = 1;
  296. int anz = games->getEintragAnzahl();
  297. bool* fertig = new bool[anz];
  298. for (int i = 0; i < anz; i++)
  299. fertig[i] = 0;
  300. for (int i = 0; i < anz; i++)
  301. {
  302. int pos = -1;
  303. int p = -1;
  304. for (int j = 0; j < anz; j++)
  305. {
  306. if ((notF || (games->z(j)->zName()->hat(filter) && (pos == -1 || games->z(j)->zName()->positionVon(filter) < pos))) && !fertig[j])
  307. {
  308. p = j;
  309. pos = games->z(j)->zName()->positionVon(filter);
  310. games->z(j)->setSichtbar(1);
  311. }
  312. }
  313. if (p < 0)
  314. break;
  315. fertig[p] = 1;
  316. games->z(p)->setPosition(10 + 10 * (i % 3) + 250 * (i % 3), 50 + 10 * (i / 3) + 100 * (i / 3));
  317. }
  318. for (int i = 0; i < anz; i++)
  319. {
  320. if (!fertig[i])
  321. games->z(i)->setSichtbar(0);
  322. }
  323. delete[] fertig;
  324. }
  325. void MiniGames::doPublicMausEreignis(MausEreignis& me)
  326. {
  327. if (laden->istSichtbar() || !sichtbar)
  328. return;
  329. me.mx -= pos.x;
  330. me.my -= pos.y;
  331. if (alpha2)
  332. {
  333. suchName->doPublicMausEreignis(me);
  334. bool vera = me.verarbeitet;
  335. suchen->doPublicMausEreignis(me);
  336. if (!vera && me.verarbeitet && me.id == ME_RLinks)
  337. filter();
  338. int anz = games->getEintragAnzahl();
  339. for (int i = 0; i < anz; i++)
  340. {
  341. bool vera = me.verarbeitet;
  342. games->z(i)->doPublicMausEreignis(me);
  343. if (!vera && me.verarbeitet && me.id == ME_RLinks)
  344. { // spiel starten
  345. laden->setSichtbar(1);
  346. if (mgl)
  347. mgl = (MGLaden*)mgl->release();
  348. mgInitialized = 0;
  349. mgl = new MGLaden(games->z(i)->zName()->getText());
  350. }
  351. }
  352. }
  353. if (!slo && mgl && mgl->zGame())
  354. mgl->zGame()->doPublicMausEreignis(me);
  355. me.mx += pos.x;
  356. me.my += pos.y;
  357. }
  358. void MiniGames::doTastaturEreignis(TastaturEreignis& te)
  359. {
  360. if (laden->istSichtbar() || !sichtbar)
  361. return;
  362. if (alpha2)
  363. {
  364. bool vera = te.verarbeitet;
  365. suchName->doTastaturEreignis(te);
  366. if (!vera && te.verarbeitet && te.taste == T_Enter && te.id == TE_Release)
  367. filter();
  368. }
  369. if (!slo && mgl && mgl->zGame())
  370. mgl->zGame()->doTastaturEreignis(te);
  371. }
  372. bool MiniGames::tick(double z)
  373. {
  374. if (laden->istSichtbar() && mgl && mgl->fertig())
  375. {
  376. if (!mgl->zGame())
  377. {
  378. mgl = (MGLaden*)mgl->release();
  379. nachLogin->zNachrichtenListe()->addNachricht(new Text("Fehler"), new Text("Das Minigame konnte nicht geladen werden."), new Text("Ok"));
  380. mgInitialized = 0;
  381. slo = 0;
  382. }
  383. else if (!mgInitialized)
  384. {
  385. mgl->zGame()->setMinigameAPI(this);
  386. mgl->zGame()->setUIFactory(uiFactory);
  387. mgInitialized = 1;
  388. }
  389. if (!slo)
  390. laden->setSichtbar(0);
  391. }
  392. if (mgl && mgl->zGame() && !alpha2)
  393. {
  394. if (sichtbar && !animation)
  395. rend |= mgl->zGame()->tick(z);
  396. if (mgl->zGame()->istEnde())
  397. {
  398. mgl = (MGLaden*)mgl->release();
  399. mgInitialized = 0;
  400. }
  401. }
  402. rend |= laden->tick(z);
  403. if (alpha2)
  404. {
  405. rend |= suchName->tick(z);
  406. rend |= suchen->tick(z);
  407. int anz = games->getEintragAnzahl();
  408. for (int i = 0; i < anz; i++)
  409. rend |= games->z(i)->tick(z);
  410. }
  411. tickVal += z * 150;
  412. int val = (int)tickVal;
  413. if (val < 1)
  414. {
  415. bool ret = rend;
  416. rend = 0;
  417. return ret;
  418. }
  419. tickVal -= val;
  420. if ((animation | 0x1) == animation) // Einblenden
  421. {
  422. if (prozent1 != 100)
  423. {
  424. prozent1 += val;
  425. if (prozent1 >= 100)
  426. prozent1 = 100;
  427. pos = begPos + (Punkt)(((Vec2< double >)(pos2 - begPos) / 100.0) * prozent1);
  428. gr = begGröße + (Punkt)(((Vec2< double >)(größe2 - begGröße) / 100.0) * prozent1);
  429. }
  430. else if (alpha != 255)
  431. {
  432. alpha += val * 2;
  433. if (alpha >= 255 || (animation | 0x2) == animation)
  434. {
  435. alpha = 255;
  436. animation &= ~0x1;
  437. sichtbar = 1;
  438. prozent1 = 0;
  439. }
  440. }
  441. else
  442. {
  443. animation &= ~0x1;
  444. prozent1 = 0;
  445. pos = pos2;
  446. gr = größe2;
  447. }
  448. rend = 1;
  449. }
  450. if ((animation | 0x2) == animation) // ausblenden
  451. {
  452. if (alpha != 0)
  453. {
  454. alpha -= val * 2;
  455. if (alpha < 0)
  456. alpha = 0;
  457. }
  458. else
  459. {
  460. prozent2 += val;
  461. if (prozent2 > 100)
  462. prozent2 = 100;
  463. pos = begPos + (Punkt)(((Vec2< double >)(pos1 - begPos) / 100.0) * prozent2);
  464. gr = begGröße + (Punkt)(((Vec2< double >)(größe1 - begGröße) / 100.0) * prozent2);
  465. if (prozent2 == 100)
  466. {
  467. prozent2 = 0;
  468. animation &= ~0x2;
  469. sichtbar = 0;
  470. }
  471. }
  472. rend = 1;
  473. }
  474. if (!animation && sichtbar)
  475. {
  476. if (fullscreen)
  477. {
  478. if (pos != Punkt(0, 0))
  479. {
  480. pos -= Punkt(val, val) * 2;
  481. if (pos.x < 0)
  482. pos.x = 0;
  483. if (pos.y < 0)
  484. pos.y = 0;
  485. }
  486. if (gr != bildschirmGröße)
  487. {
  488. gr += Punkt(val, val) * 4;
  489. if (gr.x > bildschirmGröße.x - 1)
  490. gr.x = bildschirmGröße.x - 1;
  491. if (gr.y > bildschirmGröße.y - 1)
  492. gr.y = bildschirmGröße.y - 1;
  493. }
  494. }
  495. else
  496. {
  497. if (pos != pos2 || gr != größe2)
  498. {
  499. begPos = pos;
  500. begGröße = gr;
  501. animation |= 0x1;
  502. }
  503. }
  504. if (slo && alpha != minAlpha)
  505. {
  506. alpha -= val * 2;
  507. if (alpha < minAlpha)
  508. alpha = minAlpha;
  509. }
  510. else if (!slo && alpha != 255)
  511. {
  512. alpha += val * 2;
  513. if (alpha > 255)
  514. alpha = 255;
  515. }
  516. }
  517. if (mgl && alpha2)
  518. {
  519. alpha2 -= val;
  520. if (alpha2 < 0)
  521. alpha2 = 0;
  522. rend = 1;
  523. }
  524. if (!mgl && alpha2 != 255)
  525. {
  526. alpha2 += val;
  527. if (alpha2 > 255)
  528. alpha2 = 255;
  529. rend = 1;
  530. }
  531. bool ret = rend;
  532. rend = 0;
  533. return ret;
  534. }
  535. void MiniGames::render(Bild& zRObj)
  536. {
  537. if (pos == pos1)
  538. return;
  539. rahmen->setPosition(pos);
  540. rahmen->setSize(gr);
  541. rahmen->render(zRObj);
  542. if (!zRObj.setDrawOptions(pos.x + 1, pos.y + 1, gr.x - 2, gr.y - 2))
  543. return;
  544. int rbr = rahmen->getRBreite();
  545. zRObj.setAlpha((unsigned char)alpha);
  546. zRObj.setAlpha((unsigned char)alpha2);
  547. suchFilter->render(zRObj);
  548. suchName->render(zRObj);
  549. suchen->render(zRObj);
  550. int anz = games->getEintragAnzahl();
  551. for (int i = 0; i < anz; i++)
  552. games->z(i)->render(zRObj);
  553. zRObj.releaseAlpha();
  554. if (mgl && mgl->fertig() && mgl->zGame())
  555. mgl->zGame()->render(zRObj);
  556. zRObj.releaseAlpha();
  557. laden->setPosition(gr.x / 2 - laden->getBreite() / 2, gr.y / 2 - laden->getHeight() / 2);
  558. laden->render(zRObj);
  559. zRObj.releaseDrawOptions();
  560. }
  561. // constant
  562. bool MiniGames::istAnimiert() const
  563. {
  564. return animation != 0;
  565. }
  566. bool MiniGames::istSichtbar() const
  567. {
  568. return sichtbar || prozent1 != 0;
  569. }