//#TouhouDanmakufu
#ScriptVersion[3]
#Title["SakuyaS4_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 yy = 0;
let type = 0;
let animframe = 0;
let frame = 0;
let GetSpeedX = 0;
let variable = 0;
let thunder = GetCurrentScriptDirectory~"sfx/thunder.wav";
let n = 0;
let shotrate = 15;
let bombrate = 0;

@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(boom);
	LoadSound(laser);
	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(150);

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

PlaySE(cardcall);
ObjAnim_SetAttackA2(objEnemy, 75);
ObjCutin_SetSpellcardS4("No Tricks &quot;Death Lotus&quot;", sakuyacutin, "REIMU", 135, 206, 250);
ObjCutin_LaunchS3("TENSHI", sakuyacutin, "Normal");
wait(75);
attack1;
attack2;


}

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

}


@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);//?????(????)?
	if(frame>=420){
	Invuln(0, shotrate, bombrate);
	ObjRender_SetAlpha(objEnemy, 255);
	if(GetPlayerInvincibilityFrame>0 && popped==false){
	ObjEnemy_SetDamageRate(objEnemy, 2.5, 0);
	ObjRender_SetAlpha(objEnemy, 75);
	}
	}
	ObjEnemy_SetIntersectionCircleToPlayer(objEnemy, GetEnemyX, GetEnemyY, 17);//?????(?????)??
	
	frame++;
	yield;
	GetSpeedX = ObjMove_GetSpeed(objEnemy)*cos(ObjMove_GetAngle(objEnemy));
}

task attack1{
let spd = 0;
let speedvar = 0;
let slowed = false;
wait(60);
/*
TCharge01(objEnemy, 3, 45, 255, 255, 255);
PlaySE(charge2);
*/
wait(45);
ChangeToAttack1;
ObjAnim_SetAttackA1(objEnemy, true);
loop{
if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE)<=300){
speedvar = min((100/ObjEnemy_GetInfo(objEnemy, INFO_LIFE)), 0.75);
}
loop(4+yy){
	CreateDNHShotA1(GetEnemyX+40*cos(angle+angle2), GetEnemyY+40*sin(angle+angle2), 3.25+speedvar, angle+angle2, PURPLE32, 10);
	CreateDNHShotA1(GetEnemyX+40*cos(-angle+angle2), GetEnemyY+40*sin(-angle+angle2), 3.25+speedvar, -angle+angle2, PURPLE32, 10);
	CreateDNHShotA1(GetEnemyX+40*cos(angle+angle2), GetEnemyY+40*sin(angle+angle2), 3.25+speedvar, angle+angle2, PURPLE32, 20);
	CreateDNHShotA1(GetEnemyX+40*cos(-angle+angle2), GetEnemyY+40*sin(-angle+angle2), 3.25+speedvar, -angle+angle2, PURPLE32, 20);
	angle+=360/(4+yy);
	}
	angle+=9-(yy*1.5);
	if(angle%(360/(6))==0){
	angle2+=0.75;
	}
if(popped==true){ return; }
wait(6);
}
	
}

task attack2{
while(ObjEnemy_GetInfo(objEnemy, INFO_LIFE)>900 && ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID, INFO_TIMER)>90){ yield; }
TCharge01(objEnemy, 3, 45, 255, 255, 0);
PlaySE(charge2);
wait(60);
CreateFamiliar1(0);
CreateFamiliar1(1);
attack3;
}
task attack3{
while(ObjEnemy_GetInfo(objEnemy, INFO_LIFE)>600 && ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID, INFO_TIMER)>60){ yield; }
TCharge01(objEnemy, 3, 45, 0, 0, 255);
PlaySE(charge2);
wait(60);
attack4;
CreateFamiliar2;
}
task attack4{
while(ObjEnemy_GetInfo(objEnemy, INFO_LIFE)>300 && ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID, INFO_TIMER)>30){ yield; }
shotrate = 12.5;
TCharge01(objEnemy, 3, 45, 255, 0, 255);
PlaySE(charge2);
wait(60);
yy = 2;
angle = 0;
angle2 = 0;
}

task CreateFamiliar2{

let obj = ObjEnemy_Create(OBJ_ENEMY);
let rotateacc = 0;
let alpha = 255;
let familiar = GetCurrentScriptDirectory~"sprites/familiar.png";
let angle = 0;
let angle2 = 0;
ObjEnemy_SetLife(obj, 1000);
ObjMove_SetPosition(obj, GetEnemyX, GetEnemyY);
ObjPrim_SetTexture(obj, familiar);
ObjSprite2D_SetSourceRect(obj, 0, 0, 64, 64);
ObjSprite2D_SetDestCenter(obj);
ObjRender_SetScaleXYZ(obj, 0.75, 0.75, 1);
ObjEnemy_Regist(obj);
ObjMove_SetDestAtWeight(obj, GetCenterX, 20, 13, 2);


Fire;

PlaySE(shadow);
while(!Obj_IsDeleted(obj)){
ObjRender_SetAngleZ(obj, 0+rotateacc);
rotateacc+=7;
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);
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 Fire{

let one = -1;

let angle2 = 0;
let color = 0;
let angle3 = 0;
wait(90);
if(popped==true){return;}

loop{
angle = GetAngleToPlayer(obj);
PlaySE(laser);
CreateLaser1(ObjMove_GetX(obj), ObjMove_GetY(obj), angle+11, BLUE60, 500, 100, 30, 1);
CreateLaser1(ObjMove_GetX(obj), ObjMove_GetY(obj), angle-11, BLUE60, 500, 100, 30, 1);
wait(30);
loop(40){
if(popped==true){ Obj_Delete(obj); return; }
PlaySE(shot2);
CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 5, angle+11, BLUE33, 10);
CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 5, angle-11, BLUE33, 10);
wait(3);
}
loop(90-(yy*15)){
		
if(popped==true){
loop(15){
ObjRender_SetAlpha(obj, alpha);
alpha-=255/15;
yield;
}
Obj_Delete(obj);
return;
}
yield; 
}
yield;
}
}
}

task CreateFamiliar1(type){

let obj = ObjEnemy_Create(OBJ_ENEMY);
let rotateacc = 0;
let alpha = 255;
let familiar = GetCurrentScriptDirectory~"sprites/familiar.png";
let angle = 0;
let angle2 = 0;
ObjEnemy_SetLife(obj, 1000);
ObjMove_SetPosition(obj, GetEnemyX, GetEnemyY);
ObjPrim_SetTexture(obj, familiar);
ObjSprite2D_SetSourceRect(obj, 0, 0, 64, 64);
ObjSprite2D_SetDestCenter(obj);
ObjRender_SetScaleXYZ(obj, 0.75, 0.75, 1);
ObjEnemy_Regist(obj);
if(type==0){
ObjMove_SetDestAtWeight(obj, GetEnemyX+60, GetEnemyY, 13, 2);
angle = 180;
}
else{
ObjMove_SetDestAtWeight(obj, GetEnemyX-60, GetEnemyY, 13, 2);
angle = 360;
}
Fire;
Orbit;
PlaySE(shadow);
while(!Obj_IsDeleted(obj)){
ObjRender_SetAngleZ(obj, 0+rotateacc);
rotateacc+=7;
yield;
}

task Orbit{
wait(60);
loop{
ObjMove_SetPosition(obj, GetEnemyX+60*cos(angle), GetEnemyY+60*sin(angle));
angle++;
yield;
if(popped==true){ return; }
}
}

task Fire{

let one = -1;

let angle2 = 0;
let color = 0;

let angle3 = 0;
wait(90);
if(popped==true){return;}

loop{
PlaySE(shot);

CreateDNHShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 3.5, GetAngleToPlayer(obj), YELLOW31, 0);
	CreateDNHShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 3.35, GetAngleToPlayer(obj)+0.5, YELLOW31, 0);
	CreateDNHShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 3.35, GetAngleToPlayer(obj)-0.5, YELLOW31, 0);
	CreateDNHShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 3.2, GetAngleToPlayer(obj), YELLOW31, 0);
	CreateDNHShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 3.2, GetAngleToPlayer(obj)-1.0, YELLOW31, 0);
	CreateDNHShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 3.2, GetAngleToPlayer(obj)+1.0, YELLOW31, 0);
loop(75-(yy*5)){
		
if(popped==true){
loop(15){
ObjRender_SetAlpha(obj, alpha);
alpha-=255/15;
yield;
}
Obj_Delete(obj);
return;
}
yield; 
}
yield;
}
}




}





	
	
task DestroyBoss{
SetPlayerInvincibilityFrame(15*5 + 60);
SetCommonData("pausebeforebonus", true);
SetCommonData("petals", false);
popped=true;
StopSlow(TARGET_ALL);
PlaySE(boom);
StartSlow(TARGET_ALL, 30);
//ObjMove_SetDestAtWeight(objEnemy, ObjMove_GetX(objEnemy)+120, ObjMove_GetY(objEnemy)+rand(-20, 20), 20, 1);
ScreenShake(15*5, 5);
loop(15){ TExpan(ObjMove_GetX(objEnemy)+rand(-35, 35), ObjMove_GetY(objEnemy)+rand(-25, 25), 8, 25, 224, 255, 255, 100); wait(5); }
PlaySE(boom);
ScreenShake(40, 15);
TExpan(ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), 55, 120, 224, 255, 255, 255);
Obj_Delete(objEnemy);
loop(60){ yield; }
		CloseScript(GetOwnScriptID());
		StopSlow(TARGET_ALL);
		return;
}
