//#TouhouDanmakufu
#ScriptVersion[3]
#Title["SakuyaS2_N"]
#Text["Bullets"]
let white = GetCurrentScriptDirectory~"sprites/white.png";

#include"./lib/SHOT_REPLACE/shot_replace.dnh"
#include"./Sakuyainclude.txt"
let color2 = 1;
let color = 0;
let objEnemy;
let duration = 0;
let stopstuff = false;
let angle = 0;
let angle2 = 0;
let one = 1;
let type = 0;
let animframe = 0;
let frame = 0;
let objlasers = [];
let GetSpeedX = 0;
let variable = 0;
let thunder = GetCurrentScriptDirectory~"sfx/thunder.wav";
let n = 0;
let shotrate = 22.5;
let bombrate = 25;

@Event{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
	SetScriptResult(1000);
	}
	case(EV_REQUEST_TIMER){
	SetScriptResult(999);
	}
	case(EV_REQUEST_SPELL_SCORE){
	SetScriptResult(10000000);
	}
	case(EV_GAIN_SPELL)
	{
	AddScore(ObjEnemyBossScene_GetInfo(objScene, INFO_SPELL_SCORE));	
	}
}

@Initialize{
	//Resource Loading
	shotinit;
	LoadSound(shot);
	LoadSound(shot2);
	LoadSound(shot3);
	LoadSound(timestop);
	LoadSound(charge2);
	LoadSound(ping);
	LoadSound(cardcall);
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	Invuln(420, shotrate, bombrate);
	//BottomEnemyMarker(objEnemy, objEnemy);
	let Sakuya = GetCurrentScriptDirectory~"sprites/sakuya.png";
	ObjPrim_SetTexture(objEnemy, Sakuya); 
	ObjSprite2D_SetSourceRect(objEnemy, 0, 0, 128, 128);
	ascent(x in 0..3){
	ObjAnim_AddFrameA2(objEnemy, ANIM_IDLE, 0, 0+x*128, 128, 128+x*128, 0, 0);
	}
	ascent(x in 0..2){
	ObjAnim_AddFrameA2(objEnemy, ANIM_IDLE, 128, 0+x*128, 256, 128+x*128, 0, 0);
	}
	ascent(x in 0..2){ObjAnim_AddFrameA2(objEnemy, ANIM_MOVE_LEFT, 256, 128*x, 256+128, 128*x+128, 1, 0);}
	
	
	ascent(x in 2..4){ObjAnim_AddFrameA2(objEnemy, ANIM_MOVE_RIGHT, 256, 128*x, 256+128, 128*x+128, 0, 0);}
	
	ObjAnim_AddFrameA2(objEnemy, ANIM_MOVEEND_RIGHT, 256, 128*2, 128*3, 128*2+128, 0, 0);
	ObjAnim_AddFrameA2(objEnemy, ANIM_MOVEEND_RIGHT, 0, 0, 128, 128, 0, 0);
	ObjAnim_AddFrameA2(objEnemy, ANIM_MOVEEND_LEFT, 256, 0, 128*3, 128, 0, 0);
	ObjAnim_AddFrameA2(objEnemy, ANIM_MOVEEND_LEFT, 0, 0, 128, 128, 0, 0);
	
	ascent(x in 0..3){ObjAnim_AddFrameA2(objEnemy, ANIM_ATTACK, 128*3, 128*x, 128*4, 128*x+128, 0, 0);}
	ascent(x in 0..3){ObjAnim_AddFrameA2(objEnemy, ANIM_ATTACKEND, 128*3, 128*2-128*x, 128*4, 128*3-128*x, 0, 0);}
	
	ObjAnim_SetSpeedA1(objEnemy, ANIM_IDLE, 12);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_MOVE_LEFT, 8);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_MOVE_RIGHT, 8);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_ATTACK, 6);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_MOVE_RIGHT, 1);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_MOVE_LEFT, 1);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_ATTACK, 2);
	ObjAnim_StartAnimationA2(objEnemy, 0, 0);
		ObjSprite2D_SetDestCenter(objEnemy);  
	
	
	
LoadTexture(white);
	
	delayedstart;
	SetShotAutoDeleteClip(100, 100, 100, 100);
	
	
}



task delayedstart{
ObjMove_SetDestAtFrame(objEnemy, GetCenterX, 110, 60);
wait(60);

ObjEnemyBossScene_StartSpell(objScene);
ObjEnemyBossScene_SetSpellTimer(objScene, 60*50);

PlaySE(cardcall);
ObjAnim_SetAttackA2(objEnemy, 75);
ObjCutin_SetSpellcardS4("Inflate &quot;Diffuse Density&quot;", sakuyacutin, "REIMU", 135, 206, 250);
ObjCutin_LaunchS3("TENSHI", sakuyacutin, "Normal");
wait(75);
attack1;

}






@MainLoop{
	
	
	SetCommonData("sakuyax", ObjMove_GetX(objEnemy));
	SetCommonData("sakuyay", ObjMove_GetY(objEnemy));
	if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) <=0 && stopstuff==false){stopstuff=true;}

		
if(stopstuff==true && popped==false){
DestroyBoss;
}	
	
	GetEnemyX = ObjMove_GetX(objEnemy);
	GetEnemyY = ObjMove_GetY(objEnemy);
	ObjEnemy_SetIntersectionCircleToShot(objEnemy, GetEnemyX, GetEnemyY, 23);//当たり判定(自機弾用)登録
	type++;
	if(type>=330){
	Invuln(0, shotrate, bombrate);
	if(GetCommonData("timestop", false)==true){
	ObjEnemy_SetIntersectionCircleToShot(objEnemy, GetEnemyX, GetEnemyY, 600);
	ObjEnemy_SetDamageRate(objEnemy, 0, bombrate);
	}
	ObjEnemy_SetIntersectionCircleToPlayer(objEnemy, GetEnemyX, GetEnemyY, 17);//当たり判定(体当たり用)登録
	}
	frame++;
	yield;
	GetSpeedX = ObjMove_GetSpeed(objEnemy)*cos(ObjMove_GetAngle(objEnemy));
}


task CreateShot01v(x, y, v, angle, accel, maxspeed, graphic, delay, row){
		let obj = CreateShotA2(x, y, v, angle, accel, maxspeed, graphic, delay);
		let f = 0;
		let one = round(rand(1, 2));
		if(one == 1){ one = 1; }
		else { one = -1; }
		let moo = 0;
		let waitnum = 0;
		while(!Obj_IsDeleted(obj)){
		if(popped==true){ Obj_Delete(obj); }
			if(GetCommonData("timestop", false)==true){
				if(f==0){
					ObjMove_SetAcceleration(obj, 0);
					ObjMove_SetSpeed(obj, 0);
					ChangeMode(1);
			
					f++;
					
					
				}
			}else{
				
	
			}
			yield;
		}
		
		function ChangeMode(mode){
			waitnum = round(rand(0, 55));
			loop(waitnum){
			if(popped==true){ Obj_Delete(obj); }
			yield;
			}
			if(mode==1){
			if(delay!=1){
				loop(6){
				//CreateStopShotA1(ObjMove_GetX(obj)+20*cos(atan2(GetEnemyY-ObjMove_GetY(obj), GetEnemyX-ObjMove_GetX(obj))+moo), ObjMove_GetY(obj)+20*sin(atan2(GetEnemyY-ObjMove_GetY(obj), GetEnemyX-ObjMove_GetX(obj))+moo), 2.3, atan2(GetEnemyY-ObjMove_GetY(obj), GetEnemyX-ObjMove_GetX(obj))+moo+(row), BLUE31, 60-waitnum);
				CreateStopShotA1(ObjMove_GetX(obj)+20*cos(atan2(GetEnemyY-ObjMove_GetY(obj), GetEnemyX-ObjMove_GetX(obj))+moo), ObjMove_GetY(obj)+20*sin(atan2(GetEnemyY-ObjMove_GetY(obj), GetEnemyX-ObjMove_GetX(obj))+moo), 1.75, atan2(GetEnemyY-ObjMove_GetY(obj), GetEnemyX-ObjMove_GetX(obj))+moo+(row), BLUE31, 60-waitnum);
				CreateStopShotA1(ObjMove_GetX(obj)+20*cos(atan2(GetEnemyY-ObjMove_GetY(obj), GetEnemyX-ObjMove_GetX(obj))+moo), ObjMove_GetY(obj)+20*sin(atan2(GetEnemyY-ObjMove_GetY(obj), GetEnemyX-ObjMove_GetX(obj))+moo), 0.95, atan2(GetEnemyY-ObjMove_GetY(obj), GetEnemyX-ObjMove_GetX(obj))+moo-(row), BLUE31, 60-waitnum);
				moo+=360/6;
				}
			}
			if(delay==1){
				loop(6){
				CreateStopShotA1(ObjMove_GetX(obj)+20*cos(atan2(GetPlayerY-ObjMove_GetY(obj), GetPlayerX-ObjMove_GetX(obj))+moo), ObjMove_GetY(obj)+20*sin(atan2(GetPlayerY-ObjMove_GetY(obj), GetPlayerX-ObjMove_GetX(obj))+moo), 2.5, atan2(GetPlayerY-ObjMove_GetY(obj), GetPlayerX-ObjMove_GetX(obj))+moo, GREEN31, 60-waitnum);
				CreateStopShotA1(ObjMove_GetX(obj)+20*cos(atan2(GetPlayerY-ObjMove_GetY(obj), GetPlayerX-ObjMove_GetX(obj))+moo), ObjMove_GetY(obj)+20*sin(atan2(GetPlayerY-ObjMove_GetY(obj), GetPlayerX-ObjMove_GetX(obj))+moo), 2, atan2(GetPlayerY-ObjMove_GetY(obj), GetPlayerX-ObjMove_GetX(obj))+moo, GREEN31, 60-waitnum);
				CreateStopShotA1(ObjMove_GetX(obj)+20*cos(atan2(GetPlayerY-ObjMove_GetY(obj), GetPlayerX-ObjMove_GetX(obj))+moo), ObjMove_GetY(obj)+20*sin(atan2(GetPlayerY-ObjMove_GetY(obj), GetPlayerX-ObjMove_GetX(obj))+moo), 1.5, atan2(GetPlayerY-ObjMove_GetY(obj), GetPlayerX-ObjMove_GetX(obj))+moo, GREEN31, 60-waitnum);
				CreateStopShotA1(ObjMove_GetX(obj)+20*cos(atan2(GetEnemyY-ObjMove_GetY(obj), GetEnemyX-ObjMove_GetX(obj))+moo), ObjMove_GetY(obj)+20*sin(atan2(GetEnemyY-ObjMove_GetY(obj), GetEnemyX-ObjMove_GetX(obj))+moo), 0.85, atan2(GetEnemyY-ObjMove_GetY(obj), GetEnemyX-ObjMove_GetX(obj))+moo+rand(-20, 20)+(row), BLUE31, 60-waitnum);
				moo+=360/6;
				}
			}
			
			Obj_Delete(obj);
			}
		

		}
	}


	task CreateStopShotA1(x, y, v, angle, graphic, delay){
	let obj = CreateShotA1(x, y, v, angle, graphic, delay);
	let velocity = v;
	let time = 0;
	while(!Obj_IsDeleted(obj)){
	if(popped==true){ Obj_Delete(obj); }
	velocity = v;
	if(GetCommonData("timestop", false)==true){
	if(time<=120){
	ObjShot_SetDelay(obj, 30);
	}
	velocity = 0;
	}
	ObjMove_SetSpeed(obj, velocity);
	time++;
	yield;
	}
	}
	
	
task attack1{
let increment = 0;
let x = 0;
let y = 0;
let angle = 0;
let angle2 = 0;
let type = 1;
let color = 0;
ChangeToAttack1;
wait(120);
one = 0;
loop{
TCharge01(objEnemy, 3, 45, 255, 255, 255);
PlaySE(charge2);
wait(60);
PlaySE(shot);
angle = rand(0, 360);

loop(10){
	CreateShot01v(GetEnemyX, GetEnemyY, 2, angle, 0, 5, [BLUE03, GREEN03][one^2], 0+one, 15);
	CreateShot01v(GetEnemyX, GetEnemyY, 0.9, angle+36, 0, 5, [BLUE03, GREEN03][one^2], 0+one, 15);
	CreateShot01v(GetEnemyX, GetEnemyY, 1.4,angle+18, 0, 5, [BLUE03, GREEN03][one^2], 0+one, 15);
	angle+=360/10;
	}
	if(one==0){
	one=1;
	}
	else if(one==1){
	one=0;
	}
wait(20);
if(popped==true){return;}
PlaySE(charge2);
TCharge01(objEnemy, 3, 45, 200, 200, 255);
wait(60);
if(popped==true){return;}
ObjAnim_SetAttackA2(objEnemy, 60);
SetCommonData("timestop", true);
PlaySE(timestop);
RootPlayer(60, GetPlayerX, GetPlayerY);
wait(60);
if(popped==true){return;}
SetCommonData("timestop", false);
wait(120);
ExMoveHigher;
wait(90);

}
}


task ChangeToAttack1{
ascent(x in 0..3){ObjAnim_ReplaceFrameA2(objEnemy, ANIM_ATTACK, x, 128*4, 128*x, 128*5, 128*x+128, 0, 0);}
	ascent(x in 0..3){ObjAnim_ReplaceFrameA2(objEnemy, ANIM_ATTACKEND, x, 128*4, 128*2-128*x, 128*5, 128*3-128*x, 0, 0);}
}

task RootPlayer(plux, playx, playy){
	let timevar = 0;
	let timestring;
	let objText = ObjText_Create();
	ObjText_SetFontType(objText, "Corbel");
	ObjText_SetText(objText, "Timestuck!");
	ObjText_SetFontSize(objText, 18);
	ObjText_SetFontBold(objText, true);
	ObjText_SetHorizontalAlignment(objText, ALIGNMENT_CENTER);
	ObjText_SetMaxWidth(objText, 85);
	ObjText_SetFontColorTop(objText, 224, 255, 255);
	ObjText_SetFontColorBottom(objText, 128, 128, 255);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText,0, 0, 0);
	ObjText_SetFontBorderWidth(objText, 1);

	Obj_SetRenderPriority(objText, 0.9);
	ObjRender_SetX(objText, GetPlayerX-10);
	ObjRender_SetY(objText, GetPlayerY-45);
	ObjRender_SetColor(GetPlayerObjectID, 255, 255, 255);
	
	
	loop(plux){
	timestring = rtos("0.00", (plux-timevar)/60) ~"s";
		ObjText_SetText(objText, "Timestuck!" ~ timestring);
	ObjRender_SetColor(GetPlayerObjectID, 108, 108, 108);
	ObjMove_SetPosition(GetPlayerObjectID, playx, playy);
	timevar++;
	wait(1);
	if(GetPlayerState!=STATE_NORMAL){ 	ObjRender_SetColor(GetPlayerObjectID, 108, 108, 108); break; }
	if(popped==true){ ObjRender_SetColor(GetPlayerObjectID, 255, 255, 255); Obj_Delete(objText); break; }
	}
	Obj_Delete(objText);
	ObjRender_SetColor(GetPlayerObjectID, 255, 255, 255);
	PlaySE(ping);
	}
	



	
	
task DestroyBoss{
SetCommonData("timestop", false);
SetCommonData("petals", false);
popped=true;
StopSlow(TARGET_ALL);
loop(10){ yield; }
Obj_Delete(objEnemy);
loop(45){ yield; }
		CloseScript(GetOwnScriptID());
		return;
}
