#include "player.h"

#include "tinerxml.h"

#include "glex.h"

#include "iostream"
using namespace std;

void player::checkbounds() {
    if(pos.c.x < -200)
        pos.c.x++;
    if(pos.c.x > 200)
        pos.c.x--;
}

void player::setrgb(float rr,float gg,float bb) {
    r = rr;
    g = gg;
    b = bb;
}
void player::setdirkeys(int uu,int dd,int ll,int rr) {
    up = uu;
    dn = dd;
    lt = ll;
    rt = rr;
}

void player::setotherkeys(int jumpkey, int shootkey, int changekey) {
    jump = jumpkey;
    shoot = shootkey;
    change = changekey;
}

int player::pnpoly(int npol, vector2d *poly, float x, float y) {
    int i, j, c = 0;
    for (i = 0, j = npol-1; i < npol; j = i++) {
        if ((((poly[i].y <= y) && (y < poly[j].y)) ||
                ((poly[j].y <= y) && (y < poly[i].y))) &&
                (x < (poly[j].x - poly[i].x) * (y - poly[i].y) / (poly[j].y - poly[i].y) + poly[i].x))
            c = !c;
    }
    return c;
}

void player::init(float x, float y, float z) {
    nopaintimer = 0;
    pos.c.x = pos.o.x = pos.c.y = pos.o.y = 0;
    xmlfile settings;
    TiXmlElement *xGame = settings.getxmlfirstelement("settings.xml");
    TiXmlElement *xPlayerglobal = settings.getelement(xGame, "playerglobal");
    drag = (float)atof(xPlayerglobal->Attribute("drag"));
    pos.rad = (float)atof(xPlayerglobal->Attribute("radius"));
    accel = (float)atof(xPlayerglobal->Attribute("accel"));
    steerspeed = (float)atof(xPlayerglobal->Attribute("steerspeed"));
    grip = (float)atof(xPlayerglobal->Attribute("grip"));
    gravity = (float)atof(xPlayerglobal->Attribute("gravity"));
    minzvel = (float)atof(xPlayerglobal->Attribute("minzvel"));
    jumppower = (float)atof(xPlayerglobal->Attribute("jumppower"));
    //maxhp = hp = (float)atof(xPlayerglobal->Attribute("hp"));
    settings.endxml();
    float m = 1;
    pos.teleport(x, y);
    pos.c.z = pos.o.z = z;
    pos.c.y +=0.001;

    plytexid = LoadGLTexture("img/player.png");
    dirangle = 0;


}

void player::update() {}


void player::dophys(float timestep) {
    pos.dophys(drag, timestep);
}


void player::applyforces(Uint8* key, float divstep) {
    //cout << "pos is " << pos.c.x << ", " << pos.c.y << endl;
    pos.o.z = pos.c.z;
    //pos.c.z-=0.02/divstep;
    //STUFF
    vector2d v (sinf(dirangle), cosf(dirangle));
    vector2d h = v.getperp();

    jumpingvel-=gravity/divstep;
    if(jumpingvel < minzvel) {
        jumpingvel = minzvel;
    }


    if(key[jump] && onground) {
        jumpingvel = jumppower;
        key[jump] = 0;
    }


    pos.c.z +=jumpingvel/divstep;

    if(key[dn]) {
        vector2d tv = pos.o-pos.c;
        float playervel = tv.magsqr();
        if(playervel) {
            //v = pos.c-pos.o;
            v = tv.divfr(sqrt(playervel));
        }
    }

    dirvec = v;


    //GRIP - well,im turning this into hoverrace, so i dont need this anymore...
    //uncommenting may or may not work
    //needs work btw..
    /*float tempgrip = grip/divstep;
    vector2d middle = pos.o;
    vector2d plyline = pos.c - middle;
    float dist = plyline.x * v.x + plyline.y * v.y;
    vector2d nearp = middle+v.scalefr(dist);
    vector2d fixer = nearp-pos.c;
    fixer.scalef(tempgrip);
    pos.c+=fixer;
    //pos.c+=fixer.getperp();*/

    v.scalef(accel);
    v.divf(divstep);

    if(key[up])
        pos.c+=v;
    //if(key[dn])
    //    pos.c+=v.scalefr(0.5);
    if(key[rt])
        dirangle+=steerspeed/divstep;
    if(key[lt])
        dirangle-=steerspeed/divstep;


}


extern float dot(vector2d a, vector2d b);
void player::draw() {


    glColor3f(r,onground,b);
    vector2d dir = dirvec.scalefr(pos.rad);
    //dir = bar[3].dir.scalefr(-30);
    glBindTexture( GL_TEXTURE_2D, plytexid);
    float ph=pos.c.z;
    glBegin(GL_QUADS);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f( pos.c.x+dir.x-dir.y, pos.c.y+dir.y+dir.x, ph);
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f( pos.c.x-dir.x-dir.y, pos.c.y-dir.y+dir.x, ph);
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f( pos.c.x-dir.x+dir.y, pos.c.y-dir.y-dir.x, ph);
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f( pos.c.x+dir.x+dir.y, pos.c.y+dir.y-dir.x, ph);
    glEnd();

    vector2d vecvel = pos.c-pos.o;
    float drawvel = sqrt(vecvel.magsqr());
    vecvel.divf(drawvel);
    float dotdiff = dot(vecvel,dirvec.scalefr(1))*drawvel;
    glColor3f(0,0,1);
    go2d(0.f,1.f,0.f,1.f);
    glDisable(GL_TEXTURE_2D);
    glDisable(GL_DEPTH_TEST);
    glBegin(GL_QUADS);
    glVertex3f( 0,0.97,0);
    glVertex3f( drawvel,0.97,0);
    glVertex3f( drawvel,1,0);
    glVertex3f( 0,1,0);
    if(dotdiff<0){
    dotdiff=-dotdiff;
    glColor3f(1,0,0);
    }
    glVertex3f( 0,0.94,0);
    glVertex3f( dotdiff,0.94,0);
    glVertex3f( dotdiff,0.97,0);
    glVertex3f( 0,0.97,0);
    glEnd();
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);
    leave2d();

}


void player::playercol(player &otherplayer) {
    float zdis = pos.c.z - otherplayer.pos.c.z;
    zdis*=zdis;
    if(zdis < 1) {
        vector2d disvec = pos.c - otherplayer.pos.c;
        float td = disvec.magsqr();
        float rad = 50/20;
        if(td<rad*rad) {
            td=sqrtf(td);
            disvec.divf(td);
            disvec.scalef(rad/100);
            pos.c+=disvec;
            otherplayer.pos.c-=disvec;
        }
    }
}
