#include "Asteroid.h"

Asteroid::Asteroid( int id, Vertex pos, Vertex speed, float rot, float rotS, Model2DData *data )
    : Model2DObject()
{
    this->id = id;
    setPosition( pos );
    setSpeed( speed );
    setDrehung( rot );
    setDrehungSpeed( rotS );
    setModel( data );
}

bool Asteroid::tick( const WeltInfo &info, double zeit )
{
    wInfo = info;
    return Model2DObject::tick( info, zeit );
}

Asteroid *Asteroid::istTreffer( Laser *zL, RandomGenerator *zRandG, int &asteroidId, Vertex &pos )
{
    Vertex hp;
    for( int i = 0; i < 3; i++ )
    {
        for( int j = 0; j < 3; j++ )
        {
            pos = zL->getPosition() + Punkt( ( i - 1 ) * wInfo.size.x, ( j - 1 ) * wInfo.size.y );
            if( istPunktInnen( pos ) && calcHitPoint( pos - zL->getSpeed(), zL->getSpeed(), hp ) )
            {
                Polygon2D a, b;
                Punkt pa, pb;
                if( zModel()->split( getObjectPos( hp ), getObjectDir( zL->getSpeed() ) * 0.1f, ( char* )"", a, b, pa, pb, [ zRandG ]() -> double
                {
                    return zRandG->rand();
                } ) )
                {
                    impuls( pos - zL->getSpeed(), zL->getSpeed() * 0.05f );
                    hp = ( hp * getSize() ).rotation( getDrehung() ) + getPosition();
                    Vertex pav = ( (Vertex)pa ).rotation( getDrehung() ) + getPosition();
                    Vertex pbv = ( (Vertex)pb ).rotation( getDrehung() ) + getPosition();
                    Array< Polygon2D > *npaA = new Array< Polygon2D >();
                    npaA->add( a );
                    Model2DData *npdA = new Model2DData();
                    npdA->erstelleModell( npaA );
                    Array< Polygon2D > *npaB = new Array< Polygon2D >();
                    npaB->add( b );
                    Model2DData *npdB = new Model2DData();
                    npdB->erstelleModell( npaB );
                    Asteroid *astr = new Asteroid( asteroidId++, pav, getSpeed() * (float)zRandG->rand(), getDrehung(), getDrehungSpeed() * (float)zRandG->rand(), npdA );
                    setSpeed( getSpeed() * (float)zRandG->rand() );
                    setDrehungSpeed( getDrehungSpeed() * (float)zRandG->rand() );
                    setModel( npdB );
                    setPosition( pbv );
                    return astr;
                }
            }
        }
    }
    return 0;
}

int Asteroid::getId() const
{
    return id;
}