#TouhouDanmakufu
#Title[Time Master "Killer Watch"]
#Text[]
#Image[]
#BackGround[Default]
#Player[FREE]
#ScriptVersion[2]

script_enemy_main
{
	let frame=100;
	let type=0;
	let angle=0;
	let timestop=0;
	let angleb=0;
    	let BossCutIn = GetCurrentScriptDirectory~"img\sakuya_TS.png";
	#include_function ".\sakuyaanim.txt";
	#include_function ".\spellcard.txt";
task red_knife(x, y, v, angle, d)
{
	let counter=0;
	let obj=Obj_Create(OBJ_SHOT);
	Obj_SetPosition(obj, x, y);
	Obj_SetSpeed(obj, v);
	Obj_SetAngle(obj, angle);
	ObjShot_SetGraphic(obj, 218);
	ObjShot_SetDelay(obj, d);
	while(Obj_BeDeleted(obj)==false)
	{
	yield;
	if(timestop==1)
	{
	counter++;
	if(counter==rand_int(10, 60))
	{
	Obj_SetSpeed(obj, 0);
	ObjShot_SetGraphic(obj, 216);
	Obj_SetAngle(obj, atan2(GetPlayerY-Obj_GetY(obj), GetPlayerX-Obj_GetX(obj)));
	}
	}
	if(timestop==0)
	{
	Obj_SetSpeed(obj, v);
	}
	}
}
task green_knife(x, y, v, angle, d)
{
	let counter=0;
	let obj=Obj_Create(OBJ_SHOT);
	Obj_SetPosition(obj, x, y);
	Obj_SetSpeed(obj, v);
	Obj_SetAngle(obj, angle);
	ObjShot_SetGraphic(obj, 218);
	ObjShot_SetDelay(obj, d);
	while(Obj_BeDeleted(obj)==false)
	{
	yield;
	if(timestop==1)
	{
	counter++;
	if(counter==rand_int(10, 60))
	{
	Obj_SetSpeed(obj, 0);
	ObjShot_SetGraphic(obj, 220);
	Obj_SetAngle(obj, rand(0, 359));
	}
	}
	if(timestop==0)
	{
	Obj_SetSpeed(obj, v);
	}
	}
}
task yellow_knife(x, y, v, angle, d)
{
	let counter=0;
	let obj=Obj_Create(OBJ_SHOT);
	Obj_SetPosition(obj, x, y);
	Obj_SetSpeed(obj, v);
	Obj_SetAngle(obj, angle);
	ObjShot_SetGraphic(obj, 218);
	ObjShot_SetDelay(obj, d);
	while(Obj_BeDeleted(obj)==false)
	{
	yield;
	if(timestop==1)
	{
	counter++;
	if(counter==rand_int(10, 60))
	{
	Obj_SetSpeed(obj, 0);
	ObjShot_SetGraphic(obj, 221);
	}
	}
	if(timestop==0 && counter>=1)
	{
	loop(8)
	{
	CreateShot01(Obj_GetX(obj), Obj_GetY(obj), 3, angle, 221, 0);
	angle+=45;
	}
	ObjShot_FadeDelete(obj);
	}
	}
}
function knife(x, y, v, angle, d)
{
	let a=rand_int(1, 8);
	if(a==1 || a==4 || a==7 || a==8)
	{
	red_knife(x, y, v, angle, d);
	}
	if(a==2 || a==5 || a==6)
	{
	green_knife(x, y, v, angle, d);
	}
	if(a==3)
	{
	yellow_knife(x, y, v, angle, d);
	}
}
	@Initialize
	{
		SetInvincibility(200);
		SetLife(500);
		SetDamageRate(10,10);
		
		LoadGraphic(BossCutIn);

		SetMovePosition02(GetCenterX,120,20);
		CutIn(YOUMU, "Time Master ''Killer Watch'' ",BossCutIn,0,0,256,512);
		LoadUserShotData(GetCurrentScriptDirectory~"supershot.txt");

		SetCommonData("ONSPELL", "TRUE");
		spellbonus(3000000);
		timer;
		SetCommonData("timercount", 5400);
	}
	
	@MainLoop
	{
		SetDamageRate(10+floor(30-30*GetCommonData("timercount")/5400),5);
		SetCollisionA(GetX(),GetY(),24);
		SetCollisionB(GetX(),GetY(),24);
		frame++;
		yield;
	if(frame<450 && frame%20==0 && frame>=200)
	{
	PlaySE(GetCurrentScriptDirectory~"sound\Shot1.wav");
		angleb+=6;
		ascent(i in 0..8)
		{
		ascent(j in -2..3)
		{
		CreateShotA(1, GetX+30*cos(i*45+j*2), GetY+30*sin(i*45+j*2), 20);
		SetShotDataA(1, 0, 4, i*45+75+j*2+angleb, 0, 0, 0, 62);
		FireShot(1);
		CreateShotA(1, GetX+30*cos(i*45+22.5+j*2), GetY+30*sin(i*45+22.5+j*2), 20);
		SetShotDataA(1, 0, 4, i*45-47.5+j*2+angleb, 0, 0, 0, 67);
		FireShot(1);
		}
		}
	}
	if(frame==300)
	{
	Concentration01(195);
	}
	if(frame==480)
	{
	type=1;
	}
	if(frame==620)
	{
	type=0;
	}
	if(frame==500)
	{
	angle=GetAngleToPlayer;
	PlaySE(GetCurrentScriptDirectory~"sound\Shot1.wav");
	loop(30)
	{
	knife(GetX, GetY, 4, angle, 0);
	knife(GetX, GetY, 3, angle, 0);
	knife(GetX, GetY, 2, angle, 0);
	angle+=12;
	}
	}
	if(frame==520)
	{
	TimeStop(100, 1, 1, 0);
	timestop=1;
	}
	if(frame==620)
	{
		PlaySE(GetCurrentScriptDirectory~"sound\BulletWave.wav");
	timestop=0;
	frame=200;
	}
}
	
	@Finalize
	{
		AddScore(spellbonustotal);
		if(spellbonustotal>0)
		{
		PlaySE(GetCurrentScriptDirectory~"sound\se_cardget.wav");
		loop(50)
		{
			CreateItem(ITEM_SCORE,GetX+rand(-100,100),GetY+rand(20,100));
		}
		}else{
		loop(15)
		{
			CreateItem(ITEM_SCORE,GetX+rand(-100,100),GetY+rand(20,100));
		}
		}
		DeleteGraphic(BossCutIn);
		SetCommonData("ONSPELL", "FALSE");
	}
	
	@DrawLoop
	{
	spellbg;
	if(type==0)
	{
	anim2;
	}
	if(type==1)
	{
	animation3;
	}
	}
}