#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["FlanSpell01"]
#Text["FlanSpell01"]
#BGM["./Overrate.mp3"]
#System["./NoobSystem/Default_System2.txt"]
#Background["./NoobSystem/Default_Background_IceMountain.txt"]
#Player["./Reisen/Reisen.txt"]

#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 objScene;
@Initialize
{
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	ObjEnemy_SetDamageRate(objEnemy, 0, 0);
	TWork;
	TAnimate("Mima",GetCurrentScriptDirectory~"dot_flandre.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(2000);
	}
	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,120);
	let cutinimage = GetCurrentScriptDirectory~"null.png";
	ObjCutin_SetSpellcardS4("Another Kagome Kagome", cutinimage, NAZRIN, 240, 104, 104);
	wait(30);
	objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);
	ObjCutin_LaunchS3(YABUSAME, cutinimage, "Normal");
	CreateGrid;
	wait(60);
	CreateBounceShot;
	wait(120);
	Movement;
}

task CreateBounceShot
{
	let color = [BL_SMALL_RED,BL_SMALL_ORANGE,BL_SMALL_YELLOW,BL_SMALL_GREEN,BL_SMALL_AQUA,BL_SMALL_BLUE,BL_SMALL_PURPLE];
	ascent(i in 0..6)
	{
		PlaySound("Shot2",100,0);
		BounceShot(i*60+20);
		wait(15);
	}
	task BounceShot(angle)
	{
		let bshot = CreateShotA1(GetEnemyX,GetEnemyY,2.25,angle,LARGE_RED,10);
		ObjShot_SetSpellResist(bshot,true);
		let gangle = 0;
		let gx = 0;
		let gy = 0;
		while(!Obj_IsDeleted(bshot))
		{
			if(ObjMove_GetX(bshot)<0 || ObjMove_GetX(bshot)>GetStgFrameWidth)
			{
				ObjMove_SetAngle(bshot,180-ObjMove_GetAngle(bshot));
				if(ObjMove_GetX(bshot)<0){gx=0;gangle = 0;}
				else{gx=GetStgFrameWidth;gangle = 180;}
				GimmickShotY(gx,truncate(ObjMove_GetY(bshot)/113) , gangle);
			}
			if(ObjMove_GetY(bshot)<0 || ObjMove_GetY(bshot)>GetStgFrameHeight)
			{
				ObjMove_SetAngle(bshot,-ObjMove_GetAngle(bshot));
				if(ObjMove_GetY(bshot)<0){gy=0;gangle = 90;}
				else{gy=GetStgFrameHeight;gangle = 270;}
				GimmickShotX(gy,truncate(ObjMove_GetX(bshot)/95) , gangle);
			}
			yield;
		}
	}
	task GimmickShotY(x,yvalue,ga)
	{
		PlaySound("BulletWave",100,0);
		WriteLog(yvalue);
		let v = rand(1,2.75);
		ascent(i in 0..14)
		{
			CreateShotA1(x,113*yvalue+i*(113/14)+113/28,v,ga,color[i%7]+500,15);
		}
	}
	task GimmickShotX(y,xvalue,ga)
	{
		PlaySound("BulletWave",100,0);
		WriteLog(xvalue);
		let v = rand(1,2.75);
		ascent(i in 0..14)
		{
			CreateShotA1(95*xvalue+i*(95/14)+95/28,y,v,ga,color[i%7]+500,15);
		}
	}
}

task CreateGrid
{
	ascent(i in 0..5)
	{
		Grid(i*95,-10,90);
		Grid(-10,i*113,0);
	}
	task Grid(px,py,angle)
	{
		let g = CreateStraightLaserA1(px,py,angle,500,30,99999,ST_LASER_RED,99999);
		ObjShot_SetIntersectionEnable(g,false);
	}
}

task Movement
{
	while(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0)
	{
		BorderMove(90,GetStgFrameWidth-90,90,150,10,120);
		wait(240);
	}
}


//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);
	DeleteAllSound;
	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());
}
