// Class automatically generated by Dev-C++ New Class wizard

#include "CEnvEffect.h" // class's header file

// class constructor
CEnvEffect::CEnvEffect()
{
    init();
}

// class destructor
CEnvEffect::~CEnvEffect()
{
	// insert your code here
}

void CEnvEffect::init(void)
{
    for(int n=0 ; n<MAX_ENV_PARTS ; n++)
    {
        part[n].in_use=false;
        part[n].x=0;
        part[n].y=0;
        part[n].dx=0;
        part[n].dy=0;
    }    
    parts_to_use=0;   
    effect_on=false;
    counter=0;
}

void CEnvEffect::move(int sx, int sy)
{
    if(effect_on==true)
    {
        for(int n=0 ; n<parts_to_use ; n++)   
        {
            part[n].x+=part[n].dx;
            part[n].y+=part[n].dy;    
            
            if((part[n].y-sy)>(480+400))
            {
                part[n].y=sy+(-30)-rand()%60;
                part[n].x=sx+(rand()%640);    
            }      
            
            if(part[n].x-sx>640 && tx>sx)
                part[n].x=sx+ rand()%(tx-sx);
            else if(part[n].x-sx<0 && sx>tx)
                part[n].x=tx+640+ rand()%(sx-tx);
        } 
        
        if(counter>0)
        {
            counter--;
        }    
        else
        {
            if(atts->type==RAIN)
            {
                counter = rand()%600 + 300; //reset it for the lightning
            }            
        
        }               
    }    
    tx=sx;
}

void CEnvEffect::draw(BITMAP *backgr, int sx, int sy)
{
    if(effect_on==true)
    {
        for(int n=0 ; n<parts_to_use ; n++)
        {
            if(atts->type==RAIN)
            {
                line(backgr,(int)part[n].x-sx,(int)part[n].y-sy,(int)part[n].x-sx,(int)part[n].y+13-sy,makecol(90,90,110));   
            }    
        }
        //circlefill(backgr,0,0,20,makecol(0,0,0));
         
    }        
}       

void CEnvEffect::draw_background(BITMAP *backgr, int sx, int sy)
{
    if(effect_on==true)
    {
        if(atts->type==RAIN) //do a lightning effect
        {
            if(counter==30)
            {
                rectfill(backgr,0,0,640,480,makecol(255,255,255));
            }          
            else if(counter==1)
            {
                play_sample((SAMPLE*)atts->sound[rand()%2],100,155,1000,FALSE);
            }     
        }    
    }      
}    

void CEnvEffect::stop_effect(void)
{
    for(int n=0 ; n<MAX_ENV_PARTS ; n++)
        part[n].in_use=false;
    effect_on=false; 
}
    
void CEnvEffect::start_effect(bool start_at_top, int sx, int sy, int _parts_to_use)
{
    parts_to_use = _parts_to_use;
    if(parts_to_use>MAX_ENV_PARTS)
        parts_to_use=MAX_ENV_PARTS;
        
    if(start_at_top==true)
    {
        for(int n=0 ; n<parts_to_use ; n++)  
        {
            part[n].y=sy-rand()%480;      
            part[n].x=sx+(rand()%640);
            part[n].in_use=true;
            part[n].dy=atts->y_speed; 
            part[n].dy*=((float)(rand()%100))/100 + .5;
        }    
    }
    else
    {
        for(int n=0 ; n<parts_to_use ; n++)  
        {
            part[n].y=sy+((rand()%(480+400+60)))-60;      
            part[n].x=sx+(rand()%640);
            part[n].in_use=true;
            part[n].dy=atts->y_speed;
            part[n].dy*=((float)(rand()%100))/100 + .5;
        }         
    }       
    effect_on=true;          
    
    tx = sx;
    ty = sy;   
    
    
    if(atts->type==RAIN)
    {
        counter = rand()%100 + 100;     //since it is rain, counter is for lightning   
    }    
}    

void CEnvEffect::set_effect(EffectAtts *_atts)
{
    atts = _atts;   
}
