#include "Laser.h"
#include "../Karte/Karte.h"
#include "../Spieler/Spieler.h"
#include "../Define.h"

// Inhalt der Laser Klasse aus Laser.h
// Konstruktor
Laser::Laser( int id, Vertex pos, Vertex speed, int sNum, double intensit�t, int tf )
    : Object2D()
{
    this->id = id;
    setPosition( pos );
    setSpeed( speed );
    this->sNum = sNum;
    this->intensit�t = intensit�t;
    this->startIntensit�t = intensit�t;
    this->tf = tf;
    setCollision( 0 );
    save();
}

// privat
char Laser::getOutCode( Punkt& p, Bild &zrObj ) const
{
    char ret = 0;
    if( p.x < 0 )
        ret |= 1;
    else if( p.x >= zrObj.getDrawGr().x )
        ret |= 2;
    if( p.y < 0 )
        ret |= 4;
    else if( p.y >= zrObj.getDrawGr().y )
        ret |= 8;
    return ret;
}

// nicht constant
Rect2< float > Laser::getBoundingBox() const
{
    Rect2< float > r = Rect2< float >();
    r.topLeft.x = -abs( getSpeed().x );
    r.topLeft.y = -abs( getSpeed().y );
    r.bottomRight.x = abs( getSpeed().x );
    r.bottomRight.y = abs( getSpeed().y );
    r.topLeft = getWorldPos( r.topLeft );
    r.bottomRight = getWorldPos( r.bottomRight );
    return r;
}

bool Laser::tick( const WeltInfo &info, double tv )
{
    __super::tick( info, tv );
    intensit�t -= tv * 2;
    return 1;
}

void Laser::render( Mat3< float > &kamMat, Bild &zRObj, const char *kamName )
{
    int fa = (int)( ( intensit�t / startIntensit�t ) * 255 );
    int f = ( ( fa << 24 ) & 0xFF000000 ) | ( tf & 0xFFFFFF );
    Mat3< float > mat = kamMat * getObjectMatrix();
    Punkt a = (Punkt)( mat * Vertex( 0, 0 ) );
    Punkt b = (Punkt)( mat * ( getSpeed() / getSpeed().getLength() * 10 ) );
    zRObj.drawLinieBorderedAlpha( a, b, f, 0xFFFFFFFF );
}

void Laser::save()
{
    last.pos = getPosition();
    last.speed = getSpeed();
    last.intensity = intensit�t;
}

void Laser::load()
{
    intensit�t = last.intensity;
    setPosition( last.pos );
    setSpeed( last.speed );
}

// constant
int Laser::getId() const
{
    return id;
}

int Laser::getSpieler() const
{
    return sNum;
}

double Laser::getIntensit�t( Vertex targetSpeed ) const
{
    return intensit�t * ( ( speed - targetSpeed ).getLength() / 200 );
}