
let MainSTG = GetMainStgScriptDirectory;
let img_effect = MainSTG~"ds_system/img/effect.png"; // Effect Sheet

task StarOnBoss(objID, com1X, com2Y){ 
	
	let StarX = -400;
	let StarY = -400;
	
	let objxpos=StarX;
	let objypos=StarY;
	let object_time = 30;
	let obj_angle = 330;
	let osize = 0;
	let obj_sizeset = 0;
	let sizeinc = 5;
	let angleinc = 5;
	let fullsize = 0;

	ObjRender_SetBlendType(objID, BLEND_ALPHA);
	Obj_SetRenderPriority(objID, 0.3);
	ObjPrim_SetTexture(objID, img_effect);
	ObjSprite2D_SetSourceRect(objID, 128, 80, 256, 208);
	ObjSprite2D_SetDestRect(objID, -64, -64, 64, 64);
	
	
	ObjRender_SetAlpha(objID, 145);
	ObjRender_SetColor(objID, 200,200,200);
	
	while(!Obj_IsDeleted(objID)){

		objxpos = GetCommonData(com1X, -400);
		objypos = GetCommonData(com2Y, -400);
		
		StarSize;
		ObjRender_SetX(objID, objxpos);
		ObjRender_SetY(objID, objypos);
		ObjRender_SetScaleXYZ(objID, osize/95, osize/95, 1);
		ObjRender_SetAngleZ(objID, obj_angle);
		yield;
	}
	
	task StarSize{
		let ax2 = 0.04;
		let bx2 = 1.35;
		if(fullsize==2){
			if(obj_sizeset==0){sizeinc+=ax2}
			if(obj_sizeset==1){sizeinc-=ax2}
			
			if(sizeinc<-bx2){obj_sizeset=0}
			if(sizeinc>bx2){obj_sizeset=1}
			
			obj_angle-=angleinc;
			
			if(angleinc<5){angleinc+=0.5}else{angleinc=5;}
			
			osize+=sizeinc;
		}
		if(fullsize!=2){obj_angle-=angleinc;angleinc-=0.06;}
		if(osize<145&&fullsize==0){osize+=5;}
		if(osize>144&&fullsize==0){
			fullsize=1;
			loop(55){yield;}
			sizeinc=0;
			angleinc=0;
			fullsize=2;
		}			
	}	
}

task BulgeBackgroundA1(bossid){
	/*this looked so good but then stuff happened and now idk
	http://www.shrinemaiden.org/forum/index.php/topic,5164.msg440550.html#msg440550
		image = path of the image, duh.
		nx, ny = number of rows and columns in the effect object. The higher the numbers, the smoother the background, but also more slowdown. Due to danmakufu's bad array handling, should not exceed 10/15.
		dmax = maximum buldge of any given vertex. Should not exceed (width_of_playing_field / nx) or (height_of_playing_field / ny).
		dfac = value between 0 and 1. Determines the crassness of the buldge edge. A good value is ~0.98.
	*/
	let BulgeX = ObjRender_GetX(bossid);
	let BulgeY = ObjRender_GetY(bossid);
	
	let Default2DCameraX = Get2DCameraX;
	let Default2DCameraY = Get2DCameraY;
	let Default2DCameraSet = true;
	
	let nx = 9;
	let ny = 9;
	let dmax = 26;
	let dfac = 0.9825;
	
	let GetClipMinX = 0;
	let GetClipMinY = 0;
	let GetClipMaxX = GetStgFrameWidth;
	let GetClipMaxY = GetStgFrameHeight;
	
	let dx=(GetClipMaxX-GetClipMinX)/nx;
	let dy=(GetClipMaxY-GetClipMinY)/ny;
	let du=GetClipMaxX/nx;              //image width
	let dv=GetClipMaxY/ny;          	//image height
	let vertnum=floor(ny/2)*4*nx + floor(0.5+ny/2)*(2*nx+2) +1 -ny;
	
	yield;yield;

	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetVertexCount(obj, vertnum);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	ObjPrim_SetTexture(obj, NULL);
	Obj_SetRenderPriority(obj, 0.39);
	ObjRender_SetPosition(obj, -32, -16, 0);
	
	
	let xar=[];
	let yar=[];
	let by=0;
	while(by<=(ny)){
		if(by%2==0){
			ascent(i in 0..(2*nx+2)){
				xar=xar~[floor(i/2)];
				yar=yar~[by + i%2 ];
			}
		}else{
			ascent(i in 0..(4*nx)){
				xar=xar~[nx - (i%2 + floor(i/4) ) ];
				yar=yar~[by + floor(i/2)%2 ];
			}
		}

		by++;
		if(by<(ny)){ xar=erase(xar, length(xar)-1); yar=erase(yar, length(yar)-1); }
	}
	let td=0;
	let ta=0;
	let ophi=rand(0, 360);
	while(Obj_IsDeleted(bossid)==false){
	
		ophi+=7;
		
		BulgeX = ObjRender_GetX(bossid)+32;
		BulgeY = ObjRender_GetY(bossid);
		
		let currentFrameTexture = GetReservedRenderTargetName(0);
		RenderToTextureA1(currentFrameTexture, 20, 39, false);
		ObjPrim_SetTexture(obj, currentFrameTexture);
		
		ascent(i in 0..vertnum){                
			td= ((GetClipMinX + xar[i]*dx - BulgeX)^2 + (GetClipMinY + yar[i]*dy - BulgeY)^2)^0.5;                    // Change to GetX and GetY to
			ta= atan2(GetClipMinY + yar[i]*dy - BulgeY, GetClipMinX + xar[i]*dx - BulgeX);                          // center the bulge on the boss and not the player
			ObjPrim_SetVertexPosition(obj, i, xar[i]*dx + cos(ta)*dmax*(dfac^td)*(4+sin(ophi+ta*6))/5 , yar[i]*dy + sin(ta)*dmax*(dfac^td)*(4+sin(ophi+ta*6))/5 , 0);
			ObjPrim_SetVertexUVT(obj, i, xar[i]*du, yar[i]*dv);
			ObjPrim_SetVertexColor(obj, i, 255-102*(dfac^td), 255-237*(dfac^td), 255-102*(dfac^td));
		}
		
		yield;
	}
	Obj_Delete(obj);
}

task BackgroundBulgeA2(objEnemy){
	if(GetCommonData("quality", 2) !=3){return;}
	let renderTexture = GetReservedRenderTargetName(0);

	let frame = 0;
	let baseEffectRadius = 0; 
	let maxEffectRadius = 144;
	let outerFluct = 16;
	let effectRadius = 0;
	let circleVertexCount = 16; 
	let innerCircleCount = 4;
	let gscale = 0;

	let priEffectMin = 20; 
	let priEffectMax = 28; 




	let frameWidth = GetStgFrameWidth;
	let frameHeight = GetStgFrameHeight;
	let frameLeft = GetStgFrameLeft;
	let frameRight = frameLeft + frameWidth;
	let frameTop = GetStgFrameTop;
	let frameBottom = frameTop + frameHeight;


	let path = GetCurrentScriptDirectory ~ "img/effect4.png";
	let objShadow = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(objShadow, 25); 
	ObjPrim_SetTexture(objShadow, path);
	ObjRender_SetBlendType(objShadow, BLEND_MULTIPLY);
	ObjSprite2D_SetSourceRect(objShadow, 1, 1, 255, 255);
	ObjSprite2D_SetDestCenter(objShadow);
	ObjRender_SetScaleXYZ(objShadow, 0, 0, 0);
	
	SetInvalidRenderPriorityA1(priEffectMin, priEffectMax);

	let objInnerC = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	Obj_SetRenderPriorityI(objInnerC, priEffectMax + 1);
	ObjPrim_SetTexture(objInnerC, renderTexture);
	ObjPrim_SetVertexCount(objInnerC, 6 * circleVertexCount * innerCircleCount);


	let objOuterC = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	Obj_SetRenderPriorityI(objOuterC, priEffectMax + 1);
	ObjPrim_SetTexture(objOuterC, renderTexture);
	ObjPrim_SetVertexCount(objOuterC, 3 * circleVertexCount);


	let objOuterE = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	Obj_SetRenderPriorityI(objOuterE, priEffectMax + 1);
	ObjPrim_SetTexture(objOuterE, renderTexture);


	while(!Obj_IsDeleted(objEnemy)){

		ObjRender_SetScaleXYZ(objShadow, gscale, gscale, 0);
		gscale = min(gscale+(1.2/85), 1.2);
		baseEffectRadius = min(baseEffectRadius+(maxEffectRadius/85), maxEffectRadius);
		
		if(GetCommonData("ForceLifebar", 0) == 1){gscale=0;baseEffectRadius=0;}
		
		effectRadius = baseEffectRadius + outerFluct * sin(frame*4);

		let enemyX = ObjRender_GetX(objEnemy);
		let enemyY = ObjRender_GetY(objEnemy); 
		let circleLeft = enemyX - effectRadius + frameLeft;
		let circleRight = enemyX + effectRadius + frameLeft; 
		let circleTop = enemyY - effectRadius + frameTop; 
		let circleBottom = enemyY + effectRadius + frameTop; 


		RenderToTextureA1(renderTexture, priEffectMin, priEffectMax, true);


		ObjRender_SetPosition(objShadow, enemyX, enemyY, 0);


		ObjRender_SetPosition(objInnerC, -frameLeft, -frameTop, 0);
		ascent(let iPosI in 0 .. circleVertexCount){
			let xyOldRadius = 0;
			ascent(let iCircle in 0 .. innerCircleCount){
				let vertexIndex = (iPosI * innerCircleCount + iCircle) * 6;
				let angle1 =  iPosI * (360 / circleVertexCount);
				let angle2 =  (iPosI + 1) * (360 / circleVertexCount);


				let uvRadius1 = iCircle * effectRadius / innerCircleCount;
				let uvRadius2 = (iCircle + 1) * effectRadius / innerCircleCount;

				let xyRadius1t = iCircle * effectRadius / innerCircleCount;
				let xyRadius2t = (iCircle + 1) * effectRadius / innerCircleCount;
				let xyRadius1 = xyOldRadius;
				let xyBias = (xyRadius2t - xyRadius1t) / 2 + (xyRadius2t - xyRadius1t) * sin(frame*16 + (iCircle * 360 / innerCircleCount)) / 4;
				let xyRadius2 = xyRadius2t + xyBias / ((innerCircleCount - iCircle + 1) / 2);

				xyOldRadius = xyRadius2;
				if(iCircle == innerCircleCount - 1){
					xyRadius2 = uvRadius2;
				}

				let leftBias = enemyX + frameLeft;
				let topBias = enemyY + frameTop;
				ObjPrim_SetVertexPosition(objInnerC, vertexIndex  , leftBias + xyRadius1 * cos(angle1), topBias + xyRadius1 * sin(angle1), 0);
				ObjPrim_SetVertexPosition(objInnerC, vertexIndex+1, leftBias + xyRadius2 * cos(angle1), topBias + xyRadius2 * sin(angle1), 0);
				ObjPrim_SetVertexPosition(objInnerC, vertexIndex+2, leftBias + xyRadius1 * cos(angle2), topBias + xyRadius1 * sin(angle2), 0);
			
				ObjPrim_SetVertexPosition(objInnerC, vertexIndex+3, leftBias + xyRadius1 * cos(angle2), topBias + xyRadius1 * sin(angle2), 0);
				ObjPrim_SetVertexPosition(objInnerC, vertexIndex+4, leftBias + xyRadius2 * cos(angle1), topBias + xyRadius2 * sin(angle1), 0);
				ObjPrim_SetVertexPosition(objInnerC, vertexIndex+5, leftBias + xyRadius2 * cos(angle2), topBias + xyRadius2 * sin(angle2), 0);

				ObjPrim_SetVertexUVT(objInnerC, vertexIndex  , leftBias + uvRadius1 * cos(angle1), topBias + uvRadius1 * sin(angle1));
				ObjPrim_SetVertexUVT(objInnerC, vertexIndex+1, leftBias + uvRadius2 * cos(angle1), topBias + uvRadius2 * sin(angle1));
				ObjPrim_SetVertexUVT(objInnerC, vertexIndex+2, leftBias + uvRadius1 * cos(angle2), topBias + uvRadius1 * sin(angle2));
			
				ObjPrim_SetVertexUVT(objInnerC, vertexIndex+3, leftBias + uvRadius1 * cos(angle2), topBias + uvRadius1 * sin(angle2));
				ObjPrim_SetVertexUVT(objInnerC, vertexIndex+4, leftBias + uvRadius2 * cos(angle1), topBias + uvRadius2 * sin(angle1));
				ObjPrim_SetVertexUVT(objInnerC, vertexIndex+5, leftBias + uvRadius2 * cos(angle2), topBias + uvRadius2 * sin(angle2));

			}
		}


		ObjRender_SetPosition(objOuterC, -frameLeft, -frameTop, 0);
		ascent(let iPosO in 0 .. circleVertexCount){
			let angle1 = iPosO * (360 / circleVertexCount);
			let angle2 = (iPosO + 1) * (360 / circleVertexCount);
			let rectX = 0;
			let rectY = 0;
			if(angle1 >= 0 && angle1 < 90){
				rectX = circleRight;
				rectY = circleBottom;
			}
			else if(angle1 >= 90 && angle1 < 180){
				rectX = circleLeft;
				rectY = circleBottom;
			}
			else if(angle1 >= 180 && angle1 < 270){
				rectX = circleLeft;
				rectY = circleTop;
			}
			else if(angle1 >= 270 && angle1 < 360){
				rectX = circleRight;
				rectY = circleTop;
			}
			let vertexIndex = iPosO * 3;
			ObjPrim_SetVertexPosition(objOuterC, vertexIndex, enemyX + effectRadius * cos(angle1) + frameLeft, enemyY + effectRadius * sin(angle1) + frameTop, 0);
			ObjPrim_SetVertexPosition(objOuterC, vertexIndex+1, rectX, rectY, 0);
			ObjPrim_SetVertexPosition(objOuterC, vertexIndex+2, enemyX + effectRadius * cos(angle2) + frameLeft, enemyY + effectRadius * sin(angle2) + frameTop, 0);

			ObjPrim_SetVertexUVT(objOuterC, vertexIndex, enemyX + effectRadius * cos(angle1) + frameLeft, enemyY + effectRadius * sin(angle1) + frameTop);
			ObjPrim_SetVertexUVT(objOuterC, vertexIndex+1, rectX, rectY);
			ObjPrim_SetVertexUVT(objOuterC, vertexIndex+2, enemyX + effectRadius * cos(angle2) + frameLeft, enemyY + effectRadius * sin(angle2) + frameTop);
		}
		

		ObjSpriteList2D_ClearVertexCount(objOuterE);
		ObjRender_SetPosition(objOuterE, -frameLeft, -frameTop, 0);
		ObjSpriteList2D_SetSourceRect(objOuterE, frameLeft, frameTop, frameRight, circleTop + 1);
		ObjSpriteList2D_SetDestRect(objOuterE, frameLeft, frameTop, frameRight, circleTop + 1);
		ObjSpriteList2D_AddVertex(objOuterE);

		ObjSpriteList2D_SetSourceRect(objOuterE, frameLeft, circleTop, circleLeft + 1, circleBottom);
		ObjSpriteList2D_SetDestRect(objOuterE, frameLeft, circleTop, circleLeft + 1, circleBottom);
		ObjSpriteList2D_AddVertex(objOuterE);

		ObjSpriteList2D_SetSourceRect(objOuterE, circleRight, circleTop, frameRight, circleBottom);
		ObjSpriteList2D_SetDestRect(objOuterE, circleRight, circleTop, frameRight, circleBottom);
		ObjSpriteList2D_AddVertex(objOuterE);

		ObjSpriteList2D_SetSourceRect(objOuterE, frameLeft, circleBottom, frameRight, frameBottom);
		ObjSpriteList2D_SetDestRect(objOuterE, frameLeft, circleBottom, frameRight, frameBottom);
		ObjSpriteList2D_AddVertex(objOuterE);

		frame++;
		yield;
	}

	Obj_Delete(objInnerC);
	Obj_Delete(objOuterC);
	Obj_Delete(objOuterE);
	Obj_Delete(objShadow);
	ClearInvalidRenderPriority;
}


