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

#include"./lib/SHOT_REPLACE/shot_replace.dnh"
#include"./Mageinclude.txt"
let color = 0;
let objEnemy;
let phase = 0;
let anglex = 0;
let duration = 0;
let offset = 1;
let stopstuff = false;
let stopbowap = 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));	
	}
	case(EV_REQUEST_IS_DURABLE_SPELL){
	SetScriptResult(true);
	}
}
let Patchy = GetCurrentScriptDirectory~"sprites/patchy.png";
@Initialize{
	//Resource Loading
	shotinit;
	LoadSound(shot);
	LoadSound(shot2);
	LoadSound(shot3);
	LoadSound(iceblock);
	LoadSound(timeslow);
	LoadSound(shot4);
	LoadSound(slash);
	LoadSound(thunder);
	LoadSound(charge2);
	LoadSound(ping);
	LoadSound(counterspell);
	LoadSound(stealth);
	LoadSound(firecast);
	LoadSound(siren);
	LoadSound(fireball);
	LoadSound(cardcall);
	LoadSound(skillcall);
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	Invuln(150, 60, 0);
	//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_SetDestAtWeight(objEnemy, GetCenterX, 120, 12, 4);  
wait(120);
monitorhp;
attackfiller;
ChangeToAttack;


}

task monitorhp{
loop{

if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE)<1 && phase == 0){ ObjEnemy_SetLife(objEnemy, 10); phase = 1; stopbowap = true; ObjEnemy_SetDamageRate(objEnemy, 0, 0); NotifyEventAll(EV_USER_SYSTEM + 1, 0);
ObjAnim_SetAttackA1(objEnemy, false); delayedstart2;
wait(120);

}
yield;
}
}

task filllifebar{
let colorvar = 0;
loop(60*4){ ObjEnemy_AddLife(objEnemy, 999/(60*4)); yield; }
loop(60){ colorvar++;
ObjRender_SetColor(objEnemy, 255, 255-(colorvar*1.29166666667), 255-(colorvar*1.29166666667)); yield;
//255, 100, 100
}
loop(60*15){ ObjEnemy_AddLife(objEnemy, -999/(60*15)); yield; }
phase = 2;
NotifyEventAll(EV_USER_SYSTEM + 1, 0);
ObjRender_SetColor(objEnemy, 255, 255, 255);
SetCommonData("turnoffaura", true);
wait(1);
SetCommonData("turnoffaura", false);

break;
}

task attackfiller{
let angle = 0;
let a = 9;
let color = 0;
let radius = 40;
ObjAnim_SetAttackA1(objEnemy, true);
loop{
if(stopbowap==true){ break; }

	loop(9){
	CreateDNHShotA1(GetEnemyX+radius*cos(angle), GetEnemyY+radius*sin(angle), 2.75, angle, [RED04, PURPLE04, BLUE04][color%3], 10);
	angle+=360/9;
	}
	color++;
angle+=a*1.5*sin(a);
a+=0.375;
wait(3);
}
}


task delayedstart2{
ObjMove_SetDestAtWeight(objEnemy, GetCenterX, 90, 12, 4);  
wait(60);
ChangeToSpell;
ObjEnemyBossScene_StartSpell(objScene);
ObjEnemyBossScene_SetSpellTimer(objScene, 60*79);
ObjAnim_SetAttackA2(objEnemy, 90);
PlaySE(cardcall);
ObjCutin_SetSpellcardS4("&quot;Survival Tactics&quot;", kogasacutin, "AYA", 255, 255, 255);
ObjCutin_LaunchS3(AYA, kogasacutin, "Hard");
wait(90);
PlaySE(charge2);
TCharge01(objEnemy, 3, 45, 255, 0, 0);
wait(60);
ChangeToAttack;
PlaySE(skillcall);
Aura(objEnemy, Patchy, 255, 90, 90);

ObjCutin_SetSpellcardS4("Healing Flames -Cauterize- ", kogasacutin, "MOKOU", 255, 59, 0);
filllifebar;
/*
ObjCutin_SetSpellcardS4("Ice Block  ", kogasacutin, "MOKOU",  100, 149, 237);
ObjCutin_SetSpellcardS4("Greater Invisibility  ", kogasacutin, "MOKOU", 255, 60, 255);
*/
attack1;
firerain;


}

task delayedstart3{
let colorvar = 0;
ObjMove_SetDestAtFrame(objEnemy, GetCenterX, GetCenterY, 60);
wait(60);
PlaySE(charge2);
TCharge01(objEnemy, 3, 45, 0, 0, 255);
wait(60);
ChangeToAttack;
PlaySE(skillcall);
Aura(objEnemy, Patchy, 90, 90, 255);

ObjCutin_SetSpellcardS4("Cryostasis -Ice Block-", kogasacutin, "MOKOU", 100, 149, 237);
loop(90){ colorvar++;
ObjRender_SetColor(objEnemy, 255-(colorvar*1.29166666667), 255-(colorvar*1.29166666667), 255); yield;
//255, 100, 100
}
PlaySE(iceblock);
iceblok;

/*
ObjCutin_SetSpellcardS4("Ice Block  ", kogasacutin, "MOKOU",  100, 149, 237);
ObjCutin_SetSpellcardS4("Greater Invisibility  ", kogasacutin, "MOKOU", 255, 60, 255);
*/

}

task delayedstart4{
NotifyEventAll(EV_USER_SYSTEM + 1, 0);
let colorvar = 0;
let alpha = 0;
SetCommonData("turnoffaura", true);
wait(1);
SetCommonData("turnoffaura", false);
ObjMove_SetDestAtFrame(objEnemy, GetCenterX, GetCenterY, 60);
wait(69);
PlaySE(charge2);
TCharge01(objEnemy, 3, 45, 0, 0, 255);
wait(60);
ChangeToAttack;
PlaySE(skillcall);
Aura(objEnemy, Patchy, 255, 60, 255);

ObjCutin_SetSpellcardS4("Elsewhere -Greater Invisibility-", kogasacutin, "MOKOU", 255, 60, 255);
loop(90){ colorvar++;
alpha-=255/90;
SetShotAutoDeleteClip(50, 50, 50, 50);
ObjRender_SetAlpha(objEnemy, alpha);
ObjRender_SetColor(objEnemy, 255, 255-(colorvar*2.16666666667), 255); yield;
//255, 100, 100
}
PlaySE(stealth);
//iceblok;
wait(30);
loop(5){
	ascent(h in -3..4){
		ascent(w in -3..4){
Indicator2(GetCenterX+(w*100)+(offset*25), GetCenterY+(h*100), 120);
	}
	offset*=-1;
	}
	wait(180);
}

}

task invis{
let sinvar = 0;
let shotvar = 0;
let angle = 90;
let angle2 = 0;
let angle3 = GetAngleToPlayer(objEnemy)+(360/4/2);
while(phase==2){

if(ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID, INFO_TIMER)<30){
phase = 3;
delayedstart4;
}

if(shotvar%25==0){
	loop(4){
ShootShape(GetEnemyX, GetEnemyY, 2+shotvar/2500, 0, 2+shotvar/2500, 10+(shotvar/350), angle*2, angle, 3, 3, BLUE23, 10);
angle+=360/4;
	}
	angle+=12;
}
loop(5){
let obj = CreateShotA1(GetEnemyX+rand(-55, 55), GetEnemyY+rand(-55, 55), 0, rand(0, 360), BLUE51, rand(45, 60));
ObjShot_SetDeleteFrame(obj, 0);
}
if(ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID, INFO_TIMER)<45){
if(shotvar%55==0){
	loop(6){
		CreateDNHShotA1(GetEnemyX, GetEnemyY, 1.5, -angle3, BLUE02, 10);
		angle3+=360/6;
		}
		PlaySE(ping);
		}
		}

if(shotvar%7==0){
ascent(i in 0..2){
	loop(3){
CreateDNHShotA1(GetEnemyX, GetEnemyY, 2+(i/4)+(shotvar/2500), angle3, BLUE50, 10);
angle3+=360/3;
}
}
angle3-=min(4.25, 1.95754575454550+(shotvar/200));}
wait(1);


shotvar++;

}
let colorvar = 0;
break;
}


@MainLoop{
	
	
	SetCommonData("kogasax", ObjMove_GetX(objEnemy));
	SetCommonData("kogasay", ObjMove_GetY(objEnemy));
	if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) <=0 && stopstuff==false && stopbowap==true){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 firerain{
let sinvar = 0;
while(phase==1){
ascent(i in -4..5){
CreateDNHShotA1(GetStgFrameWidth/2+(GetStgFrameWidth/2)*sin(sinvar)+(i*125), 0, 2.35+(sinvar/2000), 90+8*sin(sinvar), SP01, 20);
}
sinvar+=15;
wait(10-floor(sinvar/600));
}
}

function ShootShape(spawnX, spawnY, v, vacc, v2, rad, angleOff, angleShoot, num, bside, graphic, delay) {
let angle = angleOff;
        ascent(i in 0..num) {
                let sx = spawnX+cos(angle+i*360/num); let sy = spawnY+sin(angle+i*360/num); let sxN = spawnX+cos(angle+(i+1)*360/num); let syN = spawnY+sin(angle+(i+1)*360/num);
                let tempAng = atan2(sy-spawnY,sx-spawnX);
                let tempDist = ((sx-spawnX)^2+(sy-spawnY)^2)^0.5;
                CreateDNHShotA2(spawnX+rad*cos(tempAng), spawnY+rad*sin(tempAng), v, angleShoot, vacc, v2, graphic, delay);
                ascent(j in 0..bside) {
                        let toAngle = atan2(syN-sy,sxN-sx); let toDist = (((sxN-sx)^2+(syN-sy)^2)^0.5); let sx2 = sx+toDist/bside*j*cos(toAngle); let sy2 = sy+toDist/bside*j*sin(toAngle);
                        let tempDist2 = ((sx2-spawnX)^2+(sy2-spawnY)^2)^0.5;
                        let tempAng2 = atan2(sy2-spawnY,sx2-spawnX);
                        CreateDNHShotA2(spawnX+rad*tempDist2*cos(tempAng2), spawnY+rad*tempDist2*sin(tempAng2), v, angleShoot, vacc, v2, graphic, delay);
                }
        }
}


task iceblok{
let sinvar = 0;
let shotvar = 0;
let angle = 90;
let angle2 = 0;
let angle3 = GetAngleToPlayer(objEnemy)+(360/4/2);
while(phase==2){

if(ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID, INFO_TIMER)<30){
phase = 3;
delayedstart4;
}

if(shotvar%25==0){
	loop(4){
ShootShape(GetEnemyX, GetEnemyY, 2+shotvar/2500, 0, 2+shotvar/2500, 10+(shotvar/350), angle*2, angle, 3, 3, BLUE23, 10);
angle+=360/4;
	}
	angle+=12;
}
loop(5){
let obj = CreateShotA1(GetEnemyX+rand(-55, 55), GetEnemyY+rand(-55, 55), 0, rand(0, 360), BLUE03, rand(45, 60));
ObjShot_SetDeleteFrame(obj, 0);
}
if(ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID, INFO_TIMER)<45){
if(shotvar%55==0){
	loop(6){
		CreateDNHShotA1(GetEnemyX, GetEnemyY, 1.5, -angle3, BLUE02, 10);
		angle3+=360/6;
		}
		PlaySE(ping);
		}
		}

if(shotvar%7==0){
ascent(i in 0..2){
	loop(3){
CreateDNHShotA1(GetEnemyX, GetEnemyY, 2+(i/4)+(shotvar/2500), angle3, BLUE50, 10);
angle3+=360/3;
}
}
angle3-=min(4.25, 1.95754575454550+(shotvar/200));}
wait(1);


shotvar++;

}
let colorvar = 0;
break;
}

task Indicator2(x, y, seconds) {
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	let angle2 = 90;
	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);
	ObjRender_SetPosition(obj,x,y,0);
	
	let angle = 270;
	let scale = 0.25;
	
	loop(seconds) {
		
		//ObjRender_SetColor(obj, 255, 0, 255);
		ObjRender_SetAngleXYZ(obj,0,0,angle);
		ObjRender_SetScaleXYZ(obj,scale,scale,scale);
		angle+=270/seconds;
		scale-=0.25/seconds;
		wait(1);
		if(Obj_IsDeleted(objEnemy) || (GetVirtualKeyState(VK_USER1)==KEY_PUSH && GetCommonData("interruptavail", false)==true && GetPlayerState==STATE_NORMAL) ){ Obj_Delete(obj); }
	}
	loop(6){
	CreateDNHShotA2(x, y, 0, angle2, 0.025, 1, PURPLE01, 30); 
	angle2+=360/6;
	}
	Obj_Delete(obj);

}


task Indicator(x, y, seconds) {
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	let angle2 = 0;
	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.75;
	
	loop(seconds) {
		ObjRender_SetPosition(obj,GetPlayerX,GetPlayerY,0);
		//ObjRender_SetColor(obj, 255, 0, 255);
		ObjRender_SetAngleXYZ(obj,0,0,angle);
		ObjRender_SetScaleXYZ(obj,scale,scale,scale);
		angle+=270/seconds;
		scale-=0.75/seconds;
		wait(1);
		if(Obj_IsDeleted(objEnemy) || (GetVirtualKeyState(VK_USER1)==KEY_PUSH && GetCommonData("interruptavail", false)==true && GetPlayerState==STATE_NORMAL) ){ Obj_Delete(obj); }
	}
	Obj_Delete(obj);

}


task attack1{
wait(60);	
wait(60*4);
let numcasts = 0;
let one = 1;
let angle2 = 0;
let color = 0;
let angle3 = 0;
let spd = 0.75;
PlaySE(skillcall);
Sun;
//CreateLooseLaserA1(GetCenterX, GetCenterY, 0, 400, 400, 400, RED12, 1);
ObjCutin_SetSpellcardS4("-Backfire-", kogasacutin, "MOKOU", 255, 59, 0);
Indicator(0, 0, 180);

loop{
ObjRender_SetAngleZ(objEnemy, angle2);
angle2+=angle3;
angle3+=0.1;
spd+=0.0005;
ObjMove_SetSpeed(objEnemy, spd);
ObjMove_SetAngle(objEnemy, GetAngleToPlayer(objEnemy));
if(phase==2){ObjMove_SetSpeed(objEnemy, 0);
ObjMove_SetAngle(objEnemy, 90); ObjRender_SetAngleZ(objEnemy, 0); delayedstart3; break;  }
yield;
	if(popped==true){break;}
	
	
}
}

task Sun{
let length = 100;
let obj = CreateStraightLaserA1(GetEnemyX, GetEnemyY, 0, length, length, 9999, RED51, 0);
ObjStLaser_SetSource(obj, false);
ObjShot_SetSpellResist(obj, true);
while(!Obj_IsDeleted(obj)){
if(phase!=1){ Obj_Delete(obj); }

ObjLaser_SetLength(obj, length);
ObjLaser_SetIntersectionWidth(obj, length);
ObjLaser_SetIntersectionWidth(obj, length*0.1);
ObjLaser_SetRenderWidth(obj, length);
ObjStLaser_SetAngle(obj, ObjStLaser_GetAngle(obj)+5);
ObjMove_SetPosition(obj, GetEnemyX-((length/2)*cos(ObjStLaser_GetAngle(obj))), GetEnemyY-((length/2)*sin(ObjStLaser_GetAngle(obj))));
//length+=0.025;
yield;
}
}

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


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 ChangeToSpell{
ascent(x in 0..3){ObjAnim_ReplaceFrameA2(objEnemy, ANIM_ATTACK, x, 128*3, 128*x, 128*4, 128*x+128, 0, 0); }
	descent(x in 0..3){ObjAnim_ReplaceFrameA2(objEnemy, ANIM_ATTACKEND, 2-x, 128*3, 128*x, 128*4, 128*x+128, 0, 0);}
	
	}
	


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