Minigames.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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 = infoClient->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 = infoClient->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. if( !minigameClient )
  152. minigameClient = mainClient->createMinigameServerClient();
  153. if( minigameClient )
  154. game->setMinigameClientZ( minigameClient->getThis() );
  155. if( !game->laden() )
  156. {
  157. game = game->release();
  158. dllDateien->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. // Reference Counting
  172. MGLaden *MGLaden::getThis()
  173. {
  174. ref++;
  175. return this;
  176. }
  177. MGLaden *MGLaden::release()
  178. {
  179. ref--;
  180. if( !ref )
  181. delete this;
  182. return 0;
  183. }
  184. // Inhalt der MiniGames Klasse aus MiniGames.h
  185. // Konstruktor
  186. MiniGames::MiniGames( Schrift *zSchrift, Fenster *zNachLoginFenster, int x )
  187. : Zeichnung()
  188. {
  189. schrift = zSchrift->getThis();
  190. bildschirmGröße = BildschirmGröße();
  191. pos = Punkt( x, 67 );
  192. gr = Punkt( 102, 32 );
  193. rahmen = new LRahmen();
  194. rahmen->setFarbe( 0xFFFFFFFF );
  195. rahmen->setSize( 102, 32 );
  196. alpha = 0;
  197. alpha2 = 0;
  198. animation = 0;
  199. sichtbar = 0;
  200. tickVal = 0;
  201. prozent1 = 0;
  202. prozent2 = 0;
  203. begPos = Punkt( 0, 0 );
  204. begGröße = Punkt( 0, 0 );
  205. größe1 = Punkt( 102, 32 );
  206. pos1 = Punkt( x, 67 );
  207. größe2 = Punkt( 800, 500 );
  208. pos2 = bildschirmGröße / 2 - größe2 / 2;
  209. laden = (Animation2D*)ladeAnimation->dublizieren();
  210. laden->setSichtbar( 0 );
  211. laden->setPosition( 375, 225 );
  212. games = new RCArray< MiniGame >();
  213. suchFilter = initTextFeld( 10, 10, 100, 20, zSchrift, TextFeld::Style::Text | TextFeld::Style::VCenter, "Suchfilter:" );
  214. zSchrift->setSchriftSize( 12 );
  215. suchFilter->setSize( zSchrift->getTextBreite( suchFilter->zText() ), 20 );
  216. suchName = initTextFeld( 20 + suchFilter->getBreite(), 10, 200, 20, zSchrift, TextFeld::Style::TextFeld, "" );
  217. suchen = initKnopf( 230 + suchFilter->getBreite(), 10, 100, 20, zSchrift, Knopf::Style::Sichtbar, "Suchen" );
  218. gefiltert = 0;
  219. zNachLoginFenster->addMember( this );
  220. dg = 0;
  221. mgl = 0;
  222. ref = 1;
  223. new MGSuchen( getThis() );
  224. }
  225. // Destruktor
  226. MiniGames::~MiniGames()
  227. {
  228. if( schrift )
  229. schrift->release();
  230. rahmen->release();
  231. suchName->release();
  232. suchFilter->release();
  233. suchen->release();
  234. laden->release();
  235. games->release();
  236. if( mgl )
  237. mgl->release();
  238. }
  239. // nicht constant
  240. void MiniGames::setSichtbar( bool sicht )
  241. {
  242. begPos = pos;
  243. begGröße = gr;
  244. animation |= ( sicht ? 0x1 : 0x2 );
  245. rend = 1;
  246. }
  247. void MiniGames::addMiniGame( MiniGame *mg )
  248. {
  249. games->add( mg );
  250. if( gefiltert )
  251. filter();
  252. else
  253. {
  254. int i = games->getEintragAnzahl() - 1;
  255. games->z( i )->setPosition( 10 + 10 * ( i % 3 ) + 250 * ( i % 3 ), 50 + 10 * ( i / 3 ) + 100 * ( i / 3 ) );
  256. }
  257. if( dg && updateH->hat( 0, dg ) )
  258. updateH->remove( 0, dg );
  259. }
  260. void MiniGames::setAktuell( bool aktuell, int dg )
  261. {
  262. this->aktuell = aktuell;
  263. if( aktuell )
  264. new MGSuchen( getThis() );
  265. if( !this->dg )
  266. this->dg = dg;
  267. if( !aktuell && !updateH->hat( 0, this->dg ) )
  268. updateH->erstellen( schrift, 0, this->dg );
  269. if( !aktuell )
  270. updateH->setSichtbar( 0, 1, this->dg );
  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( !aktuell )
  314. {
  315. me.mx -= 200;
  316. me.my -= 400;
  317. updateH->doMausEreignis( 0, me, dg );
  318. me.mx += 200;
  319. me.my += 400;
  320. }
  321. if( alpha2 )
  322. {
  323. suchName->doMausEreignis( me );
  324. bool vera = me.verarbeitet;
  325. suchen->doMausEreignis( me );
  326. if( !vera && me.verarbeitet && me.id == ME_RLinks )
  327. filter();
  328. int anz = games->getEintragAnzahl();
  329. for( int i = 0; i < anz; i++ )
  330. {
  331. bool vera = me.verarbeitet;
  332. games->z( i )->doMausEreignis( me );
  333. if( !vera && me.verarbeitet && me.id == ME_RLinks )
  334. { // spiel starten
  335. laden->setSichtbar( 1 );
  336. if( mgl )
  337. mgl = mgl->release();
  338. mgl = new MGLaden( games->z( i )->zName()->getText() );
  339. }
  340. }
  341. }
  342. if( mgl && mgl->zGame() )
  343. mgl->zGame()->doMausEreignis( me );
  344. me.mx += pos.x;
  345. me.my += pos.y;
  346. }
  347. void MiniGames::doTastaturEreignis( TastaturEreignis &te )
  348. {
  349. if( laden->istSichtbar() || !sichtbar )
  350. return;
  351. if( alpha2 )
  352. {
  353. bool vera = te.verarbeitet;
  354. suchName->doTastaturEreignis( te );
  355. if( !vera && te.verarbeitet && te.taste == T_Enter && te.id == TE_Release )
  356. filter();
  357. }
  358. if( mgl && mgl->zGame() )
  359. mgl->zGame()->doTastaturEreignis( te );
  360. }
  361. bool MiniGames::tick( double z )
  362. {
  363. if( !aktuell )
  364. rend |= updateH->tick( 0, z, dg );
  365. if( laden->istSichtbar() && mgl && mgl->fertig() )
  366. {
  367. if( !mgl->zGame() )
  368. {
  369. mgl = mgl->release();
  370. nachLogin->zNachrichtenListe()->addNachricht( new Text( "Fehler" ), new Text( "Das Minigame konnte nicht geladen werden." ), new Text( "Ok" ) );
  371. }
  372. else
  373. {
  374. mgl->zGame()->setSchriftZ( schrift->getThis() );
  375. mgl->zGame()->setBildschirmZ( hauptScreen->getThis() );
  376. }
  377. laden->setSichtbar( 0 );
  378. }
  379. if( mgl && mgl->zGame() && !alpha2 )
  380. {
  381. if( sichtbar && !animation )
  382. rend |= mgl->zGame()->tick( z );
  383. if( mgl->zGame()->istEnde() )
  384. mgl = mgl->release();
  385. }
  386. rend |= laden->tick( z );
  387. if( alpha2 )
  388. {
  389. rend |= suchName->tick( z );
  390. rend |= suchen->tick( z );
  391. int anz = games->getEintragAnzahl();
  392. for( int i = 0; i < anz; i++ )
  393. rend |= games->z( i )->tick( z );
  394. }
  395. tickVal += z * 150;
  396. int val = (int)tickVal;
  397. if( val < 1 )
  398. {
  399. bool ret = rend;
  400. rend = 0;
  401. return ret;
  402. }
  403. tickVal -= val;
  404. if( ( animation | 0x1 ) == animation ) // Einblenden
  405. {
  406. if( prozent1 != 100 )
  407. {
  408. prozent1 += val;
  409. if( prozent1 >= 100 )
  410. prozent1 = 100;
  411. pos = begPos + (Punkt)( ( ( Vec2< double > )( pos2 - begPos ) / 100.0 ) * prozent1 );
  412. gr = begGröße + (Punkt)( ( ( Vec2< double > )( größe2 - begGröße ) / 100.0 ) * prozent1 );
  413. }
  414. else if( alpha != 255 )
  415. {
  416. alpha += val * 2;
  417. if( alpha >= 255 || ( animation | 0x2 ) == animation )
  418. {
  419. alpha = 255;
  420. animation &= ~0x1;
  421. sichtbar = 1;
  422. prozent1 = 0;
  423. }
  424. }
  425. rend = 1;
  426. }
  427. if( ( animation | 0x2 ) == animation ) // ausblenden
  428. {
  429. if( alpha != 0 )
  430. {
  431. alpha -= val * 2;
  432. if( alpha < 0 )
  433. alpha = 0;
  434. }
  435. else
  436. {
  437. prozent2 += val;
  438. if( prozent2 > 100 )
  439. prozent2 = 100;
  440. pos = begPos + (Punkt)( ( ( Vec2< double > )( pos1 - begPos ) / 100.0 ) * prozent2 );
  441. gr = begGröße + (Punkt)( ( ( Vec2< double > )( größe1 - begGröße ) / 100.0 ) * prozent2 );
  442. if( prozent2 == 100 )
  443. {
  444. prozent2 = 0;
  445. animation &= ~0x2;
  446. sichtbar = 0;
  447. }
  448. }
  449. rend = 1;
  450. }
  451. if( mgl && alpha2 )
  452. {
  453. alpha2 -= val;
  454. if( alpha2 < 0 )
  455. alpha2 = 0;
  456. rend = 1;
  457. }
  458. if( !mgl && alpha2 != 255 )
  459. {
  460. alpha2 += val;
  461. if( alpha2 > 255 )
  462. alpha2 = 255;
  463. rend = 1;
  464. }
  465. bool ret = rend;
  466. rend = 0;
  467. return ret;
  468. }
  469. void MiniGames::render( Bild &zRObj )
  470. {
  471. if( pos == pos1 )
  472. return;
  473. rahmen->setPosition( pos );
  474. rahmen->setSize( gr );
  475. rahmen->render( zRObj );
  476. if( !zRObj.setDrawOptions( pos.x + 1, pos.y + 1, gr.x - 2, gr.y - 2 ) )
  477. return;
  478. int rbr = rahmen->getRBreite();
  479. zRObj.setAlpha( (unsigned char)alpha );
  480. zRObj.setAlpha( (unsigned char)alpha2 );
  481. suchFilter->render( zRObj );
  482. suchName->render( zRObj );
  483. suchen->render( zRObj );
  484. int anz = games->getEintragAnzahl();
  485. for( int i = 0; i < anz; i++ )
  486. games->z( i )->render( zRObj );
  487. if( !aktuell )
  488. updateH->render( 0, 200, 400, zRObj, dg );
  489. zRObj.releaseAlpha();
  490. laden->render( zRObj );
  491. if( mgl && mgl->fertig() && mgl->zGame() )
  492. mgl->zGame()->render( zRObj );
  493. zRObj.releaseAlpha();
  494. zRObj.releaseDrawOptions();
  495. }
  496. // constant
  497. bool MiniGames::istAnimiert() const
  498. {
  499. return animation != 0;
  500. }
  501. bool MiniGames::istSichtbar() const
  502. {
  503. return sichtbar || prozent1 != 0;
  504. }
  505. // Reference Counting
  506. MiniGames *MiniGames::getThis()
  507. {
  508. ref++;
  509. return this;
  510. }
  511. MiniGames *MiniGames::release()
  512. {
  513. ref--;
  514. if( !ref )
  515. delete this;
  516. return 0;
  517. }