//#TouhouDanmakufu
#ScriptVersion[3]
#Title["MageS5_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 frostroot = GetCurrentScriptDirectory~"sfx/frostroot.wav";
let frostrootbreak = GetCurrentScriptDirectory~"sfx/frostrootbreak.wav";
let objEnemy;
let duration = 0;
let stopstuff = false;
let frostnova = GetCurrentScriptDirectory~"sfx/frostnova.wav";
let angle = 90;
let angle2 = 0;
let one = 1;
let type = 0;
let Patchy = GetCurrentScriptDirectory~"sprites/patchy.png";
let animframe = 0;
let frame = 0;
let objlasers = [];
let GetSpeedX = 0;
let variable = 0;
let initspeed = GetPlayerSpeed;
let thunder = GetCurrentScriptDirectory~"sfx/thunder.wav";
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(frostnova);
	LoadSound(shot2);
	LoadSound(shot3);
	LoadSound(timeslow);
	LoadSound(charge);
	LoadSound(shot4);
	LoadSound(slash);
	LoadSound(thunder);
	LoadSound(boom);
	LoadSound(frostroot);
	LoadSound(frostrootbreak);
	LoadSound(charge2);
	LoadSound(frostslow);
	LoadSound(ping);
	LoadSound(counterspell);
	LoadSound(frostcast);
	LoadSound(fireball);
	LoadSound(cardcall);
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	Invuln(450, 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, 100, 30, 20);
	
	
}



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

ObjEnemyBossScene_StartSpell(objScene);
ObjEnemyBossScene_SetSpellTimer(objScene, 60*67);
ObjAnim_SetAttackA2(objEnemy, 90);
PlaySE(cardcall);
ObjCutin_SetSpellcardS4("Icebound &quot;Splitting Ice&quot;", kogasacutin, "AYA", 100, 149, 237);
ObjCutin_LaunchS3(AYA, kogasacutin, "Hard");
wait(60);
Aura(objEnemy, Patchy, 90, 90, 255);
attack1;
attackcharge;
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);}
	}

task CheckForFlip{
yield;
loop{
if(CheckDirectionAL1(objEnemy, "left")){
SetCommonData("flipsprite", true);
}
else if(CheckDirectionAL1(objEnemy, "right")){
SetCommonData("flipsprite", false);
}
if(GetCommonData("flipsprite", false)==true && CheckDirectionAL1(objEnemy, "none")){ ObjRender_SetScaleX(objEnemy, -1)}
else{ ObjRender_SetScaleX(objEnemy, 1); }


yield;
}
}


@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+230);
	ObjRender_SetY(objText, GetStgFrameHeight/2+25);

	if(sound == frostcast){ PlaySE(frostcast); }
	
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);
}

if(spell == "Ice Lance"){}
StopSound(sound);
ObjRender_SetColor(obj, 255, 0, 0);


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){ StopSound(sound); Obj_Delete(objText); Obj_Delete(obj); } yield; } }
}


	


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 attackcharge{
let bleh = 0;
let bloop = 1;
angle = 90;


	
	}


	task attack2{
let bleh = 0;
let bloop = 1;
let w = 0;

ascent(k in 2..17){
w = 0;
if(k>=6){
w = 1;
}

if(popped==true){ break; }
PlaySE(shot);
bloop = 1-bloop;
	bleh = (360/8/3)*bloop;
	
		ascent(i in -26-round((k+1)/5)..27+round((k+1)/5)){
let obj = CreateShotA1(GetEnemyX, GetEnemyY, 3, angle-180+(i*6), BLUE51, 10);
ObjMove_AddPatternA2(obj, 0, NO_CHANGE, NO_CHANGE, -3/35, 0, 0);
ObjMove_AddPatternA2(obj, 120, NO_CHANGE, NO_CHANGE, 0.08, 0, 12);

}
	
	
	wait(8);
	}
	wait(30);
	
	
	}
	
	
	
task CreateLaser1(x, y, angle, graphic, length, width, delay, type){
let obj2 = ObjShot_Create(OBJ_STRAIGHT_LASER);
let angle2 = rand(0, 360);
let bulletgraphic = 0;
let one = 1;
let index = 0;
let waitincrement = 0;
ObjMove_SetPosition(obj2, x, y);
ObjStLaser_SetAngle(obj2, angle);
ObjShot_SetGraphic(obj2, graphic);
ObjShot_SetDelay(obj2, delay*20);
ObjLaser_SetLength(obj2, length);
ObjLaser_SetRenderWidth(obj2, width);
ObjShot_SetSpellResist(obj2, true);
ObjStLaser_SetSource(obj2, false);
ObjShot_Regist(obj2);
wait2(delay-5);
if(type==0){ wait(5); }
if(type==1){ObjShot_FadeDelete(obj2); }
if(type==0){
PlaySE(ping);
}
ObjLaser_SetRenderWidth(obj2, width/2);
ObjLaser_SetIntersectionWidth(obj2, width/2-3);
wait2(delay*2);
ObjShot_FadeDelete(obj2);

function wait2(w){ loop(w){ yield; if(popped==true){ Obj_Delete(obj2); } } }

}



task slow {
let xx = 0;
let slowed = false;
loop{

if(GetDistance(GetEnemyX, GetEnemyY, GetPlayerX, GetPlayerY)<=xx && slowed == false){
slowtext;
PlaySE(frostslow);
SetPlayerSpeed(0.5+initspeed[1]/1.75, initspeed[1]/1.75);
slowed = true;
}
if(slowed == true){
ObjRender_SetColor(GetPlayerObjectID, 140, 140, 255);
SetPlayerSpeed(0.5+initspeed[1]/1.75, initspeed[1]/1.75);
}
xx+=128/30;
wait(1);
if(popped==true){ return; }
}
}


task slowtext{
let alpha = 255;
let objText = ObjText_Create();
	ObjText_SetFontType(objText, "Corbel");
	ObjText_SetText(objText, "Slowed!");
	ObjText_SetFontSize(objText, 18);
	ObjText_SetFontBold(objText, true);
	ObjText_SetHorizontalAlignment(objText, ALIGNMENT_CENTER);
	ObjText_SetMaxWidth(objText, 75);
	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-6);
	ObjRender_SetY(objText, GetPlayerY-45);
	loop(120){
	ObjRender_SetX(objText, GetPlayerX-6);
	ObjRender_SetY(objText, GetPlayerY-45);
	yield;
	}
	loop(60){
	ObjRender_SetX(objText, GetPlayerX-6);
	ObjRender_SetY(objText, GetPlayerY-45);
	alpha-=255/60;
	ObjRender_SetAlpha(objText, alpha);
	yield;
	}
	Obj_Delete(objText);
	}
	

	
task attack1{	
wait(120);
let firsttime = 0;
let numcasts = 0;

let one = 1;
let attackchoice = 0;
let angle2 = 0;
let color = 0;
let x = 60;
let xp = 0;
let yp = 0;
let angle3 = 0;
PlaySE(charge2);
TCharge01(objEnemy, 3, 45, 0, 0, 255);
wait(60);
PlaySE(frostnova); TExpan2(GetEnemyX, GetEnemyY, 4, 118, 141, 175, 180); slow;


wait(180-56);
PlaySE(charge2);
TCharge01(objEnemy, 2, 30, 0, 191, 255);
wait(56);
loop{

ascent(j in 0..3){
if(popped==true){ return; }
angle = angletoplayer(objEnemy);
	ascent(i in 0..20){
xp = rand(-3, 3);
yp = rand(-3, 3);
loop(8){
boomshot(GetEnemyX+xp, GetEnemyY+yp, 3.25, angle+rand(-1, 1), AQUA23, 10);
angle+=360/8;
}

if(i==0 && j!=3){
PlaySE(ping);
loop(8){
			CreateLaser1(ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), angle, BLUE12, 600, 200, 15, 1);
			angle+=360/8;
			}
}
if(i==12 && j<=2){
PlaySE(charge2);
TCharge01(objEnemy, 2, 30, 0, 191, 255);
}


wait(7);
}



	

}

PlaySE(charge);
wait(90);
loop(10){
PlaySE(ping);
loop(2){
boomshotline(GetEnemyX+xp, GetEnemyY+yp, 3.25, angle, AQUA23, 10);
CreateLaser1(ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), angle+0.25, BLUE12, 600, 300,  25, 10);
angle+=360/2;
}
angle+=360/11;
wait(15);
}

wait(180-56);
PlaySE(charge2);
TCharge01(objEnemy, 2, 30, 0, 191, 255);
wait(56);


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



	task boomshot(x, y, v, angle, graphic, delay){
		let obj = CreateDNHShotA1(x, y, v, angle, graphic, delay);
		let angle2 = rand(0, 360);
		while(!Obj_IsDeleted(obj)){
		
		if(ObjMove_GetX(obj)>GetStgFrameWidth || ObjMove_GetX(obj)<0 || ObjMove_GetY(obj)<0 || ObjMove_GetY(obj)>GetStgFrameHeight){
		//PlaySE(ping);
			loop(1){
				CreateDNHShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 1.75, ObjMove_GetAngle(obj)+180+rand(-17, 17), AQUA50, 10);
				}
				
				Obj_Delete(obj);
				}
		yield;
		}
		}

		
		task boomshotline(x, y, v, angle, graphic, delay){
		loop(20){
		boomshot(x+rand(-1, 1), y+rand(-1, 1), v, angle+rand(-1, 1), graphic, delay);
		wait(7);
		}
		}
		
/*
task DestroyBoss{
SetCommonData("pausebeforebonus", true);
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);
ScreenShake(15*5, 15);
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);
ScreenShake(40, 30);
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;
}
*/

task DestroyBoss{
SetCommonData("petals", false);
SetPlayerSpeed(initspeed[0], initspeed[1]);
ObjRender_SetColor(GetPlayerObjectID, 255, 255, 255);
popped=true;
StopSlow(TARGET_ALL);
loop(10){ yield; }
Obj_Delete(objEnemy);
loop(45){ yield; }
		CloseScript(GetOwnScriptID());
		return;
}
