//#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 objEnemy;
let duration = 0;
let stopstuff = false;
let angle = 0;
let invulnvar = 0;
let angle2 = 0;
let one = 1;
let phase = 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 = 13.5;
let bombrate = 0;
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));	
	}
}

@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, 13);
	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(10, 10, 10, 10);
	
	
}



task delayedstart{
ObjMove_SetDestAtFrame(objEnemy, GetCenterX, 120, 120);
wait(120);
let zoomvar = 0;
let dify = GetCenterY-GetEnemyY;
let camerax = GetCenterX;
let cameray = GetCenterY;
let zoom = 1;
PlaySoundEffect("charge2", 85, -1);
TCharge01(objEnemy, 3, 45, 155, 155, 255);
wait(60);
loop(60){
cameray-=dify/60;
Set2DCameraFocusY(cameray);
Set2DCameraRatio(zoom);
zoom+=0.75/60;
yield;
}
ObjAnim_SetAttackA2(objEnemy, 75+25);
wait(30);
ObjEnemyBossScene_StartSpell(objScene);
ObjEnemyBossScene_SetSpellTimer(objScene, 60*110);
PlaySoundEffect("lastspell", 60, GetCenterX);
PlaySoundEffect("cardcall", 100, GetCenterX);
Aura(objEnemy, GetCurrentScriptDirectory~"sprites/letty.png", 30, 144, 255);
ObjCutin_SetSpellcardS4("&quot;Crystallized Silver&quot;", reimucut, "AYA", 135, 206, 250);
ObjCutin_LaunchS3("AYA", reimucut, "Hard");
//NotifyEventAll(EV_SPELLBG, 5);
wait(105);
loop(60){
cameray+=dify/60;
Set2DCameraFocusY(cameray);
Set2DCameraRatio(zoom);
zoom-=0.75/60;
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(invulnvar<=0){
	ObjEnemy_SetIntersectionCircleToShot(objEnemy, GetEnemyX, GetEnemyY, 25);//当たり判定(自機弾用)登録
	type++;
	ObjEnemy_SetIntersectionCircleToPlayer(objEnemy, GetEnemyX, GetEnemyY, 17);//当たり判定(体当たり用)登録
	}
	
	frame++;
	yield;
	GetSpeedX = ObjMove_GetSpeed(objEnemy)*cos(ObjMove_GetAngle(objEnemy));
}



task attack1{	
let one = -1;
//wait(90);
ChangeToAttack1;
ObjAnim_SetAttackA1(objEnemy, true);
nodamage;
let angle2 = 0;
let shotarray = [];
let angle3 = 0;
let radius = 40;
if(popped==true){return;}
//PlaySoundEffect("charge2", 85, -1);
//TCharge01(objEnemy, 3, 45, 155, 155, 255);
loop(2){
		let obj = CreateCurveLaserA1(GetEnemyX, GetEnemyY, 1, angle, 50, 10, WHITE50, 0);
		ManipulateLaser2(obj, angle, one, GetEnemyX, GetEnemyY);
		angle+=360/2;
		}
if(popped==true){ return; }
while(phase!=1){
if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE)<825){
phase = 1;
}
yield;
}
Invuln(90, shotrate, bombrate);
PlaySoundEffect("charge2", 85, -1);
TCharge01(objEnemy, 3, 45, 155, 155, 255);
wait(30);
loop(2){
		let obj = CreateCurveLaserA1(GetEnemyX, GetEnemyY, 1, angle, 50, 10, WHITE50, 0);
		ManipulateLaser(obj, angle, one, GetEnemyX, GetEnemyY);
		angle+=360/2;
		}
while(phase!=2){
if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE)<600){
phase = 2;
}
yield;
}
Invuln(90, shotrate, bombrate);
PlaySoundEffect("charge2", 85, -1);
TCharge01(objEnemy, 3, 45, 155, 155, 255);
wait(30);
SideShots;
while(phase!=3){
if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE)<350){
phase = 3;
}
yield;
}
Invuln(90, shotrate, bombrate);
PlaySoundEffect("charge2", 85, -1);
TCharge01(objEnemy, 3, 45, 155, 155, 255);
wait(30);
phase = 4;
yield;
}
task SideShots{
let angleshot = 0;
let x = GetEnemyX+(GetCenterX/2)+5;
let x2 = GetEnemyX-(GetCenterX/2)-5;
let y = GetEnemyY+30;
loop{
PlaySoundEffect("laser", 85, GetCenterX);
CreateLaser1(x, y, AngleBetweenPoints(x, y, GetPlayerX, GetPlayerY), WHITE60, 400, 20, 60, 100); 
CreateLaser1(x2, y, AngleBetweenPoints(x2, y, GetPlayerX, GetPlayerY), WHITE60, 400, 20, 60, 100); 
wait(120);
if(popped==true){ return; }
if(phase==4){
PlaySoundEffect("laser", 85, GetCenterX);
CreateLaser1(GetEnemyX, GetEnemyY, GetAngleToPlayer(objEnemy), WHITE60, 400, 30, 60, 100); 
}
wait(120);
if(popped==true){ return; }
angleshot+=15;
if(popped==true){ return;}
}
}

task CreateLaser1(x, y, angle, graphic, length, width, delay, duration){
let obj2 = ObjShot_Create(OBJ_STRAIGHT_LASER);

let bulletgraphic = 0;
let waitincrement = 0;
let wide = width;

ObjMove_SetPosition(obj2, x, y);
ObjStLaser_SetAngle(obj2, angle);
ObjShot_SetGraphic(obj2, graphic);
ObjShot_SetDelay(obj2, delay);
ObjLaser_SetLength(obj2, length);
ObjLaser_SetRenderWidth(obj2, width);
ObjLaser_SetIntersectionWidth(obj2, wide*0.5);
ObjShot_SetSpellResist(obj2, true);
ObjStLaser_SetSource(obj2, true);
ObjLaser_SetInvalidLength(obj2, 10, 10);
ObjShot_Regist(obj2);
ObjMove_SetPosition(obj2, x, y);


Wait2(delay);
if(popped!=true){
PlaySoundEffect("ping", 75, GetCenterX);
}

while(!Obj_IsDeleted(obj2)){

if(waitincrement>duration){
	if(wide<=0){
	Obj_Delete(obj2);
	}
	
wide-=width/15;
ObjLaser_SetRenderWidth(obj2, wide);
ObjLaser_SetIntersectionWidth(obj2, wide*0.5);
}
waitincrement++;
Wait2(1);
}

function Wait2(f){ loop(f){ if(popped==true){ Obj_Delete(obj2); return; } yield;  } }

}

task ManipulateLaser(obj, angle, dir, x, y){
let radius = 0;
let radiusacc = 2.25;
let angle2 = angle;
let shotvar = 0;
let shotangle = 0;
let shotangle2 = 0;


loop{

shotangle = AngleBetweenPoints(ObjMove_GetX(obj), ObjMove_GetY(obj), GetEnemyX, GetEnemyY);
ObjMove_SetPosition(obj, x+radius*cos(angle), y+radius*sin(angle));
radius+=radiusacc;
if(radiusacc>0){ radiusacc-=0.05; }
if(radiusacc<0){ radiusacc = 0; }

shotvar++;
if(shotvar>30 && shotvar%6==0){
if(popped==true){ return; }

PlaySoundEffect("shot2", 60, GetCenterX);
loop(2){
CreateDNHShotA2(ObjMove_GetX(obj), ObjMove_GetY(obj), 0.25, shotangle, 0.025, 2.3+rand(-0.10, 0.10)+[0,0,0,0,0.15][phase%5], BLUE01, rand(10, 20));
shotangle+=360/2;
}

shotangle2+=rand(170, 190);
}

if(popped==true){
ObjShot_FadeDelete(obj);
return;
}

angle+=(2+shotvar/300)*dir;
yield;

if(popped==true){ ObjShot_FadeDelete(obj); return; }
}
}

task ManipulateLaser2(obj, angle, dir, x, y){
let radius = 0;
let radiusacc = 3.2;
let angle2 = angle;
let shotvar = 0;
let shotangle = 0;
let shotangle2 = 0;


loop{

shotangle = AngleBetweenPoints(ObjMove_GetX(obj), ObjMove_GetY(obj), GetEnemyX, GetEnemyY);
ObjMove_SetPosition(obj, x+radius*cos(angle), y+radius*sin(angle));
radius+=radiusacc;
if(radiusacc>0){ radiusacc-=0.05; }
if(radiusacc<0){ radiusacc = 0; }

shotvar++;
if(shotvar>30 && shotvar%7==0){
if(popped==true){ return; }

PlaySoundEffect("shot2", 60, GetCenterX);
loop(3){
CreateDNHShotA2(ObjMove_GetX(obj), ObjMove_GetY(obj), 0, shotangle, 0.025, 1.65+[0,0,0,0,0.25][phase%5], WHITE23, rand(10, 20));
shotangle+=360/3;
}

shotangle2+=rand(170, 190);
}

if(popped==true){
ObjShot_FadeDelete(obj);
return;
}

angle-=(2+shotvar/300)*dir;
yield;

if(popped==true){ ObjShot_FadeDelete(obj); return; }
}
}

function TExpan2(px,py,v,r,g,b,alpha){
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	let img=GetCurrentScriptDirectory~"../sprites/charge.png";
	let scale = 0;
	let time = 0;
	
	let malpha = alpha;
	ObjPrim_SetTexture(obj,img);
	ObjRender_SetAlpha(obj, alpha);
	ObjSprite2D_SetSourceRect(obj,0,0,128,128);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj,px,py,0);
	ObjRender_SetColor(obj,r,g,b);
	Obj_SetRenderPriorityI(obj,55);
	Expan;
		task Expan{
		loop{
			ObjRender_SetAlpha(obj,malpha);
			scale += v/60;
			ObjRender_SetScaleXYZ(obj,scale,scale,0);
			yield;
			time++;
			if(popped==true){
			Obj_Delete(obj);
			}
			if(time>90){ malpha-=alpha/30; ObjRender_SetAlpha(obj,malpha); }
			if(time>=120){ Obj_Delete(obj); }
			
			}
		}
	}

task nodamage{
loop{
ObjRender_SetAlpha(objEnemy, 255);
if(GetPlayerInvincibilityFrame>0){
invulnvar = 30;
}
if(invulnvar>0){
ObjRender_SetAlpha(objEnemy, 60);
Invuln(1, shotrate, 0);
}
invulnvar--;
wait(1);
}
}

	
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{

SetCommonData("timestop", false);
SetCommonData("petals", false);
ObjMove_SetSpeed(objEnemy, 0);
StopSlow(TARGET_ALL);
StartSlow(TARGET_ALL, 30);
ObjMove_SetDestAtWeight(objEnemy, ObjMove_GetX(objEnemy)-60, ObjMove_GetY(objEnemy)+rand(-40, 40), 20, 1);
PlaySoundEffect("se_enep01", 100, GetEnemyX);
popped=true;
ascent(i in 0..24){ TExpan(ObjMove_GetX(objEnemy)+rand(-35, 35), ObjMove_GetY(objEnemy)+rand(-25, 25), 8, 25, 224, 255, 255, 100); wait(5);  }
PlaySoundEffect("se_enep01", 100, GetEnemyX);
StopSlow(TARGET_ALL);
TExpan(ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), 55, 120, 224, 255, 255, 255);
NotifyEventAll(EV_SPELLBGEND, 1);
Obj_Delete(objEnemy);
loop(60){ yield; }
		CloseScript(GetOwnScriptID());
		StopSlow(TARGET_ALL);
		return;
}
