#TouhouDanmakufu[Single]
#ScriptVersion[3]
#System["./system/Default_System.txt"]

#include"./Effects_Main.txt"

let current = GetCurrentScriptDirectory;
let enm = ID_INVALID;
let GetX = 0;
let GetY = 0;
let EnemyX = 0;
let EnemyY = 0;

let s=1;
let angle2=0;
let c=6; let c2=15; let c3=7;
let ang=0;

let objScene=GetEnemyBossSceneObjectID();
// your best friend, forever.
function wait(w) { loop(w) { yield; } } 

@Event{
	alternative(GetEventType)
	//1;500;2500
	case(EV_REQUEST_LIFE){SetScriptResult(8000);}
	case(EV_REQUEST_TIMER){SetScriptResult(65);}
	case(EV_REQUEST_SPELL_SCORE){
			SetScriptResult(2500000);
		}
	case(EV_REQUEST_IS_DURABLE_SPELL){SetScriptResult(true);}
}

@Initialize{

	
		ObjEnemy_SetDamageRate(enm, 50, 0);


SetAutoDeleteObject(true);
	enm = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(enm);
	CreateLifeBar(enm, 60, 64, 41, 0, [255,255,255], "SpellCard" , [false,false] );
AnimeKutakaWBaWC(enm);

TAura(enm, 0);
	ObjMove_SetDestAtWeight(enm, GetStgFrameWidth/2, GetStgFrameHeight/2-70, 12, 5);
	Invincible(200);
	BossActions;
}


@MainLoop{
	GetX = ObjMove_GetX(enm);
	GetY = ObjMove_GetY(enm);
EnemyX = ObjMove_GetX(enm);
angle2+=s;
	EnemyY = ObjMove_GetY(enm);
	ObjEnemy_SetIntersectionCircleToShot(enm, GetX, GetY, 28);
	ObjEnemy_SetIntersectionCircleToPlayer(enm, GetX, GetY, 32);
	yield;
}

function angleToBoss{
	let dir = atan2(GetY-GetPlayerY,GetX-GetPlayerX);
	return dir;
}

function angleToPlayer{
	let dir = atan2(GetPlayerY-GetY,GetPlayerX-GetX);
	return dir;
}



task BossActions{
	let dir = angleToPlayer;
	let angleT = rand(0,360);

	 
	PerformActions;
	while(ObjEnemy_GetInfo(enm, INFO_LIFE) > 0){
		yield;
	}
	

	NotifyEventAll(EV_USER+10, [ObjMove_GetX(enm), ObjMove_GetY(enm)]);
	Obj_Delete(enm);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);

	yield;
	CloseScript(GetOwnScriptID);
	
	task PerformActions{
	loop(90){yield;}
	LoadMagicCircle(1);
	StartSpellMiddle("Morning Sign \"Ordeal of Crowing Roosters\" ", GetCurrentScriptDirectory~"./img/KutakaCutin.png", 255, 155, 10,400,551,0.9);
	
	loop(30){yield;}

let angle = -90;

if (GetCommonData("rank", "Normal")=="Normal"){c=6; c2=3; c3=10;}
else{c=8; c2=4; c3=12;}
let a =0;
Shot2;
ChargeUp(enm, [ObjMove_GetX(enm), ObjMove_GetY(enm)], -12,2, angle, 10, 25, [229, 18, 18], "DARK");
ChargeUp(enm, [ObjMove_GetX(enm), ObjMove_GetY(enm)], 12,2, angle, 10, 25, [18, 218, 229], "DARK");
wait(60);
		while(!Obj_IsDeleted(enm)){
		let ang=rand(0,360);
		
		Obj_SetValue(enm,"cast",1);
		
		Shot1;
		wait(155);}
		//Move(moon,0);
	}}
	
	task Shot2{
wait(650);

let graphic=REST_GRAY;
ChargeUp(enm, [ObjMove_GetX(enm), ObjMove_GetY(enm)], -12,2, angle, 10, 25, [229, 18, 18], "DARK");
ChargeUp(enm, [ObjMove_GetX(enm), ObjMove_GetY(enm)], 12,2, angle, 10, 25, [18, 218, 229], "DARK");
wait(60);
	while(!Obj_IsDeleted(enm)){
		let ang=rand(0,360);
		PlaySoundEffect("se_lgods4");
		ascent(i in 0..45){
	CreateShotA5(EnemyX+55*cos(ang),EnemyY+15*sin(ang),4,ang,-0.08,0,graphic,15);
	
	ang+=360/45;
		}
		if(graphic>=354){graphic=346;}
		graphic+=1;
	wait(65);
	}
task CreateShotA5(x, y, speed, angle,accel,maxspd, graphic, delay){

DelayShot(x, y, graphic, angle, speed, BLEND_ADD_ARGB, 3, delay);
wait(delay);
let shota1=CreateShotA2(x+speed*delay*cos(angle), y+speed*delay*sin(angle), speed, angle,accel,maxspd, graphic, 0);
ObjMove_AddPatternA2(shota1,60, NO_CHANGE,NO_CHANGE,0.05,0,1.5);}
	
}

task Shot1{
	
	
	PlaySoundEffect("se_boon01");
	
	CreateShotA4(135,GetStgFrameHeight/2-190,0,45,ORB_RED+500,0.03,2.15,15);
	
}

task CreateShotA4(x, y, speed, angle,graphic,accel,maxspd, delay){

let graphnote=T_NOTE_GRAY;

DelayShot(x, y, graphic, angle, speed, BLEND_ADD_ARGB, 3, delay);
wait(delay);
let shota1=CreateShotA2(x+speed*delay*cos(angle), y+speed*delay*sin(angle), speed, angle,accel,maxspd, graphic, 0);
ObjMove_SetAngularVelocity(shota1,-0.35);
ObjShot_SetSpellResist(shota1,true);
ObjMove_ReflectSTGWallsFinal(shota1,5,graphic);
movingdown(shota1);
Deletecorner(shota1);

let D=90;

while(!Obj_IsDeleted(shota1)){
	let ang=rand(0,360);
	let graphrand=round(rand(T_NOTE_RED,T_NOTE_ORANGE));
	PlaySoundEffect("se_lgods1");
	
	
		ascent(i in 0..5){
		
		CreateAdder(ObjMove_GetX(shota1), ObjMove_GetY(shota1), 1.25, ang, graphnote, 15,shota1,graphrand);
		
		ang+=360/5;}


	wait(round(rand(36,47)));
}

task movingdown(obj){
	let YY=0;
	while(!Obj_IsDeleted(obj)){
	
ObjMove_SetY(obj,ObjMove_GetY(obj)+YY);
YY+=0.0011;
wait(1);
}
	
}

task CreateAdder(x, y, speed, angle, graphic, delay,obj,graphrand){

DelayShot(x, y, graphic, angle, speed, BLEND_ADD_ARGB, 3, delay);
wait(delay);
let shota1=CreateShotA1(x+speed*delay*cos(angle), y+speed*delay*sin(angle), speed, angle, graphic, 0);
ObjMove_AddPatternA2(shota1,0,NO_CHANGE,NO_CHANGE,-0.06,0,0);
ObjMove_AddPatternA3(shota1,60,NO_CHANGE,angle,0.015,0,1.75,graphrand);
Deletecorner(shota1);


}

}

task ObjMove_ReflectSTGWallsFinal(obj, limit,graph){
	let count = 0;
	while(!Obj_IsDeleted(obj)){
	
		if(ObjMove_GetX(obj) <= 130 && ObjMove_GetX(obj) >= 110 && count < limit){
			PlaySoundEffect("se_lgods2");
			ObjMove_SetAngle(obj, ObjMove_GetAngle(obj) + 180);
			ObjShot_SetGraphic(obj,graph-1+round(rand(1,6)));
			//graph++;	
			ObjMove_SetAngularVelocity(obj,-0.35);
			count++;
		}

		if(ObjMove_GetX(obj) >= GetStgFrameWidth-130 && ObjMove_GetX(obj) <= GetStgFrameWidth -110 && count < limit){
			PlaySoundEffect("se_lgods3");
			ObjMove_SetAngle(obj, ObjMove_GetAngle(obj) + 180);
			ObjShot_SetGraphic(obj,graph-1+round(rand(1,6)));
			//graph++;		
			ObjMove_SetAngularVelocity(obj,0.35);
			count++;
		}
		
		
		yield;
	}
	
}

 task Deletecorner(obj){
	let angleT=0;
	let a = 120;
	let gravity1=rand(-0.75,-1);
	let speedx = 1;
		 
 while(ObjMove_GetX(obj)>120 && ObjMove_GetX(obj)<GetStgFrameWidth-90 && ObjMove_GetY(obj)<GetStgFrameHeight){yield;}
	if(ObjMove_GetX(obj)<=120){
	 
	}
	
	else if(ObjMove_GetX(obj)>=GetStgFrameWidth-90){
	

	}
	
	else if(ObjMove_GetY(obj)>=GetStgFrameHeight){
	

	}


	Obj_Delete(obj);
    }


	
	task AfterImage(tobj,texture,color,startscaleX,startscaleY,scaleinc,time,x1,y1,x2,y2,Yangle,Zangle){     ////// From AJS
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	let alpha = 100;
	let Red = color[0];
	let Green = color[1];
	let Blue = color[2];
	
	ObjPrim_SetTexture(obj, texture);
	ObjSprite2D_SetSourceRect(obj, x1, y1, x2, y2);
	ObjRender_SetScaleXYZ(obj,startscaleX,startscaleY,0);
	Obj_SetRenderPriorityI(obj,26);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj,ObjRender_GetX(tobj),ObjRender_GetY(tobj),0);
	ObjRender_SetAlpha(obj,alpha);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	ObjRender_SetColor(obj,Red,Green,Blue);
	ObjRender_SetAngleXYZ(obj,0,Yangle,Zangle);
	
	loop(time){
	startscaleX+=scaleinc/time;
	startscaleY+=scaleinc/time;
	ObjRender_SetScaleXYZ(obj,startscaleX,startscaleY,0);
	
	alpha-=100/time;
	ObjRender_SetAlpha(obj,alpha);
	yield;}
	
	Obj_Delete(obj);
}

	task Invincible(frame){
		SetInvincibility(enm, frame);
		
	}


//BulletBreak([ObjMove_GetX(obj2), ObjMove_GetY(obj2)],rand(0,360),ObjShot_GetImageID(obj2),BLEND_ALPHA);


function ObjRender_SetScale(obj,scale)
{
	return ObjRender_SetScaleXYZ(obj,scale,scale,scale);
}

function getDotAngle(x1,y1,x2,y2){
	let angle=atan2(y1-y2,x1-x2);
	return angle;
}


function CreateEffectObject(px,py,img,blendtype,left,top,right,bottom){
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	LoadTexture(img);
	ObjPrim_SetTexture(obj,img);
	ObjSprite2D_SetSourceRect(obj,left,top,right,bottom);
	ObjSprite2D_SetDestCenter(obj);
	if(blendtype=="ALPHA"){
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	}
	if(blendtype=="RGB"){
	ObjRender_SetBlendType(obj,BLEND_ADD_RGB);
	}
	if(blendtype=="ARGB"){
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	}
	if(blendtype=="INVERT"){
	ObjRender_SetBlendType(obj,BLEND_INV_DESTRGB);
	}
	ObjRender_SetPosition(obj,px,py,0);
	ObjMove_SetPosition(obj,px,py,);
	SetAutoDeleteObject(true);
	return obj;
}

task AnimeKutakaWBaWC(obj){

	let wi = 64;
	let he = 95;
	
	let dir;
	let speed;
	let whichway = 0;
	let frame = 0;
	let idleframe = 0;
	let castframe = 0;
	Obj_SetValue(obj,"cast",0);

	ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "./img/KutakaSprite.png");
	ObjSprite2D_SetSourceRect(obj,0,0,wi,he);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,1,1,0);
	ObjRender_SetAngleXYZ(obj,0,0,0);
	
	while(!Obj_IsDeleted(obj)){
		
		dir = ObjMove_GetAngle(obj);
		speed = ObjMove_GetSpeed(obj);

		if(speed == 0 && frame > 0){whichway = -1;}
		else if(speed == 0 && frame < 0){whichway = 1;}
		else if(speed == 0 && frame == 0){whichway = 0;}

		else if(cos(dir) < 0){whichway=-1;}
		else if(cos(dir) > 0){whichway=1;}
		
		if(Obj_GetValueD(obj,"cast",0) == 0 && speed == 0 && frame == 0){

			castframe = 0;
			if(idleframe >= 0 && idleframe < 5){ObjSprite2D_SetSourceRect(obj,0,0,wi,he);}
			else if(idleframe >= 5 && idleframe < 10){ObjSprite2D_SetSourceRect(obj,wi,0,wi*2,he);}
			else if(idleframe >= 10 && idleframe < 15){ObjSprite2D_SetSourceRect(obj,wi*2,0,wi*3,he);}
			else if(idleframe >= 15 && idleframe < 20){ObjSprite2D_SetSourceRect(obj,wi*3,0,wi*4,he);}
			else if(idleframe >= 20 && idleframe < 25){ObjSprite2D_SetSourceRect(obj,wi*4,0,wi*5,he);}
			else if(idleframe >= 25 && idleframe < 30){ObjSprite2D_SetSourceRect(obj,wi*3,0,wi*4,he);}
			else if(idleframe >= 30 && idleframe < 35){ObjSprite2D_SetSourceRect(obj,wi*2,0,wi*3,he);}
			else if(idleframe >= 35 && idleframe < 40){ObjSprite2D_SetSourceRect(obj,wi,0,wi*2,he);}
			if(idleframe < 40){idleframe++;}
			else if(idleframe == 40){idleframe=5;}
			
		}
		
		else if(Obj_GetValueD(obj,"cast",0) == 1 && speed == 0 && frame == 0){
			
			idleframe=0;
			frame=0;
			if(castframe >= 0 && castframe < 5){ObjSprite2D_SetSourceRect(obj,0, he*3,wi, he*4);}
			if(castframe >= 5 && castframe < 10){ObjSprite2D_SetSourceRect(obj,wi,he*3,wi*2,	he*4);}
			if(castframe >= 10 && castframe < 15){ObjSprite2D_SetSourceRect(obj,wi*2,he*3,wi*3,	he*4);}
			if(castframe >= 15 && castframe < 20){ObjSprite2D_SetSourceRect(obj,wi*3,he*3,wi*4,	he*4);}
			
			if(castframe < 20){castframe++;}
			else if(castframe == 20){castframe=5;}
		}

		else if(speed != 0 || frame != 0){
		
			if(frame < 0 && frame >= -5){ObjSprite2D_SetSourceRect(obj,0,he*2,wi,he*3); ObjRender_SetAngleXYZ(obj,0,0,0);}
			if(frame < -5 && frame >= -10){ObjSprite2D_SetSourceRect(obj,wi,he*2,wi*2,he*3); ObjRender_SetAngleXYZ(obj,0,0,0);}
			if(frame < -10 && frame >= -15){ObjSprite2D_SetSourceRect(obj,wi*2,he*2,wi*3,he*3); ObjRender_SetAngleXYZ(obj,0,0,0);}
			if(frame < -15 && frame >= -20){ObjSprite2D_SetSourceRect(obj,wi*3,he*2,wi*4,he*3); ObjRender_SetAngleXYZ(obj,0,0,0);}
			if(frame < -20 && frame >= -25){ObjSprite2D_SetSourceRect(obj,wi*4,he*2,wi*5,he*3); ObjRender_SetAngleXYZ(obj,0,0,0);}

			if(frame > 0 && frame <= 5){ObjSprite2D_SetSourceRect(obj,0,he,wi,he*2); ObjRender_SetAngleXYZ(obj,0,0,0);}
			if(frame > 5 && frame <= 10){ObjSprite2D_SetSourceRect(obj,wi,he,wi*2,he*2); ObjRender_SetAngleXYZ(obj,0,0,0);}
			if(frame > 10 && frame <= 15){ObjSprite2D_SetSourceRect(obj,wi*2,he,wi*3,he*2); ObjRender_SetAngleXYZ(obj,0,0,0);}
			if(frame > 15 && frame <= 20){ObjSprite2D_SetSourceRect(obj,wi*3,he,wi*4,he*2); ObjRender_SetAngleXYZ(obj,0,0,0);}
			if(frame > 20 && frame <= 25){ObjSprite2D_SetSourceRect(obj,wi*4,he,wi*5,he*2); ObjRender_SetAngleXYZ(obj,0,0,0);}

			idleframe=0;
			castframe=0;

			
		}
		ObjSprite2D_SetDestCenter(obj);
		frame=frame+whichway;
		if(frame > 16){frame=16;}
		if(frame < -16){frame=-16;}
		yield;
		
	}
	
}
