Minigames.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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( 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->getThis() ) )
  94. {
  95. pfad->release();
  96. run = 0;
  97. return;
  98. }
  99. pfad->append( "/mg.ini" );
  100. if( !DateiExistiert( pfad->getThis() ) )
  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->getThis() ) )
  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( minigameClient->getThis() );
  155. if( !game->laden() )
  156. {
  157. game = 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( Schrift * zSchrift, Fenster * zNachLoginFenster, int x )
  174. : Zeichnung()
  175. {
  176. schrift = zSchrift->getThis();
  177. bildschirmGröße = BildschirmGröße();
  178. pos = Punkt( x, 67 );
  179. gr = Punkt( 102, 32 );
  180. rahmen = new LRahmen();
  181. rahmen->setFarbe( 0xFFFFFFFF );
  182. rahmen->setSize( 102, 32 );
  183. alpha = 0;
  184. alpha2 = 0;
  185. animation = 0;
  186. sichtbar = 0;
  187. tickVal = 0;
  188. prozent1 = 0;
  189. prozent2 = 0;
  190. begPos = Punkt( 0, 0 );
  191. begGröße = Punkt( 0, 0 );
  192. größe1 = Punkt( 102, 32 );
  193. pos1 = Punkt( x, 67 );
  194. größe2 = Punkt( 800, 500 );
  195. pos2 = bildschirmGröße / 2 - größe2 / 2;
  196. laden = (Animation2D *)ladeAnimation->dublizieren();
  197. laden->setSichtbar( 0 );
  198. laden->setPosition( 375, 225 );
  199. games = new RCArray< MiniGame >();
  200. suchFilter = initTextFeld( 10, 10, 100, 20, zSchrift, TextFeld::Style::Text | TextFeld::Style::VCenter, "Suchfilter:" );
  201. TextRenderer tr( zSchrift->getThis() );
  202. tr.setSchriftSize( 12 );
  203. suchFilter->setSize( tr.getTextBreite( suchFilter->zText()->getText() ), 20 );
  204. suchName = initTextFeld( 20 + suchFilter->getBreite(), 10, 200, 20, zSchrift, TextFeld::Style::TextFeld, "" );
  205. suchen = initKnopf( 230 + suchFilter->getBreite(), 10, 100, 20, zSchrift, Knopf::Style::Sichtbar, "Suchen" );
  206. gefiltert = 0;
  207. zNachLoginFenster->addMember( getThis() );
  208. dg = 0;
  209. mgl = 0;
  210. mgInitialized = 0;
  211. fullscreen = 0;
  212. new MGSuchen( (MiniGames *)getThis() );
  213. }
  214. // Destruktor
  215. MiniGames::~MiniGames()
  216. {
  217. if( schrift )
  218. schrift->release();
  219. rahmen->release();
  220. suchName->release();
  221. suchFilter->release();
  222. suchen->release();
  223. laden->release();
  224. games->release();
  225. if( mgl )
  226. mgl->release();
  227. }
  228. // nicht constant
  229. void MiniGames::setSichtbar( bool sicht )
  230. {
  231. begPos = pos;
  232. begGröße = gr;
  233. animation |= ( sicht ? 0x1 : 0x2 );
  234. rend = 1;
  235. }
  236. void MiniGames::addMiniGame( MiniGame * mg )
  237. {
  238. games->add( mg );
  239. if( gefiltert )
  240. filter();
  241. else
  242. {
  243. int i = games->getEintragAnzahl() - 1;
  244. games->z( i )->setPosition( 10 + 10 * ( i % 3 ) + 250 * ( i % 3 ), 50 + 10 * ( i / 3 ) + 100 * ( i / 3 ) );
  245. }
  246. }
  247. void MiniGames::setAktuell( bool aktuell, int dg )
  248. {
  249. this->aktuell = aktuell;
  250. if( aktuell )
  251. new MGSuchen( (MiniGames *)getThis() );
  252. if( !this->dg )
  253. this->dg = dg;
  254. if( !aktuell && !updateH->hat( dg ) )
  255. {
  256. nachLogin->zNachrichtenListe()->addNachricht( new SpielUpdateNachricht( schrift, new Text( "Update" ), new Text( "Die minigames müssen aktualisiert werden." ), dg,
  257. []()
  258. {
  259. if( nachLogin &&nachLogin->zMGFenster() )
  260. nachLogin->zMGFenster()->setAktuell( 1 );
  261. } ) );
  262. }
  263. }
  264. void MiniGames::setFullScreenMode( bool enabled )
  265. {
  266. fullscreen = enabled;
  267. if( fullscreen )
  268. nachLogin->hideBars();
  269. else
  270. nachLogin->showBars();
  271. }
  272. void MiniGames::filter()
  273. {
  274. Text filter = suchName->zText()->getText();
  275. bool notF = 0;
  276. if( !filter.getLength() )
  277. notF = 1;
  278. int anz = games->getEintragAnzahl();
  279. bool *fertig = new bool[ anz ];
  280. for( int i = 0; i < anz; i++ )
  281. fertig[ i ] = 0;
  282. for( int i = 0; i < anz; i++ )
  283. {
  284. int pos = -1;
  285. int p = -1;
  286. for( int j = 0; j < anz; j++ )
  287. {
  288. if( ( notF || ( games->z( j )->zName()->hat( filter ) && ( pos == -1 || games->z( j )->zName()->positionVon( filter ) < pos ) ) ) && !fertig[ j ] )
  289. {
  290. p = j;
  291. pos = games->z( j )->zName()->positionVon( filter );
  292. games->z( j )->setSichtbar( 1 );
  293. }
  294. }
  295. if( p < 0 )
  296. break;
  297. fertig[ p ] = 1;
  298. games->z( p )->setPosition( 10 + 10 * ( i % 3 ) + 250 * ( i % 3 ), 50 + 10 * ( i / 3 ) + 100 * ( i / 3 ) );
  299. }
  300. for( int i = 0; i < anz; i++ )
  301. {
  302. if( !fertig[ i ] )
  303. games->z( i )->setSichtbar( 0 );
  304. }
  305. delete[] fertig;
  306. }
  307. void MiniGames::doMausEreignis( MausEreignis & me )
  308. {
  309. if( laden->istSichtbar() || !sichtbar )
  310. return;
  311. me.mx -= pos.x;
  312. me.my -= pos.y;
  313. if( alpha2 )
  314. {
  315. suchName->doMausEreignis( me );
  316. bool vera = me.verarbeitet;
  317. suchen->doMausEreignis( me );
  318. if( !vera && me.verarbeitet &&me.id == ME_RLinks )
  319. filter();
  320. int anz = games->getEintragAnzahl();
  321. for( int i = 0; i < anz; i++ )
  322. {
  323. bool vera = me.verarbeitet;
  324. games->z( i )->doMausEreignis( me );
  325. if( !vera && me.verarbeitet && me.id == ME_RLinks )
  326. { // spiel starten
  327. laden->setSichtbar( 1 );
  328. if( mgl )
  329. mgl = (MGLaden *)mgl->release();
  330. mgInitialized = 0;
  331. mgl = new MGLaden( games->z( i )->zName()->getText() );
  332. }
  333. }
  334. }
  335. if( mgl &&mgl->zGame() )
  336. mgl->zGame()->doMausEreignis( me );
  337. me.mx += pos.x;
  338. me.my += pos.y;
  339. }
  340. void MiniGames::doTastaturEreignis( TastaturEreignis & te )
  341. {
  342. if( laden->istSichtbar() || !sichtbar )
  343. return;
  344. if( alpha2 )
  345. {
  346. bool vera = te.verarbeitet;
  347. suchName->doTastaturEreignis( te );
  348. if( !vera && te.verarbeitet &&te.taste == T_Enter && te.id == TE_Release )
  349. filter();
  350. }
  351. if( mgl &&mgl->zGame() )
  352. mgl->zGame()->doTastaturEreignis( te );
  353. }
  354. bool MiniGames::tick( double z )
  355. {
  356. if( laden->istSichtbar() && mgl && mgl->fertig() )
  357. {
  358. if( !mgl->zGame() )
  359. {
  360. mgl = (MGLaden *)mgl->release();
  361. nachLogin->zNachrichtenListe()->addNachricht( new Text( "Fehler" ), new Text( "Das Minigame konnte nicht geladen werden." ), new Text( "Ok" ) );
  362. mgInitialized = 0;
  363. }
  364. else if( !mgInitialized )
  365. {
  366. mgl->zGame()->setMinigameAPI( this );
  367. mgl->zGame()->setSchriftZ( schrift->getThis() );
  368. mgl->zGame()->setBildschirmZ( hauptScreen );
  369. mgInitialized = 1;
  370. }
  371. laden->setSichtbar( 0 );
  372. }
  373. if( mgl &&mgl->zGame() && !alpha2 )
  374. {
  375. if( sichtbar && !animation )
  376. rend |= mgl->zGame()->tick( z );
  377. if( mgl->zGame()->istEnde() )
  378. {
  379. mgl = (MGLaden *)mgl->release();
  380. mgInitialized = 0;
  381. }
  382. }
  383. rend |= laden->tick( z );
  384. if( alpha2 )
  385. {
  386. rend |= suchName->tick( z );
  387. rend |= suchen->tick( z );
  388. int anz = games->getEintragAnzahl();
  389. for( int i = 0; i < anz; i++ )
  390. rend |= games->z( i )->tick( z );
  391. }
  392. tickVal += z * 150;
  393. int val = (int)tickVal;
  394. if( val < 1 )
  395. {
  396. bool ret = rend;
  397. rend = 0;
  398. return ret;
  399. }
  400. tickVal -= val;
  401. if( ( animation | 0x1 ) == animation ) // Einblenden
  402. {
  403. if( prozent1 != 100 )
  404. {
  405. prozent1 += val;
  406. if( prozent1 >= 100 )
  407. prozent1 = 100;
  408. pos = begPos + (Punkt)( ( ( Vec2< double > )( pos2 - begPos ) / 100.0 ) * prozent1 );
  409. gr = begGröße + (Punkt)( ( ( Vec2< double > )( größe2 - begGröße ) / 100.0 ) * prozent1 );
  410. }
  411. else if( alpha != 255 )
  412. {
  413. alpha += val * 2;
  414. if( alpha >= 255 || ( animation | 0x2 ) == animation )
  415. {
  416. alpha = 255;
  417. animation &= ~0x1;
  418. sichtbar = 1;
  419. prozent1 = 0;
  420. }
  421. }
  422. else
  423. {
  424. animation &= ~0x1;
  425. prozent1 = 0;
  426. pos = pos2;
  427. gr = größe2;
  428. }
  429. rend = 1;
  430. }
  431. if( ( animation | 0x2 ) == animation ) // ausblenden
  432. {
  433. if( alpha != 0 )
  434. {
  435. alpha -= val * 2;
  436. if( alpha < 0 )
  437. alpha = 0;
  438. }
  439. else
  440. {
  441. prozent2 += val;
  442. if( prozent2 > 100 )
  443. prozent2 = 100;
  444. pos = begPos + (Punkt)( ( ( Vec2< double > )( pos1 - begPos ) / 100.0 ) * prozent2 );
  445. gr = begGröße + (Punkt)( ( ( Vec2< double > )( größe1 - begGröße ) / 100.0 ) * prozent2 );
  446. if( prozent2 == 100 )
  447. {
  448. prozent2 = 0;
  449. animation &= ~0x2;
  450. sichtbar = 0;
  451. }
  452. }
  453. rend = 1;
  454. }
  455. if( !animation && sichtbar )
  456. {
  457. if( fullscreen )
  458. {
  459. if( pos != Punkt( 0, 0 ) )
  460. {
  461. pos -= Punkt( val, val ) * 2;
  462. if( pos.x < 0 )
  463. pos.x = 0;
  464. if( pos.y < 0 )
  465. pos.y = 0;
  466. }
  467. if( gr != bildschirmGröße )
  468. {
  469. gr += Punkt( val, val ) * 4;
  470. if( gr.x > bildschirmGröße.x - 1 )
  471. gr.x = bildschirmGröße.x - 1;
  472. if( gr.y > bildschirmGröße.y - 1 )
  473. gr.y = bildschirmGröße.y - 1;
  474. }
  475. }
  476. else
  477. {
  478. if( pos != pos2 || gr != größe2 )
  479. {
  480. begPos = pos;
  481. begGröße = gr;
  482. animation |= 0x1;
  483. }
  484. }
  485. }
  486. if( mgl &&alpha2 )
  487. {
  488. alpha2 -= val;
  489. if( alpha2 < 0 )
  490. alpha2 = 0;
  491. rend = 1;
  492. }
  493. if( !mgl && alpha2 != 255 )
  494. {
  495. alpha2 += val;
  496. if( alpha2 > 255 )
  497. alpha2 = 255;
  498. rend = 1;
  499. }
  500. bool ret = rend;
  501. rend = 0;
  502. return ret;
  503. }
  504. void MiniGames::render( Bild & zRObj )
  505. {
  506. if( pos == pos1 )
  507. return;
  508. rahmen->setPosition( pos );
  509. rahmen->setSize( gr );
  510. rahmen->render( zRObj );
  511. if( !zRObj.setDrawOptions( pos.x + 1, pos.y + 1, gr.x - 2, gr.y - 2 ) )
  512. return;
  513. int rbr = rahmen->getRBreite();
  514. zRObj.setAlpha( (unsigned char)alpha );
  515. zRObj.setAlpha( (unsigned char)alpha2 );
  516. suchFilter->render( zRObj );
  517. suchName->render( zRObj );
  518. suchen->render( zRObj );
  519. int anz = games->getEintragAnzahl();
  520. for( int i = 0; i < anz; i++ )
  521. games->z( i )->render( zRObj );
  522. zRObj.releaseAlpha();
  523. laden->render( zRObj );
  524. if( mgl &&mgl->fertig() && mgl->zGame() )
  525. mgl->zGame()->render( zRObj );
  526. zRObj.releaseAlpha();
  527. zRObj.releaseDrawOptions();
  528. }
  529. // constant
  530. bool MiniGames::istAnimiert() const
  531. {
  532. return animation != 0;
  533. }
  534. bool MiniGames::istSichtbar() const
  535. {
  536. return sichtbar || prozent1 != 0;
  537. }