//#TouhouDanmakufu
#ScriptVersion[3]
#Title["idk"]
#Text["Bullets"]


#include"./../lib/SHOT_REPLACE/shot_replace.dnh"
#include"./../includes/BossIncludes.txt"

let objEnemy;
let duration = 0;
let stopstuff = false;
let angle = 0;
let frame = 0;
let angle2 = 0;
let animframe = 0;
let GetSpeedX = 0;
let variable = 0;
let n = 0;
@Event{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
	SetScriptResult(3000);
	}
	case(EV_REQUEST_TIMER){
	SetScriptResult(47);
	}
}


@Initialize{
	//Resource Loading
	shotinit;
	
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	Invuln(240, 60, 75);
	SetCommonData("alive", true);
	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*4, 128*x, 128*5, 128*x+128, 0, 0);}
	ascent(x in 0..3){ObjAnim_AddFrameA2(objEnemy, ANIM_ATTACKEND, 128*4, 128*2-128*x, 128*5, 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);  
	ObjMove_SetDestAtFrame(objEnemy, GetCenterX, GetClipMinY+120, 90);
	
	attack1;
	attack2;
	SetShotAutoDeleteClip(10, 20, 10, 10);
	
	
	
}

@MainLoop{
	
	
	if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) <=0 && stopstuff==false){stopstuff=true;}

	GetEnemyX = ObjMove_GetX(objEnemy);
	GetEnemyY = ObjMove_GetY(objEnemy);
	ObjEnemy_SetIntersectionCircleToShot(objEnemy, GetEnemyX, GetEnemyY, 25);//当たり判定(自機弾用)登録
	ObjEnemy_SetIntersectionCircleToPlayer(objEnemy, GetEnemyX, GetEnemyY, 17);//当たり判定(体当たり用)登録

	frame++;
	yield;
	GetSpeedX = ObjMove_GetSpeed(objEnemy)*cos(ObjMove_GetAngle(objEnemy));
	SetCommonData("sakuyax", ObjMove_GetX(objEnemy));
	SetCommonData("sakuyay", ObjMove_GetY(objEnemy));




if(stopstuff==true && popped==false){
DestroyBoss;
}	
	
}

task DestroyBoss{

popped=true;
SetCommonData("alive", false);
loop(10){ yield; }
Obj_Delete(objEnemy);
loop(20){ yield; }
		CloseScript(GetOwnScriptID());
		return;
}

task attack2{
wait(105);
let one = -1;
let angle2 = 45;
let color = 0;
let angle3 = 0;
let randnum = 7;
loop{
	loop(2){
			ascent(i in -4..5){
				let obj3 = CreateDNHShotA2(GetEnemyX, GetEnemyY, 5.25, angle2+(i*13), -0.125, 2.55, GREEN12, 10);
				Obj_SetRenderPriorityI(obj3, Obj_GetRenderPriorityI(obj3)-1);
				}
				angle2+=360/2;
				}
				
				angle2+=13.75*one;

color++;
if(color%30==0){
//PlaySoundEffect("shot5", 90, GetCenterX);
angle = rand(0, 360);
loop(33){
//let obj3 = CreateDNHShotA2(GetEnemyX, GetEnemyY, 5.25, angle3, -0.075, 1, AQUA54, 15);
//Obj_SetRenderPriorityI(obj3, Obj_GetRenderPriorityI(obj3)-1);
angle3+=360/33;
}
if(color%60==0){
one*=-1;
}
//ExMove(120, 5, rand(10, 15));
}
wait(6);
if(popped==true){ return; }

}
}

task attack1{
let angle2 = 0;
let color = 0;
let angle3 = 0;
let randnum = 7;
wait(240);
let one = -1;
PlaySoundEffect("charge2", 85, -1);
TCharge01(objEnemy, 3, 45, 155, 155, 255);
wait(45);

loop{
	loop(2){
FogChains([rand(10, 30), rand(150, 170)][randnum%2]);

randnum++;
	}
	if(popped==true){ return; }
wait(90);
}
}

task FogChains(angle){
let x = GetEnemyX;
let y = GetEnemyY;
let radius = 25;
loop(20){
let obj = Fog(x+radius*cos(angle+rand(-5, 5)), y+radius*sin(angle+rand(-5, 5)), 0.25, rand(0, 360), 3, 60, 10);
Manage(obj, 90+radius);
wait(5);
radius+=15;
}
}


task Manage(obj,num){

	loop(60){
	yield;
	if(popped==true || Obj_IsDeleted(obj)==true){ return; Obj_Delete(obj); }
	}
	let x = ObjMove_GetX(obj)+rand(-15,15);
	let y = ObjMove_GetY(obj)+rand(-15,15);
	Obj_Delete(obj);
	angle = 0;
	PlaySoundEffect("shot2",25,x);
	ascent(i in 0..3){
	
	if(i%2==0){}
	let shot = CreateDNHShotA2(x+rand(-15,15),y+rand(-15,15),rand(4,5),i*120+num,-0.15,rand(1.35,1.85), BLUE01,20);
	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);
	}
	
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);
	}
}

