//****************************************************************************************************
//	Version: 1.0
//	Author: Helepolis
//  Library:
//		setAura 		- sets the aura for the boss
//		burnaura		- handles the aura for the boss
//		burnparticle	- handles the particle
//		explosionspray	- Sprays spiral maple when boss is finished
//		explosionspray2 - Sprays spiral maple when boss is finished
//		explosioncustom	- Explodes with circular maple 
//		mapleexplo		- Maple leaf sprite
//****************************************************************************************************

#include "script/thdcs/functions/function_loaddata.txt"

// actual code for setting the aura including colour.
task setAura(obj,red,green,blue) {
	while(!Obj_IsDeleted(obj)) {
		burnaura(obj,ObjMove_GetX(obj),ObjMove_GetY(obj),round(rand(20,40)),4,round(rand(90,120)),red,green,blue);
		burnparticle(obj,200,300,rand(2.1,2.5),round(rand(0,360)),rand(0.04,0.08),red,green,blue);
		wait(5);
	}
}

// burn aura, expands upwards depending on parameters
task burnaura(objMaster,x,y,width,bspeed,bframes,red,green,blue) {
	
	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	let aburn = 0;
	let alphaValue = 155;
	
	ObjPrim_SetPrimitiveType(obj,PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetVertexCount(obj,4);
	ObjRender_SetBlendType(obj,BLEND_ADD_RGB);
	Obj_SetRenderPriorityI(obj,20);
	ObjPrim_SetTexture(obj,effaura);
	ObjRender_SetColor(obj,red,green,blue);
	
	while(!Obj_IsDeleted(bossObj)) {
		ObjPrim_SetVertexPosition(obj,0,-width,-aburn,0);
		ObjPrim_SetVertexUVT(obj,0,0,0);
		ObjPrim_SetVertexPosition(obj,1,width,-aburn,0);
		ObjPrim_SetVertexUVT(obj,1,50,0);
		ObjPrim_SetVertexPosition(obj,2,-width,0,0);
		ObjPrim_SetVertexUVT(obj,2,0,46);
		ObjPrim_SetVertexPosition(obj,3,width,0,0);
		ObjPrim_SetVertexUVT(obj,3,50,46);
		
		ascent(i in 0..4) { ObjPrim_SetVertexAlpha(obj,i,alphaValue); } 
		ObjRender_SetPosition(obj,ObjMove_GetX(objMaster),ObjMove_GetY(objMaster),0);
	
		aburn += bspeed;
		alphaValue -= 5;
		if(aburn > bframes) { Obj_Delete(obj); break; }	
		yield;
	}
	Obj_Delete(obj);
}

// burn particles continously appearing at centre of the boss
task burnparticle(objMaster,x,y,scale,rotate,scalespeed,red,green,blue) {

	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	let alphaValue = 0;
	let aswitch = 0;
	let acolor = 32;
	let ared = red;
	let agreen = green;
	let ablue = blue;
	
	ObjRender_SetBlendType(obj,BLEND_ADD_RGB);
	ObjPrim_SetTexture(obj,effaura);
	ObjRender_SetAngleXYZ(obj,0,0,rotate);
	ObjRender_SetScaleXYZ(obj,scale,scale,0);
	ObjRender_SetPosition(obj,bossX,bossY,0);	
	Obj_SetRenderPriorityI(obj,20);
	ObjSprite2D_SetSourceRect(obj,46,0,102,54);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetColor(obj,red,green,blue);
	
	while(!Obj_IsDeleted(bossObj)) {
		
		ascent(i in 0..4){ ObjPrim_SetVertexAlpha(obj,i,alphaValue); }
		ObjRender_SetPosition(obj,ObjMove_GetX(objMaster),ObjMove_GetY(objMaster),0);
		ObjRender_SetScaleXYZ(obj,scale,scale,0);
	
		if(alphaValue < 255 && aswitch == 0){ alphaValue+=255/10; if( alphaValue == 255 ) { aswitch=1; } }
		if(alphaValue > 0 && aswitch == 1) { alphaValue-=1; }
		if(ared < 255 ) { ared++; } 
		if(agreen < 255) { agreen++; } 
		if(ablue < 255) { ablue++; } 
		if(scale < 0.5) { Obj_Delete(obj); break; }
		scale-=scalespeed;

		yield;
	}
	Obj_Delete(obj);
}

// Spinning pattern of maple spray (upon death)
task explosionspray(obj) {
	let x = 0;
	let dir = 0;
	let scaler = 0;
	
	PlaySFX(SFX_ENEP00);
	
	while(x < 72) {
		yield;
		mapleexplo(ObjMove_GetX(obj),ObjMove_GetY(obj),rand_int(2,4),dir,scaler,scaler,rand_int(1,4));
		dir+=13.7;
		x++;
	}
	explosioncustom(obj);
}

// Spinning pattern of maple spray (upon death)
task explosionspray2(obj) {
	let x = 0;
	let dir = 0;
	let scaler = 0;
	
	PlaySFX(SFX_ENEP00);
	
	while(x < 72) {
		yield;
		mapleexplo(ObjMove_GetX(obj),ObjMove_GetY(obj),rand_int(2,4),dir,scaler,scaler,rand_int(1,4));
		dir+=13.7;
		x++;
	}
}

// Circular pattern of maple explosion (finale)
task explosioncustom(obj) {
	let x = 0;
	let scaler = 0;

	PlaySFX(SFX_ENEP01);

	while(x < 24) {
		scaler = rand(1,3);
		mapleexplo(ObjMove_GetX(obj),ObjMove_GetY(obj),3,rand(0,359),scaler,scaler,rand_int(1,4));
		mapleexplo(ObjMove_GetX(obj),ObjMove_GetY(obj),2.8,rand(0,359),scaler,scaler,rand_int(1,4));
		mapleexplo(ObjMove_GetX(obj),ObjMove_GetY(obj),2.6,rand(0,359),scaler,scaler,rand_int(1,4));
		x++;
	}
}

// Maple explosion 
task mapleexplo(x,y,v,dir,sx,sy,color) {
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	let alphaValue = 0;
	let as = 0;
	let c = 0;
	let scalex = sx;
	let scaley = sy;
	let spinny = rand_int(-4,4);
	let randomspinny = rand(1,2);
	
	ObjPrim_SetTexture(obj,boom);
	ObjRender_SetBlendType(obj,BLEND_ADD_RGB);
	Obj_SetRenderPriorityI(obj,40);
	ObjRender_SetPosition(obj,x,y,0);
	ObjRender_SetAlpha(obj,55);
	ObjMove_SetSpeed(obj,v);
	ObjMove_SetAngle(obj,rand(0,359));
	ObjRender_SetScaleXYZ(obj,scalex,scaley,0);
	ObjSprite2D_SetSourceRect(obj,0,0,34,34);
	ObjSprite2D_SetDestCenter(obj);	
	
	if(color==1) {
		ObjRender_SetColor(obj,255,64,64);
	}
	if(color==2) {
		ObjRender_SetColor(obj,64,64,255);
	}
	if(color==3) {
		ObjRender_SetColor(obj,64,255,64);
	}
	if(color==4) {
		ObjRender_SetColor(obj,255,64,255);
	}

	ObjMove_Special(obj,x,y,v,dir);
	
	while(!Obj_IsDeleted(obj)) {
		ObjRender_SetScaleXYZ(obj,scalex,scaley,0);
		ObjRender_SetAngleZ(obj,spinny);
		if(c < 30) { scalex+=0.1; scaley+=0.1; }
		if(c >= 30) { if(scaley >= 0){ scalex-=0.1; scaley-=0.1; } }
		if(scaley < 0) { Obj_Delete(obj); }
		c++;
		if(randomspinny == 1 && randomspinny <= 1.5) { spinny+=4; }
		if(randomspinny > 1.5 && randomspinny < 2.0) { spinny-=4; }
		yield;
	}
}

let magicCircTex2 = "script/thdcs/system/effects/magiccircle.png";
let spellCircTex2 = "script/thdcs/system/effects/funkycircle.png";
let circblura = 0;
let circblur2a = 0;

task bossMagicCircle2 {
	let cardtype = GetCommonData("bossCircleType",NULL);

	// vars
	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	let circsize = 112;
	let circx = 0;
	let circsp = 0;
	let circblurdelay = 0;
	let spscale = 0.8;
	let spframe = 0;

	ObjPrim_SetPrimitiveType(obj,PRIMITIVE_TRIANGLEFAN);
	ObjRender_SetBlendType(obj,BLEND_ADD_RGB);
	if(cardtype == 0) { ObjPrim_SetTexture(obj,magicCircTex2); }
	if(cardtype == 1 || cardtype == 2 || cardtype == 3) { ObjPrim_SetTexture(obj,spellCircTex2); }
	ObjRender_SetScaleXYZ(obj,spscale,spscale,0);
	Obj_SetRenderPriorityI(obj,21);
	ObjPrim_SetVertexCount(obj,37);
	ObjPrim_SetVertexPosition(obj,0,0,0,0);
	ObjPrim_SetVertexUVT(obj,0,64,64);

	// magic circle behaviour. Burst out at start, "pulse" while spinning.
	while(!Obj_IsDeleted(boss2Obj)) {

		cardtype = GetCommonData("bossCircleType",NULL);
		if(cardtype == 0) { ObjPrim_SetTexture(obj,magicCircTex2); }
		if(cardtype == 1) { ObjPrim_SetTexture(obj,spellCircTex2); }

		ascent(circx in 0..37) {
				ObjPrim_SetVertexPosition(obj,circx,circsize*cos(circx*360/35)+floor(rand(circblura,circblur2a)),circsize*sin(circx*360/35)+floor(rand(circblura,circblur2a)),0);
				if(cardtype == 0) { ObjPrim_SetVertexUVT(obj,circx,64*cos(circx*360/35)+64,64*sin(circx*360/35)+64); }
				if(cardtype == 1) { ObjPrim_SetVertexUVT(obj,circx,128*cos(circx*360/35)+128,128*sin(circx*360/35)+128); }
				if(cardtype == 2) { ObjPrim_SetVertexUVT(obj,circx,128*cos(circx*360/35)+384,128*sin(circx*360/35)+128); }
				if(cardtype == 3) { ObjPrim_SetVertexUVT(obj,circx,128*cos(circx*360/35)+640,128*sin(circx*360/35)+128); }
		}

		ObjRender_SetPosition(obj, ObjMove_GetX(boss2Obj), ObjMove_GetY(boss2Obj), 0);
		ObjRender_SetAngleZ(obj,circsp);
		ObjRender_SetScaleXYZ(obj,spscale,spscale,0);
		circsp-=3;
		spscale += sin(spframe)/220;
		spframe+=2;
		yield;
	}
	Obj_Delete(obj);
}
