// ------------------------------------------------------------------
//Bismillahirrahmanirrahim
//VEDAT MEYDANERĘ
// ------------------------------------------------------------------
//Nodelarin eklendigi yer;

#include "mostemp.h"



MOSSprite * s2;
MOSSpriteList DuvarSprites;

void sprite_ekle_Duvar(MOSTile *tile,int type,int x,int y,int state,int frame,int delay)
{
	//	top.Olustur(resim,pencere.g_EkranY,"data/spr/top",false,"data/spr/topm",16,16);
			
	       
	        s2 = new MOSSprite();
		    s2->Olustur(tile);
			s2->SetRenk(1,1,1);
			s2->SetType(type);					// Sprite Type
			s2->SetPosX(x);					// Xpos of Sprite
			s2->SetPosY(y);					// Ypos of Sprite
			s2->SetVelX(0);							// XVel of Sprite
			s2->SetVelY(0);							// YVel of Sprite
			s2->SetState(state);						// IT's ALIVE!!!
			s2->SetFrame(frame);				// Frame to start with
			s2->SetDelay(delay);		// Set but, value is global for Vaders. 

			DuvarSprites.EkleSprite(s2);
}
void sprite_ekle_Duvar_renk(MOSTile *tile,int type,int x,int y,int state,int frame,int delay,float r,float g,float b)
{
	//	top.Olustur(resim,pencere.g_EkranY,"data/spr/top",false,"data/spr/topm",16,16);
			
	       
	        s2 = new MOSSprite();
		    s2->Olustur(tile);
			s2->SetRenk(r,g,b);
			s2->SetType(type);					// Sprite Type
			s2->SetPosX(x);					// Xpos of Sprite
			s2->SetPosY(y);					// Ypos of Sprite
			s2->SetVelX(0);							// XVel of Sprite
			s2->SetVelY(0);							// YVel of Sprite
			s2->SetState(state);						// IT's ALIVE!!!
			s2->SetFrame(frame);				// Frame to start with
			s2->SetDelay(delay);		// Set but, value is global for Vaders. 

			DuvarSprites.EkleSprite(s2);
}
HITS HitBlock(RECT src1, RECT src2)
{

	RECT dest;

	if(!IntersectRect(&dest, &src1, &src2))
		return NO_HIT;

	if(dest.right - dest.left > dest.bottom - dest.top)
		return HIT_Y;
	else
		return HIT_X;
}
BOOL UpdateCarp()
{
     MOSSprite* Nodes;

	HITS rval;

    RECT Rect1 = { top.GetPosX()-3,top.GetPosY()-3 ,top.GetPosX()+top.en+3 ,top.GetPosY()+top.boy+3};
   

	

	// Loop the list and update the sprites
	for(Nodes = DuvarSprites.GetFirst(); Nodes != NULL; Nodes = DuvarSprites.GetNext(Nodes))
	{
        if (Nodes->GetState() == CANLI)
        {
 
		
			RECT Rect2 = {Nodes->GetPosX(),Nodes->GetPosY(),Nodes->GetPosX() + 30,Nodes->GetPosY() + 15};
			//Duvarlarin hepsi

			
            if (Rect1.top < Rect2.bottom &&
                Rect1.bottom > Rect2.top &&
                Rect1.right > Rect2.left &&
                Rect1.left < Rect2.right)
            
            {
				 switch(Nodes->GetType())
                    {
					

							case TYPE_DUVAR:
                        { 
					
							if(Nodes->GetState() == CANLI)
				{
					
				
							rval = HitBlock(Rect2, Rect1);

			if(rval == HIT_Y) 
			{
				top.SetVelY(-top.GetVelY());
			
					}
			else if(rval == HIT_X) {
				
				top.SetVelX(-top.GetVelX());
			}
			Sounds[SOUND_TOP]->Play();
			Nodes->SetState(KUCUL);
				
							}
										
                        } break;
							
					

				 }
			}
		}
	}

	return TRUE;
}

void update_sprite_duvar()
{


	
	MOSSprite* Nodes;


	for(Nodes = DuvarSprites.GetFirst(); Nodes != NULL; Nodes = DuvarSprites.GetNext(Nodes))
	{
       // What state
        switch(Nodes->GetType())
        {
          
            case TYPE_DUVAR:    
            {
				if(Nodes->GetState() == OYUNAG)
				{
					
					Nodes->SetState(CANLI);
				
				}
                if (Nodes->GetState() == CANLI)
                {  
				   
					
				}
				if (Nodes->GetState() == KUCUL)
                {  
				   Nodes->boy -=0.2; 
				   Nodes->en -=0.2;  
				   Nodes->xpos +=0.1;
				   Nodes->ypos +=0.1;

				   if(Nodes->boy < 0)
				   {
					   Nodes->SetState(PATLA);
				   }
					
				}
				if (Nodes->GetState() == ASAGI_GIT)
                {  
				   Nodes->SetPosY(Nodes->GetPosY()-6);
				   if(Nodes->GetPosY() < -30)
				   {
					   Nodes->SetState(BOS);
				   }
					
				}
				if (Nodes->GetState() == PATLA)
                {  
						//Sounds[SOUND_PATLA]->Play();
				   	  //  sprite_ekle_efek(exp2tile,TYPE_EFEK,Nodes->GetPosX()-25,Nodes->GetPosY()-25,CANLI,1,1);
					 //	sprite_ekle_efek(exp2tile,TYPE_EFEK,Nodes->GetPosX()-25,Nodes->GetPosY()-20,CANLI,1,1);
					
					   Nodes->SetState(BOS);
				   
					
				}
		
				if (Nodes->GetState() == BOS)
                {
				
			       DuvarSprites.KaldirSprite(Nodes);
				                       
                }
                
        
            } break;   
					

		}
			

		}
}

void Sprites_Update_Duvar(DWORD drawturu,bool blend,bool mask)
{
	UpdateCarp();
	update_sprite_duvar();

	DuvarSprites.Draw(drawturu,blend,mask);


}
			
