#include "cParticle.h"

/*define class*/

    /* Constructor */
    cParticle::cParticle() {
        posi.x = 200;
        posi.y = 450; //Default position.
        /*velo.x = vx;
        velo.y = vy;*/
        //ChangePosi();
        acce.y = -0.00001; 
        acce.x = -0.00001; //Acceleration£»
        life = 9; //Defaut life span(Seconds);
        activate = FALSE; //Activate flag: default: False
        color = { 1.0f,0.84f,0 };//Default color;

    };
    /* void creParticle(int pn) {

        if (pn < 25) {
            x = -(pn * 2);
            y = sqrt(2500 - x * x) +50;
        }
        else if (pn >= 25 && pn < 50) {
            x = (pn - 25) * 2 - 50;
            y = -sqrt(2500 - x * x) + 50;
        }
        else if (pn >= 50 && pn < 75) {
            x = (pn - 50) * 2;
            y = sqrt(2500 - x * x) - 50;
        }
        else{
            x = 50 - (pn - 75) * 2;
            y = -sqrt(2500 - x * x) - 50;
        }

        posi.x = 250 +x;

        posi.y = 300+y;

        if (x>=0&&y>=0) {
            velo.x = y / 10;
            velo.y = -x / 10;
            acce.x = -y * PI;
            acce.y = -x * PI;
        }
        else if(x>=0&&y<0){
            velo.x = -y / 10;
            velo.y = -x / 10;
            acce.x = -y * PI;
            acce.y = x * PI;
        }
        else if (x < 0 && y <= 0){
            velo.x = -y / 10;
            velo.y = x / 10;
            acce.x = y * PI;
            acce.y = x * PI;

        }
        else{
            velo.x = y / 10;
            velo.y = x / 10;
            acce.x = y * PI;
            acce.y = -x * PI;
        }
        life = 10;
        activate = FALSE;


    };*/
    /*void circleupdate() {
        x = posi.x - 250;
        y = posi.y - 300;

        if (x >= 0 && y >= 0) {
            velo.x = y / 10;
            velo.y = -x / 10;
            acce.x = -y * PI;
            acce.y = -x * PI;
        }
        else if (x >= 0 && y < 0) {
            velo.x = -y / 10;
            velo.y = -x / 10;
            acce.x = -y * PI;
            acce.y = x * PI;
        }
        else if (x < 0 && y <= 0) {
            velo.x = -y / 10;
            velo.y = x / 10;
            acce.x = y * PI;
            acce.y = x * PI;

        }
        else {
            velo.x = y / 10;
            velo.y = x / 10;
            acce.x = y * PI;
            acce.y = -x * PI;
        }
    }*/
    /*cParticle::cParticle(int r) {
        posi.x = 250;
        posi.y = 0;
        velo.x = 0;
        velo.y = 0.3;
        acce.x = 0;
        acce.y = -0.00003;
        life = 4;
        activate = FALSE;
        color = { 1.0f,1.0f,1.0f };
        hightlimit = 300;
    }*/
    /*Set Particle Velocity*/
     void cParticle::setVelo(float vx, float vy) { 
        this->velo.x = vx;
        this->velo.y = vy;

    }
     /* Change position*/
    void cParticle::ChangePosi(int x, int y) {
        this->posi.x = float(x);
        this->posi.y = float(y);
    }
    /*Loop Change color*/
    void cParticle::ChangeColor() {
        //Red
        if (colornum == 0) {
            this->color.R = 1.0f;
            this->color.G = 0;
            this->color.B = 0;
        }
        //Sky blue
        else if (colornum == 1) {
            this->color.R = 0.53f;
            this->color.G = 0.80f;
            this->color.B = 1.0;
        }
        //Golden
        else {
            this->color.R = 1.0f;
            this->color.G = 0.84f;
            this->color.B = 0.0f;
        }



        colornum = (colornum + 1) % 3;
    }
    /*  Activate the particle */
    void cParticle::Activate() {
        this->activate = TRUE;
        
        
        this->starttick = GetTickCount64();//Obtain current tick;

    }

    /* ~cParticle() - destructor */
    cParticle::~cParticle() {
     //   cout << "Delete" << endl;
    }

    /*Update function*/
    /* decide the particle's movement */
    void cParticle::update() {
        this->savedtick = 0;

        //while (activate) {             //Update only when the particle activated;
        this->currenttick = GetTickCount64();         //Get Current tick; 

        /* Get the tick gap; */
        if (savedtick == 0) {
            tickspent = currenttick - starttick;
        }
        else {
            tickspent = currenttick - savedtick;
        }

        this->savedtick = currenttick;          //Save tick count;

        /* Update the posotion */
        this->posi.x = posi.x + float(tickspent) * velo.x / 1000;
        this->posi.y = posi.y + float(tickspent) * velo.y / 1000;

        /*update velocity*/
        velo.x = velo.x + acce.x * float(tickspent) / 1000;
        velo.y = velo.y + acce.y * float(tickspent) / 1000;

        //circleupdate();
        /*Limit the velocity */
        if ((velo.x * acce.x) > 0) {
            this->acce.x = 0;
        }

        /*Test code*/
        /*
        cout << "Position: (" << posi.x << ", " << posi.y << ") for current time;" << endl;
        cout << currenttick << endl;
        */

        int spent = currenttick - starttick;
        if (spent % 1000 <= 300 && spent % 100 >= 0) {
            this->ChangeColor();
        }
        if (spent > life * 1000 ||  SCREEN_HEIGHT1< posi.y||posi.y<0||SCREEN_WIDTH1<posi.x||posi.x<0) {
            this->activate = FALSE;
            this->~cParticle();//when run out of life or be outside of the screen, delete object.

            this->status = FALSE;
        }

        // }

    }

//};