#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["MimaSpell02"]
#Text["MimaSpell02"]
#BGM["./Novum.mp3"]
#System["./NoobSystem/Default_System2.txt"]
#Background["./NoobSystem/Default_Background_IceMountain.txt"]
#Player[DEFAULT]

#include"./Shot/AllStarShot_Const.dnh"
#include"./NoobSystem/Default_Effect.txt"
#include"./Function/Basic.txt"
#include"./Function/SoundFunctions.txt"
#include"./Function/EffectFunctions.txt"
#include"./Animations.txt"
#include"./Cutin/Cutin.txt"

let objEnemy;
let cx = GetStgFrameWidth/2;
let ct = GetStgFrameHeight/2;
let phase = false;
let objScene;
@Initialize
{
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	ObjEnemy_SetDamageRate(objEnemy, 0, 0);
	TWork;
	TAnimate("Mima",GetCurrentScriptDirectory~"dot_mima.png");
	TEnd;
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
}

@MainLoop
{
	ObjEnemy_SetIntersectionCircleToPlayer(objEnemy,ObjMove_GetX(objEnemy),ObjMove_GetY(objEnemy),18);
	ObjEnemy_SetIntersectionCircleToShot(objEnemy,ObjMove_GetX(objEnemy),ObjMove_GetY(objEnemy),24);
	yield;
}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
	{
		SetScriptResult(2500);
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(60);
	}
	case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(3500000);
	}
}

//----------------------------------------------------

//----------------------------------------------------
task TWork
{
	yield;
	ObjMove_SetDestAtWeight(objEnemy, GetStgFrameWidth/2, 120, 10,60);
	DamageRateAtFrame(50, 25,240);
	let cutinimage = GetCurrentScriptDirectory~"null.png";
	ObjCutin_SetSpellcardS4("Reincarnation of the Phantom Spirit", cutinimage, NAZRIN, 240, 104, 104);
	wait(30);
	objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);
	ObjCutin_LaunchS3(YABUSAME, cutinimage, "Normal");
	wait(90);
	PlaySound("Charge2",100,0);
	SakuraCharge("PURPLE");
	wait(60);
	Opening;
	wait(120);
	WallStar;
}

task WallStar
{
	while(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0)
	{
		let y = GetStgFrameHeight/2+rand(-90,90);
		WStar(-15,y);
		if(phase==true){LStar(GetStgFrameWidth/2+rand(-60,-30),0)}
		wait(120);
		y = GetStgFrameHeight/2+rand(-90,90);
		WStar(GetStgFrameWidth+15,y);
		if(phase==true){LStar(GetStgFrameWidth/2+rand(60,30),0)}
		wait(120);
		BorderMove(120,GetStgFrameWidth-120,90,150,10,5);
		if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) < 1500 && phase==false)
		{
			PlaySound("Charge2",100,0);
			SakuraCharge("PURPLE");
			wait(60);
			phase = true;
		}
	}
	task WStar(x,y)
	{
		let angle = rand(310,320)+10;
		let count = 0;
		let angular = 0.25;
		if(x>0){angle = rand(220,230)-10;}
		let obj = CreateShotA2(x,y,3,angle,-0.02,1,round(rand(267,274)),0);
		if(x>0){angular = -0.25}
		ObjMove_SetAngularVelocity(obj,angular);
		ObjMove_AddPatternA2(obj,180,NO_CHANGE,NO_CHANGE,0.03,angular,6);
		Trail(obj);
		ObjShot_SetSpellResist(obj,true);
		while(!Obj_IsDeleted(obj))
		{
			if(count%40==0)
			{
				StarWave;
			}
			count++;
			yield;
		}
		task StarWave
		{
			let randangle = rand(0,45);
			ascent(n in 0..4)
			{
				PlaySound("Shot6",95,0);
				ascent(i in -2..3)
				{
					if(!Obj_IsDeleted(obj))
					{
						CreateShotOA1(obj,1.25,ObjMove_GetAngle(obj)+180+i*45+randangle,ObjShot_GetImageID(obj)-24+500,8);
					}
				}
				wait(4);
			}
		}
	}
	task LStar(x,y)
	{
		let angle = rand(95,105);
		let count = 0;
		let angular = 0.4;
		if(x>GetStgFrameWidth/2){angle = rand(85,75);}
		let obj = CreateShotA2(x,y,3,angle,-0.02,1,round(rand(267,274)),0);
		if(x>GetStgFrameWidth/2){angular = -0.4}
		ObjMove_SetAngularVelocity(obj,angular);
		Trail(obj);
		ObjShot_SetSpellResist(obj,true);
		ObjMove_AddPatternA2(obj,180,NO_CHANGE,NO_CHANGE,0.03,angular,6);
		while(!Obj_IsDeleted(obj))
		{
			if(count%60==59)
			{
				PlaySound("Laser6",100,0);
				LaserWave;
			}
			count++;
			yield;
		}
		task LaserWave
		{
			let randangle = rand(0,45);
			ascent(i in -2..3)
			{
				if(!Obj_IsDeleted(obj))
				{
					CreateLooseLaserA1(ObjMove_GetX(obj),ObjMove_GetY(obj),2,ObjMove_GetAngle(obj)+180+i*45+randangle,120,15,ObjShot_GetImageID(obj)+782,10);
				}
			}
		}
	}
}

task Opening
{
	ascent(n in 0..6)
	{
		ascent(i in 0..2)
		{
			PlaySound("Eff1",100,0);
			let obj = CreateShotA2(GetEnemyX,GetEnemyY,0,i*180+n*30,0.05,10,round(rand(267,274)),12);
			ObjMove_SetAngularVelocity(obj,1);
			Trail(obj);
			obj = CreateShotA2(GetEnemyX,GetEnemyY,0,i*180-n*30,0.05,10,round(rand(267,274)),12);
			ObjMove_SetAngularVelocity(obj,-1);
			Trail(obj);
			wait(3);
		}
	}
}

		task Trail(obj)
		{
			while(!Obj_IsDeleted(obj))
			{
				let dummy = CreateShotOA1(obj,0,ObjMove_GetAngle(obj),ObjShot_GetImageID(obj)+500,0);
				Shrink(dummy,25);
				ObjShot_SetDeleteFrame(dummy,25);
				ObjShot_SetIntersectionEnable(dummy,false);
				wait(2);
			}
		}
		
	task Shrink(obj,time)
	{
		ObjShot_SetSpellResist(obj,true);
		descent(i in 0..time)
		{
			ObjRender_SetScaleXYZ(obj,i/time,i/time,0);
			ObjRender_SetColor(obj,128,128,128);
			yield;
		}
		Obj_Delete(obj);
	}

//BorderMove(90,GetStgFrameWidth-90,90,150,10,90);

//----------------------------------------------------
task TEnd
{
	while(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0)
	{
		yield;
	}

	let ex = ObjMove_GetX(objEnemy);
	let ey = ObjMove_GetY(objEnemy);
	TExplosionA(ex, ey, 10, 0.6);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	Obj_Delete(objEnemy);
	if(ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SHOOTDOWN_COUNT) +ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SPELL_COUNT) == 0)
	{
		AddScore(ObjEnemyBossScene_GetInfo(objScene,INFO_SPELL_SCORE));
	}
	loop(30)
	{
		DeleteShotAll(TYPE_ALL, TYPE_IMMEDIATE);
		yield;
	}

	CloseScript(GetOwnScriptID());
}
