task SpellStart(objEnemy, text, img, x, y, scale)
{
	SetSpellName(objEnemy, text);
	CutInImage(img, x, y, scale);
	SpellCardAttackEffect;
}
task SetSpellName(objEnemy, text)
{
	let objName = ObjText_Create;
	ObjText_SetText(objName, text);

	ObjText_SetFontSize(objName, 18);
	ObjText_SetFontType(objName,"Cabin");
	ObjText_SetFontColorTop(objName, 255, 255, 255);
	ObjText_SetFontColorBottom(objName, 255, 255, 255);
	ObjText_SetFontBorderType(objName, BORDER_FULL);
	ObjText_SetFontBorderWidth(objName, 1);
	ObjText_SetFontBorderColor(objName, 0, 0, 0);
	ObjText_SetMaxWidth(objName, 500);
	ObjText_SetHorizontalAlignment(objName, ALIGNMENT_RIGHT);
	Obj_SetRenderPriorityI(objName, 80);
	ObjRender_SetX(objName, -125);
	ObjRender_SetY(objName, 408);
	ObjRender_SetAlpha(objName, 255);
	
	let scale = 10;
	loop(30)
	{
		scale -= 0.3;
		ObjRender_SetScaleXYZ(objName, scale, scale, scale);
		yield;
	}

	let objEffect = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(objEffect, BLEND_ALPHA);
	Obj_SetRenderPriority(objEffect, 0.40);
	ObjPrim_SetTexture(objEffect, GetCurrentScriptDirectory() ~ "../img/effectSpell.png");
	ObjSprite2D_SetDestRect(objEffect, -128, -20, 128, 20);
	ObjSprite2D_SetSourceRect(objEffect, 0, 0, 256, 40);

	let alpha = 0;
	let y = 443;
	loop(15)
	{
		ObjRender_SetX(objEffect, 254);
		ObjRender_SetY(objEffect, y);
		ObjRender_SetAlpha(objEffect, alpha);

		alpha += 13;
		y -= 2;
		yield;
	}
	loop(15){yield;}
	loop(30)
	{
		ObjRender_SetY(objName, y - 5);
		ObjRender_SetY(objEffect, y);
		
		y -= 388 / 30;
		yield;
	}

	let objBonus = ObjText_Create;
	ObjText_SetText(objBonus, "BONUS:");

	ObjText_SetFontType(objBonus,"Cabin");
	ObjText_SetFontSize(objBonus, 10);
	ObjText_SetFontColorTop(objBonus, 255, 255, 255);
	ObjText_SetFontColorBottom(objBonus, 255, 255, 255);
	ObjText_SetFontBorderType(objBonus, BORDER_FULL);
	ObjText_SetFontBorderWidth(objBonus, 1);
	ObjText_SetFontBorderColor(objBonus, 0, 0, 0);
	ObjText_SetMaxWidth(objBonus, 500);
	ObjText_SetHorizontalAlignment(objBonus, ALIGNMENT_LEFT);
	Obj_SetRenderPriorityI(objBonus, 80);
	ObjRender_SetX(objBonus, 300);
	ObjRender_SetY(objBonus, 53);


	let objScore = ObjText_Create;

	ObjText_SetFontSize(objScore, 12);
	ObjText_SetFontColorTop(objScore, 255, 255, 255);
	ObjText_SetFontColorBottom(objScore, 255, 255, 255);
	ObjText_SetFontBorderType(objScore, BORDER_FULL);
	ObjText_SetFontBorderWidth(objScore, 1);
	ObjText_SetFontBorderColor(objScore, 0, 0, 0);
	ObjText_SetMaxWidth(objScore, 500);
	ObjText_SetHorizontalAlignment(objScore, ALIGNMENT_RIGHT);
	Obj_SetRenderPriorityI(objScore, 80);
	ObjRender_SetX(objScore, -67);
	ObjRender_SetY(objScore, 52);

	AlphaChange(objName);
	AlphaChange(objEffect);
	AlphaChange(objBonus);
	AlphaChange(objScore);
	AutoDelete(objEnemy, objName);
	AutoDelete(objEnemy, objEffect);
	AutoDelete(objEnemy, objBonus);
	AutoDelete(objEnemy, objScore);
	while(!Obj_IsDeleted(objEnemy))
	{
		let objScene = GetEnemyBossSceneObjectID();
		if(ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SHOOTDOWN_COUNT) + ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SPELL_COUNT) == 0)
		{
			let score = truncate(ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID, INFO_SPELL_SCORE));
			ObjText_SetText(objScore, score);
		}
		else
		{
			ObjText_SetText(objScore, "FAILED");
			ObjRender_SetX(objScore, -123);
			ObjRender_SetY(objScore, 53);
			ObjText_SetFontSize(objScore, 10);
		}	
		yield;
	}
	Obj_Delete(objScore);
}

task AlphaChange(obj)
{
	let alpha = 255;
	while(!Obj_IsDeleted(obj))
	{
		if(GetPlayerY < 50)
		{
			alpha -= 16;
			alpha = max(alpha, 95);
		}
		else
		{
			alpha += 16;
			alpha = min(alpha, 255);
		}
		ObjRender_SetAlpha(obj, alpha);
		yield;
	}
}

task CutInImage(img, x, y, scale)
{	
	let objCutin = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(objCutin, img);
	ObjSprite2D_SetSourceRect(objCutin, 0, 0, x, y);
	ObjRender_SetScaleXYZ(objCutin, scale, scale, scale);
  	ObjSprite2D_SetDestCenter(objCutin);
	ObjRender_SetPosition(objCutin, 400, 120, 0);
	Obj_SetRenderPriority(objCutin, 0.80);

	loop(30)
	{
		ObjRender_SetX(objCutin, ObjRender_GetX(objCutin) - 6.6);
		ObjRender_SetY(objCutin, ObjRender_GetY(objCutin) + 3.6);
		yield;
	}
	loop(30){ yield; }
	let alpha = 255;
	loop(30)
	{
		alpha -= 8.5;
		ObjRender_SetX(objCutin, ObjRender_GetX(objCutin) - 9.9);
		ObjRender_SetY(objCutin, ObjRender_GetY(objCutin) + 4.5);
		ObjRender_SetAlpha(objCutin, alpha);
		yield;
	}
	Obj_Delete(objCutin);
}

task SpellCardAttackEffect
{
	let alpha = 1;
	ascent(i in 0..5)
	{
		LineEffect(192, 129 + 75 * ( i - 1), 1);
		LineEffect(192, 129 + 75 * ( i - 0.5), -1);
	}
	CircleEffect(384, 448, -1.15, 180, 1);
	CircleEffect(384, 448, 1.05, 240, 0);
	CircleEffect(384, 448, 1.05, 295, 0);
	CircleEffect(38.4, 44.8, 0.75, 170, 1);
	ascent(i in 0..20)
	{
		alpha = 0.1 + 7.25 *i;
		yield;
	}
	loop(50){yield;}
	descent(i in 0..20)
	{
		alpha = 7.25 *i;
		yield;
	}
	task LineEffect(x, y, dir)
	{
		let objAttack = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjRender_SetBlendType(objAttack, BLEND_ALPHA);
		Obj_SetRenderPriority(objAttack, 0.75);
		ObjPrim_SetTexture(objAttack, GetCurrentScriptDirectory() ~ "../img/SpellCardAttack.png");
		ObjSprite2D_SetSourceRect(objAttack, 0, 0, 512, 16);
		ObjSprite2D_SetDestRect(objAttack, -256, -8, 256, 8);
		ObjRender_SetPosition(objAttack, x, y, 0);
		ObjRender_SetAngleZ(objAttack, -35);
		let sourceX = 0;
		while( alpha >0)
		{
			sourceX += dir * 1.5;
			ObjSprite2D_SetSourceRect(objAttack, sourceX, 0, 512 + sourceX, 16);
			ObjRender_SetAlpha(objAttack, alpha);
			yield;
		}
		Obj_Delete(objAttack);
	}

	task CircleEffect(x, y, sp, rad, dir)
	{	
		let NUM_VERTEX = 16;
		let listRadius = [];
		loop(NUM_VERTEX)
		{
			listRadius = listRadius ~ [0];
		}

		let objSpell = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjPrim_SetPrimitiveType(objSpell, PRIMITIVE_TRIANGLESTRIP);
		ObjPrim_SetVertexCount(objSpell, NUM_VERTEX);
		ObjRender_SetBlendType(objSpell, BLEND_ALPHA);
		Obj_SetRenderPriority(objSpell, 0.76);
		ObjPrim_SetTexture(objSpell, GetCurrentScriptDirectory() ~ "../img/SpellCardAttack.png");
	
		ascent (iVert in 0..NUM_VERTEX / 2)
		{
			let left = iVert * 128;
			let indexVert = iVert * 2;
			ObjPrim_SetVertexUVT(objSpell, indexVert + 0, left, 0);
			ObjPrim_SetVertexUVT(objSpell, indexVert + 1, left, 16);
		}			

		let frame = 0;
		let rRate = 1;
		let angle = 0;
	
		while(alpha>0)
		{
			angle += sp;
			VertexSize;
			ObjRender_SetPosition(objSpell, x, y, 0);
			ObjRender_SetAngleZ(objSpell, angle);
			ObjRender_SetAlpha(objSpell, alpha);
			frame++;
			yield;
		}
		Obj_Delete(objSpell);
		
		task VertexSize
		{	
			if(frame>=35){return;}
			ascent (iVert in 0..NUM_VERTEX / 2)
			{
				let indexVert = iVert * 2;
				let angle = (360 / (NUM_VERTEX / 2 - 1) * iVert);

				let vx1 = listRadius[indexVert] * cos(angle);
				let vy1 = listRadius[indexVert] * sin(angle);
				ObjPrim_SetVertexPosition(objSpell, indexVert + 0, vx1, vy1, 0);

				let vx2 = listRadius[indexVert + 1] * cos(angle);
				let vy2 = listRadius[indexVert + 1] * sin(angle);
				ObjPrim_SetVertexPosition(objSpell, indexVert + 1, vx2, vy2, 0);

				let dr = (rad * rRate - listRadius[indexVert]) / 16;
				listRadius[indexVert] = listRadius[indexVert] + dr;
				if(frame > dir)
				{
					listRadius[indexVert + 1] = listRadius[indexVert + 1] + dr;
				}
			}
		}
	}
}

task AutoDelete(objEnemy, obj)
{
	while(!Obj_IsDeleted(objEnemy)){ yield; }
	Obj_Delete(obj);
}

task EnemyName(objEnemy, text)
{
	let obj = ObjText_Create;
	let spin = 0; 
	
	ObjText_SetFontType(obj,"Helvetica");
	ObjText_SetText(obj, text);
	ObjText_SetFontSize(obj, 15);
	Obj_SetRenderPriorityI(obj, 200);
	ObjText_SetHorizontalAlignment(obj,ALIGNMENT_LEFT);
	ObjText_SetLinePitch(obj,0);
	if(GetCommonData("boss", "EIKA") == "EIKA")
	{
		ObjText_SetFontColorTop(obj, 255, 200, 200);
		ObjText_SetFontColorBottom(obj, 255, 200, 200);
	}
	else if(GetCommonData("boss", "EIKA") == "KOISHI")
	{
		ObjText_SetFontColorTop(obj, 255, 200, 200);
		ObjText_SetFontColorBottom(obj, 255, 200, 200);
	}
	ObjText_SetFontBold(obj, true);
	ObjText_SetFontBorderType(obj, BORDER_FULL);
	ObjText_SetFontBorderWidth(obj, 1);
	ObjText_SetFontBorderColor(obj, 0, 0, 0);
	ObjRender_SetPosition(obj, 35, 20, 0);
	AutoDelete(objEnemy, obj);
	AlphaChange(obj);
}