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


#include"./lib/SHOT_REPLACE/shot_replace.dnh"
#include"./Youmuinclude.txt"
let color2 = 1;
let color = 0;
let objEnemy;
let duration = 0;
let stopstuff = false;
let angle = 0;
let angle2 = 0;
let type = 0;
let youmucutin2 = GetCurrentScriptDirectory~"sprites/youmughost.png";
let animframe = 0;
let frame = 0;
let objghosts = [];

let GetSpeedX = 0;
let variable = 0;
let n = 0;
@Event{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
	SetScriptResult(1000);
	}
	case(EV_REQUEST_TIMER){
	SetScriptResult(999);
	}
	case(EV_REQUEST_SPELL_SCORE){
	SetScriptResult(10000000);
	}
	case(EV_GAIN_SPELL)
	{
	AddScore(ObjEnemyBossScene_GetInfo(objScene, INFO_SPELL_SCORE));	
	}
}

@Initialize{
	//Resource Loading
	shotinit;
	LoadSound(shot);
	LoadSound(shot2);
	LoadSound(shot3);
	LoadSound(weird);
	LoadSound(shot4);
	LoadSound(slash);
	LoadSound(charge2);
	LoadSound(ping);
	LoadSound(cardcall);
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	ObjEnemy_SetDamageRate(objEnemy, 0, 0);
	SetScalingDamageRate(360, 10, 60, 60*37);
	//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(4){ 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); 
	
	
	ObjMove_SetDestAtFrame(objEnemy, GetCenterX, 120, 60);

	
	delayedstart;
	//attack2;
	SetShotAutoDeleteClip(100, 100, 100, 100);
	
	
}

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

function TCharge02(boss,size,frame,r,g,b){ // Draw a charge circle that has an acceleration on shrinking
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	let img=GetCurrentScriptDirectory~"sprites/charge.png";
	let set = 360/(frame*4);
	ObjPrim_SetTexture(obj,img);
	ObjSprite2D_SetSourceRect(obj,0,0,128,128);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj,ObjMove_GetX(boss),ObjMove_GetY(boss),0);
	ObjRender_SetAlpha(obj,0);
	ObjRender_SetColor(obj, r, g, b);
	ObjRender_SetScaleXYZ(obj,size,size,0);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	Obj_SetRenderPriorityI(obj,25);
	TAlpha;
	TShrink;
	return obj;
	task TAlpha{
		let l=10;
		let alpha=255/l;
		loop(l){
		
			ObjRender_SetAlpha(obj,alpha);
			alpha+=255/(l*2);
			yield;
			}
		}
	task TShrink{
		let l=frame;
		let a = size*2/l/l;
		ascent(i in 0..l){
			ObjRender_SetScaleXYZ(obj,size,size,0);
			ObjRender_SetPosition(obj,ObjMove_GetX(boss),ObjMove_GetY(boss),0);
			//ObjRender_SetColor(obj,0,128+64*sin(i*set+240),64+64*sin(i*set+0));
			size = size-i*a;
			if(Obj_IsDeleted(boss)){Obj_Delete(obj);}
			yield;
			}
		Obj_Delete(obj);
		
	}
}


task delayedstart{
magiccircle(objEnemy, 0.7, true);
ObjMove_SetDestAtFrame(objEnemy, GetCenterX, 120, 60);
wait(60);
ObjEnemyBossScene_StartSpell(objScene);
ObjEnemyBossScene_SetSpellTimer(objScene, 60*67);
PlaySE(cardcall);
ObjCutin_SetSpellcardS4("&quot;Ultimate Truth&quot;", youmucutin, YABUSAME, 32, 178, 170);
ObjCutin_LaunchS3(BYAKUREN, youmucutin, "Normal");
wait(60);
PlaySE(cardcall);
ObjCutin_SetSpellcardS4("&quot;Wicked Soul&quot;", youmucutin2, BYAKUREN, 0, 255, 255);
wait(60);
attack1;
}



@MainLoop{
	
	
	SetCommonData("youmux", ObjMove_GetX(objEnemy));
	if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) <=0 && stopstuff==false){stopstuff=true;}

		
if(stopstuff==true && popped==false){
DestroyBoss;
}	
	
	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));
}




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(4){ 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(4){ 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 Myon{
let one = -1;
let gotox = 0;
let gotoy = 0;
PlaySE(weird);
let obj = ObjShot_Create(OBJ_SHOT);
objghosts = objghosts ~ [obj];
let pangle = 0;
ObjMove_SetPosition(obj, ObjMove_GetX(objEnemy)+70, ObjMove_GetY(objEnemy));
ObjShot_SetGraphic(obj, SP02);
ObjShot_SetDelay(obj, 10);
ObjShot_SetSpellResist(obj, true);
ObjShot_SetAutoDelete(obj, false);
ObjShot_Regist(obj);
wait2(30);
PlaySE(ping);
loop(28){ CreateOrbitars(40, pangle); pangle+=360/28; }
//loop(26){ CreateOrbitars(35, pangle); pangle+=360/26; }
//loop(14){ CreateOrbitars(20, pangle); pangle+=360/14; }
	wait2(30);
while(!Obj_IsDeleted(obj) && ObjEnemy_GetInfo(objEnemy, INFO_LIFE)>0){
	loop(3){
	PlaySE(charge2);
	TCharge02(obj, 2, 60, 65, 105, 225);
	gotox = ObjMove_GetX(GetPlayerObjectID);
	gotoy = ObjMove_GetY(GetPlayerObjectID);
	Indicator(gotox, gotoy);
	if(popped==true){ Obj_Delete(obj); }
ObjMove_SetDestAtFrame(obj, gotox, gotoy, 90);
wait2(90);
if(popped==true){ ObjShot_FadeDelete(obj); }
	}
	PlaySE(charge2);
	TCharge01(obj, 2, 60, 138, 43, 226);
	wait2(60);
	if(popped==true){ ObjShot_FadeDelete(obj); }
	loop(20){
	PlaySE(shot);
		loop(9){
	CreateWavyBullet(ObjMove_GetX(obj), ObjMove_GetY(obj), 2, pangle);
	pangle+=360/9;
		}
		//pangle+=0.85*one;
		wait2(9);
		if(popped==true){ ObjShot_FadeDelete(obj); break; }
		}
	wait2(90);
	if(popped==true){ Obj_Delete(obj);}
	one*=-1;
	pangle = rand(0, 360);
	
}
Obj_Delete(obj);


function wait2(f){ loop(f){ if(popped==true){ ObjMove_SetDestAtFrame(obj, 1000000000, 1000000, 0); Obj_Delete(obj); } yield;  } }

function Indicator(x, y) {
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"sprites/Target.png");
	ObjSprite2D_SetSourceRect(obj,0,0,256,256);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
	Obj_SetRenderPriorityI(obj, 50);
	
	let angle = 270;
	let scale = 1.25;
	
	loop(60) {
		ObjRender_SetPosition(obj,x,y,0);
		//ObjRender_SetColor(obj, 255, 0, 0);
		ObjRender_SetAngleXYZ(obj,0,0,angle);
		ObjRender_SetScaleXYZ(obj,scale,scale,scale);
		angle+=270/60;
		scale-=1.25/60;
		wait2(1);
		if(Obj_IsDeleted(objghosts[0])){ Obj_Delete(obj); }
	}
	Obj_Delete(obj);

}

}


task CreateWavyBullet(x, y, v, angle){
let radius = 0;
let angle2 = 0;
let originalangle = 0;
let variance = 0;
let obj2 = ObjShot_Create(OBJ_SHOT);
ObjMove_SetSpeed(obj2, v);
ObjMove_SetPosition(obj2, x, y);
ObjMove_SetAngle(obj2, angle);
ObjShot_SetSpellResist(obj2, true);
ObjShot_SetGraphic(obj2, PURPLE21);
ObjShot_SetDelay(obj2, 0);
ObjShot_SetAutoDelete(obj2, true);
ObjMove_SetPosition(obj2, ObjMove_GetX(objghosts[0]), ObjMove_GetY(objghosts[0]));
ObjShot_Regist(obj2);
originalangle = angle;
while(!Obj_IsDeleted(obj2) && ObjEnemy_GetInfo(objEnemy, INFO_LIFE)>0){
ObjMove_SetAngle(obj2, originalangle+30*cos(variance));
variance+=8;
wait(1);
}
Obj_Delete(obj2);
}




task CreateOrbitars(radius, angle){
let obj2 = ObjShot_Create(OBJ_SHOT);

let angle2 = 0;
ObjShot_SetSpellResist(obj2, true);
ObjShot_SetGraphic(obj2, WHITE50);
ObjShot_SetDelay(obj2, 0);
ObjShot_Regist(obj2);
ObjShot_SetAutoDelete(obj2, false);
ObjMove_SetPosition(obj2, ObjMove_GetX(objghosts[0])+radius*cos(angle+angle2), ObjMove_GetY(objghosts[0])+radius*sin(angle+angle2));

while(!Obj_IsDeleted(obj2) && ObjEnemy_GetInfo(objEnemy, INFO_LIFE)>0){
ObjMove_SetPosition(obj2, ObjMove_GetX(objghosts[0])+radius*cos(angle+angle2), ObjMove_GetY(objghosts[0])+radius*sin(angle+angle2));
angle2+=2;
wait(1);
}
Obj_Delete(obj2);

}



	
task attack1{
wait(120);
let a = 0;
let randtilt = 0;
let positionx = 0;
let positiony = 0;
let objlasers = [];
let angle = 0;
let one2 = 1;
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;
PlaySE(charge2);
ObjAnim_SetAttackA2(objEnemy, 60);
TCharge01(objEnemy, 3, 60, 65, 105, 225);
wait(60);
Myon;
wait(180);
PlaySE(charge2);
//ObjAnim_SetAttackA2(objEnemy, 60);
TCharge01(objEnemy, 3, 60, 255, 215, 0);
wait(60);
loop{
	loop(3){
		ascent(i in 0..3){
		CreateDNHShotA1(ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), 1.25+(i/2.25), angle+(i*7*one2), YELLOW33, 10);
			}
			angle+=360/3;
			}
			angle+=(15.236+rand(-0.5, 0.5))*one2; //don't do this at home kids

wait(9);
a++;
if(a%60==0){ ExMoveNon; one2*=-1; }
if(popped==true){break;}

}
}


task CreateLaser2(x, y, angle, graphic, length, width, delay){
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, 2000);
ObjLaser_SetLength(obj, length);
ObjLaser_SetRenderWidth(obj, width);
ObjLaser_SetIntersectionWidth(obj, 20);
ObjShot_SetSpellResist(obj, true);
ObjStLaser_SetSource(obj, false);

ObjShot_Regist(obj);
wait2(delay);
Obj_Delete(obj);


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

}

task DestroyBoss{
SetCommonData("petals", false);
popped=true;
StopSlow(TARGET_ALL);
loop(10){ yield; }
Obj_Delete(objEnemy);
loop(40){ yield; }
		CloseScript(GetOwnScriptID());
		return;
}
