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


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

let objEnemy;
let duration = 0;
let stopstuff = false;
let angle = 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(42-9);
	}
}


@Initialize{
	//Resource Loading
	shotinit;
	InstallFont("./fontSEI.ttf");
	LoadSound(shot);
	LoadSound(shot3);
	LoadSound(poof);
	LoadSound(charge2);
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	Invuln(180, 60, 75);
	SetCommonData("alive", true);
	let Sakuya = GetCurrentScriptDirectory~"sprites/sakuya.png";
	ObjPrim_SetTexture(objEnemy, Sakuya); 
	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..2){ObjAnim_AddFrameA2(objEnemy, ANIM_ATTACK, 128*5, 128*x, 128*6, 128*x+128, 0, 0);}
	ascent(x in 0..2){ObjAnim_AddFrameA2(objEnemy, ANIM_ATTACKEND, 128*5, 128*1-128*x, 128*6, 128*2-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, 6);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_MOVE_RIGHT, 1);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_MOVE_LEFT, 1);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_ATTACK, 1);
	ObjAnim_StartAnimationA2(objEnemy, 0, 0);
		ObjSprite2D_SetDestCenter(objEnemy);  
	//BottomEnemyMarker(objEnemy, objEnemy);
	ObjMove_SetPosition(objEnemy, -400, -400);
	
	attack1;
	SetShotAutoDeleteClip(20, 40, 20, 20);
	
	
}

task screenflash{
let objWhite = ObjPrim_Create(OBJ_SPRITE_2D);
let white = GetCurrentScriptDirectory~"sprites/white.png";
let alphavar = 0;
ObjPrim_SetTexture(objWhite, white);
ObjSprite2D_SetSourceRect(objWhite, 0, 0, 691, 607);
ObjSprite2D_SetDestCenter(objWhite);
ObjRender_SetAlpha(objWhite, 127.5);
ObjMove_SetX(objWhite, GetStgFrameWidth/2);
ObjMove_SetY(objWhite, GetStgFrameHeight/2);
ObjRender_SetScaleXYZ(objWhite, 2, 2, 1);
loop(20){
	ObjRender_SetAlpha(objWhite, 125+125*sin(alphavar));
	alphavar+=180/20;
	wait(1);
	}
Obj_Delete(objWhite);
}

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 ChangeToSpell{

ascent(x in 0..3){ObjAnim_ReplaceFrameA2(objEnemy, ANIM_ATTACK, x, 128*3, 128*x, 128*4, 128*x+128, 0, 0);}
	ascent(x in 0..3){ObjAnim_ReplaceFrameA2(objEnemy, ANIM_ATTACKEND, x, 128*3, 128*2-128*x, 128*4, 128*3-128*x, 0, 0);}
	}


@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("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;
}


function ExMoveInstant { ExMoveA(GetClipMinY+120, 10, rand(40, 60)); }
function ExMoveA(height, ud, length) {
if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0){
	if(GetPlayerX < GetEnemyX) {
		if(GetEnemyX - length <= GetClipMinX+20) {
			ObjMove_SetPosition(objEnemy, GetEnemyX + length+20, GetClipMinY+120);
		} else {
			ObjMove_SetPosition(objEnemy, GetEnemyX - length-20, GetClipMinY+120);
		}
	} else {
		if(GetEnemyX + length >= GetClipMaxX-20) {
			ObjMove_SetPosition(objEnemy, GetEnemyX - length-20, GetClipMinY+120);
		} else {
			ObjMove_SetPosition(objEnemy, GetEnemyX + length+20, GetClipMinY+120);
		}
	}
}
}

task thin{
ascent(i in 0..7){
ObjRender_SetScaleXYZ(objEnemy, 1-(i/7), 1+(i/7), 1);
yield;
}
descent(i in 0..7){
ObjRender_SetScaleXYZ(objEnemy, 1-(i/7), 1+(i/7), 1);
yield;
}
}

task attack1{	
let one = 1;
let angle2 = 0;
let color = 0;
let angle3 = 0;
ObjAnim_SetAttackA2(objEnemy, 30);
screenflash;
wait(10);
if(popped==true){return;}
PlaySE(poof);
ObjMove_SetPosition(objEnemy, GetCenterX, GetClipMinY+120);
wait(110);
if(popped==true){return;}
PlaySE(charge2);
TCharge01(objEnemy, 3, 45, 155, 155, 255);
wait(60);
if(popped==true){return;}
loop{
	loop(2){
PlaySE(poof);
ObjAnim_SetAttackA2(objEnemy, 30);
thin;
wait(7);
if(popped==true){return;}
ExMoveInstant;
wait(1);
if(popped==true){return;}
loop(12){
			CreateDNHShotA1(GetEnemyX+35*cos(angle), GetEnemyY+35*sin(angle), 0.45, angle, [BLUE32, RED32][color%2], 30);
			angle+=360/12;
			}
ascent(i in 0..10){
			loop(13){
			CreateDNHShotA1(GetEnemyX+35*cos(angle), GetEnemyY+35*sin(angle), max(0.55+(i/3.75), 0.6), angle+(i*7.42857142857*one), [BLUE32, RED32][color%2], 10);
			angle+=360/13;
			}
	}
	
	
	color++;
	if(one==-1){
loop(70){
		ascent(i in 0..3){
		CreateShotA1(GetEnemyX+15*cos(angle), GetEnemyY+15*sin(angle), 0.8+(i/20), angle, GREEN32, 30);
		}
		
		angle+=360/70;
		}
		}
	angle = rand(0, 360);
wait(82);
one*=-1;
}

PlaySE(charge2);
angle2 = GetAngleToPlayer(objEnemy);

TCharge01(objEnemy, 3, 45, 255, 255, 255);
wait(30);
loop(24){
CreateLaser1(ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), angle2, BLUE12, 600, 100, 120, 1);
angle2+=360/24;
}
wait(30);
ObjAnim_SetAttackA2(objEnemy, 150);
ascent(i in 0..75){
	loop(7){
		CreateDNHShotA1(GetEnemyX+40*cos(angle2+i*15), GetEnemyY+40*sin(angle2+i*15), 8-(i/10), angle2+i*15, PURPLE32, 10);
		angle2+=360/6;
		}
wait(2);
if(popped==true){return;}
}
wait(60);
if(popped==true){return;}
}
}

task CreateLaser1(x, y, angle, graphic, length, width, delay, type){
let obj2 = ObjShot_Create(OBJ_STRAIGHT_LASER);
let angle2 = rand(0, 360);
let bulletgraphic = 0;
let one = 1;
let index = 0;
let waitincrement = 0;
ObjMove_SetPosition(obj2, x, y);
ObjStLaser_SetAngle(obj2, angle);
ObjShot_SetGraphic(obj2, graphic);
ObjShot_SetDelay(obj2, delay*20);
ObjLaser_SetLength(obj2, length);
ObjLaser_SetRenderWidth(obj2, width);
ObjShot_SetSpellResist(obj2, true);
ObjStLaser_SetSource(obj2, false);
ObjShot_Regist(obj2);
wait2(delay-5);
if(type==0){ wait(5); }
if(type==1){ObjShot_FadeDelete(obj2); }
if(type==0){
PlaySE(ping);
}
ObjLaser_SetRenderWidth(obj2, width/2);
ObjLaser_SetIntersectionWidth(obj2, width/2-3);
wait2(delay*2);
ObjShot_FadeDelete(obj2);

function wait2(w){ loop(w){ if(popped==true){ Obj_Delete(obj2); return; } yield; } }
}
