
#ScriptVersion[3]
#Title["YoumuN1_N"]
#Text["Bullets"]


#include"./lib/SHOT_REPLACE/shot_replace.dnh"
#include"./Youmuinclude.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);
	}
}

@Initialize{
	//Resource Loading
	shotinit;
	LoadSound(shot);
	LoadSound(shot3);
	LoadSound(charge2);
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	Invuln(210, 90.5, 75);
	SetCommonData("alive", true);
	let Youmu = GetCurrentScriptDirectory~"sprites/Youmu.png";
	ObjPrim_SetTexture(objEnemy, Youmu); 
	ObjSprite2D_SetSourceRect(objEnemy, 0, 0, 128, 128);
	ascent(x in 0..3){ObjAnim_AddFrameA1(objEnemy, ANIM_IDLE, 0, 128*x, 128, 128*x+128);}
	ascent(x in 0..2){ObjAnim_AddFrameA1(objEnemy, ANIM_IDLE, 128, 128*x, 256, 128*x+128);}
	ascent(x in 0..2){ObjAnim_AddFrameA1(objEnemy, ANIM_MOVE_LEFT, 256, 128*x, 256+128, 128*x+128);}
	ObjAnim_AddFrameA1(objEnemy, ANIM_MOVEEND_LEFT, 0, 0, 128, 128);
	ascent(x in 2..4){ObjAnim_AddFrameA1(objEnemy, ANIM_MOVE_RIGHT, 256, 128*x, 256+128, 128*x+128);}
	ObjAnim_AddFrameA1(objEnemy, ANIM_MOVEEND_RIGHT, 0, 0, 128, 128);
	ascent(x in 0..4){ObjAnim_AddFrameA1(objEnemy, ANIM_ATTACK, 128*4, 128*x, 128*5, 128*x+128);}
	ascent(x in 2..4){ObjAnim_AddFrameA1(objEnemy, ANIM_ATTACKEND, 128*5, 128*x, 128*6, 128*x+128);}
	loop(8){ ObjAnim_AddFrameA1(objEnemy, ANIM_ATTACKEND, 128*5, 128*7, 128*6, 128*7+128); }
	descent(x in 2..4){ ObjAnim_AddFrameA1(objEnemy, ANIM_ATTACKEND, 128*5, 128*x, 128*6, 128*x+128);}
	descent(x in 0..3){ObjAnim_AddFrameA1(objEnemy, ANIM_ATTACKEND, 128*4, 128*x, 128*5, 128*x+128);}
	ObjAnim_SetSpeedA1(objEnemy, ANIM_IDLE, 12);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_MOVE_LEFT, 6);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_MOVE_RIGHT, 6);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_ATTACK, 10);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_MOVE_RIGHT, 1);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_MOVE_LEFT, 1);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_ATTACK, 3);
	ObjAnim_StartAnimationA2(objEnemy, 0, 0);
	ObjSprite2D_SetDestCenter(objEnemy); 
	//BottomEnemyMarker(objEnemy, objEnemy);
	
	ObjMove_SetX(objEnemy, 102910920129);
	MoveToPosition;
	
	attack1;
	SetShotAutoDeleteClip(100, 70, 100, 40);
	
	
}

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 MoveToPosition{
wait(45);
screenflash;
wait(10);
ObjMove_SetDestAtFrame(objEnemy, GetCenterX, 120, 1);
magiccircle(objEnemy, 0.7, true);

}

@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("youmux", ObjMove_GetX(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 ChangeToVertical{
animframe = 0;
	ascent(x in 2..4){ ObjAnim_ReplaceFrameA2(objEnemy, ANIM_ATTACKEND, animframe, 128*5, 128*x, 128*6, 128*x+128, 0, 0); animframe++;}
	loop(8){ ObjAnim_ReplaceFrameA2(objEnemy, ANIM_ATTACKEND, animframe, 128*5, 128*7, 128*6, 128*7+128, 0, 0); animframe++;}
	descent(x in 2..4){ ObjAnim_ReplaceFrameA2(objEnemy, ANIM_ATTACKEND, animframe, 128*5, 128*x, 128*6, 128*x+128, 0, 0); animframe++;}
	descent(x in 0..3){ObjAnim_ReplaceFrameA2(objEnemy, ANIM_ATTACKEND, animframe, 128*4, 128*x, 128*5, 128*x+128, 0, 0); animframe++;}
	}
task ChangeToHorizontal{
animframe = 0;
	ascent(x in 0..2){ ObjAnim_ReplaceFrameA2(objEnemy, ANIM_ATTACKEND, animframe, 128*5, 128*x, 128*6, 128*x+128, 0, 0); animframe++;}
	loop(8){ ObjAnim_ReplaceFrameA2(objEnemy, ANIM_ATTACKEND, animframe, 128*5, 128*1, 128*6, 128*1+128, 0, 0); animframe++;}
	descent(x in 0..2){ ObjAnim_ReplaceFrameA2(objEnemy, ANIM_ATTACKEND, animframe, 128*5, 128*x, 128*6, 128*x+128, 0, 0); animframe++;}
	descent(x in 0..3){ObjAnim_ReplaceFrameA2(objEnemy, ANIM_ATTACKEND, animframe, 128*4, 128*x, 128*5, 128*x+128, 0, 0); animframe++;}
	}

task attack1{
wait(120);
let positionx = 0;
let positiony = 0;
let color = 0;
let angle = 0;
let angle2 = 0;
let type = 1;
let dir = 0;
let yvar = 0;
let increment = 1;
let positionx2 = 0;
let positiony2 = 0;
let xoffset = 0;
let yoffset = 0;
loop{
ChangeToVertical;
ObjAnim_SetAttackA2(objEnemy, 60);
PlaySE(charge2);
TCharge01(objEnemy, 3, 45, 0, 100, 0);
wait(15);
PlaySE(charge2);
TCharge01(objEnemy, 3, 45, 255, 255, 0);
wait(45);
	if(popped==true){break;}
	//angle = AngleToPlayer;
	//angle2 = AngleToPlayer;
	ascent(i in 0..40){
	PlaySE(shot3);
	positionx = GetEnemyX+120*sin(i*9.5+45);
	positiony = GetEnemyY+100-i*(8-i/15);
	if(popped==true){break;}
	loop(6){
	CreateDNHShotA1(positionx, positiony, 1.85, angle-(i*14), [GREEN01,YELLOW01][color%2], 15);
	angle+=360/6;
	}
	
	positionx = GetEnemyX-120*sin(i*9.5+45);
	loop(6){
	CreateDNHShotA1(positionx, positiony, 1.85, angle+(i*14), [YELLOW01,GREEN01][color%2], 15);
	angle+=360/6;
	}
	wait(4);
	if(popped==true){break;}
	}
	if(popped==true){break;}
	PlaySE(shot);
	loop(14){
		ascent(i in 0..7){
	CreateDNHShotA1(positionx, positiony, 2.3-(i/4.5), angle-(i*11.25), [GREEN02,YELLOW02][color%2], 30);
	}
	angle+=360/14;
	}
	positionx = GetEnemyX+120*sin(45+9.5*45);
	if(popped==true){break;}
	loop(14){
		ascent(i in 0..7){
	CreateDNHShotA1(positionx, positiony, 2.3-(i/4.5), angle+(i*11.25), [YELLOW02,GREEN02][color%2], 30);
	}
	angle+=360/14;
	}
	//wait(60);
	if(popped==true){break;}
	ExMoveNon;
	color++;
	if(popped==true){break;}
	wait(75);
	ChangeToHorizontal;
	ObjAnim_SetAttackA2(objEnemy, 60);
	PlaySE(charge2);
	TCharge01(objEnemy, 3, 45, 178, 34, 34);
	wait(15);
	PlaySE(charge2);
	TCharge01(objEnemy, 3, 45, 65, 105, 225);
	wait(45);
	loop(18){
		if(popped==true){break;}
		xoffset = rand(-20, 20);
		positionx2 = ObjMove_GetX(objEnemy)+xoffset;
		positiony2 = ObjMove_GetY(objEnemy)+yoffset;
		if(xoffset<=0){ dir = 1;} if(xoffset>0){ dir=-1;}
		yvar = rand(-6, 6);
		if(type==1){
		
		ascent(i in 0..9){
		if(increment%3==0){
		PlaySE(shot);
		}
		if(popped==true){break;}
		CreateDNHShotA1(positionx2+((i*17)*dir), positiony2+(yvar*i), 4.55, atan2(GetPlayerY-((positiony2+(yvar*i))), GetPlayerX-(positionx2+(i*17)*dir)), BLUE21, 15);
		CreateDNHShotA1(positionx2+((i*17)*dir), positiony2+(yvar*i), 3.8, atan2(GetPlayerY-((positiony2+(yvar*i))), GetPlayerX-(positionx2+(i*17)*dir)), BLUE21, 15);
		//CreateDNHShotA1(positionx2+((i*15)*dir), positiony2+(yvar*i), 2.75, atan2(GetPlayerY-((positiony2+(yvar*i))), GetPlayerX-(positionx2+(i*15)*dir)), BLUE21, 15);
		increment++;
		wait(1);
		
		}
		}
		if(type==-1){
		ascent(i in 0..9){
		if(increment%3==0){
		PlaySE(shot);
		}
		if(popped==true){break;}
			ascent(w in 0..2){
		CreateDNHShotA1(positionx2+((i*17)*dir), positiony2+(yvar*i), 4.55, (atan2(GetPlayerY-((positiony2+(yvar*i))), GetPlayerX-(positionx2+(i*17)*dir)))-16+(w*32), RED21, 15);
		CreateDNHShotA1(positionx2+((i*17)*dir), positiony2+(yvar*i), 3.8, (atan2(GetPlayerY-((positiony2+(yvar*i))), GetPlayerX-(positionx2+(i*17)*dir)))-16+(w*32), RED21, 15);
		//CreateDNHShotA1(positionx2+((i*15)*dir), positiony2+(yvar*i), 2.75, (atan2(GetPlayerY-((positiony2+(yvar*i))), GetPlayerX-(positionx2+(i*15)*dir)))-15+(w*30), RED21, 15);
			}
		increment++;
		wait(1);
		
		}
		}
		type*=-1;
		}
		wait(20);
		ExMoveNon;
		wait(45);
	
}
}
