#include "PlayerKam.h"
#include "Globals.h"

PlayerKam::PlayerKam( Framework::Bildschirm3D* zScreen )
    : Kam3D()
{
    setBildschirmPosition( 0, 0 );
    setBildschirmSize( zScreen->getBackBufferSize() );
    setStyle( Kam3D::Style::Tick | Kam3D::Style::Movable | Kam3D::Style::Rotatable );
    setRotation( { 0, 0, 0 } );
}

void PlayerKam::setDirection( Framework::Vec2<float> direction )
{
    if( direction.getLengthSq() > 0 )
    {
        float rotZ = std::atan2( direction.y, direction.x ) + (float)PI / 2;
        setRotation( { (float)PI / 2.f, getRotation().y, rotZ } );
    }
}

void PlayerKam::doTastaturEreignis( Framework::TastaturEreignis& te )
{
    char action[ 2 ];
    if( te.id == TE_Press )
    {
        action[ 0 ] = 1;
        if( te.taste == 'w' || te.taste == 'W' )
        {
            action[ 1 ] = 0;
            network->zFactoryClient()->sendPlayerAction( action, 2 );
        }
        if( te.taste == 'a' || te.taste == 'A' )
        {
            action[ 1 ] = 1;
            network->zFactoryClient()->sendPlayerAction( action, 2 );
        }
        if( te.taste == 's' || te.taste == 'S' )
        {
            action[ 1 ] = 2;
            network->zFactoryClient()->sendPlayerAction( action, 2 );
        }
        if( te.taste == 'd' || te.taste == 'D' )
        {
            action[ 1 ] = 3;
            network->zFactoryClient()->sendPlayerAction( action, 2 );
        }
        if( te.taste == T_Shift )
        {
            action[ 1 ] = 4;
            network->zFactoryClient()->sendPlayerAction( action, 2 );
        }
        if( te.taste == 'q' || te.taste == 'Q' )
        {
            action[ 1 ] = 5;
            network->zFactoryClient()->sendPlayerAction( action, 2 );
        }
        if( te.taste == 'e' || te.taste == 'E' )
        {
            action[ 1 ] = 6;
            network->zFactoryClient()->sendPlayerAction( action, 2 );
        }
        if( te.taste == T_Space )
        {
            action[ 1 ] = 7;
            network->zFactoryClient()->sendPlayerAction( action, 2 );
        }
    }
    if( te.id == TE_Release )
    {
        action[ 0 ] = 0;
        if( te.taste == 'w' || te.taste == 'W' )
        {
            action[ 1 ] = 0;
            network->zFactoryClient()->sendPlayerAction( action, 2 );
        }
        if( te.taste == 'a' || te.taste == 'A' )
        {
            action[ 1 ] = 1;
            network->zFactoryClient()->sendPlayerAction( action, 2 );
        }
        if( te.taste == 's' || te.taste == 'S' )
        {
            action[ 1 ] = 2;
            network->zFactoryClient()->sendPlayerAction( action, 2 );
        }
        if( te.taste == 'd' || te.taste == 'D' )
        {
            action[ 1 ] = 3;
            network->zFactoryClient()->sendPlayerAction( action, 2 );
        }
        if( te.taste == T_Shift )
        {
            action[ 1 ] = 4;
            network->zFactoryClient()->sendPlayerAction( action, 2 );
        }
        if( te.taste == 'q' || te.taste == 'Q' )
        {
            action[ 1 ] = 5;
            network->zFactoryClient()->sendPlayerAction( action, 2 );
        }
        if( te.taste == 'e' || te.taste == 'E' )
        {
            action[ 1 ] = 6;
            network->zFactoryClient()->sendPlayerAction( action, 2 );
        }
        if( te.taste == T_Space )
        {
            action[ 1 ] = 7;
            network->zFactoryClient()->sendPlayerAction( action, 2 );
        }
    }
}

void PlayerKam::doMausEreignis( Framework::MausEreignis& me )
{

}

bool PlayerKam::tick( double time )
{
    __int64 style = 0;
    if( hatStyle( Style::Movable ) )
        style |= Style::Movable;
    if( hatStyle( Style::Rotatable ) )
        style |= Style::Rotatable;
    if( hatStyle( Style::Zoomable ) )
        style |= Style::Zoomable;
    removeStyle( Style::Movable | Style::Rotatable | Style::Zoomable );
    bool result = Kam3D::tick( time );
    addStyle( style );
    return result;
}