//#TouhouDanmakufu
#ScriptVersion[3]
#Title["KogasaN3_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 a = 0;
let variable = 0;
let n = 0;
@Event{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
	SetScriptResult(3000);
	}
	case(EV_REQUEST_TIMER){
	SetScriptResult(44);
	}
}


@Initialize{
	//Resource Loading
	shotinit;
	InstallFont("./fontSEI.ttf");
	LoadSound(shot);
	LoadSound(shot3);
	LoadSound(charge2);
	LoadSound(ping);
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	Invuln(180, 100, 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);  
	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-5);
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 angleblah = 0;
let angle3 = 0;
angle = 30;
let radius = 30+15-sin(angleacc*10);
wait(120);

loop{
PlaySE(charge2);

if(color%2==0){
TCharge01(objEnemy, 3, 45, 255, 0, 0);
}
else{
TCharge01(objEnemy, 3, 45, 255, 0, 255);
}
wait(45);

angle = angletoplayer(objEnemy);
a = 0;
loop(120){
	loop(11){
CreateDNHShotA1(GetEnemyX+20*cos(angle+angle2), GetEnemyY+20*sin(angle+angle2), 3, angle+angle2, [RED21, PURPLE21][color%2], 10);
angle+=360/11;
}
angle2=14*sin(angle3); 
angle3+=4.1;
wait(2);
if(popped==true){break;}
if(a%15==0){
PlaySE(shot);
ascent(i in 0..6){
	loop(12){
	CreateDNHShotA1(GetEnemyX, GetEnemyY, 1+(i/4)+rand(-0.1, 0.1), angle+(i*12)+angleblah, [PURPLE56, RED56][color%2], 10);
	angle+=360/12;
	}
	}
	angleblah = rand(0, 360);
}
a++
}
wait(15);
color++;
if(popped==true){break;}
ExMoveBarely;

wait(60);
if(popped==true){break;}
}
}
function ExMoveBarely { ExMoveBarely_(GetClipMinY+120, 10, rand(15, 25)); }
function ExMoveBarely_(height, ud, length) {
if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0){
	if(GetPlayerX < GetEnemyX) {
		if(GetEnemyX - length <= GetClipMinX+20) {
			ObjMove_SetDestAtWeight(objEnemy, GetEnemyX + length+10, rand(height+4 - ud, height + ud), 15, 3);
		} else {
			ObjMove_SetDestAtWeight(objEnemy, GetEnemyX - length-10, rand(height - ud, height + ud), 15, 3);
		}
	} else {
		if(GetEnemyX + length >= GetClipMaxX-20) {
			ObjMove_SetDestAtWeight(objEnemy, GetEnemyX - length-10, rand(height - ud, height + ud), 15, 3);
		} else {
			ObjMove_SetDestAtWeight(objEnemy, GetEnemyX + length+10, rand(height - ud, height + ud), 15, 3);
		}
	}
}
}

