let mainEnemyX;
let mainEnemyY;

sub LocateEnemy{ 		//Call this before the explosion, if the enemy will disappear or move spontaneously
    mainEnemyX = GetEnemyX;	//If we just use GetEnemyX it will default to center screen
    mainEnemyY = GetEnemyY;	//And not where the enemy actually was
}				//This is a placeholder function until we get transitions between scripts


let effectimg = GetCurrentScriptDirectory ~ "etama02.png"; 	//Directory of the used images
LoadGraphic(effectimg);						//Load the image, of course


////////////////////////////////////////////////////////////////////////////////////////////
/////////////////Generates the explosion of maple leaves when the boss dies/////////////////
////////////////////////////////////////////////////////////////////////////////////////////

task StartMapleExplode{

    loop(55){
	MapleExplode(7, -0.035);	//Calls for the first effect, the random scattering of leaves
	yield;
    }
    loop(6){yield;}
    loop(70){
	MapleExplode(8, -0.04); 	//Calls for the second effect, the main explosion of leaves
    }
}


task MapleExplode(speedcap, scaleadd){

    let mplangleinit = rand(0, 360);				//Inital angle of the leaf
    let mplangle = 0;						//Will attribute to current angle
    let mplangleacc = rand(-10, 10);				//The speed at which the leaf spins
    let mplscale = rand(3, 4.5);				//Initial graphic scaling

    let mplalpha = 255;						//Inital alpha value
    let mplspeed = rand(4.5, speedcap);				//Initial speed

    let obj = Obj_Create(OBJ_EFFECT);				//Create object
    Obj_SetPosition(obj, mainEnemyX, mainEnemyY);		//Sets starting position to enemy
    Obj_SetAngle(obj, mplangleinit);				//Needed to actually initialize angle

    ObjEffect_SetRenderState(obj, ADD);
    ObjEffect_SetLayer(obj, 2);					//Goes below bullets
    ObjEffect_SetTexture(obj, effectimg); 			//etama2

    ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);	//Next code involves drawing
 
    ObjEffect_CreateVertex(obj, 4);
 
    ObjEffect_SetVertexXY(obj, 0, -17, 17); 
    ObjEffect_SetVertexUV(obj, 0, 32, 256);

    ObjEffect_SetVertexXY(obj, 1, -17, -17); 
    ObjEffect_SetVertexUV(obj, 1, 32, 225);

    ObjEffect_SetVertexXY(obj, 2, 17, 17);
    ObjEffect_SetVertexUV(obj, 2, 64, 256);

    ObjEffect_SetVertexXY(obj, 3, 17, -17);
    ObjEffect_SetVertexUV(obj, 3, 64, 225);


    while(!Obj_BeDeleted(obj)){
	Obj_SetAngle(obj, mplangleinit+(mplangle/4));			//Updates directional angle
	Obj_SetSpeed(obj, mplspeed);					//Sets current speed
	ObjEffect_SetAngle(obj, 0, 0, mplangleinit+mplangle);		//Updates graphic angle
	ObjEffect_SetScale(obj, mplscale, mplscale);			//Updates graphic xy scaling

	ascent(i in 0..3){
	    ObjEffect_SetVertexColor(obj, i, mplalpha, 255, 255, 255);	//Updates the alpha values
	}

	mplangle += mplangleacc;	//Current graphic angle is changed (spins)
	mplspeed -= 0.05;		//Decelerates each frame
	mplscale += scaleadd; 		//Decreases the graphic scaling

	if(mplspeed>3){
	    mplalpha -= 0.5;	//Alpha decreases each frame
	}else{
	    mplalpha -= 2;	//Even more so when it slows down
	}

	if(mplscale<=0){	//If the leaf shrinks to nothing, delete it
	    Obj_Delete(obj);
	}

	yield;
    }
}


////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////A new Concentration function using maple leaves///////////////////////
////////////////////////////////////////////////////////////////////////////////////////////

task StartMapleConcentrate{
    CircleConcentrate;
    loop(10){
	loop(3){
	    MapleConcentrate;
	}
	loop(2){yield;}
    }
}


task CircleConcentrate{

    let circscale = 10;						//Initial graphic scaling
    let circalpha = 0;						//Inital alpha value

    let obj = Obj_Create(OBJ_EFFECT);				//Create object

    Obj_SetPosition(obj, GetEnemyX, GetEnemyY);			//Sets starting position at the enemy

    ObjEffect_SetRenderState(obj, ADD);
    ObjEffect_SetLayer(obj, 2);					//Goes below bullets
    ObjEffect_SetTexture(obj, effectimg); 			//etama2

    ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);	//Next code involves drawing
 
    ObjEffect_CreateVertex(obj, 4);
 
    ObjEffect_SetVertexXY(obj, 0, -32, 32); 
    ObjEffect_SetVertexUV(obj, 0, 275, 77);

    ObjEffect_SetVertexXY(obj, 1, -32, -32); 
    ObjEffect_SetVertexUV(obj, 1, 275, 15);

    ObjEffect_SetVertexXY(obj, 2, 32, 32);
    ObjEffect_SetVertexUV(obj, 2, 337, 77);

    ObjEffect_SetVertexXY(obj, 3, 32, -32);
    ObjEffect_SetVertexUV(obj, 3, 337, 15);

    while(!Obj_BeDeleted(obj)){

	ObjEffect_SetScale(obj, circscale, circscale);			//Updates graphic xy scaling
    	ascent(i in 0..4){
	    ObjEffect_SetVertexColor(obj, i, circalpha, 255, 50, 50);	//Updates the alpha value and changes color
	}

	circscale -= 0.15; 						//Decreases the graphic scaling
	circalpha += (15/circscale);

	if(circscale<=0){						//If it shrinks to nothing, delete it
	    Obj_Delete(obj);
	}

	yield;
    }
}


task MapleConcentrate{

    let mpldist = rand(150,275);				//Inital radius from enemy
    let mplangleinit = rand(0,360);				//Initial angle
    let mplangle = 0;						//Will attribute to current angle
    let mplangleacc = rand(-6, 6);				//The speed at which the leaf spins
    let mplscale = rand(3, 3.5);				//Initial graphic scaling


    let obj = Obj_Create(OBJ_EFFECT);				//Create object

    Obj_SetPosition(obj, GetEnemyX - (cos(mplangleinit)*mpldist), GetEnemyY - (sin(mplangleinit)*mpldist));	
								//Sets starting position to a radius around the enemy

    Obj_SetAngle(obj, mplangleinit);				//Sets constant angle

    ObjEffect_SetRenderState(obj, ADD);
    ObjEffect_SetLayer(obj, 2);					//Goes below bullets
    ObjEffect_SetTexture(obj, effectimg); 			//etama2

    ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);	//Next code involves drawing
 
    ObjEffect_CreateVertex(obj, 4);
 
    ObjEffect_SetVertexXY(obj, 0, -17, 17); 
    ObjEffect_SetVertexUV(obj, 0, 32, 256);

    ObjEffect_SetVertexXY(obj, 1, -17, -17); 
    ObjEffect_SetVertexUV(obj, 1, 32, 225);

    ObjEffect_SetVertexXY(obj, 2, 17, 17);
    ObjEffect_SetVertexUV(obj, 2, 64, 256);

    ObjEffect_SetVertexXY(obj, 3, 17, -17);
    ObjEffect_SetVertexUV(obj, 3, 64, 225);


    while(!Obj_BeDeleted(obj)){

	mpldist -= 4.2;
    	Obj_SetPosition(obj, GetEnemyX - (cos(mplangleinit)*mpldist), GetEnemyY - (sin(mplangleinit)*mpldist));

	ObjEffect_SetAngle(obj, 0, 0, mplangleinit+mplangle);		//Updates graphic angle
	ObjEffect_SetScale(obj, mplscale, mplscale);			//Updates graphic xy scaling

	mplangle += mplangleacc;					//Current graphic angle is changed (spins)
	mplscale -= 0.05; 						//Decreases the graphic scaling

	if(mpldist<=20){						//If it reaches a radius around the boss, delete it
	    Obj_Delete(obj);
	}

	yield;
    }
}


////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////A new item spawning and behaviour function/////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////

task CreateItem01(type, posx, posy){

    let itemspeed = -2.5;			//The initial speed of the item, goes backwards
    let itemspeed2 = 0;				//Acceleration of the item when locked onto player
    let itemspin = 0;				//Angle of the item (it spins when spawned)
    let itemgrav = false;			//If the item has locked onto the player
    let angletoitem;

    let obj = Obj_Create(OBJ_EFFECT);		//Create the object

    Obj_SetPosition(obj, posx, posy);		//Spawns on the argumented xy point
    Obj_SetAngle(obj, 90);			//Aimed straight down

    ObjEffect_SetLayer(obj, 4);			//Drawn on the items layer, above bullets
    ObjEffect_SetTexture(obj, effectimg); 	//etama2

    ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
    ObjEffect_CreateVertex(obj, 4);			//Draw the vertices
 
    if(type==0){					//Power Items
	ObjEffect_SetVertexXY(obj, 0, -6, 6); 
	ObjEffect_SetVertexUV(obj, 0, 2, 222);

	ObjEffect_SetVertexXY(obj, 1, -6, -6); 
	ObjEffect_SetVertexUV(obj, 1, 2, 210);

	ObjEffect_SetVertexXY(obj, 2, 6, 6);
	ObjEffect_SetVertexUV(obj, 2, 14, 222);

	ObjEffect_SetVertexXY(obj, 3, 6, -6);
	ObjEffect_SetVertexUV(obj, 3, 14, 210);

    }else if(type==1){					//Point Items
	ObjEffect_SetVertexXY(obj, 0, -6, 6); 
	ObjEffect_SetVertexUV(obj, 0, 18, 222);

	ObjEffect_SetVertexXY(obj, 1, -6, -6); 
	ObjEffect_SetVertexUV(obj, 1, 18, 210);

	ObjEffect_SetVertexXY(obj, 2, 6, 6);
	ObjEffect_SetVertexUV(obj, 2, 30, 222);

	ObjEffect_SetVertexXY(obj, 3, 6, -6);
	ObjEffect_SetVertexUV(obj, 3, 30, 210);

    }else if(type==2){					//Big Power Items
	ObjEffect_SetVertexXY(obj, 0, -8, 8); 
	ObjEffect_SetVertexUV(obj, 0, 32, 224);

	ObjEffect_SetVertexXY(obj, 1, -8, -8); 
	ObjEffect_SetVertexUV(obj, 1, 32, 208);

	ObjEffect_SetVertexXY(obj, 2, 8, 8);
	ObjEffect_SetVertexUV(obj, 2, 48, 224);

	ObjEffect_SetVertexXY(obj, 3, 8, -8);
	ObjEffect_SetVertexUV(obj, 3, 48, 208);

    }else if(type==3){					//Life Pieces
	ObjEffect_SetVertexXY(obj, 0, -8, 7.5); 
	ObjEffect_SetVertexUV(obj, 0, 48, 223);

	ObjEffect_SetVertexXY(obj, 1, -8, -7.5); 
	ObjEffect_SetVertexUV(obj, 1, 48, 208);

	ObjEffect_SetVertexXY(obj, 2, 8, 7.5);
	ObjEffect_SetVertexUV(obj, 2, 64, 223);

	ObjEffect_SetVertexXY(obj, 3, 8, -7.5);
	ObjEffect_SetVertexUV(obj, 3, 64, 208);

    }else if(type==4){					//Bomb Pieces
	ObjEffect_SetVertexXY(obj, 0, -8, 7.5); 
	ObjEffect_SetVertexUV(obj, 0, 64, 223);

	ObjEffect_SetVertexXY(obj, 1, -8, -7.5); 
	ObjEffect_SetVertexUV(obj, 1, 64, 208);

	ObjEffect_SetVertexXY(obj, 2, 8, 7.5);
	ObjEffect_SetVertexUV(obj, 2, 80, 223);

	ObjEffect_SetVertexXY(obj, 3, 8, -7.5);
	ObjEffect_SetVertexUV(obj, 3, 80, 208);

    }else if(type==5){					//Life Items
	ObjEffect_SetVertexXY(obj, 0, -8, 7.5); 
	ObjEffect_SetVertexUV(obj, 0, 80, 223);

	ObjEffect_SetVertexXY(obj, 1, -8, -7.5); 
	ObjEffect_SetVertexUV(obj, 1, 80, 208);

	ObjEffect_SetVertexXY(obj, 2, 8, 7.5);
	ObjEffect_SetVertexUV(obj, 2, 96, 223);

	ObjEffect_SetVertexXY(obj, 3, 8, -7.5);
	ObjEffect_SetVertexUV(obj, 3, 96, 208);

    }else if(type==6){					//Bomb Items
	ObjEffect_SetVertexXY(obj, 0, -8, 7.5); 
	ObjEffect_SetVertexUV(obj, 0, 96, 223);

	ObjEffect_SetVertexXY(obj, 1, -8, -7.5); 
	ObjEffect_SetVertexUV(obj, 1, 96, 208);

	ObjEffect_SetVertexXY(obj, 2, 8, 7.5);
	ObjEffect_SetVertexUV(obj, 2, 112, 223);

	ObjEffect_SetVertexXY(obj, 3, 8, -7.5);
	ObjEffect_SetVertexUV(obj, 3, 112, 208);

    }else if(type==9){					//Idiot Energy
	ObjEffect_SetVertexXY(obj, 0, -5, 5); 
	ObjEffect_SetVertexUV(obj, 0, 115, 221);

	ObjEffect_SetVertexXY(obj, 1, -5, -5); 
	ObjEffect_SetVertexUV(obj, 1, 115, 211);

	ObjEffect_SetVertexXY(obj, 2, 5, 5);
	ObjEffect_SetVertexUV(obj, 2, 125, 221);

	ObjEffect_SetVertexXY(obj, 3, 5, -5);
	ObjEffect_SetVertexUV(obj, 3, 125, 211);
    }


    while(!Obj_BeDeleted(obj)){

	//if(type==0){ItemPowerBehave;}		//Later these will determine specific item behaviour
	//if(type==1){ItemPointBehave;}		//Such as adding points. power, lives, etc
	//if(type==9){ItemIdiotBehave;}


	if(itemgrav){						
	    Obj_SetX(obj, Obj_GetX(obj) + cos(angletoitem)*( (|itemspeed|)+itemspeed2 ));	//Follows the player
	    Obj_SetY(obj, Obj_GetY(obj) + sin(angletoitem)*( (|itemspeed|)+itemspeed2 ));
	    Obj_SetSpeed(obj, 0);								//Set to 0 so it doesn't fall
	    itemspeed2 += 0.16;									//Accelerate towards player

	}else{									//If the item has not locked onto the player
	    if(type==9 && itemspeed>=0){					//For IE, stop accelerating when stopped
		itemspeed = 0;
	    }else if(itemspeed>=2.5){						//For every other item, stop accelerating at 2.5
		itemspeed = 2.5;
	    }
	    Obj_SetSpeed(obj, itemspeed);					//Sets the descending speed

	}

	itemspeed+=0.04;								//Accelerates falling speed
	angletoitem = atan2(GetPlayerY - Obj_GetY(obj), GetPlayerX - Obj_GetX(obj));	//Gets the angle from item to player
						
	if(((Obj_GetX(obj) - GetPlayerX)^2 + (Obj_GetY(obj) - GetPlayerY)^2)^0.5 < 45){	//If the item gets within a certain radius
	    itemgrav = true;								//Begin gravitating towards the player
	}		

	if(type!=9 && itemspeed<= -1){							//For a while after being spawned
	    ObjEffect_SetAngle(obj, 0, 0, itemspin);					//Spin the item
	    itemspin += 30;
	}

	if(((Obj_GetX(obj) - GetPlayerX)^2 + (Obj_GetY(obj) - GetPlayerY)^2)^0.5 < 20){	//If the item gets within a certain radius
	    if(type==0){ItemPowerBehave;}						//Call item behaviour
	    Obj_Delete(obj);								//Collect it
	}					

	if(Obj_GetY(obj)>=GetClipMaxY+30){						//If the item falls off the stage
	    Obj_Delete(obj);								//Delete it
	}

	yield;
    }

}

sub ItemPowerBehave{
    let plpower = GetCommonData("PowerLevel");
    plpower+=0.05;
    SetCommonData("PowerLevel", plpower);
}