Minigames.cpp 15 KB

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