//#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 wave = 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 = 19;
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));	
	}
}

@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(75, 75, 75, 35);
	
	
}



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

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

PlaySoundEffect("cardcall", 100, GetCenterX);
ObjAnim_SetAttackA2(objEnemy, 75);
ObjCutin_SetSpellcardS4("Frost &quot;Coil of Cold&quot;", reimucut, "NAZRIN", 135, 206, 250);
ObjCutin_LaunchS3("NAZRIN", reimucut, "Hard");
NotifyEventAll(EV_SPELLBG, 5);
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, 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;
let angle2 = 0;

let angle3 = 0;
let radius = 25;
if(popped==true){return;}

if(popped==true){ return; }
loop{
PlaySoundEffect("charge2", 85, -1);
TCharge01(objEnemy, 3, 45, 155, 155, 255);

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

ObjAnim_SetAttackA1(objEnemy, true);

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

loop(2){
ascent(i in 0..45){
PlaySoundEffect("shot2", 65, GetEnemyX);
	loop(3){
let obj = Fog(GetEnemyX+radius*cos(angle), GetEnemyY+radius*sin(angle), 0, angle+rand(-2, 2), 2, 90, 10);
Manage(obj, angle+(90*one), 120-(i/2));
angle+=360/3;
}
radius+=8.35;
angle+=9.75*one;
wait(3);
if(popped==true){ return; }
}

angle = angle+360/6;
radius = 25;
wait(15);
if(popped==true){ return; } 
ObjAnim_SetAttackA1(objEnemy, true);
wait(45);
if(popped==true){ return; }

one*=-1;
}

ObjAnim_SetAttackA1(objEnemy, false);
ExMove(GetClipMinY+130, 12, rand(20, 40));
wait(40);
	ascent(i in 2..9){
loop(20){
CreateDNHShotA1(GetEnemyX, GetEnemyY, 2-(i/5), angle, [AQUA04, BLUE04][color%2], 10);
angle+=360/20;
}
angle+=360/9/3;
wait(20);

if(popped==true){ return; }
}
wait(30);
if(popped==true){ return; }
color++;
}
}

task BubbleManage(objew){
let alpha = 0;
loop(45){
ObjRender_SetAlpha(objew, alpha);
alpha+=255/45;
yield;
}
ObjRender_SetBlendType(objew, BLEND_ADD_RGB);
}

function Fog(x,y,spd,dir,maxscale,frame,fade){ 
	let shot = CreateShotA1(x,y,spd,dir, WHITE05,0);
	let dis = 0;
	ObjShot_SetIntersectionEnable(shot,false);
	ObjRender_SetAlpha(shot,0);
	ObjShot_SetSpellResist(shot, true);
	ObjShot_SetAutoDelete(shot,false);
	snowshot(x,y,1);
	return shot;
	task snowshot(x,y,way){
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	let img= GetCurrentScriptDirectory~"sprites/boss.png";
	let c = 0;
	let scale = 0;
	let start = rand(0,3);
	let alpha = 0;
	start = round(start);
	maxscale += rand(-maxscale/10,maxscale/10);
	ObjPrim_SetTexture(obj,img);
	ObjSprite2D_SetSourceRect(obj,32*start,192,32*start+32,224);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetAlpha(obj,0);
		while(!Obj_IsDeleted(shot)){
		if(popped==true){ Obj_Delete(obj); Obj_Delete(shot); }
		ObjRender_SetPosition(obj,ObjMove_GetX(shot),ObjMove_GetY(shot),0);
		if(alpha<255){alpha+=255/frame;}
		ObjRender_SetAlpha(obj,alpha);
		ObjRender_SetScaleXYZ(obj,maxscale+c/250,maxscale+c/250,0);
		c++;
		yield;
		}
		ascent(i in 0..fade){
		ObjRender_SetAlpha(obj,255-i*255/fade);
		yield;
		}
		Obj_Delete(obj);
	}
}

task Manage(obj,num, delay){
let shotangle = angle+180;
	loop(delay){
	yield;
	if(popped==true || Obj_IsDeleted(obj)==true){ return; Obj_Delete(obj); }
	}
	let x = ObjMove_GetX(obj);
	let y = ObjMove_GetY(obj);
	Obj_Delete(obj);
	

	PlaySoundEffect("shot5",85,GetCenterX);
	if(y<GetClipMaxY+5){
	loop(3){
	let shot = CreateShotA2(x, y, 0, shotangle, 0.005, 1.25, WHITE23, 20);
	shotangle+=360/3;
	}
	}
	
	wait(3);
	
	
	
	}

	task DeleteCloud(obj){
	let alpha = 255;
	loop(15){
	alpha-=255/15;
	ObjRender_SetAlpha(obj, alpha);
	if(popped==true){ Obj_Delete(obj); return; }
	yield;
	}
	Obj_Delete(obj);
	}


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