123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502 |
- #include "Map.h"
- #include <InitDatei.h>
- #include <Datei.h>
- #include <Text.h>
- #include <M2Datei.h>
- #include <DateiSystem.h>
- #include <TastaturEreignis.h>
- #include <KSGTDatei.h>
- #include <Zeit.h>
- #include <Rahmen.h>
- #include <Globals.h>
- #include <AsynchronCall.h>
- #include <Animation.h>
- // Inhalt der Map Klasse aus Map.h
- // Konstruktor
- Map::Map( KSGClient::MinigameServerClient *klient )
- : GameData( "data/Minigames/Asteroids/models/ship.m2", "data/Minigames/Asteroids/models/asteroids.m2" )
- {
- kamera = new Kamera2D();
- kamera->setWelt( world->getThis(), 1 );
- kamera->setSize( 800, 500 );
- mapKamera = new Kamera2D();
- mapKamera->setWelt( world->getThis(), 0 );
- this->klient = klient;
- aTextur = new Bild*[ 7 ]();
- sTextur = new Bild*[ 2 ]();
- // lade Flammen animation
- LTDBDatei flammenStartLTDB;
- flammenStartLTDB.setDatei( new Text( "data/Minigames/Asteroids/bilder/f_start.ltdb" ) );
- flammenStartLTDB.leseDaten( 0 );
- Animation2DData *flammenStart = new Animation2DData();
- flammenStart->ladeAnimation( flammenStartLTDB.getThis() );
- flammenStart->setFPS( 60 );
- flammenStart->setWiederhohlend( 0 );
- LTDBDatei flammenLTDB;
- flammenLTDB.setDatei( new Text( "data/Minigames/Asteroids/bilder/f_burn.ltdb" ) );
- Animation2DData *flammenBurn = new Animation2DData();
- flammenBurn->ladeAnimation( flammenLTDB.getThis() );
- flammenBurn->setFPS( 60 );
- flammenBurn->setWiederhohlend( 1 );
- flammenM = new Textur2D();
- flammenM->addAnimationZ( flammenStart->getThis() );
- flammenM->addAnimationZ( flammenBurn->getThis() );
- flammenL = new Textur2D();
- flammenL->addAnimationZ( flammenStart->getThis() );
- flammenL->addAnimationZ( flammenBurn->getThis() );
- flammenR = new Textur2D();
- flammenR->addAnimationZ( flammenStart );
- flammenR->addAnimationZ( flammenBurn );
- LTDBDatei td;
- td.setDatei( new Text( "data/Minigames/Asteroids/bilder/asteroids.ltdb" ) );
- td.leseDaten( 0 );
- for( int i = 0; i < 7; i++ )
- {
- Text name = "";
- name.append( (char)( 'a' + i ) );
- aTextur[ i ] = td.laden( 0, new Text( (char*)( Text( name ) += ".png" ) ) );
- }
- td.setDatei( new Text( "data/Minigames/Asteroids/bilder/ship.ltdb" ) );
- td.leseDaten( 0 );
- for( int i = 0; i < 2; i++ )
- {
- Text name = "";
- name.append( (char)( 'a' + i ) );
- sTextur[ i ] = td.laden( 0, new Text( (char*)( Text( name ) += ".png" ) ) );
- }
- map = new LRahmen();
- map->setFarbe( 0xFFFFFFFF );
- map->setPosition( 10, 10 );
- map->setSize( 200, 200 );
- mapKamera->setPosition( 10, 10 );
- mapKamera->setSize( 200, 200 );
- autoKam = 0;
- }
- // Destruktor
- Map::~Map()
- {
- for( int i = 0; i < 7; i++ )
- aTextur[ i ]->release();
- for( int i = 0; i < 2; i++ )
- sTextur[ i ]->release();
- delete[] aTextur;
- delete[] sTextur;
- map->release();
- if( klient )
- klient->release();
- flammenM->release();
- flammenL->release();
- flammenR->release();
- if( kamera )
- kamera->release();
- if( mapKamera )
- mapKamera->release();
- }
- Asteroid *Map::createNewAsteroid()
- {
- Asteroid *a = __super::createNewAsteroid();
- if( a )
- {
- Textur2D *text = new Textur2D();
- text->setTexturZ( aTextur[ a->getId() ]->getThis() );
- a->setTextur( text );
- }
- return a;
- }
- // nicht constant
- void Map::reset( Text *zOptionen )
- {
- flammenL->setAnimation( -1 );
- flammenR->setAnimation( -1 );
- flammenM->setAnimation( -1 );
- __super::reset( zOptionen );
- world->setSize( breite, höhe );
- world->setSize( 1 );
- world->setCircular( 1 );
- Text *tmp = zOptionen->getTeilText( zOptionen->positionVon( '=', 5 ) + 1, zOptionen->positionVon( ',', 5 ) );
- bool fortsetzen = (int)*tmp != 0;
- tmp->release();
- tmp = zOptionen->getTeilText( zOptionen->positionVon( '=', 6 ) + 1, zOptionen->positionVon( ',', 6 ) > 0 ? zOptionen->positionVon( ',', 6 ) : zOptionen->getLength() );
- autoKam = (int)*tmp != 0;
- if( !autoKam )
- kamera->setDrehung( 0 );
- tmp->release();
- if( fortsetzen && DateiExistiert( "data/Minigames/Asteroids/data/game.save" ) && klient )
- {
- M2Datei sad;
- int index = 0;
- int sadI = 0;
- Text pfad = "data/Minigames/Asteroids/data/sasave_";
- pfad += sadI;
- pfad += ".m2";
- sad.setPfad( pfad );
- sad.leseDaten();
- if( capture.istOffen() )
- capture.close();
- capture.setDatei( "data/Minigames/Asteroids/data/game.mgc" );
- capture.open( Datei::Style::schreiben | Datei::Style::ende | Datei::Style::lesen );
- Datei *save = new Datei();
- save->setDatei( "data/Minigames/Asteroids/data/game.save" );
- save->open( Datei::Style::lesen );
- int br = 0;
- int hö = 0;
- int gr = 0;
- int shn = 0;
- __int64 seed;
- if( rGen )
- rGen = rGen->release();
- save->lese( (char*)&seed, 8 );
- rGen = new RandomGenerator();
- rGen->setSeed( seed );
- save->lese( (char*)&gameTime, 8 );
- save->lese( (char*)&br, 4 );
- save->lese( (char*)&hö, 4 );
- save->lese( (char*)&gr, 4 );
- save->lese( (char*)&shn, 4 );
- if( br == breite && hö == höhe && gr == aGröße && shn == shipN )
- {
- save->lese( (char*)&score, 4 );
- scoreCheck = score * 11197;
- save->lese( (char*)&mTimer, 4 );
- // Schiff laden
- Vec2< float > shipPos( (float)( breite / 2 ), (float)( höhe / 2 ) );
- Vec2< float > shipSpeed( 0, 0 );
- float shipR = (float)-PI / 2;
- float shipRSpeed = 0;
- save->lese( (char*)&shipPos.x, 4 );
- save->lese( (char*)&shipPos.y, 4 );
- save->lese( (char*)&shipSpeed.x, 4 );
- save->lese( (char*)&shipSpeed.y, 4 );
- save->lese( (char*)&shipR, 4 );
- save->lese( (char*)&shipRSpeed, 4 );
- ship->setPRS( shipPos, shipSpeed, shipR, shipRSpeed );
- int anz = 0;
- save->lese( (char*)&anz, 4 );
- for( int i = 0; i < anz; i++ )
- { // Asteroiden Laden
- char n = 0;
- float x = 0;
- float y = 0;
- float xs = 0;
- float ys = 0;
- float rs = 0;
- float r = 0;
- float gr = 0;
- save->lese( &n, 1 );
- save->lese( (char*)&x, 4 );
- save->lese( (char*)&y, 4 );
- save->lese( (char*)&xs, 4 );
- save->lese( (char*)&ys, 4 );
- save->lese( (char*)&rs, 4 );
- save->lese( (char*)&r, 4 );
- save->lese( (char*)&gr, 4 );
- Bild *at = 0;
- Model2DData *ad = 0;
- if( n < 0 )
- {
- at = aTextur[ -n - 1 ]->getThis();
- ad = sad.ladeModel( sad.zModelName( index++ )->getText() );
- if( index == 120 )
- {
- index = 0;
- sadI++;
- Text pfad = "data/Minigames/Asteroids/data/sasave_";
- pfad += sadI;
- pfad += ".m2";
- sad = M2Datei();
- sad.setPfad( pfad );
- sad.leseDaten();
- }
- }
- else
- {
- at = aTextur[ n ]->getThis();
- ad = aData[ n ]->getThis();
- }
- asteroid->add( new Asteroid( ad, at,
- Vec2< float >( x, y ), Vec2< float >( xs, ys ), rs, r, gr, n ) );
- }
- save->lese( (char*)&anz, 4 );
- for( int i = 0; i < anz; i++ )
- { // Schüsse Laden
- float x = 0;
- float y = 0;
- float xs = 0;
- float ys = 0;
- save->lese( (char*)&x, 4 );
- save->lese( (char*)&y, 4 );
- save->lese( (char*)&xs, 4 );
- save->lese( (char*)&ys, 4 );
- if( schuss->getEintragAnzahl() > 100 )
- {
- Schuss *newS = new Schuss( Vec2< float >( x, y ), Vec2< float >( xs, ys ) );
- schuss->add( newS );
- world->addObject( newS->getThis() );
- }
- }
- }
- save->close();
- save->release();
- }
- else
- {
- if( klient )
- {
- if( capture.istOffen() )
- capture.close();
- DateiRemove( "data/Minigames/Asteroids/data/game.mgc" );
- capture.setDatei( "data/Minigames/Asteroids/data/game.mgc" );
- capture.erstellen();
- capture.open( Datei::Style::schreiben );
- __int64 seed = rGen->getSeed();
- capture.schreibe( (char*)&seed, 8 );
- }
- }
- ship->setTexture( flammenM, flammenL, flammenR, sTextur[ shipN ]->getThis() );
- }
- void Map::doMausEreignis( MausEreignis &me )
- {}
- void Map::doTastaturEreignis( TastaturEreignis &te )
- {
- cs.lock();
- bool ok = 1;
- if( !beendet )
- {
- char tmp = tastenStände;
- if( te.taste == 'w' || te.taste == 'W' || te.taste == T_Oben )
- {
- if( te.id == TE_Press )
- {
- if( (tastenStände | 1) != tastenStände )
- flammenM->setAnimation( 0 );
- tastenStände |= 1;
- }
- else
- {
- tastenStände &= ~1;
- flammenM->setAnimation( -1 );
- }
- }
- if( te.taste == 'd' || te.taste == 'D' || te.taste == T_Rechts )
- {
- if( te.id == TE_Press )
- {
- if( ( tastenStände | 2 ) != tastenStände )
- flammenL->setAnimation( 0 );
- tastenStände |= 2;
- }
- else
- {
- tastenStände &= ~2;
- flammenL->setAnimation( -1 );
- }
- }
- if( te.taste == 'a' || te.taste == 'A' || te.taste == T_Links )
- {
- if( te.id == TE_Press )
- {
- if( ( tastenStände | 4 ) != tastenStände )
- flammenR->setAnimation( 0 );
- tastenStände |= 4;
- }
- else
- {
- tastenStände &= ~4;
- flammenR->setAnimation( -1 );
- }
- }
- if( te.taste == T_Space )
- {
- if( te.id == TE_Press )
- {
- if( ( tastenStände | 8 ) != tastenStände )
- ok = 0;
- tastenStände |= 8;
- }
- else
- tastenStände &= ~8;
- }
- if( tmp != tastenStände && klient )
- {
- if( klient )
- {
- capture.schreibe( (char*)&gameTime, 8 );
- capture.schreibe( &tastenStände, 1 );
- }
- }
- }
- if( te.taste == T_Space && te.id == TE_Press )
- {
- if( klient && ok )
- {
- capture.schreibe( (char*)&gameTime, 8 );
- capture.schreibe( "\x10", 1 );
- }
- if( schuss->getEintragAnzahl() < 100 )
- {
- Schuss *newS = ship->getSchuss();
- schuss->add( newS );
- world->addObject( newS->getThis() );
- }
- }
- cs.unlock();
- }
- bool Map::tick( double t )
- {
- if( beendet )
- return 0;
- bool ret = __super::tick( t );
- ret |= kamera->tick( t );
- if( beendet && klient )
- {
- capture.close();
- DateiRemove( "data/Minigames/Asteroids/data/upload.mgc" );
- DateiUmbenennen( "data/Minigames/Asteroids/data/game.mgc", "data/Minigames/Asteroids/data/upload.mgc" );
- int tmpScore = score;
- KSGClient::MinigameServerClient *tmpKlient = klient->getThis();
- new AsynchronCall( [ tmpScore, tmpKlient ]()
- {
- InitDatei *opd = new InitDatei( "data/Minigames/Asteroids/data/optionen.ini" );
- opd->laden();
- Text optionen = "Width=";
- optionen += opd->zWert( "Breite" )->getText();
- optionen += ",Height=";
- optionen += opd->zWert( "Höhe" )->getText();
- optionen += ",Size=";
- optionen += opd->zWert( "Größe" )->getText();
- optionen += ",Timer=";
- optionen += opd->zWert( "Timer" )->getText();
- optionen += ",Ship=";
- optionen += opd->zWert( "Ship" )->getText();
- opd->release();
- Datei d;
- d.setDatei( "data/Minigames/Asteroids/data/upload.mgc" );
- tmpKlient->reportEndOfGame( "Asteroids", optionen, tmpScore, &d );
- DateiRemove( "data/Minigames/Asteroids/data/upload.mgc" );
- tmpKlient->release();
- } );
- KSGTDatei *stb = new KSGTDatei( "data/Minigames/Asteroids/data/score.ksgt" );
- if( !stb->laden() )
- DateiPfadErstellen( "data/Minigames/Asteroids/data/score.ksgt" );
- RCArray< Text > *zeile = new RCArray< Text >();
- Zeit *zeit = getZeit();
- zeile->add( zeit->getZeit( "y-m-d h:i:s" ) );
- zeit->release();
- Text *scoreT = new Text();
- scoreT->append( score );
- zeile->add( scoreT );
- Text *breiteT = new Text();
- breiteT->append( breite );
- zeile->add( breiteT );
- Text *höheT = new Text();
- höheT->append( höhe );
- zeile->add( höheT );
- Text *timerT = new Text();
- timerT->append( maxTimer );
- zeile->add( timerT );
- Text *aGrößeT = new Text();
- aGrößeT->append( aGröße );
- zeile->add( aGrößeT );
- Text *shipT = new Text();
- shipT->append( shipN );
- zeile->add( shipT );
- stb->addZeile( 7, zeile );
- zeile->release();
- stb->speichern();
- stb->release();
- DateiRemove( "data/Minigames/Asteroids/data/game.save" );
- }
- ret |= flammenM->tick( t );
- ret |= flammenL->tick( t );
- ret |= flammenR->tick( t );
- return ret;
- }
- void Map::render( Bild &zRObj )
- {
- cs.lock();
- kamera->lookAtWorldPos( (int)ship->getPosition().x, (int)ship->getPosition().y );
- if( autoKam )
- kamera->setDrehung( -ship->getDrehung() );
- kamera->render( zRObj );
- if( breite >= 800 || höhe >= 500 )
- { // Minimap
- zRObj.setAlpha( 50 );
- if( breite > höhe )
- mapKamera->setSize( 200, (int)( ( 200.0 / world->getWorldInfo().size.x ) * world->getWorldInfo().size.y ) );
- else
- mapKamera->setSize( (int)( ( 200.0 / world->getWorldInfo().size.y ) * world->getWorldInfo().size.x ), 200 );
- mapKamera->lookAtWorldPos( world->getWorldInfo().size.x / 2, world->getWorldInfo().size.y / 2 );
- mapKamera->lookAtWorldArea( world->getWorldInfo().size.x, world->getWorldInfo().size.y );
- mapKamera->render( zRObj );
- map->setSize( mapKamera->getBreite(), mapKamera->getHeight() );
- map->render( zRObj );
- zRObj.releaseAlpha();
- }
- cs.unlock();
- }
- void Map::speichern()
- {
- if( !beendet && klient )
- {
- if( capture.istOffen() )
- capture.close();
- cs.lock();
- int anz = asteroid->getEintragAnzahl();
- int index = 0;
- for( int i = 0; index < anz; i++ )
- {
- M2Datei sad;
- Text pfad = "data/Minigames/Asteroids/data/sasave_";
- pfad += i;
- pfad += ".m2";
- DateiRemove( pfad );
- sad.setPfad( pfad );
- sad.leseDaten();
- char name[ 2 ];
- name[ 1 ] = 0;
- for( char n = 1; n <= 120 && index < anz; n++ )
- {
- name[ 0 ] = n;
- if( asteroid->z( index++ )->getId() < 0 )
- sad.saveModel( asteroid->z( index - 1 )->zModel(), name );
- else
- n--;
- }
- }
- Datei *d = new Datei();
- d->setDatei( "data/Minigames/Asteroids/data/game.save" );
- d->erstellen();
- d->open( Datei::Style::schreiben );
- __int64 seed = rGen->getSeed();
- d->schreibe( (char*)&seed, 8 );
- d->schreibe( (char*)&gameTime, 8 );
- d->schreibe( (char*)&breite, 4 );
- d->schreibe( (char*)&höhe, 4 );
- d->schreibe( (char*)&aGröße, 4 );
- d->schreibe( (char*)&shipN, 4 );
- d->schreibe( (char*)&score, 4 );
- d->schreibe( (char*)&mTimer, 4 );
- ship->save( d );
- d->schreibe( (char*)&anz, 4 );
- for( int i = 0; i < anz; i++ )
- asteroid->z( i )->save( d );
- anz = schuss->getEintragAnzahl();
- d->schreibe( (char*)&anz, 4 );
- for( int i = 0; i < anz; i++ )
- schuss->z( i )->save( d );
- d->close();
- d->release();
- cs.unlock();
- }
- else if( klient )
- DateiRemove( "data/Minigames/Asteroids/data/game.save" );
- }
|