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 ~ "sprites\Blossom.png";
let effectimg2 = GetCurrentScriptDirectory ~ "sprites\circle.png"; 	//Directory of the used images
LoadGraphic(effectimg);	
LoadGraphic(effectimg2);						//Load the image, of course
let enep00 = GetCurrentScriptDirectory ~ "sfx\se_enep00.wav";
let enep01 = GetCurrentScriptDirectory ~ "sfx\se_enep01.wav";

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

task StartMapleExplode{
    PlaySE(enep00);PlaySE(enep01);
    loop(55){
	MapleExplode(7, -0.035);	//Calls for the first effect, the random scattering of leaves
	yield;
    }
    loop(6){yield;}
    PlaySE(enep00);PlaySE(enep01);
    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	mainEnemyX = GetEnemyX;
    let	mainEnemyY = GetEnemyY;

    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, 4);					//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, 0, 224);

    ObjEffect_SetVertexXY(obj, 1, -17, -17); 
    ObjEffect_SetVertexUV(obj, 1, 0, 0);

    ObjEffect_SetVertexXY(obj, 2, 17, 17);
    ObjEffect_SetVertexUV(obj, 2, 272, 224);

    ObjEffect_SetVertexXY(obj, 3, 17, -17);
    ObjEffect_SetVertexUV(obj, 3, 227, 0);


    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, effectimg2); 			//etama2

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

    ObjEffect_SetVertexXY(obj, 1, -32, -32); 
    ObjEffect_SetVertexUV(obj, 1, 0, 0);

    ObjEffect_SetVertexXY(obj, 2, 32, 32);
    ObjEffect_SetVertexUV(obj, 2, 480, 480);

    ObjEffect_SetVertexXY(obj, 3, 32, -32);
    ObjEffect_SetVertexUV(obj, 3, 480, 0);

    while(!Obj_BeDeleted(obj)){

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

	circscale -= 0.20; 						//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(-1, 1);				//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, 0, 224);

    ObjEffect_SetVertexXY(obj, 1, -17, -17); 
    ObjEffect_SetVertexUV(obj, 1, 0, 0);

    ObjEffect_SetVertexXY(obj, 2, 17, 17);
    ObjEffect_SetVertexUV(obj, 2, 272, 224);

    ObjEffect_SetVertexXY(obj, 3, 17, -17);
    ObjEffect_SetVertexUV(obj, 3, 227, 0);


    while(!Obj_BeDeleted(obj)){

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

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

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

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

	yield;
    }
}

