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

#include"./lib/SHOT_REPLACE/shot_replace.dnh"
#include"./Mageinclude.txt"
let color2 = 1;
let fireattack = false;
let arcaneattack = false;
let frostattack = false;
let color = 0;
let objEnemy;
let anglex = 0;
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(thunder);
	LoadSound(charge2);
	LoadSound(ping);
	LoadSound(counterspell);
	LoadSound(firecast);
	LoadSound(frostcast);
	LoadSound(arcanecast);
	LoadSound(arcaneblast);
	LoadSound(skillcall);
	LoadSound(siren);
	LoadSound(fireball);
	LoadSound(cardcall);
	LoadSound(flamestrike);
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	Invuln(360, 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, 50, 30, 30);
	
	
}



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

PlaySE(charge2);
TCharge01(objEnemy, 3, 45, 255, 255, 255);
wait(60);
ObjEnemyBossScene_StartSpell(objScene);
ObjEnemyBossScene_SetSpellTimer(objScene, 60*120);
ObjAnim_SetAttackA2(objEnemy, 90);
PlaySE(cardcall);
ObjCutin_SetSpellcardS4("Illusion of a Mage &quot;Motley Magic&quot;", kogasacutin, "AYA", 255, 255, 255);
ObjCutin_LaunchS3(AYA, kogasacutin, "Hard");
wait(60);
//Aura(objEnemy, Patchy, 255, 255, 255);
wait(60);
PlaySE(skillcall);
ObjCutin_SetSpellcardS4("Mirror Image -Fire-", kogasacutin, "MOKOU", 255, 59, 0);
CreateMirror(0);
wait(60);
PlaySE(skillcall);
ObjCutin_SetSpellcardS4("Mirror Image -Frost-", kogasacutin, "MOKOU",  100, 149, 237);
CreateMirror(1);
wait(60);
PlaySE(skillcall);
ObjCutin_SetSpellcardS4("Mirror Image -Arcane-", kogasacutin, "MOKOU",   255, 60, 255);
CreateMirror(2);
wait(60);



attack1;
ChangeToAttack;


}

task CreateMirror(type){
let obj = ObjEnemy_Create(OBJ_ENEMY);
ObjEnemy_SetLife(obj, 250);
ObjMove_SetPosition(obj, GetEnemyX, GetEnemyY);
ObjEnemy_SetDamageRate(obj, 100, 5);
ObjPrim_SetTexture(obj, Patchy);
ObjSprite2D_SetSourceRect(obj, 0, 0, 128, 128);
ObjSprite2D_SetDestCenter(obj);
ObjRender_SetScaleXYZ(obj, 1, 1, 1);
ObjSprite2D_SetSourceRect(obj, 0, 0, 128, 128);
	ascent(i in 0..3){
	ObjAnim_AddFrameA2(obj, ANIM_IDLE, 0, 128*i, 128, 128*i+128, 0, 0);
	}
	ascent(i in 0..2){
	ObjAnim_AddFrameA2(obj, ANIM_IDLE, 128, 128*i, 256, 128*i+128, 0, 0);
	}
	ascent(x in 0..2){ObjAnim_AddFrameA2(obj, ANIM_MOVE_LEFT, 256, 128*x, 256+128, 128*x+128, 0, 0);}
	descent(x in 0..2){
	ObjAnim_AddFrameA2(obj, ANIM_MOVEEND_LEFT, 256, 128*x, 256+128, 128*x+128, 0, 0);
	}
	
	ascent(x in 2..4){ObjAnim_AddFrameA2(obj, ANIM_MOVE_RIGHT, 256, 128*x, 256+128, 128*x+128, 0, 0);}
	descent(x in 2..4){
	ObjAnim_AddFrameA2(obj, ANIM_MOVEEND_RIGHT, 256, 128*x, 256+128, 128*x+128, 0, 0);
	}
	
	
	ascent(x in 0..3){ObjAnim_AddFrameA2(obj, ANIM_ATTACK, 128*5, 128*x, 128*6, 128*x+128, 0, 0);}
	descent(x in 0..3){ObjAnim_AddFrameA2(obj, ANIM_ATTACKEND, 128*5, 128*x, 128*6, 128*x+128, 0, 0);}
	
	ObjAnim_SetSpeedA1(obj, ANIM_IDLE, 14);
	ObjAnim_SetSpeedA1(obj, ANIM_MOVE_LEFT, 6);
	ObjAnim_SetSpeedA1(obj, ANIM_MOVE_RIGHT, 6);
	ObjAnim_SetSpeedA1(obj, ANIM_ATTACK, 8);
	ObjAnim_SetLoopPointA1(obj, ANIM_MOVE_RIGHT, 1);
	ObjAnim_SetLoopPointA1(obj, ANIM_MOVE_LEFT, 1);
	ObjAnim_SetLoopPointA1(obj, ANIM_ATTACK, 2);
	ObjAnim_StartAnimationA2(obj, 0, 0);
	ObjRender_SetAlpha(obj, 200);
	
alternative(type)
case(0){
Aura2(obj,Patchy, 255, 90, 90);
}
case(1){
Aura2(obj, Patchy, 30, 30, 255);
}
case(2){
Aura2(obj, Patchy, 255, 90, 255);
}

alternative(type)
case(0){
ObjMove_SetDestAtFrame(obj, GetEnemyX-(GetCenterX/2)-20, GetEnemyY+90, 60);
}
case(1){
ObjMove_SetDestAtFrame(obj, GetEnemyX, GetEnemyY+60, 60);
}
case(2){
ObjMove_SetDestAtFrame(obj, GetEnemyX+(GetCenterX/2)+20, GetEnemyY+90, 60);
}

ObjEnemy_Regist(obj);
wait(60);



while(!Obj_IsDeleted(obj)){

alternative(type)
case(0){
	if(fireattack == true){ ObjAnim_SetAttackA1(obj, true); }
	else{
	ObjAnim_SetAttackA1(obj, false);
	}
}
case(1){
if(frostattack == true){ ObjAnim_SetAttackA1(obj, true); }
	else{
	ObjAnim_SetAttackA1(obj, false);
	}
}
case(2){
if(arcaneattack == true){ ObjAnim_SetAttackA1(obj, true); }
	else{
	ObjAnim_SetAttackA1(obj, false);
	}
}


yield;
}

}
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 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);




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);

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 == "Meteor"){ PlaySE(fireball);
	
		CreateMeteor(GetPlayerX, -60, 3.5, rand(85, 95));
	}
	
		task CreateMeteor(x, y, v, ang){
			let shot = CreateLooseLaserA1(x, y, v, ang, 160, 120, SP01, 0);
			let flamestrike = false; 
			Obj_SetRenderPriorityI(shot, Obj_GetRenderPriorityI(shot)+1);
			let time = 0;
			ObjShot_SetAutoDelete(shot, false);
			while(!Obj_IsDeleted(shot) && popped==false){
				if(time==45){
					PlaySE(firecast);
					}
					if(ObjMove_GetY(shot)>GetClipMaxY+60 && flamestrike == false){
						flamestrike = true;
						
						DelayClouds(ObjMove_GetX(shot), GetClipMaxY, 180);
					
						}
						if(ObjMove_GetY(shot)>GetClipMaxY+150){
						Obj_Delete(shot);
						}
				time++;
				yield;
				}
				Obj_Delete(shot);
				}
				
			task DelayClouds(x, y, duration){
			let time = 0;
			let xspeed = 0;
			let xaccel = 0;
				loop(duration){
					loop(3){
						let obj = CreateShotA1(x+rand(-40, 40), y+rand(-10, 10), 0, 0, RED03, 20);
						ObjShot_SetDeleteFrame(obj, 0);
						}
						time++;
						if(time == 60){
						StopSound(firecast);
						PlaySE(flamestrike);
						}
						if(time>60){
							xspeed = rand(-4.5, 4.5);
							xaccel = -xspeed/150;
							CreateShotB2(x+rand(-20, 20), GetClipMaxY, xspeed, rand(-5, -7), xaccel, rand(0.03, 0.045), 0, rand(2.5, 3.5), SP01, 0);
							if(time%2==0){
							xspeed = rand(-4.5, 4.5);
							xaccel = -xspeed/150;
							CreateShotB2(x+rand(-20, 20), GetClipMaxY, xspeed, rand(-5, -7), xaccel, rand(0.03, 0.045), 0, rand(2.5, 3.5), SP01, 0);
							}
							}
						yield;
						if(popped==true){ break; }
						}
}
			
	
if(spell == "Comet Storm"){ PlaySE(ping); 
	loop(20){
		CreateDNHShotA1(GetEnemyX, GetEnemyY+60, 2, angle, BLUE01, 10);  
		angle+=360/20;
		}
	}
if(spell == "Arcane Orb"){ PlaySE(arcaneblast); 
loop(20){
		CreateDNHShotA1(GetEnemyX+(GetCenterX/2)+20, GetEnemyY+90, 2, angle, PURPLE01, 10);  
		angle+=360/20;
		}
	}
}
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(180);

let numcasts = -1;
let one = 1;
let doublerandom = 0;
let lastnum = -1;
loop{


numcasts++;
if(numcasts>2){ numcasts = 0; }
if(numcasts == 0){ fireattack = true; 

	if(castbar(90, "Meteor", firecast, 20, 60) == true){ fireattack = false; wait(60);}
		else{ fireattack = false; wait(240); }


}
if(numcasts == 1){ frostattack = true; 

	if(castbar(90, "Comet Storm", frostcast, 16, 56) == true){ frostattack = false; wait(60);}
		else{ frostattack = false; wait(15); } 
		
		
		
	}
if(numcasts == 2){ arcaneattack = true;

	if(castbar(90, "Arcane Orb", arcanecast, 16, 60) == true){ arcaneattack = false; wait(60);}
		else{ arcaneattack = false; wait(15); } 
		
	
 }
lastnum = numcasts;
fireattack = false; frostattack = false; arcaneattack = false;

yield;
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;
}
