
#ScriptVersion[3]
#Title["YoumuN4_H"]
#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(54);
	}
}

@Initialize{
	//Resource Loading
	shotinit;
	LoadSound(shot);
	LoadSound(shot3);
	LoadSound(charge2);
	LoadSound(slash);
	LoadSound(ping);
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	Invuln(180, 100, 75);
	SetScalingDamageRate(180, 100, 150, 25*60);
	//BottomEnemyMarker(objEnemy, objEnemy);
	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(1){ 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, 9);
	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); 
	
	
	ObjMove_SetDestAtFrame(objEnemy, GetCenterX, 120, 60);
	
	attack1;
	attack2;
	SetShotAutoDeleteClip(70, 70, 70, 30);
	magiccircle(objEnemy, 0.7, true);
	
}


task SetScalingDamageRate(delay, initial, final, frames){
let value = final-initial;
let value2 = initial;
wait(delay);
ObjEnemy_SetDamageRate(objEnemy, initial, 75);
loop(frames){
ObjEnemy_SetDamageRate(objEnemy, value2, 75);
value2+=value/frames;
wait(1);
}
}

@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 attack2{
angle2 = 0;
let color = 0;
wait(330);
loop{
ExMoveSlow;
angle = atan2(ObjMove_GetY(objEnemy)-GetPlayerY, ObjMove_GetX(objEnemy)-GetPlayerX)-180; //starting angle;
	//let x = GetEnemyX;
			//let y = GetEnemyY;
			let waves = 8; //lines of knives
			let bullet_start = 1; //number of starting knives
			let bullet_inc = 1; //number of knives increased per wave
			let start_ang = 10; //how wide the knives are, with the starting angle being the center
			let ang_inc = 2; //how much wider the knives get per wave
			
			ascent(i in 0..waves){
				let bullets = bullet_start+bullet_inc*i;
				let ang_offset = (start_ang+ang_inc*i)/bullets;
				//PlaySE(shot);
				color++;
				ascent(o in 0..bullets){
				if(popped==true){break;}
					let ang = angle + ang_offset*o - (ang_offset*(bullets-1))/2;
					CreateDNHShotA1(GetEnemyX, GetEnemyY, 1.4+(0.25*i), ang, RED33, 10);
				}
				wait(6);
			}
wait(120-66);
}
}
/*
task attack2{
angle2 = 0;
wait(300);
loop{
if(popped==true){break;}
//ExMoveSlow;
angle2 = atan2(ObjMove_GetY(objEnemy)-GetPlayerY, ObjMove_GetX(objEnemy)-GetPlayerX)-180;
	ascent(i in 0..10){
	if(popped==true){break;}
	if(i%2==0){
		ascent(k in -i..i+1){
		CreateDNHShotA1(GetEnemyX, GetEnemyY, 2.75+(i/24), angle2+(k*12), RED33, 10);
		}
		}
		else{
		ascent(k in -i..i){
		CreateDNHShotA1(GetEnemyX, GetEnemyY, 2.75+(i/24), angle2+(k*12)+(12/2), RED33, 10);
		}
		}
		wait(12);
		}
wait(30);
if(popped==true){break;}
}
}
*/
task DestroyBoss{
popped=true;
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(1){ 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(1){ 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 one = 1;
let dir = 0;
let speed = 0;
let yvar = 0;
let increment = 1;
let positionx2 = 0;
let positiony2 = 0;
let xoffset = 0;
let yoffset = 0;
let a = 0;
ChangeToHorizontal;
ObjAnim_SetAttackA2(objEnemy, 60);
PlaySE(charge2);
TCharge01(objEnemy, 3, 60, 245, 245, 240);
wait(60);
if(popped==true){break;}
PlaySE(slash);
ScreenShake(15, 20);
CreateLaser1((GetCenterX)-(50*cos(45)), (GetClipMinY-20)+50*sin(45), 45, WHITE12, 100, 20, 60, 0);
CreateLaser1((GetCenterX)-(50*cos(135)), (GetClipMinY-20)+50*sin(135), 135, WHITE12, 100, 20, 60, 0);		
if(popped==true){break;}
wait(90);
angle = 45;
loop{

	loop(3){
	angle2 = rand(-2, 2);
	speed = rand(-0.15, 0.15);
CreateDNHShotA1(GetCenterX, GetClipMinY+50, 3.95+speed, angle+angle2, WHITE03, 10);
CreateDNHShotA1(GetCenterX, GetClipMinY+50, 3.95+speed, angle+angle2, WHITE51, 10);
angle+=360/3;
}
//PlaySE(shot3);
wait(6);
angle+=33.657293417276286834264*one;
angle2+=8.75*one;
a++;
if(a%45==0){ one*=-1; }
if(popped==true){break;}
}
}

task CreateLaser1(x, y, angle, graphic, length, width, delay, type){
let obj = ObjShot_Create(OBJ_STRAIGHT_LASER);
let angle2 = rand(0, 360);
let bulletgraphic = 0;
let one = 1;
let waitincrement = 0;
ObjMove_SetPosition(obj, x, y);
ObjStLaser_SetAngle(obj, angle);
ObjShot_SetGraphic(obj, graphic);
ObjShot_SetDelay(obj, delay);
ObjLaser_SetLength(obj, length);
ObjLaser_SetRenderWidth(obj, width);
ObjLaser_SetIntersectionWidth(obj, 20);
ObjShot_SetSpellResist(obj, true);
ObjStLaser_SetSource(obj, false);

ObjShot_Regist(obj);
wait(delay);
PlaySE(ping);
ObjLaser_SetRenderWidth(obj, width/2);
ObjLaser_SetIntersectionWidth(obj, width/2-5);
while(ObjEnemy_GetInfo(GetEnemyBossObjectID, INFO_LIFE)>=0){ wait2(1); }
ObjShot_FadeDelete(obj);


function wait2(f){ loop(f){ if(popped==true){ ObjShot_FadeDelete(obj); } yield;  } }	

}