//#TouhouDanmakufu
#ScriptVersion[3]
#Title["SakuyaN2_H"]
#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(46);
	}
}


@Initialize{
	//Resource Loading
	shotinit;
	InstallFont("./fontSEI.ttf");
	LoadSound(shot);
	LoadSound(shot2);
	LoadSound(shot3);
	LoadSound(poof);
	LoadSound(shadow);
	LoadSound(explode);
	LoadSound(charge2);
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	Invuln(180, 80, 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, 8);
	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_SetDestAtFrame(objEnemy, GetCenterX, GetClipMinY+120, 90);
	
	attack1;
	SetShotAutoDeleteClip(40, 40, 40, 20);
	
	
}



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;
wait(120);
if(popped==true){return;}
PlaySE(charge2);
TCharge01(objEnemy, 3, 45, 155, 155, 255);
wait(60);
if(popped==true){return;}

loop{
ObjAnim_SetAttackA2(objEnemy, 24);
	loop(7){
CreateFamiliar(angle, 0.4, one);
one*=-1;
angle+=360/7;
	}
	
	angle = rand(0, 360);
	ascent(i in 0..10){
	PlaySE(shot2);
		loop(14){
		CreateDNHShotA1(GetEnemyX+10*cos(angle+i*1.75), GetEnemyY+10*sin(angle+i*1.75), 2, angle+i*1.75, YELLOW32, 10);
		angle+=360/14;
			}
			if(popped==true){ return; }
			wait(8);
		}
		ExMoveNon;
		wait(30);
	ascent(i in 0..10){
	PlaySE(shot2);
		loop(14){
		CreateDNHShotA1(GetEnemyX+10*cos(angle+i*-1.75), GetEnemyY+10*sin(angle+i*-1.75), 2, angle+i*-1.75, ORANGE32, 10);
		angle+=360/14;
			}
			if(popped==true){ return; } 
			wait(8);
		}
		wait(30);
	wait(60);

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

task CreateFamiliar(dir, speed, type){
let obj = ObjEnemy_Create(OBJ_ENEMY);
let rotateacc = 0;
let alpha = 255;
let familiar = GetCurrentScriptDirectory~"sprites/familiar.png";
let angle = dir;
let angle2 = 0;
if(type==-1){
angle2 = 180;
}
let graphic = BLUE31;
if(type==-1){ graphic = RED31; }
ObjEnemy_SetLife(obj, 1000);
ObjPrim_SetTexture(obj, familiar);
ObjSprite2D_SetSourceRect(obj, 0, 0, 64, 64);
ObjSprite2D_SetDestCenter(obj);
ObjRender_SetScaleXYZ(obj, 0.75, 0.75, 1);
ObjMove_SetPosition(obj, GetEnemyX+30*cos(dir), GetEnemyY+30*sin(dir));
ObjEnemy_Regist(obj);
ObjMove_SetAngle(obj, dir);
ObjMove_SetSpeed(obj, speed);
Fire;
PlaySE(shadow);
while(!Obj_IsDeleted(obj)){
ObjRender_SetAngleZ(obj, 0+rotateacc);
rotateacc+=7;
yield;
}

task Fire{
let time = 0;
loop{
while(!Obj_IsDeleted(obj)){
PlaySE(shot);
ascent(i in -1..2){
CreateDNHShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 2, angle+angle2+i*1.25, graphic, 10);
}
angle+=23*type;
wait(10);
time+=10;
if(time>=320 || popped==true){
loop(15){
ObjRender_SetAlpha(obj, alpha);
alpha-=255/15;
yield;
}
Obj_Delete(obj);
}
}
return;
yield;
}
}



}


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