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


#include"./lib/SHOT_REPLACE/shot_replace.dnh"
#include"./Kogasainclude.txt"

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


@Initialize{
	//Resource Loading
	shotinit;
	InstallFont("./fontSEI.ttf");
	LoadSound(shot);
	LoadSound(shot3);
	LoadSound(charge2);
	LoadSound(laser);
	LoadSound(ping);
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	Invuln(180, 82.5, 75);
	SetCommonData("alive", true);
	let Kogasa = GetCurrentScriptDirectory~"sprites/kogasa.png";
	ObjPrim_SetTexture(objEnemy, Kogasa); 
	ObjSprite2D_SetSourceRect(objEnemy, 0, 0, 128, 128);
	ObjAnim_AddFrameA2(objEnemy, ANIM_IDLE, 0, 0, 128, 128, 10, 1);
	ascent(x in 0..2){ObjAnim_AddFrameA2(objEnemy, ANIM_MOVE_LEFT, 256, 128*x, 256+128, 128*x+128, 10, 1);}
	
	
	ascent(x in 2..4){ObjAnim_AddFrameA2(objEnemy, ANIM_MOVE_RIGHT, 256, 128*x, 256+128, 128*x+128, 10, 1);}
	CheckForFlip;
	ObjAnim_AddFrameA2(objEnemy, ANIM_MOVEEND_RIGHT, 256, 128*2, 128*3, 128*2+128, 10, 1);
	ObjAnim_AddFrameA2(objEnemy, ANIM_MOVEEND_RIGHT, 0, 0, 128, 128, 10, 1);
	ObjAnim_AddFrameA2(objEnemy, ANIM_MOVEEND_LEFT, 256, 128*2, 256+128, 128*2+128, 10, 1);
	ascent(x in 0..2){ObjAnim_AddFrameA2(objEnemy, ANIM_ATTACK, 128*3, 128*x, 128*4, 128*x+128, 10, 1);}
	ascent(x in 0..4){ObjAnim_AddFrameA2(objEnemy, ANIM_ATTACKEND, 128*5, 128*x, 128*6, 128*x+128, 10, 1);}
	ObjAnim_SetSpeedA1(objEnemy, ANIM_IDLE, 9);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_MOVE_LEFT, 6);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_MOVE_RIGHT, 6);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_ATTACK, 12);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_MOVE_RIGHT, 1);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_MOVE_LEFT, 1);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_ATTACK, 0);
	ObjAnim_StartAnimationA2(objEnemy, 0, 0);
		ObjSprite2D_SetDestCenter(objEnemy);  


	ObjMove_SetDestAtFrame(objEnemy, GetCenterX, 120, 110);  
	attack2;
	attack1;
	SetShotAutoDeleteClip(100, 70, 100, 40);
	
	
}



task CheckForFlip{
yield;
loop{
if(CheckDirectionAL1(objEnemy, "left")){
SetCommonData("flipsprite", true);
}
else if(CheckDirectionAL1(objEnemy, "right")){
SetCommonData("flipsprite", false);
}
if(GetCommonData("flipsprite", false)==true && CheckDirectionAL1(objEnemy, "none")){ ObjRender_SetScaleX(objEnemy, -1)}
else{ ObjRender_SetScaleX(objEnemy, 1); }


yield;
}
}

task CreateLaser1(x, y, angle, graphic, length, width, delay){
let obj2 = ObjShot_Create(OBJ_STRAIGHT_LASER);
let bulletgraphic = 0;
let waitincrement = 0;
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, 20);
ObjShot_SetSpellResist(obj2, true);
ObjStLaser_SetSource(obj2, false);
ObjLaser_SetInvalidLength(obj2, 10, 10);
ObjShot_Regist(obj2);
ObjMove_SetPosition(obj2, x, y);


Wait2(delay);
PlaySE(ping);
ObjLaser_SetRenderWidth(obj2, width/2);
ObjLaser_SetIntersectionWidth(obj2, width/2);
Wait2(delay);
ObjShot_FadeDelete(obj2);

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

}


@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, 23);//当たり判定(自機弾用)登録
	ObjEnemy_SetIntersectionCircleToPlayer(objEnemy, GetEnemyX, GetEnemyY, 17);//当たり判定(体当たり用)登録

	frame++;
	yield;
	GetSpeedX = ObjMove_GetSpeed(objEnemy)*cos(ObjMove_GetAngle(objEnemy));
	SetCommonData("kogasax", ObjMove_GetX(objEnemy));
	SetCommonData("kogasay", 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 attack1{	
let one = 1;
let angleacc = 60;
let angle2 = 0;

let angle3 = 0;
angle = 30;
let radius = 30+15-sin(angleacc*10);
wait(120);
loop{
one*=-1;
PlaySE(charge2);
if(color%2==0){
TCharge01(objEnemy, 3, 45, 0, 255, 255);
}
else{
TCharge01(objEnemy, 3, 45, 148, 0, 211);
}
if(popped==true){break;}
wait(60);
ObjAnim_SetAttackA1(objEnemy, true);
ascent(i in 0..6000){
color+=0.025;
if(popped==true){break;}
loop(10){
if(GetCommonData("bullettype", 1)==1){
let obj = CreateDNHShotA1(ObjMove_GetX(objEnemy)+(radius)*cos(angle), ObjMove_GetY(objEnemy)+(radius)*sin(angle), 2.85, angle, WHITEI04, 10);
ObjRender_SetColorHSV(obj, color*40, 255, 255);
}
else{
let obj = CreateShotA1(ObjMove_GetX(objEnemy)+(radius)*cos(angle), ObjMove_GetY(objEnemy)+(radius)*sin(angle), 2.85, angle, WHITEI04, 10);
ObjRender_SetColorHSV(obj, color*40, 255, 255);
}
angle+=360/10;
}
radius = 30+15*-sin(angleacc*10);
angle+=angleacc;
angleacc+=0.35;
//angle+=7;
wait(3);
}

wait(15);
if(popped==true){break;}
angle3=rand(0,360);
loop(26){

	ascent(i in 0..8){
	color++;
	CreateShotA1(ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), 1.25+i/2.85, angle+angle2, [RED51, ORANGE51, YELLOW51, GREEN51, BLUE51, AQUA51, PURPLE51][2], 20);
	}
	angle+=360/26;

	}


ExMove;
wait(90);
	if(popped==true){break;}
	
	
}
}

task attack2{
let angle4 = 0;
wait(240);
loop{
PlaySE(charge2);
if(round(color)%7==0){
TCharge01(objEnemy, 3, 45, 255, 0, 0);
}
else if(round(color)%7==1){
TCharge01(objEnemy, 3, 45, 255, 165, 0);
}
else if(round(color)%7==2){
TCharge01(objEnemy, 3, 45, 255, 255, 0);
}
else if(round(color)%7==3){
TCharge01(objEnemy, 3, 45, 0, 255, 0);
}
else if(round(color)%7==4){
TCharge01(objEnemy, 3, 45, 0, 255, 255);
}
else if(round(color)%7==5){
TCharge01(objEnemy, 3, 45, 0, 0, 255);
}
else if(round(color)%7==6){
TCharge01(objEnemy, 3, 45, 160, 32, 240);
}
wait(45);
angle4 = angletoplayer(objEnemy);
PlaySE(laser);
loop(16){
CreateLaser1(GetEnemyX+10*cos(angle4), GetEnemyY+10*sin(angle4), angle4, [RED60, ORANGE60, YELLOW60, GREEN60, AQUA60, BLUE60, PURPLE60][round(color)%7], 400, 35, 60);
angle4+=360/16;
}
wait(90);
}
}