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

#include"./../lib/SHOT_REPLACE/shot_replace.dnh"
#include"./../includes/BossIncludes.txt"
let color2 = 1;
let color = 0;
let mode = 0;
let spawneddolls = 0;
let objEnemy;
let duration = 0;
let dollsarray = [];
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 = 65;
let bombrate = 25;
let reimucut = GetCurrentScriptDirectory~"sprites/lettycut.png";

@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));	
	}
	case(EV_REQUEST_IS_DURABLE_SPELL){
	SetScriptResult(true);
	}
}

@Initialize{
	//Resource Loading
	shotinit;
	
	
	
	
	
	
	
	LoadTexture(reimucut);
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	Invuln(600, shotrate, bombrate);
	//BottomEnemyMarker(objEnemy, objEnemy);
	let Reimu = GetCurrentScriptDirectory~"sprites/letty.png";
	ObjPrim_SetTexture(objEnemy, Reimu); 
	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, 8);
	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(65, 85, 65, 35);
	
	
}



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

ObjEnemyBossScene_StartSpell(objScene);
ObjEnemyBossScene_SetSpellTimer(objScene, 30+(60*60));

PlaySoundEffect("cardcall", 100, GetCenterX);
ObjAnim_SetAttackA2(objEnemy, 75);
ObjCutin_SetSpellcardS4("Alcolve &quot;Rotating Recess&quot;", reimucut, "NAZRIN", 135, 206, 250);
ObjCutin_LaunchS3("NAZRIN", reimucut, "Hard");
NotifyEventAll(EV_SPELLBG, 5);
Aura2(objEnemy, GetCurrentScriptDirectory~"sprites/letty.png", 255, 255, 255);
wait(30);
ascent(i in 0..75){
ObjRender_SetAlpha(objEnemy, 255-i*(255/75));
yield;
}
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);
	if(type<270){
	ObjEnemy_SetIntersectionCircleToShot(objEnemy, GetEnemyX, GetEnemyY, 25);//当たり判定(自機弾用)登録
	}
	type++;
	ObjEnemy_SetIntersectionCircleToPlayer(objEnemy, GetEnemyX, GetEnemyY, 27);//当たり判定(体当たり用)登録
	
	frame++;
	yield;
	GetSpeedX = ObjMove_GetSpeed(objEnemy)*cos(ObjMove_GetAngle(objEnemy));
}



task attack1{	
let one = -1;
wait(90);
ChangeToAttack1;
let angle2 = 0;
let color = 0;
let angle3 = 0;
PlaySoundEffect("laser", 85, GetCenterX);
ascent(i in -6..7){
CreateLaser((i*24)-90, i);
}
wait(60);
PlaySoundEffect("ping", 85, GetCenterX);
wait(75);
PlaySoundEffect("charge2", 85, -1);
TCharge01(objEnemy, 3, 45, 155, 155, 255);
wait(30);
ObjAnim_SetAttackA1(objEnemy, true);
wait(15);
if(popped==true){ return; }
wait(60*3);
mode = 1;
wait(60*12);
mode = -90;
PlaySoundEffect("charge2", 85, -1);
TCharge01(objEnemy, 3, 45, 155, 155, 255);
wait(30);
wait(60);
mode = 2;
wait(60*13);
mode = -90;
PlaySoundEffect("charge2", 85, -1);
TCharge01(objEnemy, 3, 45, 155, 155, 255);
wait(30);
wait(60);
mode = 3;
wait(60*10);
PlaySoundEffect("charge2", 85, -1);
TCharge01(objEnemy, 3, 45, 155, 155, 255);
wait(30);
PlaySoundEffect("shot3", 90, GetEnemyX,);
loop(80){
let objbleh = CreateDNHShotA1(GetEnemyX+320*cos(angle), GetEnemyY+320*sin(angle), -0.25, angle, WHITE54, 10);
ObjShot_SetSpellResist(objbleh, true);
ObjShot_SetAutoDelete(objbleh, false);
ManageOvals(objbleh);
angle+=360/80;
}
if(popped==true){ return; }
}

task ManageOvals(objbleh){
while(!Obj_IsDeleted(objbleh)){
if(popped==true){ ObjShot_FadeDelete(objbleh); return; }
yield;
}
}

task CreateLaser(initangle, num){
let anglee = initangle;
let vari = 0;
vari = num%2;
let anglez = 0;
let angleacc = 0;
let anglemax = 0.3;
let time = 0;
let obj1 = ObjShot_Create(OBJ_STRAIGHT_LASER);
ObjMove_SetPosition(obj1, GetEnemyX+45*cos(anglee), GetEnemyY+45*sin(anglee));
ObjShot_SetGraphic(obj1, WHITE51);
ObjShot_SetDelay(obj1, 60);
ObjStLaser_SetAngle(obj1, anglee);
ObjShot_SetSpellResist(obj1, true);
ObjLaser_SetLength(obj1, 300);
ObjLaser_SetRenderWidth(obj1, 30);
ObjLaser_SetIntersectionWidth(obj1, 17);
ObjShot_Regist(obj1);
ObjLaser_SetInvalidLength(obj1, 0, 0);

while(!Obj_IsDeleted(obj1)){
ObjMove_SetPosition(obj1, GetEnemyX+45*cos(anglee), GetEnemyY+45*sin(anglee));
ObjStLaser_SetAngle(obj1, anglee);

let point = GetLineBorderPoint(ObjMove_GetX(obj1), ObjMove_GetY(obj1), ObjMove_GetX(obj1)+10*cos(anglee), ObjMove_GetY(obj1)+10*sin(anglee));
anglez = AngleBetweenPoints(point[0], point[1], GetEnemyX, GetEnemyY);
	if(mode==3){
if((time+(vari*40))%80==0 && time>180){
PlaySoundEffect("ping", 75, GetCenterX);
loop(6){
CreateDNHShotA1(point[0], point[1], 0.95, anglez, WHITE04, 10);
anglez+=360/6;
}
}
	}
	if(mode==1){
	if(time%90==0){
	PlaySoundEffect("ping", 75, GetCenterX);
loop(5){
CreateDNHShotA1(point[0], point[1], 0.95, anglez, WHITE04, 10);
anglez+=360/5;
}
}
}
if(mode==2){

if((time+(vari*50))%100==0 && time>180){
PlaySoundEffect("ping", 75, GetCenterX);
loop(6){
CreateDNHShotA1(point[0], point[1], 0.95, anglez, WHITE04, 10);
anglez+=360/6;
}
}
}
	
	if(mode==3){
anglez+=360/6/3;
}
time++;
if(time>180){
if(mode==0){
anglemax = 0.25;
}
if(mode==1){
anglemax = 0.3;
}
if(mode==2){
anglemax = 0.325;
}
if(mode==3){
anglemax = 0.375;
}
if(angleacc<anglemax){
angleacc+=0.0025;
}
anglee+=angleacc;


}

yield;
if(popped==true){ Obj_Delete(obj1); }
}
}



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 DestroyBoss{
//NotifyEventAll(EV_SPELLBGEND, 1);
SetCommonData("timestop", false);
SetCommonData("petals", false);
popped=true;
StopSlow(TARGET_ALL);
loop(10){ yield; }
Obj_Delete(objEnemy);
loop(45){ yield; }
		CloseScript(GetOwnScriptID());
		return;
}
