let itemzpic=GetCurrentScriptDirectory~"system\items.png";
 LoadGraphic(itemzpic);//Declare and load the graphic used
CreateCommonDataArea("MOF");
task dropitem(type, x, y)
{
	
	let angle = 0;
   let yacc=-5; //so that the item jumps up when spawned like in touhou games
   let collection=0; //checks if the item's moving to the player (also used it to control how fast the item's moving toward the player)
   let xpos=x;
   let ypos=y;
   let width = 0;
   let obj=Obj_Create(OBJ_EFFECT);
   ObjEffect_SetTexture(obj, itemzpic);
   ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
   ObjEffect_CreateVertex(obj, 4); //declare the effect object
   if(type=="POWER_S") //set UV rects... change this depending on the image you're using
   {
   ObjEffect_SetVertexUV(obj, 0, 2, 2);
   ObjEffect_SetVertexUV(obj, 1, 13, 2);
   ObjEffect_SetVertexUV(obj, 2, 2, 13);
   ObjEffect_SetVertexUV(obj, 3, 13, 13);
	width = 11;
   }
   if(type=="POINT")
   {
   ObjEffect_SetVertexUV(obj, 0, 18, 2);
   ObjEffect_SetVertexUV(obj, 1, 29, 2);
   ObjEffect_SetVertexUV(obj, 2, 18, 13);
   ObjEffect_SetVertexUV(obj, 3, 29, 13);
   width = 11;
   }
   if(type=="VALUE_S") //small point item value
   {
   ObjEffect_SetVertexUV(obj, 0, 115, 3);
   ObjEffect_SetVertexUV(obj, 1, 124, 3);
   ObjEffect_SetVertexUV(obj, 2, 115, 12);
   ObjEffect_SetVertexUV(obj, 3, 124, 12);
   width = 9;
   }
   if(type=="VALUE_L")
   {
   ObjEffect_SetVertexUV(obj, 0, 32, 0);
   ObjEffect_SetVertexUV(obj, 1, 47, 0);
   ObjEffect_SetVertexUV(obj, 2, 32, 14);
   ObjEffect_SetVertexUV(obj, 3, 47, 14);
   width = 14;
   }
   if(type=="POWER_L")
   {
   ObjEffect_SetVertexUV(obj, 0, 48, 0);
   ObjEffect_SetVertexUV(obj, 1, 63, 0);
   ObjEffect_SetVertexUV(obj, 2, 48, 15);
   ObjEffect_SetVertexUV(obj, 3, 63, 15);
   width = 15;
   }
   if(type=="POWER_F")
   {
   ObjEffect_SetVertexUV(obj, 0, 80, 0);
   ObjEffect_SetVertexUV(obj, 1, 95, 0);
   ObjEffect_SetVertexUV(obj, 2, 80, 15);
   ObjEffect_SetVertexUV(obj, 3, 95, 15);
   width = 15;
   }
   if(type=="1UP_S")
   {
   ObjEffect_SetVertexUV(obj, 0, 64, 0);
   ObjEffect_SetVertexUV(obj, 1, 79, 0);
   ObjEffect_SetVertexUV(obj, 2, 64, 15);
   ObjEffect_SetVertexUV(obj, 3, 79, 15);
   width = 30;
   }
   if(type=="1UP_F")
   {
   ObjEffect_SetVertexUV(obj, 0, 96, 0);
   ObjEffect_SetVertexUV(obj, 1, 111, 0);
   ObjEffect_SetVertexUV(obj, 2, 96, 15);
   ObjEffect_SetVertexUV(obj, 3, 111, 15);
   width = 30;
   }
   if(type=="BOMB_S")
   {
   ObjEffect_SetVertexUV(obj, 0, 130, 0);
   ObjEffect_SetVertexUV(obj, 1, 145, 0);
   ObjEffect_SetVertexUV(obj, 2, 130, 15);
   ObjEffect_SetVertexUV(obj, 3, 145, 15);
   width = 30;
   }
   if(type=="BOMB_F")
   {
   ObjEffect_SetVertexUV(obj, 0, 146, 0);
   ObjEffect_SetVertexUV(obj, 1, 161, 0);
   ObjEffect_SetVertexUV(obj, 2, 146, 15);
   ObjEffect_SetVertexUV(obj, 3, 161, 15);
   width = 30;
   }
   while(!Obj_BeDeleted(obj))
   {
   
   if(GetCommonDataDefault("itemcollect",0)==1) //I set this to 1 whenever I want to auto collect all items like on event scripts, etc
   {
   collection=15;
   }
   if(ypos>GetClipMaxY+50) //deletes the item if it's way below the bottom of the screen
   {
   Obj_Delete(obj);
   }
   ypos+=yacc; //controls the falling
   if(yacc<3)
   {
	angle+=7.2;
	
   yacc+=0.1;
   }
   ObjEffect_SetVertexXY(obj, 0, xpos-width/2, ypos-width/2); //sets XY rects
   ObjEffect_SetVertexXY(obj, 1, xpos+width/2, ypos-width/2);
   ObjEffect_SetVertexXY(obj, 2, xpos-width/2, ypos+width/2);
   ObjEffect_SetVertexXY(obj, 3, xpos+width/2, ypos+width/2);
   if(((xpos-GetPlayerX)^2+(ypos-GetPlayerY)^2)^0.5<=30) //if distance between item and player is less than 30, item moves to the player
   {
   collection=6;
   }
   if(((xpos-GetPlayerX)^2+(ypos-GetPlayerY)^2)^0.5<=10 && OnPlayerMissed==false) //if distance between item and player is less than 10 and player isn't dead, item is collected
   {
		PlaySE(GetCurrentScriptDirectory~"se\se_item00.wav");
      if(type=="POWER_S") //if item is a small power item
      {
      SetCommonDataEx("MOF","playerpower", GetCommonDataDefaultEx("MOF","playerpower",0)+5);
      AddScore(10);
      }
      if(type=="POWER_L") //if item is a large power item
      {
       SetCommonDataEx("MOF","playerpower", GetCommonDataDefaultEx("MOF","playerpower",0)+100);
      AddScore(100);
      }
      if(type=="POWER_F") //if item is a full power item
      {
      SetCommonDataEx("MOF","playerpower", 500);
      AddScore(1000);
      }
      if(type=="POINT") //if item is a point item... I still have to fix this :V
      {
      if(GetPlayerY>GetClipMinY+120)
      {
      AddScore(truncate(5*(1000)/(GetPlayerY-GetClipMinY))+GetCommonDataDefaultEx("MOF","pointvalue",10000));
      }else{
      AddScore(truncate(5*(1000)/(GetPlayerY-GetClipMinY))+GetCommonDataDefaultEx("MOF","pointvalue",10000));
      }
      AddPoint(1);
      }
      if(type=="VALUE_S") //if item is a small point value item
      {
      SetCommonDataEx("MOF","pointvalue", GetCommonDataDefaultEx("MOF","pointvalue",10000)+50);
      AddScore(50);
      }
      if(type=="VALUE_L") //if item is a large collection item
      {
      SetCommonDataEx("MOF","pointvalue", GetCommonDataDefaultEx("MOF","pointvalue",10000)+200);
      AddScore(500);
      }
	  if(type=="1UP_S") //life pieces
      {
      SetCommonDataEx("MOF","lifepieces", GetCommonDataDefaultEx("MOF","lifepieces",0)+1);
	  if(GetCommonDataDefaultEx("MOF","lifepieces",0)>=5){
			 ExtendPlayer(1);
	  PlaySE(GetCurrentScriptDirectory~"se\se1UP.wav");
			SetCommonDataEx("MOF","lifepieces", GetCommonDataDefaultEx("MOF","lifepieces",0)-5);
		}
	  }
	  
     
      
	if(type=="1UP_F") //extra lifes
	 {
      ExtendPlayer(1);
	  PlaySE(GetCurrentScriptDirectory~"se\se1UP.wav");
      }
 if(type=="BOMB_S") //BOMB pieces
      {
      SetCommonDataEx("MOF","lifepieces", GetCommonDataDefaultEx("MOF","bombpieces",0)+1);
	  if(GetCommonDataDefaultEx("MOF","bombpieces",0)>=5){
			 AddBomb(1);
	  PlaySE(GetCurrentScriptDirectory~"se\se_cardget.wav");
			SetCommonDataEx("MOF","bombpieces", GetCommonDataDefaultEx("MOF","bombpieces",0)-5);
		}
	  }
	  
     
      
	if(type=="BOMB_F") //extra lifes
	 {
      AddBomb(1);
	  PlaySE(GetCurrentScriptDirectory~"se\se_cardget.wav");
      }
   Obj_Delete(obj);
   }
   if(GetCommonDataDefaultEx("MOF","playerpower",0)==500) //if power is full, I turned power items to my extra special item (item_drop is a function that sets common data so that the main stage script creates the items)
   {
      if(type=="POWER_S")
     {
      dropitem("VALUE_S", xpos, ypos);
      Obj_Delete(obj);
      }
      if(type=="POWER_L")
     {
      dropitem("VALUE_L", xpos, ypos);
      Obj_Delete(obj);
      }
   }
   if(GetPlayerY<GetClipMinY+150 || OnBomb==true) //when the player is above the collection line or bombing, and isn't dead, collect all the items...
   {
      if(OnPlayerMissed==false)
      {
   collection=10;
      }
   }

   if(collection>=1) //control movement when the item's being collected
   {
   xpos+=collection*cos(atan2(GetPlayerY-ypos, GetPlayerX-xpos));
   ypos+=collection*sin(atan2(GetPlayerY-ypos, GetPlayerX-xpos));
   }
   yield;
   
   }
}
// item waiter
task waitItem{

SetCommonData("playerlives",GetPlayerLife);
let arra = GetCommonDataDefaultEx("MOF","itemrequest",[]);
let xcoord = GetCommonDataDefaultEx("MOF","itemxcoord",[]);
let ycoord = GetCommonDataDefaultEx("MOF","itemycoord",[]);

	if(length(arra)==0){
	}else{
	ascent(i in 0..length(arra)){
		dropitem(arra[i],xcoord[i],ycoord[i]);
	}
	SetCommonDataEx("MOF","itemrequest",[]);
	SetCommonDataEx("MOF","itemxcoord",[]);
	SetCommonDataEx("MOF","itemycoord",[]);
	}

}
