123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
- #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>
- // Inhalt der Map Klasse aus Map.h
- // Konstruktor
- Map::Map()
- {
- schuss = new RCArray< Schuss >();
- ship = 0;
- asteroid = new RCArray< Asteroid >();
- aData = new Model2DData*[ 7 ]();
- aTextur = new Bild*[ 7 ]();
- sData = new Model2DData*[ 2 ]();
- sTextur = new Bild*[ 2 ]();
- M2Datei m2d( "data/Minigames/Asteroids/models/asteroids.m2" );
- m2d.leseDaten();
- 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 ) );
- aData[ i ] = m2d.ladeModel( name );
- aTextur[ i ] = td.laden( 0, new Text( (char*)( Text( name ) += ".png" ) ) );
- }
- m2d.setPfad( "data/Minigames/Asteroids/models/ship.m2" );
- m2d.leseDaten();
- 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 ) );
- sData[ i ] = m2d.ladeModel( name );
- sTextur[ i ] = td.laden( 0, new Text( (char*)( Text( name ) += ".png" ) ) );
- }
- kam = new LRahmen();
- kam->setFarbe( 0xFF777777 );
- map = new LRahmen();
- map->setFarbe( 0xFFFFFFFF );
- map->setPosition( 10, 10 );
- map->setSize( 200, 200 );
- shipN = 0;
- score = 0;
- breite = 0;
- höhe = 0;
- aGröße = 0;
- maxTimer = 0;
- mTimer = 0;
- timer = 0;
- beendet = 1;
- rend = 0;
- sr = 1;
- ref = 1;
- }
- // Destruktor
- Map::~Map()
- {
- speichern();
- schuss->release();
- if( ship )
- ship->release();
- asteroid->release();
- for( int i = 0; i < 7; i++ )
- {
- aData[ i ]->release();
- aTextur[ i ]->release();
- }
- for( int i = 0; i < 2; i++ )
- {
- sData[ i ]->release();
- sTextur[ i ]->release();
- }
- delete[] aData;
- delete[] aTextur;
- delete[] sData;
- delete[] sTextur;
- kam->release();
- map->release();
- }
- // nicht constant
- void Map::reset()
- {
- timer = 0;
- beendet = 0;
- score = 0;
- if( ship )
- ship = ship->release();
- schuss->leeren();
- asteroid->leeren();
- InitDatei *opd = new InitDatei( "data/Minigames/Asteroids/data/optionen.ini" );
- if( !opd->laden() )
- DateiPfadErstellen( "data/Minigames/Asteroids/data/optionen.ini" );
- breite = 800;
- if( opd->wertExistiert( "Breite" ) )
- breite = (int)*opd->zWert( "Breite" );
- else
- opd->addWert( "Breite", "800" );
- höhe = 500;
- if( opd->wertExistiert( "Höhe" ) )
- höhe = (int)*opd->zWert( "Höhe" );
- else
- opd->addWert( "Höhe", "500" );
- aGröße = 1000;
- if( opd->wertExistiert( "Größe" ) )
- aGröße = (int)*opd->zWert( "Größe" );
- else
- opd->addWert( "Größe", "1000" );
- maxTimer = 5000;
- if( opd->wertExistiert( "Timer" ) )
- maxTimer = (int)*opd->zWert( "Timer" );
- else
- opd->addWert( "Timer", "5000" );
- mTimer = maxTimer;
- shipN = 0;
- if( opd->wertExistiert( "Ship" ) )
- shipN = (int)*opd->zWert( "Ship" );
- else
- opd->addWert( "Ship", "0" );
- bool fortsetzen = 0;
- if( opd->wertExistiert( "Fortsetzen" ) )
- fortsetzen = (int)*opd->zWert( "Fortsetzen" ) != 0;
- else
- opd->addWert( "Fortsetzen", "0" );
- opd->speichern();
- opd->release();
- Vec2< float > shipPos( (float)( breite / 2 ), (float)( höhe / 2 ) );
- Vec2< float > shipSpeed( 0, 0 );
- float shipR = (float)-PI / 2;
- if( fortsetzen && DateiExistiert( "data/Minigames/Asteroids/data/game.save" ) )
- {
- 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;
- 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 );
- save->lese( (char*)&mTimer, 4 );
- // Schiff laden
- 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 );
- 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 );
- asteroid->add( new Asteroid( aData[ n ]->getThis(), aTextur[ n ]->getThis(),
- 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 );
- schuss->add( new Schuss( Vec2< float >( x, y ), Vec2< float >( xs, ys ) ) );
- }
- }
- save->close();
- save->release();
- }
- ship = new Ship( sData[ shipN ]->getThis(), sTextur[ shipN ]->getThis(), shipPos, shipSpeed, shipR );
- }
- void Map::doMausEreignis( MausEreignis &me )
- {
- }
- void Map::doTastaturEreignis( TastaturEreignis &te )
- {
- if( te.taste == T_Space && te.id == TE_Press )
- schuss->add( ship->getSchuss() );
- }
- bool Map::tick( double tickVal )
- {
- if( beendet )
- {
- bool ret = rend;
- rend = 0;
- return ret;
- }
- if( sr )
- srand( (int)time( 0 ) );
- sr = 0;
- // Timer
- timer -= tickVal;
- if( timer <= 0 )
- {
- mTimer -= 25;
- if( mTimer < 500 )
- mTimer = 500;
- timer = mTimer / 1000.0;
- for( int i = 0; i < rand() % 4 + 1; i++ )
- {
- int num = rand() % 7;
- double sw = ( rand() % 360 ) / 180.0 * PI;
- Vec2< float > speed( (float)cos( sw ), (float)sin( sw ) );
- speed *= (float)( rand() % 40 );
- Vec2< float > pos( -200.f, höhe / 2.f );
- if( speed.x < 0 )
- pos.x = (float)( breite + 200 );
- asteroid->add( new Asteroid( aData[ num ]->getThis(), aTextur[ num ]->getThis(), pos, speed, ( rand() % 100 ) / 75.f, (float)sw, aGröße / 1000.f, num ) );
- }
- }
- // Update
- int aAnz = asteroid->getEintragAnzahl();
- for( int i = 0; i < aAnz; i++ )
- asteroid->z( i )->tick( tickVal, breite, höhe );
- int sAnz = schuss->getEintragAnzahl();
- for( int i = 0; i < sAnz; i++ )
- schuss->z( i )->tick( tickVal );
- ship->tick( tickVal, breite, höhe );
- for( int i = aAnz - 1; i >= 0; i-- )
- {
- if( !asteroid->z( i )->amLeben() )
- {
- score++;
- asteroid->remove( i );
- aAnz--;
- }
- }
- for( int i = sAnz - 1; i >= 0; i-- )
- {
- Vec2< float > pos = schuss->z( i )->getPos();
- if( pos.x < 0 || pos.y < 0 || pos.x > breite || pos.y > höhe )
- {
- sAnz--;
- schuss->remove( i );
- }
- }
- // Collision
- for( int i = sAnz - 1; i >= 0; i-- )
- {
- Schuss *zs = schuss->z( i );
- bool b = 0;
- for( int j = 0; j < aAnz; j++ )
- b |= asteroid->z( j )->istGetroffen( zs );
- if( b )
- {
- schuss->remove( i );
- sAnz--;
- }
- }
- for( int i = 0; i < aAnz && !beendet; i++ )
- beendet |= ship->istTod( asteroid->z( i ) );
- if( beendet )
- {
- 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" );
- }
- return 1;
- }
- void Map::render( Bild &zRObj )
- {
- Punkt kamP = ship->getKamPos( breite, höhe );
- zRObj.addScrollOffset( kamP.x, kamP.y );
- int aAnz = asteroid->getEintragAnzahl();
- for( int i = 0; i < aAnz; i++ )
- asteroid->z( i )->render( zRObj );
- int sAnz = schuss->getEintragAnzahl();
- for( int i = 0; i < sAnz; i++ )
- schuss->z( i )->render( zRObj );
- ship->render( zRObj );
- zRObj.addScrollOffset( -kamP.x, -kamP.y );
- if( breite >= 800 || höhe >= 500 )
- { // Minimap
- const Punkt &dOff = zRObj.getDrawOff();
- map->render( zRObj );
- zRObj.setPixelDP( 10 + ( ship->getPos().x * 200 ) / breite + dOff.x, 10 + ( ship->getPos().y * 200 ) / höhe + dOff.y, 0xFF00FF00 );
- for( int i = 0; i < aAnz; i++ )
- zRObj.setPixelDP( 10 + ( asteroid->z( i )->getPos().x * 200 ) / breite + dOff.x, 10 + ( asteroid->z( i )->getPos().y * 200 ) / höhe + dOff.y, 0xFFFF0000 );
- for( int i = 0; i < sAnz; i++ )
- zRObj.setPixelDP( 10 + ( (int)schuss->z( i )->getPos().x * 200 ) / breite + dOff.x, 10 + ( (int)schuss->z( i )->getPos().y * 200 ) / höhe + dOff.y, 0xFF00FFFF );
- kam->setPosition( 10 + ( 200 * kamP.x ) / breite, 10 + ( 200 * kamP.y ) / höhe );
- kam->setSize( ( 200 * 800 ) / breite, ( 200 * 500 ) / höhe );
- if( kam->getBreite() > 200 )
- kam->setSize( 200, kam->getHeight() );
- if( kam->getHeight() > 200 )
- kam->setSize( kam->getBreite(), 200 );
- kam->render( zRObj );
- }
- }
- // constant
- void Map::speichern() const
- {
- if( !beendet )
- {
- Datei *d = new Datei();
- d->setDatei( "data/Minigames/Asteroids/data/game.save" );
- d->erstellen();
- d->open( Datei::Style::schreiben );
- 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 );
- int anz = asteroid->getEintragAnzahl();
- 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();
- }
- else
- DateiRemove( "data/Minigames/Asteroids/data/game.save" );
- }
- int Map::getScore() const
- {
- return score;
- }
- bool Map::istBeendet() const
- {
- return beendet;
- }
- // Reference Counting
- Map *Map::getThis()
- {
- ref++;
- return this;
- }
- Map *Map::release()
- {
- ref--;
- if( !ref )
- delete this;
- return 0;
- }
|