123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- #include "Asteroid.h"
- #include <Text.h>
- #include <Globals.h>
- #include <TastaturEreignis.h>
- #include "Schuss.h"
- #include <iostream>
- #include <Textur2D.h>
- #include "Schuss.h"
- // Inhalt der Asteroid Klasse aus Asteroid.h
- // Konstruktor
- Asteroid::Asteroid( Model2DData *data, Bild *textur, Vec2< float > p, Vec2< float > s, float rS, float r, float gr, char num )
- {
- txt = 0;
- asteroid = new Model2D();
- asteroid->setStyle( Model2D::Style::Sichtbar | Model2D::Style::Textur );
- asteroid->setModel( data );
- asteroid->setDrehung( r );
- asteroid->setSize( gr );
- if( textur )
- setTextur( textur );
- pos = p;
- speed = s;
- rSpeed = rS;
- id = num;
- ref = 1;
- }
- // Destruktor
- Asteroid::~Asteroid()
- {
- if( txt )
- txt->release();
- asteroid->release();
- }
- // nicht constant
- void Asteroid::setTextur( Bild *textur )
- {
- if( txt )
- txt->release();
- txt = textur->getThis();
- Textur2D *txt = new Textur2D();
- txt->setTexturZ( textur );
- asteroid->setTextur( txt->getThis() );
- }
- bool Asteroid::tick( double zeit, int breite, int höhe )
- {
- Vertex minP = (Vertex)asteroid->zModel()->minP * asteroid->getSize() + asteroid->getPosition();
- Vertex maxP = (Vertex)asteroid->zModel()->maxP * asteroid->getSize() + asteroid->getPosition();
- if( maxP.x < 0 && speed.x < 0 )
- pos.x += breite + asteroid->zModel()->maxP.x * asteroid->getSize();
- if( maxP.y < 0 && speed.y < 0 )
- pos.y += höhe + asteroid->zModel()->maxP.y * asteroid->getSize();
- if( minP.x > breite && speed.x > 0 )
- pos.x -= breite + asteroid->zModel()->maxP.x * asteroid->getSize();
- if( minP.y > höhe && speed.y > 0 )
- pos.y -= höhe + asteroid->zModel()->maxP.y * asteroid->getSize();
- asteroid->setPosition( pos );
- if( rSpeed )
- asteroid->addDrehung( rSpeed * (float)zeit );
- if( ( asteroid->zModel()->maxP - asteroid->zModel()->minP ).x < 50 || ( asteroid->zModel()->maxP - asteroid->zModel()->minP ).y < 50 )
- {
- asteroid->setSize( asteroid->getSize() - (float)zeit * 3 );
- if( asteroid->getSize() < 0 )
- asteroid->setSize( 0 );
- }
- if( speed.x || speed.y )
- {
- pos += speed * (float)zeit;
- asteroid->setPosition( pos );
- }
- return asteroid->tick( zeit );
- }
- void Asteroid::render( Bild &zRObj )
- {
- asteroid->render( zRObj );
- }
- bool Asteroid::istGetroffen( Schuss *zSchuss, Polygon2D &a, Polygon2D &b, Punkt &pa, Punkt &pb, RandomGenerator *zRand )
- {
- if( ( asteroid->zModel()->maxP - asteroid->zModel()->minP ).x < 50 || ( asteroid->zModel()->maxP - asteroid->zModel()->minP ).y < 50 )
- return 0;
- Vertex sp( 0, 0);
- float r = 0;
- Vertex hp;
- if( zSchuss->istInM2( *asteroid, sp, r, hp ) )
- {
- asteroid->zModel()->split( hp, sp.rotation( -asteroid->getDrehung() ) * 0.1f, "", a, b, pa, pb, [ zRand ]()
- {
- return zRand->rand();
- } );
- hp = ( hp * asteroid->getSize() ).rotation( asteroid->getDrehung() ) + asteroid->getPosition();
- speed += sp * 0.1f;
- rSpeed += r * 0.1f;
- zSchuss->getPos();
- return 1;
- }
- return 0;
- }
- void Asteroid::setDead()
- {
- asteroid->setSize( 0 );
- }
- // constant
- void Asteroid::save( Datei *zD ) const
- {
- zD->schreibe( (char*)&id, 1 );
- zD->schreibe( (char*)&pos.x, 4 );
- zD->schreibe( (char*)&pos.y, 4 );
- zD->schreibe( (char*)&speed.x, 4 );
- zD->schreibe( (char*)&speed.y, 4 );
- zD->schreibe( (char*)&rSpeed, 4 );
- float r = asteroid->getDrehung();
- zD->schreibe( (char*)&r, 4 );
- float gr = asteroid->getSize();
- zD->schreibe( (char*)&gr, 4 );
- }
- bool Asteroid::amLeben() const
- {
- return asteroid->getSize() != 0;
- }
- Model2D *Asteroid::zModel() const
- {
- return asteroid;
- }
- Punkt Asteroid::getPos() const
- {
- return (Punkt)pos;
- }
- Bild *Asteroid::getTextur() const
- {
- return txt ? txt->getThis() : 0;
- }
- Vec2< float > Asteroid::getSpeed() const
- {
- return speed;
- }
- float Asteroid::getRSpeed() const
- {
- return rSpeed;
- }
- char Asteroid::getId() const
- {
- return id;
- }
- int Asteroid::getScore() const
- {
- if( ( asteroid->zModel()->maxP - asteroid->zModel()->minP ).x < 50 || ( asteroid->zModel()->maxP - asteroid->zModel()->minP ).y < 50 )
- return 1;
- return 0;
- }
- // Refernece Counting
- Asteroid *Asteroid::getThis()
- {
- ref++;
- return this;
- }
- Asteroid *Asteroid::release()
- {
- ref--;
- if( !ref )
- delete this;
- return 0;
- }
|