Minigames.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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. typedef MiniGameV *( *GetMiniGame )( );
  10. // Inhalt der MGLaden Klasse aus MiniGames.h
  11. // Konstruktor
  12. MGSuchen::MGSuchen( MiniGames *mGames )
  13. {
  14. this->mGames = mGames;
  15. start();
  16. }
  17. // Destruktor
  18. MGSuchen::~MGSuchen()
  19. {
  20. mGames->release();
  21. }
  22. // nicht constant
  23. void MGSuchen::thread()
  24. {
  25. KSGTDatei *dgt = new KSGTDatei( "data/dg.ksgt" );
  26. dgt->laden();
  27. bool ak = 0;
  28. int dgId = infoKlient->getDateiGruppeIdVonPfad( "data/Minigames" );
  29. for( int i = 0; i < dgt->getZeilenAnzahl(); i++ )
  30. {
  31. if( dgt->zFeld( i, 0 ) && TextZuInt( dgt->zFeld( i, 0 )->getText(), 10 ) == dgId )
  32. {
  33. int lv = dgt->zFeld( i, 2 ) ? TextZuInt( dgt->zFeld( i, 2 )->getText(), 10 ) : 0;
  34. int ov = infoKlient->getDateiGruppeVersion( dgId );
  35. if( lv == ov )
  36. ak = 1;
  37. break;
  38. }
  39. }
  40. dgt->release();
  41. if( !ak )
  42. {
  43. mGames->setAktuell( 0, dgId );
  44. delete this;
  45. return;
  46. }
  47. Datei *d = new Datei();
  48. d->setDatei( "data/Minigames" );
  49. if( !d->existiert() )
  50. DateiPfadErstellen( "data/MiniGames/" );
  51. RCArray< Text > *list = d->getDateiListe();
  52. if( list )
  53. {
  54. for( int i = 0; i < list->getEintragAnzahl(); i++ )
  55. {
  56. MiniGame *mg = new MiniGame( list->z( i )->getText() );
  57. if( !mg->istOk() )
  58. {
  59. mg->release();
  60. continue;
  61. }
  62. mGames->addMiniGame( mg );
  63. }
  64. list->release();
  65. }
  66. d->release();
  67. delete this;
  68. }
  69. // Inhalt der MGLaden Klasse aus MiniGameV.h
  70. // Konstruktor
  71. MGLaden::MGLaden( char *name )
  72. {
  73. this->name = new Text( name );
  74. game = 0;
  75. ref = 1;
  76. start();
  77. }
  78. // Destruktor
  79. MGLaden::~MGLaden()
  80. {
  81. if( game )
  82. {
  83. game->release();
  84. dllDateien->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 = dllDateien->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. dllDateien->releaseDLL( name->getText() );
  141. run = 0;
  142. return;
  143. }
  144. game = getMiniGame();
  145. if( !game )
  146. {
  147. dllDateien->releaseDLL( name->getText() );
  148. run = 0;
  149. return;
  150. }
  151. game->setMinigameClientZ( minigameClient->getThis() );
  152. if( !game->laden() )
  153. {
  154. game = game->release();
  155. dllDateien->releaseDLL( name->getText() );
  156. }
  157. run = 0;
  158. }
  159. // constant
  160. bool MGLaden::fertig() const
  161. {
  162. return !isRunning();
  163. }
  164. MiniGameV *MGLaden::zGame() const
  165. {
  166. return game;
  167. }
  168. // Reference Counting
  169. MGLaden *MGLaden::getThis()
  170. {
  171. ref++;
  172. return this;
  173. }
  174. MGLaden *MGLaden::release()
  175. {
  176. ref--;
  177. if( !ref )
  178. delete this;
  179. return 0;
  180. }
  181. // Inhalt der MiniGames Klasse aus MiniGames.h
  182. // Konstruktor
  183. MiniGames::MiniGames( Schrift *zSchrift, Fenster *zNachLoginFenster, int x )
  184. : Zeichnung()
  185. {
  186. schrift = zSchrift->getThis();
  187. bildschirmGröße = BildschirmGröße();
  188. pos = Punkt( x, 67 );
  189. gr = Punkt( 102, 32 );
  190. rahmen = new LRahmen();
  191. rahmen->setFarbe( 0xFFFFFFFF );
  192. rahmen->setSize( 102, 32 );
  193. alpha = 0;
  194. alpha2 = 0;
  195. animation = 0;
  196. sichtbar = 0;
  197. tickVal = 0;
  198. prozent1 = 0;
  199. prozent2 = 0;
  200. begPos = Punkt( 0, 0 );
  201. begGröße = Punkt( 0, 0 );
  202. größe1 = Punkt( 102, 32 );
  203. pos1 = Punkt( x, 67 );
  204. größe2 = Punkt( 800, 500 );
  205. pos2 = bildschirmGröße / 2 - größe2 / 2;
  206. laden = (Animation2D*)ladeAnimation->dublizieren();
  207. laden->setSichtbar( 0 );
  208. laden->setPosition( 375, 225 );
  209. games = new RCArray< MiniGame >();
  210. suchFilter = initTextFeld( 10, 10, 100, 20, zSchrift, TextFeld::Style::Text | TextFeld::Style::VCenter, "Suchfilter:" );
  211. zSchrift->setSchriftSize( 12 );
  212. suchFilter->setSize( zSchrift->getTextBreite( suchFilter->zText() ), 20 );
  213. suchName = initTextFeld( 20 + suchFilter->getBreite(), 10, 200, 20, zSchrift, TextFeld::Style::TextFeld, "" );
  214. suchen = initKnopf( 230 + suchFilter->getBreite(), 10, 100, 20, zSchrift, Knopf::Style::Sichtbar, "Suchen" );
  215. gefiltert = 0;
  216. zNachLoginFenster->addMember( this );
  217. dg = 0;
  218. mgl = 0;
  219. ref = 1;
  220. new MGSuchen( getThis() );
  221. }
  222. // Destruktor
  223. MiniGames::~MiniGames()
  224. {
  225. if( schrift )
  226. schrift->release();
  227. rahmen->release();
  228. suchName->release();
  229. suchFilter->release();
  230. suchen->release();
  231. laden->release();
  232. games->release();
  233. if( mgl )
  234. mgl->release();
  235. }
  236. // nicht constant
  237. void MiniGames::setSichtbar( bool sicht )
  238. {
  239. begPos = pos;
  240. begGröße = gr;
  241. animation |= ( sicht ? 0x1 : 0x2 );
  242. rend = 1;
  243. }
  244. void MiniGames::addMiniGame( MiniGame *mg )
  245. {
  246. games->add( mg );
  247. if( gefiltert )
  248. filter();
  249. else
  250. {
  251. int i = games->getEintragAnzahl() - 1;
  252. games->z( i )->setPosition( 10 + 10 * ( i % 3 ) + 250 * ( i % 3 ), 50 + 10 * ( i / 3 ) + 100 * ( i / 3 ) );
  253. }
  254. if( dg && updateH->hat( 0, dg ) )
  255. updateH->remove( 0, dg );
  256. }
  257. void MiniGames::setAktuell( bool aktuell, int dg )
  258. {
  259. this->aktuell = aktuell;
  260. if( aktuell )
  261. new MGSuchen( getThis() );
  262. if( !this->dg )
  263. this->dg = dg;
  264. if( !aktuell && !updateH->hat( 0, this->dg ) )
  265. updateH->erstellen( schrift, 0, this->dg );
  266. if( !aktuell )
  267. updateH->setSichtbar( 0, 1, this->dg );
  268. }
  269. void MiniGames::filter()
  270. {
  271. Text filter = suchName->zText()->getText();
  272. bool notF = 0;
  273. if( !filter.getLength() )
  274. notF = 1;
  275. int anz = games->getEintragAnzahl();
  276. bool *fertig = new bool[ anz ];
  277. for( int i = 0; i < anz; i++ )
  278. fertig[ i ] = 0;
  279. for( int i = 0; i < anz; i++ )
  280. {
  281. int pos = -1;
  282. int p = -1;
  283. for( int j = 0; j < anz; j++ )
  284. {
  285. if( ( notF || ( games->z( j )->zName()->hat( filter ) && ( pos == -1 || games->z( j )->zName()->positionVon( filter ) < pos ) ) ) && !fertig[ j ] )
  286. {
  287. p = j;
  288. pos = games->z( j )->zName()->positionVon( filter );
  289. games->z( j )->setSichtbar( 1 );
  290. }
  291. }
  292. if( p < 0 )
  293. break;
  294. fertig[ p ] = 1;
  295. games->z( p )->setPosition( 10 + 10 * ( i % 3 ) + 250 * ( i % 3 ), 50 + 10 * ( i / 3 ) + 100 * ( i / 3 ) );
  296. }
  297. for( int i = 0; i < anz; i++ )
  298. {
  299. if( !fertig[ i ] )
  300. games->z( i )->setSichtbar( 0 );
  301. }
  302. delete[] fertig;
  303. }
  304. void MiniGames::doMausEreignis( MausEreignis &me )
  305. {
  306. if( laden->istSichtbar() || !sichtbar )
  307. return;
  308. me.mx -= pos.x;
  309. me.my -= pos.y;
  310. if( !aktuell )
  311. {
  312. me.mx -= 200;
  313. me.my -= 400;
  314. updateH->doMausEreignis( 0, me, dg );
  315. me.mx += 200;
  316. me.my += 400;
  317. }
  318. if( alpha2 )
  319. {
  320. suchName->doMausEreignis( me );
  321. bool vera = me.verarbeitet;
  322. suchen->doMausEreignis( me );
  323. if( !vera && me.verarbeitet && me.id == ME_RLinks )
  324. filter();
  325. int anz = games->getEintragAnzahl();
  326. for( int i = 0; i < anz; i++ )
  327. {
  328. bool vera = me.verarbeitet;
  329. games->z( i )->doMausEreignis( me );
  330. if( !vera && me.verarbeitet && me.id == ME_RLinks )
  331. { // spiel starten
  332. laden->setSichtbar( 1 );
  333. if( mgl )
  334. mgl = mgl->release();
  335. mgl = new MGLaden( games->z( i )->zName()->getText() );
  336. }
  337. }
  338. }
  339. if( mgl && mgl->zGame() )
  340. mgl->zGame()->doMausEreignis( me );
  341. me.mx += pos.x;
  342. me.my += pos.y;
  343. }
  344. void MiniGames::doTastaturEreignis( TastaturEreignis &te )
  345. {
  346. if( laden->istSichtbar() || !sichtbar )
  347. return;
  348. if( alpha2 )
  349. {
  350. bool vera = te.verarbeitet;
  351. suchName->doTastaturEreignis( te );
  352. if( !vera && te.verarbeitet && te.taste == T_Enter && te.id == TE_Release )
  353. filter();
  354. }
  355. if( mgl && mgl->zGame() )
  356. mgl->zGame()->doTastaturEreignis( te );
  357. }
  358. bool MiniGames::tick( double z )
  359. {
  360. if( !aktuell )
  361. rend |= updateH->tick( 0, z, dg );
  362. if( laden->istSichtbar() && mgl && mgl->fertig() )
  363. {
  364. if( !mgl->zGame() )
  365. {
  366. mgl = mgl->release();
  367. nachLogin->zNachrichtenListe()->addNachricht( new Text( "Fehler" ), new Text( "Das Minigame konnte nicht geladen werden." ), new Text( "Ok" ) );
  368. }
  369. else
  370. {
  371. mgl->zGame()->setSchriftZ( schrift->getThis() );
  372. mgl->zGame()->setBildschirmZ( hauptScreen->getThis() );
  373. }
  374. laden->setSichtbar( 0 );
  375. }
  376. if( mgl && mgl->zGame() && !alpha2 )
  377. {
  378. if( sichtbar && !animation )
  379. rend |= mgl->zGame()->tick( z );
  380. if( mgl->zGame()->istEnde() )
  381. mgl = mgl->release();
  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. rend = 1;
  423. }
  424. if( ( animation | 0x2 ) == animation ) // ausblenden
  425. {
  426. if( alpha != 0 )
  427. {
  428. alpha -= val * 2;
  429. if( alpha < 0 )
  430. alpha = 0;
  431. }
  432. else
  433. {
  434. prozent2 += val;
  435. if( prozent2 > 100 )
  436. prozent2 = 100;
  437. pos = begPos + (Punkt)( ( ( Vec2< double > )( pos1 - begPos ) / 100.0 ) * prozent2 );
  438. gr = begGröße + (Punkt)( ( ( Vec2< double > )( größe1 - begGröße ) / 100.0 ) * prozent2 );
  439. if( prozent2 == 100 )
  440. {
  441. prozent2 = 0;
  442. animation &= ~0x2;
  443. sichtbar = 0;
  444. }
  445. }
  446. rend = 1;
  447. }
  448. if( mgl && alpha2 )
  449. {
  450. alpha2 -= val;
  451. if( alpha2 < 0 )
  452. alpha2 = 0;
  453. rend = 1;
  454. }
  455. if( !mgl && alpha2 != 255 )
  456. {
  457. alpha2 += val;
  458. if( alpha2 > 255 )
  459. alpha2 = 255;
  460. rend = 1;
  461. }
  462. bool ret = rend;
  463. rend = 0;
  464. return ret;
  465. }
  466. void MiniGames::render( Bild &zRObj )
  467. {
  468. if( pos == pos1 )
  469. return;
  470. rahmen->setPosition( pos );
  471. rahmen->setSize( gr );
  472. rahmen->render( zRObj );
  473. if( !zRObj.setDrawOptions( pos.x + 1, pos.y + 1, gr.x - 2, gr.y - 2 ) )
  474. return;
  475. int rbr = rahmen->getRBreite();
  476. zRObj.setAlpha( (unsigned char)alpha );
  477. zRObj.setAlpha( (unsigned char)alpha2 );
  478. suchFilter->render( zRObj );
  479. suchName->render( zRObj );
  480. suchen->render( zRObj );
  481. int anz = games->getEintragAnzahl();
  482. for( int i = 0; i < anz; i++ )
  483. games->z( i )->render( zRObj );
  484. if( !aktuell )
  485. updateH->render( 0, 200, 400, zRObj, dg );
  486. zRObj.releaseAlpha();
  487. laden->render( zRObj );
  488. if( mgl && mgl->fertig() && mgl->zGame() )
  489. mgl->zGame()->render( zRObj );
  490. zRObj.releaseAlpha();
  491. zRObj.releaseDrawOptions();
  492. }
  493. // constant
  494. bool MiniGames::istAnimiert() const
  495. {
  496. return animation != 0;
  497. }
  498. bool MiniGames::istSichtbar() const
  499. {
  500. return sichtbar || prozent1 != 0;
  501. }
  502. // Reference Counting
  503. MiniGames *MiniGames::getThis()
  504. {
  505. ref++;
  506. return this;
  507. }
  508. MiniGames *MiniGames::release()
  509. {
  510. ref--;
  511. if( !ref )
  512. delete this;
  513. return 0;
  514. }