//#TouhouDanmakufu
#ScriptVersion[3]
#Title["MageS3_H"]
#Text["Bullets"]
let white = GetCurrentScriptDirectory~"sprites/white.png";

#include"./lib/SHOT_REPLACE/shot_replace.dnh"
#include"./Mageinclude.txt"
let color2 = 1;
let color = 0;
let objEnemy;
let duration = 0;
let stopstuff = false;
let Patchy = GetCurrentScriptDirectory~"sprites/patchy.png";
let angle = 0;
let angle2 = 0;
let one = 1;
let type = 0;
let animframe = 0;
let frame = 0;
let objlasers = [];
let GetSpeedX = 0;
let variable = 0;
let thunder = GetCurrentScriptDirectory~"sfx/thunder.wav";
let siren = GetCurrentScriptDirectory~"sfx/se_alert.mp3";
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(timeslow);
	LoadSound(shot4);
	LoadSound(slash);
	LoadSound(thunder);
	LoadSound(charge2);
	LoadSound(ping);
	LoadSound(counterspell);
	LoadSound(arcanecast);
	LoadSound(arcaneexplosion);
	LoadSound(siren);
	
	LoadSound(cardcall);
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	Invuln(420, 17.5, 25);
	//BottomEnemyMarker(objEnemy, objEnemy);
	
	ObjPrim_SetTexture(objEnemy, Patchy); 
	ObjSprite2D_SetSourceRect(objEnemy, 0, 0, 128, 128);
	ascent(i in 0..3){
	ObjAnim_AddFrameA2(objEnemy, ANIM_IDLE, 0, 128*i, 128, 128*i+128, 0, 0);
	}
	ascent(i in 0..2){
	ObjAnim_AddFrameA2(objEnemy, ANIM_IDLE, 128, 128*i, 256, 128*i+128, 0, 0);
	}
	ascent(x in 0..2){ObjAnim_AddFrameA2(objEnemy, ANIM_MOVE_LEFT, 256, 128*x, 256+128, 128*x+128, 0, 0);}
	descent(x in 0..2){
	ObjAnim_AddFrameA2(objEnemy, ANIM_MOVEEND_LEFT, 256, 128*x, 256+128, 128*x+128, 0, 0);
	}
	
	ascent(x in 2..4){ObjAnim_AddFrameA2(objEnemy, ANIM_MOVE_RIGHT, 256, 128*x, 256+128, 128*x+128, 0, 0);}
	descent(x in 2..4){
	ObjAnim_AddFrameA2(objEnemy, ANIM_MOVEEND_RIGHT, 256, 128*x, 256+128, 128*x+128, 0, 0);
	}
	
	
	ascent(x in 0..3){ObjAnim_AddFrameA2(objEnemy, ANIM_ATTACK, 128*3, 128*x, 128*4, 128*x+128, 0, 0);}
	descent(x in 0..3){ObjAnim_AddFrameA2(objEnemy, ANIM_ATTACKEND, 128*3, 128*x, 128*4, 128*x+128, 0, 0);}
	
	ObjAnim_SetSpeedA1(objEnemy, ANIM_IDLE, 14);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_MOVE_LEFT, 6);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_MOVE_RIGHT, 6);
	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, 2);
	ObjAnim_StartAnimationA2(objEnemy, 0, 0);
	ObjSprite2D_SetDestCenter(objEnemy); 
	
	
	
LoadTexture(white);
	
	delayedstart;
	SetShotAutoDeleteClip(30, 50, 30, 30);
	
	
}



task delayedstart{
ObjMove_SetDestAtFrame(objEnemy, GetCenterX, 120, 60);
wait(60);

ObjEnemyBossScene_StartSpell(objScene);
ObjEnemyBossScene_SetSpellTimer(objScene, 60*57);
ObjAnim_SetAttackA2(objEnemy, 90);
PlaySE(cardcall);
ObjCutin_SetSpellcardS4("Eternal Force &quot;Arcane Explosion&quot;", kogasacutin, "AYA", 255, 60, 255);
ObjCutin_LaunchS3(AYA, kogasacutin, "Hard");
wait(60);
Aura(objEnemy, Patchy, 255, 90, 255);
attack1;
ChangeToAttack;
}


task ChangeToAttack{
wait(90);
ascent(x in 0..3){ ObjAnim_ReplaceFrameA2(objEnemy, ANIM_ATTACK, x, 128*5, 128*x, 128*6, 128*x+128, 0, 0);}
	descent(x in 0..3){ ObjAnim_ReplaceFrameA2(objEnemy,  ANIM_ATTACKEND, 2-x, 128*5, 128*x, 128*6, 128*x+128, 0, 0);}
	}

@MainLoop{
	
	
	SetCommonData("kogasax", ObjMove_GetX(objEnemy));
	SetCommonData("kogasay", ObjMove_GetY(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));
}

function castbar(seconds, spell, sound, size, width){


PlaySE(sound);
let alpha = 255;
let angle = 0;
let angle2 = rand(0, 360);
let numcasts = 0;
let bulletsvar = false;
let green = 0;
let constx = rand(5.5, 9.5);
let string = "Casting:" ~ spell;
let variable = 180;
let obj = ObjPrim_Create(OBJ_SPRITE_2D);
Obj_SetRenderPriority(obj, 0.9);
ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"tricky_system/img/Default_System.png");
ObjSprite2D_SetSourceRect(obj, 1, 1, 127, 7);
ObjSprite2D_SetDestRect(obj, GetStgFrameWidth+60, GetStgFrameHeight/2+65, GetStgFrameWidth+70, GetStgFrameHeight/2+65);
ObjRender_SetColor(obj, 255, 165, 0);
ObjRender_SetAlpha(obj, 255);
ObjMove_SetX(obj, GetStgFrameWidth/2);
ObjMove_SetY(obj, GetStgFrameHeight/2);
ObjRender_SetScaleXYZ(obj, 1, 1, 1);




let objText = ObjText_Create();
ObjText_SetFontType(objText, "Corbel");
	ObjText_SetText(objText, spell);
	ObjText_SetFontSize(objText, size);
	ObjText_SetFontBold(objText, true);
	ObjText_SetHorizontalAlignment(objText, ALIGNMENT_CENTER);
	ObjText_SetMaxWidth(objText, width);
	ObjText_SetFontColorTop(objText, 224, 255, 255);
	ObjText_SetFontColorBottom(objText, 128, 128, 255);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText,0, 0, 0);
	ObjText_SetFontBorderWidth(objText, 1);

	Obj_SetRenderPriority(objText, 0.9);
	ObjRender_SetX(objText, GetStgFrameWidth/2+225);
	ObjRender_SetY(objText, GetStgFrameHeight/2+25);

	task Indicator(x, y, seconds) {
	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 = 0.25;
	let alpha = 255;
	let time = 0;
	loop(seconds) {
	time++;
		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/seconds;
		if(time>=60){
		alpha-=255/(seconds-60);
		}
		ObjRender_SetAlpha(obj,alpha);
		wait(1);
		if(Obj_IsDeleted(objEnemy) || (GetVirtualKeyState(VK_USER1)==KEY_PUSH && GetCommonData("interruptavail", false)==true && GetPlayerState==STATE_NORMAL) ){ Obj_Delete(obj); }
	}
	loop(10){ CreateDNHShotA1(x, y, 1, angle, PURPLE50, 10); angle+=360/10; }
	Obj_Delete(obj);

}


	
loop(seconds){
variable-=180/seconds;
ObjSprite2D_SetDestRect(obj, GetStgFrameWidth+60, GetStgFrameHeight/2+(180+65), GetStgFrameWidth+70, (GetStgFrameHeight/2+65)+variable);
if(GetVirtualKeyState(VK_USER1)==KEY_PUSH && GetCommonData("interruptavail", false)==true  && GetPlayerState==STATE_NORMAL){  
ObjRender_SetColor(obj, 0, 255, 0);
PlaySE(counterspell);
StopSound(sound);
wait2(15);
loop(30){
alpha-=255/30;
ObjRender_SetAlpha(objText, alpha);
ObjRender_SetAlpha(obj, alpha);
yield;
}
return true;
 Obj_Delete(obj); break; }
wait2(1);
}
StopSound(sound);
ObjRender_SetColor(obj, 255, 0, 0);
if(popped!=true){
if(sound == arcanecast){ PlaySE(arcaneblast); }
if(ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID, INFO_TIMER)<=50){
bulletsvar = true;
}
else{ bulletsvar = false; }
if(spell == "Arcane[r]Explosion"){ knockback(constx, bulletsvar); }
if(spell == "Arcane Power"){ }

}
wait2(15);
loop(30){
alpha-=255/30;
ObjRender_SetAlpha(objText, alpha);
ObjRender_SetAlpha(obj, alpha);
wait2(1);
}
return false;
Obj_Delete(obj);
function wait2(w){ loop(w){ if (popped==true || stopstuff==true){ StopSound(sound); Obj_Delete(objText); Obj_Delete(obj); } yield; } }
}

task knockback(const, bullets){
let one = 1;
let movangle = 0;
let timevar = 0;
let timestring;
let objText = ObjText_Create();
	ObjText_SetFontType(objText, "Corbel");
	ObjText_SetText(objText, "Knockback!");
	ObjText_SetFontSize(objText, 18);
	ObjText_SetFontBold(objText, true);
	ObjText_SetHorizontalAlignment(objText, ALIGNMENT_CENTER);
	ObjText_SetMaxWidth(objText, 80);
	ObjText_SetFontColorTop(objText, 224, 255, 255);
	ObjText_SetFontColorBottom(objText, 128, 128, 255);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText,0, 0, 0);
	ObjText_SetFontBorderWidth(objText, 1);

	Obj_SetRenderPriority(objText, 0.9);
	ObjRender_SetX(objText, GetPlayerX-15);
	ObjRender_SetY(objText, GetPlayerY-45);
	timestring = rtos("0.00", (145-timevar)/145) ~"s";
	ObjText_SetText(objText, "Knockback!" ~ timestring);
	if(ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID, INFO_TIMER)<=50){
	Obj_Delete(objText);
	}
loop(4){
if(popped==true){ break; }
one*=-1;
	angle+=360/15/3;
	//angle+=rand(-20, 20);
	ascent(i in 0..10){
	loop(16-round(i/1.75)){
	
	if(bullets==true){
	
	if(i==9){
	CreateDNHShotA2(GetEnemyX, GetEnemyY, 5, angle+((i)*const*one), -0.25, 0.5+((i/5)^1.75), PURPLE12, 10);
	}
	else{
	CreateShotA2(GetEnemyX, GetEnemyY, 5, angle+((i)*const*one), -0.25, 0.5+((i/5)^1.75), PURPLE12, 10);
	}
	angle+=360/(16-round(i/1.75));
	}
	}
	
	}
	
PlaySE(arcaneexplosion);
TExpan2(GetEnemyX, GetEnemyY, 6, 255, 0, 255, 80);
loop(25){
timestring = rtos("0.00", (145-timevar)/145) ~"s";
ObjText_SetText(objText, "Pushback!" ~ timestring);
movangle = atan2(GetPlayerY-GetEnemyY, GetPlayerX-GetEnemyX);
ObjMove_SetPosition(GetPlayerObjectID, GetPlayerX+1.5*cos(movangle), GetPlayerY+1.5*sin(movangle));
ObjRender_SetX(objText, GetPlayerX-10);
ObjRender_SetY(objText, GetPlayerY-45);
if(popped==true){ Obj_Delete(objText); break; }
if(GetPlayerState!=STATE_NORMAL){ Obj_Delete(objText); }
timevar++;
wait(1);
}

}

loop(45){
timestring = rtos("0.00", (145-timevar)/145) ~"s";
ObjText_SetText(objText, "Pushback!" ~ timestring);
ObjRender_SetX(objText, GetPlayerX-10);
ObjRender_SetY(objText, GetPlayerY-45);
movangle = atan2(GetPlayerY-GetEnemyY, GetPlayerX-GetEnemyX);
ObjMove_SetPosition(GetPlayerObjectID, GetPlayerX+1.5*cos(movangle), GetPlayerY+1.5*sin(movangle));
if(popped==true){ Obj_Delete(objText); break; }
if(GetPlayerState!=STATE_NORMAL){ Obj_Delete(objText); }
timevar++;
wait(1);
}
Obj_Delete(objText);
}

function TExpan2(px,py,v,r,g,b,alpha){
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	let img=GetCurrentScriptDirectory~"sprites/charge.png";
	let scale = 0;
	let time = 0;
	
	let malpha = alpha;
	ObjPrim_SetTexture(obj,img);
	ObjRender_SetAlpha(obj, alpha);
	ObjSprite2D_SetSourceRect(obj,0,0,128,128);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj,px,py,0);
	ObjRender_SetColor(obj,r,g,b);
	Obj_SetRenderPriorityI(obj,55);
	Expan;
		task Expan{
		loop{
			ObjRender_SetAlpha(obj,malpha);
			scale += v/60;
			ObjRender_SetScaleXYZ(obj,scale,scale,0);
			yield;
			time++;
			if(popped==true){
			Obj_Delete(obj);
			}
			if(time>90){ malpha-=alpha/30; ObjRender_SetAlpha(obj,malpha); }
			if(time>=120){ Obj_Delete(obj); }
			
			}
		}
	}
	
task attack1{	
wait(120);
let x = 30;
let numcasts = 0;
let one = 1;
let angle2 = 0;
let color = 0;
let angle3 = 0;
loop{

numcasts+=4/6;
ObjAnim_SetAttackA1(objEnemy, true);
if(castbar(x, "Arcane[r]Explosion", arcanecast, 16, 70) == true){
ObjAnim_SetAttackA1(objEnemy, false);
wait(30);
}
else{

wait(90);
ObjAnim_SetAttackA1(objEnemy, false);
wait(30);
x = 90;

ExMoveBarely;
wait(60);
}



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


task sirens{
loop(2){
PlaySE(siren);
wait(60);
}
}

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