//#TouhouDanmakufu
#ScriptVersion[3]
#Title["MageS4_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 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));	
	}
}
let Patchy = GetCurrentScriptDirectory~"sprites/patchy.png";
@Initialize{
	//Resource Loading
	shotinit;
	LoadSound(shot);
	LoadSound(shot2);
	LoadSound(shot3);
	LoadSound(timeslow);
	LoadSound(shot4);
	LoadSound(slash);
	LoadSound(boom);
	LoadSound(flamestrike);
	LoadSound(charge2);
	LoadSound(ping);
	LoadSound(counterspell);
	LoadSound(firecast);
	LoadSound(dragonsbreath);
	LoadSound(fireball);
	SetCommonData("pausebeforebonus", true);
	LoadSound(cardcall);
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	Invuln(480, 20, 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, 200, 30, 30);
	
	
}



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

ObjEnemyBossScene_StartSpell(objScene);
ObjEnemyBossScene_SetSpellTimer(objScene, 60*61);
ObjAnim_SetAttackA2(objEnemy, 90);
PlaySE(cardcall);
ObjCutin_SetSpellcardS4("Enhanced Pyrotechnics &quot;Flamestrike&quot;", kogasacutin, "AYA", 255, 0, 0);
ObjCutin_LaunchS3(AYA, kogasacutin, "Hard");
wait(60);
Aura(objEnemy, Patchy, 255, 90, 90);
attack1;
wait(60);
ChangeToAttack;


}


task ChangeToAttack{

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);}
	}
	
	task ChangeToAttack2{
ascent(x in 0..2){ ObjAnim_ReplaceFrameA2(objEnemy, ANIM_ATTACK, x, 128*4, 128*x, 128*5, 128*x+128, 0, 0);}
ObjAnim_ReplaceFrameA2(objEnemy, ANIM_ATTACK, 2, 128*4, 128*1, 128*5, 128*1+128, 0, 0);
	descent(x in 0..2){ ObjAnim_ReplaceFrameA2(objEnemy, ANIM_ATTACKEND, 1-x, 128*4, 128*x, 128*5, 128*x+128, 0, 0);}
	ObjAnim_ReplaceFrameA2(objEnemy, ANIM_ATTACKEND, 0, 128*4, 128*1, 128*5, 128*1+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 green = 0;
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);

angle = angletoplayer(objEnemy);


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);
	
	if(spell == "Dragon's Breath"){ 
	ascent(i in -3..4){
 CreateLaser1(GetEnemyX, GetEnemyY, angle+(i*4.15), RED60, 500, 35, seconds);
 }
 }
 let curx = GetEnemyX;
 let cury = GetEnemyY;
 
 if(spell == "Flamestrike"){
 DelayClouds(GetEnemyX, GetClipMaxY, seconds+210);
 }
 

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(spell == "Flamestrike"){ PlaySE(flamestrike); spewup;  }

task spewup{

ascent(i in 0..210){
loop(1){
let xspeed = rand(-4.5, 4.5);
let xaccel = -xspeed/150;
CreateShotB2(curx+rand(-20, 20), GetClipMaxY, xspeed, rand(-5, -7), xaccel, rand(0.03, 0.045), 0, rand(2.5, 3.5), SP01, 0);
if(i%2==0){
xspeed = rand(-4.5, 4.5);
xaccel = -xspeed/150;
CreateShotB2(curx+rand(-20, 20), GetClipMaxY, xspeed, rand(-5, -7), xaccel, rand(0.03, 0.045), 0, rand(2.5, 3.5), SP01, 0);
}


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

if(spell == "Dragon's Breath"){ PlaySE(dragonsbreath); spew;}
task spew{
loop(45){
loop(2){
let obj = CreateShotA1(GetEnemyX, GetEnemyY, rand(5, 7), angle+rand(-15, 15), RED51, 0);
ObjRender_SetColor(obj, 255, 69+175, 175);
}
wait(1);
if(popped==true){ break; }
}
}


}
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 attack1{	
wait(120);
let numcasts = 0;
let angle2 = 0;
let x = 0;
let angle3 = 0;
loop{
ObjAnim_SetAttackA1(objEnemy, false);
PlaySE(charge2);
TCharge01(objEnemy, 3, 45, 255, 0, 0);
wait(30);
ChangeToAttack2;
wait(30);
ObjAnim_SetAttackA1(objEnemy, true);
if(castbar(120-x, "Flamestrike", firecast, 18, 90)==true){
castbar(60, "Flamestrike", firecast, 18, 90)
}
else{
wait(30);
}
ObjAnim_SetAttackA1(objEnemy, false);
numcasts = 0;
wait(30);
ExMove;

wait(60);
x = 30;
ChangeToAttack;

ObjAnim_SetAttackA1(objEnemy, true);
loop(3){
if(castbar(75, "Dragon's Breath", firecast, 16, 60) == true){
wait(10);
}
else{
wait(10);
}
}


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

task DelayClouds(x, y, duration){

function Wait2(f){ loop(f){ if(popped==true ||(GetVirtualKeyState(VK_USER1)==KEY_PUSH && GetCommonData("interruptavail", false)==true && GetPlayerState==STATE_NORMAL)){ break;} yield;  } }

loop(duration){
loop(3){
let obj = CreateShotA1(x+rand(-30, 30), y+rand(-10, 10), 0, 0, RED03, 20);
ObjShot_SetDeleteFrame(obj, 0);
}



Wait2(1);



}


}

task CreateLaser1(x, y, angle, graphic, length, width, delay){
let obj2 = ObjShot_Create(OBJ_STRAIGHT_LASER);
ObjMove_SetPosition(obj2, x, y);
ObjStLaser_SetAngle(obj2, angle);
ObjShot_SetGraphic(obj2, graphic);
ObjShot_SetDelay(obj2, delay+1);
ObjLaser_SetLength(obj2, length);
ObjLaser_SetRenderWidth(obj2, width);
ObjLaser_SetIntersectionWidth(obj2, 20);
ObjShot_SetSpellResist(obj2, true);
ObjStLaser_SetSource(obj2, false);
ObjLaser_SetInvalidLength(obj2, 10, 10);
ObjShot_Regist(obj2);
ObjMove_SetPosition(obj2, x, y);
Wait2(delay);
Obj_Delete(obj2);

function Wait2(f){ loop(f){ if(popped==true ||(GetVirtualKeyState(VK_USER1)==KEY_PUSH && GetCommonData("interruptavail", false)==true && GetPlayerState==STATE_NORMAL)){ Obj_Delete(obj2); } yield;  } }

}



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

task DestroyBoss{
SetCommonData("petals", false);
popped=true;
StopSlow(TARGET_ALL);
PlaySE(boom);
StartSlow(TARGET_ALL, 30);
ObjMove_SetDestAtWeight(objEnemy, ObjMove_GetX(objEnemy)+120, ObjMove_GetY(objEnemy)+rand(-20, 20), 20, 1);
loop(15){ TExpan(ObjMove_GetX(objEnemy)+rand(-35, 35), ObjMove_GetY(objEnemy)+rand(-25, 25), 8, 25, 224, 255, 255, 100); wait(5); }
PlaySE(boom);
TExpan(ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), 55, 120, 224, 255, 255, 255);
Obj_Delete(objEnemy);
loop(60){ yield; }
		CloseScript(GetOwnScriptID());
		StopSlow(TARGET_ALL);
		return;
}