//#TouhouDanmakufu
#ScriptVersion[3]
#Title["SakuyaS3_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 randx = 0;
let randy = 0;
let wp = 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 = 25;
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(90, 90, 90, 20);
	
	
}



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("Conjuring &quot;Killing Doll's Accomplices&quot;", sakuyacutin, "REIMU", 135, 206, 250);
ObjCutin_LaunchS3("TENSHI", sakuyacutin, "Normal");
wait(75);
Spell;
ChangeToAttack1;

}




@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 Spell{
let spd = 0;
let slowed = false;
wait(60);
loop{

wp+=45;
TCharge01(objEnemy, 3, 45, 255, 255, 255);
PlaySE(charge2);
wait(70);
if(popped==true){ return; }
ObjAnim_SetAttackA2(objEnemy, 180);
spd = 0;
ascent(i in 0..50){
 loop(3){
	CreateKiller(GetEnemyX, GetEnemyY, 2.85+spd, angle, BLUE32, 10, i);
	angle+=360/3;
	}
angle+=7;
spd+=0.02082333333;
wait(3);
if(popped==true){ return; }
}
wait(45);
if(popped==true){ return; }
if(wp==90){
wp=-45;
}
wait(90);
if(popped==true){ return; }
TCharge01(objEnemy, 3, 45, 255, 255, 255);
PlaySE(charge2);
wait(45);
if(popped==true){ return; }
ObjAnim_SetAttackA2(objEnemy, 45);
wait(15);
if(popped==true){ return; }
StartSlow(TARGET_ALL, 12);
slowed = true;
//MotionBlur(ALL, 45, 40);
wait(15);
if(popped==true){ return; }
PlaySE(ping);
ascent(i in 0..15){
	
	ascent(c in -20..50){
	
	randx = rand(-4, 4);
	randy = rand(-2, 2);
		if(GetDistance(GetPlayerX, GetPlayerY, (GetClipMinX-50+(randx*2))+(c*42)+randx, GetClipMinY-60+(i*45)+(randy*2))>=30 && GetClipMinY-60+(i*45)+(randy*2)<=GetStgFrameHeight){ 
		let obj = CreateShotA1((GetClipMinX-50+(randx*2))+(c*42)+randx, GetClipMinY-60+(i*45)+(randy*2), 0, 0, [BLUE31, PURPLE32, WHITE31][color%3], 5);
		ObjMove_AddPatternA3(obj, 0, 0, wp+90+(randy*4), 0.001, 0, 1.1, [BLUE31, PURPLE31, WHITE31][color%3]);
		ObjMove_AddPatternA3(obj, 120, NO_CHANGE, NO_CHANGE, 0.003, 0, 1.75+rand(randy/20, -randy/20), NO_CHANGE);
		let obj2 = CreateShotA1((GetClipMinX-50+(randx*2))+(c*42)+randx, GetClipMinY-60+(i*45)+(randy*2), 0, 0, [BLUE31, PURPLE32, WHITE31][color%2], 5);
		ObjMove_AddPatternA3(obj2, 0, 0, wp+-90+(randy*4), 0.001, 0, 1.1, [BLUE31, PURPLE31, WHITE31][color%3]);
		ObjMove_AddPatternA3(obj2, 120, NO_CHANGE, NO_CHANGE, 0.003, 0, 1.75+rand(randy/20, -randy/20), NO_CHANGE);
		}
		color++;
	}
}
//DeleteEnemyShotImmediatelyInCircle(SHOT, GetPlayerX, GetPlayerY, 25);
wait(5);
if(popped==true){ return; }
StartSlow(TARGET_ALL, 60/4);
wait(5);
if(popped==true){ return; }
StartSlow(TARGET_ALL, 60/3);
wait(5);
if(popped==true){ return; }
StartSlow(TARGET_ALL, 60/2);
wait(5);
if(popped==true){ return; }
color++;
StopSlow(TARGET_ALL);
slowed = false;
wait(120);
if(popped==true){ return; }
}
	
}


task CreateKiller(x, y, v, angle, graphic, delay, i){
	let speed = v;
	let spinning=1;
	let obj = CreateShotA1(x, y, v, angle, graphic, delay);
	ObjShot_SetSpellResist(obj, true);
	
	while(!Obj_IsDeleted(obj)){
	if(popped==true){ Obj_Delete(obj); }
	if(spinning==1){
	speed-=0.06;
	}
	if(spinning>=2){
	speed+=0.0975;
	}
	if(speed<=0){
	speed = 0;
	}
	if(spinning!=2){
	ObjMove_SetSpeed(obj, speed);
	}
	
	
	if(ObjMove_GetSpeed(obj)==0 && spinning==1){
	spinning=2;
		loop(60){
			ObjMove_SetAngle(obj, ObjMove_GetAngle(obj)+360/70);
			if(popped==true){ Obj_Delete(obj); }
			yield;
			}
			spinning=2;
			loop(30){
			speed+=0.0975;
			ObjMove_SetAngle(obj, ObjMove_GetAngle(obj)+360/70);
			if(popped==true){ Obj_Delete(obj); }
			yield;
			}
			let setangle = GetAngleToPlayer(obj);
			spinning = 3;
			ObjMove_SetAngle(obj, setangle);
			if(i%5==0){
			CreateLaser1(ObjMove_GetX(obj), ObjMove_GetY(obj), setangle, BLUE60, 450, 100, 2, 1);
			}
			
			}
	
	yield;
	}
	

}
task CreateLaser1(x, y, angle, graphic, length, width, delay, type){
let obj2 = ObjShot_Create(OBJ_STRAIGHT_LASER);
let angle2 = rand(0, 360);
let bulletgraphic = 0;
let one = 1;
let index = 0;
let waitincrement = 0;
ObjMove_SetPosition(obj2, x, y);
ObjStLaser_SetAngle(obj2, angle);
ObjShot_SetGraphic(obj2, graphic);
ObjShot_SetDelay(obj2, delay*20);
ObjLaser_SetLength(obj2, length);
ObjLaser_SetRenderWidth(obj2, width);
ObjRender_SetAlpha(obj2, 200);
ObjShot_SetSpellResist(obj2, true);
ObjStLaser_SetSource(obj2, false);
ObjShot_Regist(obj2);
wait2(delay-5);
if(type==0){ wait(5); }
if(type==1){ObjShot_FadeDelete(obj2); }
if(type==0){
PlaySE(ping);
}
ObjLaser_SetRenderWidth(obj2, width/2);
ObjLaser_SetIntersectionWidth(obj2, width/2-3);
wait2(delay*2);
ObjShot_FadeDelete(obj2);

function wait2(w){ loop(w){ if(popped==true){ Obj_Delete(obj2); return; } 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;
}
