//#TouhouDanmakufu
#ScriptVersion[3]
#Title["SakuyaS1_H"]
#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 = 30;
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(30, 100, 30, 30);
	
	
}



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

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

PlaySE(cardcall);
ObjAnim_SetAttackA2(objEnemy, 75);
ObjCutin_SetSpellcardS4("Unwind &quot;Still Life&quot;", sakuyacutin, "REIMU", 135, 206, 250);
ObjCutin_LaunchS3("TENSHI", sakuyacutin, "Hard");
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 attack1{
wait(120);
let increment = 0;
let x = 0;
let y = 0;
let angle = 0;
let angle2 = 0;
let type = 1;
let color = 0;
ChangeToAttack1;
angle = GetAngleToPlayer(objEnemy)+180;
PlaySE(shot);
ascent(i in -11..12){ ExpandKnife(GetEnemyX, GetEnemyY, 3, angle+(i*12), -3/60, 0, RED32, 10); }
wait(30);
PlaySE(charge2);
TCharge01(objEnemy, 3, 45, 142, 130, 130);
wait(60);
ObjAnim_SetAttackA2(objEnemy, 60);

SetCommonData("timestop", true);
PlaySE(timestop);
RootPlayer(120, GetPlayerX, GetPlayerY);
wait(120);
SetCommonData("timestop", false);
wait(60);
loop{
if(popped==true){return;}

loop(6){
PlaySE(shot);
loop(9){
			ExpandKnife(GetEnemyX, GetEnemyY, 3, angle, -3/60, 0, RED32, 10);
			angle+=360/9;
				
				}
				angle+=360/9/10;
				wait(6);
				if(popped==true){return;}
				}



wait(15);
if(popped==true){return;}
PlaySE(shot);
ascent(i in 0..3){
loop(26){
	CreateDNHShotA1(GetEnemyX, GetEnemyY, 1-(i/3.3333333), angle, [RED03, SP01][color%2], 20);
	color++;
	angle+=360/26;
		}
		angle+=360/26/2;
		}
wait(15);
if(popped==true){return;}
PlaySE(charge2);
TCharge01(objEnemy, 3, 45, 142, 130, 130);
wait(60);
if(popped==true){return;}
ObjAnim_SetAttackA2(objEnemy, 60);
SetCommonData("timestop", true);
PlaySE(timestop);
RootPlayer(120, GetPlayerX, GetPlayerY);
wait(120);
if(popped==true){return;}
SetCommonData("timestop", false);
wait(60);
if(popped==true){return;}

x = GetPlayerX;
y = GetPlayerY;
loop(4){
angle = GetAngleToPlayer(objEnemy);
PlaySE(shot);
	ascent(i in -6..7){
			ExpandKnife(GetEnemyX, GetEnemyY, 3, angle+(i*2), -3/60, 0, RED32, 10);
			angle+=360/2;
				}
				
				loop(16){
				CreateDNHShotA1(GetEnemyX+20*cos(angle), GetEnemyY+20*sin(angle), 2.25, angle, BLUE31, 15);
				CreateDNHShotA1(GetEnemyX+20*cos(angle), GetEnemyY+20*sin(angle), 1.55, angle+360/32, BLUE31, 15);
				CreateDNHShotA1(GetEnemyX+20*cos(angle), GetEnemyY+20*sin(angle), 1.55-0.7, angle, BLUE31, 15);
				angle+=360/16;
					}

				wait(45);
				if(popped==true){return;}
				}



wait(15);
if(popped==true){return;}
PlaySE(shot);
ascent(i in 0..3){
loop(26){
	CreateDNHShotA1(GetEnemyX, GetEnemyY, 1-(i/3.3333333), angle, [RED03, SP01][color%2], 20);
	color++;
	angle+=360/26;
		}
		angle+=360/26/2;
		}
wait(15);
if(popped==true){return;}
PlaySE(charge2);
TCharge01(objEnemy, 3, 45, 142, 130, 130);
wait(60);
if(popped==true){return;}
ObjAnim_SetAttackA2(objEnemy, 60);
SetCommonData("timestop", true);
PlaySE(timestop);
RootPlayer(120, GetPlayerX, GetPlayerY);
wait(120);
if(popped==true){return;}
SetCommonData("timestop", false);
wait(60);
if(popped==true){return;}

loop(20){
PlaySE(shot);
ExpandKnife(GetEnemyX, GetEnemyY, 3, angle, -3/60, 0, RED32, 10);
angle+=360/20;
wait(3);
if(popped==true){return;}
}
wait(3);
angle+=360/20/2;
loop(20){
PlaySE(shot);
ascent(i in 0..10){
CreateDNHShotA1(GetEnemyX, GetEnemyY, 0.4+(i/5.5), angle, GREEN32, 10);
}
angle-=360/20;
wait(3);
if(popped==true){return;}
}


wait(15);
if(popped==true){return;}
PlaySE(shot);
loop(13){
	CreateDNHShotA1(GetEnemyX, GetEnemyY, 1.1, angle+360/13/2, RED03, 20);
	CreateDNHShotA1(GetEnemyX, GetEnemyY, 0.8, angle, RED03, 20);
	CreateDNHShotA1(GetEnemyX, GetEnemyY, 0.5, angle+360/13/2, RED03, 20);
		angle+=360/13;
		}
wait(15);
if(popped==true){return;}
PlaySE(charge2);
wait(60);
if(popped==true){return;}
ObjAnim_SetAttackA2(objEnemy, 60);
SetCommonData("timestop", true);
PlaySE(timestop);
RootPlayer(120, GetPlayerX, GetPlayerY);
wait(120);
if(popped==true){return;}
SetCommonData("timestop", false);
wait(60);
if(popped==true){return;}

}
}

task ExpandKnife(x, y, v, angle, acc, max, graphic, delay){
let obj = CreateShotA2(x, y, v, angle, acc, max, graphic, delay);
while(!Obj_IsDeleted(obj)){
if(GetCommonData("timestop", false)==true){ 
ascent(i in -5..40){ 
if(Obj_IsDeleted(obj)){ return; }
let obj2 = CreateShotA1(ObjMove_GetX(obj)+(i*9)*cos(angle), ObjMove_GetY(obj)+(i*9)*sin(angle), 0, angle, WHITE32, 0); 
ObjMove_AddPatternA1(obj2, 120-i, 4, angle);

yield;
}
Obj_Delete(obj); }
yield;
}
Obj_Delete(obj);
}


yield;



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;
}
