
let bSpell = false;
let current = GetCurrentScriptDirectory();
let trailpath = GetCurrentScriptDirectory~"./img/nbg3.png";
let CS = GetCurrentScriptDirectory~"./img/CS.jpg";
let greenpath = GetCurrentScriptDirectory~"./img/nbg1.png";
let pow = GetCurrentScriptDirectory~"se/lastspell.wav";
let soundplayed = false;
let counterspell = GetCurrentScriptDirectory~"./se/counterspell.wav";
let csup = GetCurrentScriptDirectory~"./se/csup.wav";
let tiltangle = 30;
let time = 0;
let objtrees = [];
let objpath = [];
let bloop = true;


let numbg = 0;
let f = 0.5;
let f1 = 0;
let f2 = 0;
let f3 = 0;
let f7 = 0;
let f4 = 0;
let f8 = 0;
let acc1 = 0;
let acc2 = 0;
let acc7 = 0;
let acc6 = -1110;
let ff = 0.5;


@Initialize
{
InstallFont("./fontSEI.ttf");
	SetCameraFocusXYZ(0, -60, 80);
	SetViewFrom(10, -90, 25+10*cos(0));
	 SetFogParam ( 350, 750, 25, 25, 112); 
LoadTexture(CS);
LoadSound(counterspell);
LoadSound(pow);
LoadSound(csup);
	StartNamelessPath;
	TSpellBackground();
	DispIntCD;
}
 
task DispIntCD {
let alph = 0;
let interruptup = true;
let count = 0;
SetCommonData("interruptavail", true);

let obj = ObjPrim_Create(OBJ_SPRITE_2D);
Obj_SetRenderPriority(obj, 0.9);
ObjPrim_SetTexture(obj, CS);
ObjSprite2D_SetSourceRect(obj, 0, 0, 56, 56);
ObjSprite2D_SetDestCenter(obj);
ObjRender_SetColor(obj, 255, 255, 255);
ObjRender_SetAlpha(obj, 255);
ObjRender_SetX(obj, 490);
ObjRender_SetY(obj, 300);
ObjRender_SetScaleXYZ(obj, 0.35, 0.35, 1);



let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
Obj_SetRenderPriority(obj2, 0.91);
ObjPrim_SetTexture(obj2, GetCurrentScriptDirectory~"./img/Default_System.png");
ObjSprite2D_SetSourceRect(obj2, 132, 1, 137, 11);
ObjSprite2D_SetDestRect(obj2, GetStgFrameWidth+55, GetStgFrameHeight/2+59.75, GetStgFrameWidth+75, GetStgFrameHeight/2+64.75);
ObjRender_SetColor(obj2, 255, 255, 255);
ObjRender_SetAlpha(obj2, 255);
ObjMove_SetX(obj2, GetStgFrameWidth/2);
ObjMove_SetY(obj2, GetStgFrameHeight/2);
ObjRender_SetScaleXYZ(obj2, 1, 1, 1);

//to put on left, change x by -433.5

let obj3 = ObjPrim_Create(OBJ_SPRITE_2D);
Obj_SetRenderPriority(obj3, 0.91);
ObjPrim_SetTexture(obj3, GetCurrentScriptDirectory~"./img/Default_System.png");
ObjSprite2D_SetSourceRect(obj3, 132, 1, 137, 11);
ObjSprite2D_SetDestRect(obj3, GetStgFrameWidth+55, GetStgFrameHeight/2+(180+67)-2.5, GetStgFrameWidth+75, GetStgFrameHeight/2+(180+67)+2.5);
ObjRender_SetColor(obj3, 255, 255, 255);
ObjRender_SetAlpha(obj3, 255);
ObjMove_SetX(obj3, GetStgFrameWidth/2);
ObjMove_SetY(obj3, GetStgFrameHeight/2);
ObjRender_SetScaleXYZ(obj3, 1, 1, 1);


let objText = ObjText_Create();
ObjText_SetFontType(objText, "Corbel");
	ObjText_SetText(objText, "READY");
	ObjText_SetFontSize(objText, 18);
	ObjText_SetFontBold(objText, true);
	ObjText_SetHorizontalAlignment(objText, ALIGNMENT_CENTER);
	ObjText_SetMaxWidth(objText, 70);
	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, 455);
	ObjRender_SetY(objText, GetStgFrameHeight/2+85);
	
	
	
	let objb = ObjPrim_Create(OBJ_SPRITE_2D);
Obj_SetRenderPriority(objb, 0.9);
ObjPrim_SetTexture(objb, CS);
ObjSprite2D_SetSourceRect(objb, 0, 0, 56, 56);
ObjSprite2D_SetDestCenter(objb);
ObjRender_SetColor(objb, 255, 255, 255);
ObjRender_SetAlpha(objb, 255);
ObjRender_SetX(objb, 60);
ObjRender_SetY(objb, 435);
ObjRender_SetScaleXYZ(objb, 0.25, 0.25, 1);

let objTextb = ObjText_Create();
ObjText_SetFontType(objTextb, "Corbel");
	ObjText_SetText(objTextb, "READY");
	ObjText_SetFontSize(objTextb, 16);
	ObjText_SetFontBold(objTextb, true);
	ObjText_SetHorizontalAlignment(objTextb, ALIGNMENT_CENTER);
	ObjText_SetMaxWidth(objTextb, 70);
	ObjText_SetFontColorTop(objTextb, 224, 255, 255);
	ObjText_SetFontColorBottom(objTextb, 128, 128, 255);
	ObjText_SetFontBorderType(objTextb, BORDER_FULL);
	ObjText_SetFontBorderColor(objTextb,0, 0, 0);
	ObjText_SetFontBorderWidth(objTextb, 1);

	Obj_SetRenderPriority(objTextb, 0.9);
	ObjRender_SetX(objTextb, 60-35);
	ObjRender_SetY(objTextb, 442);


loop{

if(GetCommonData("readytheint", false)==true){ if(count<10*60){ count = 10*60; }  SetCommonData("readytheint", false); }

if(ObjMove_GetX(GetPlayerObjectID)<=70 && ObjMove_GetY(GetPlayerObjectID)>=390){
if(alph>55){ alph-=10; }
ObjRender_SetAlpha(objb, alph);
ObjRender_SetAlpha(objTextb, alph);
}
else{
if(alph<255){ alph+=10; }
ObjRender_SetAlpha(objb, alph);
ObjRender_SetAlpha(objTextb, alph);
}


if(count==0){ ObjText_SetText(objText, "READY"); ObjText_SetFontSize(objText, 16); ObjText_SetText(objTextb, "READY"); ObjText_SetFontSize(objTextb, 15); if(soundplayed==false){ PlaySE(csup); soundplayed=true; }}
else{ ObjText_SetText(objText, IntToString(ceil(12-(count/60)))); ObjText_SetFontSize(objText, 22); ObjText_SetText(objTextb, IntToString(ceil(12-(count/60)))); ObjText_SetFontSize(objTextb, 20); }

if(interruptup == false){ count++; soundplayed = false;}
if(GetVirtualKeyState(VK_USER1)==KEY_PUSH && interruptup == true  && GetPlayerState==STATE_NORMAL && GetCommonData("interruptavail", true)==true){  PlaySE(pow); PlayPow;  SetCommonData("interruptavail", false); interruptup = false; TExpan2(GetPlayerX, GetPlayerY, 7, 72, 61, 139, 170);}
if(interruptup == false){ ObjRender_SetColor(obj, 255, 0, 0); ObjRender_SetColor(objb, 255, 0, 0); }
if(count>=12*60){ interruptup = true; SetCommonData("interruptavail", true); ObjRender_SetColor(obj, 255, 255, 255); ObjRender_SetColor(objb, 255, 255, 255); count = 0; }
yield;
}
}

task PlayPow{
wait(15);
PlaySE(counterspell); 
}

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(time>90){ malpha-=alpha/30; ObjRender_SetAlpha(obj,malpha); }
			if(time>=120){ Obj_Delete(obj); }
			
			}
		}
	}
	

@MainLoop
{

	let objScene = GetEnemyBossSceneObjectID();
	if(objScene != ID_INVALID && ObjEnemyBossScene_GetInfo(objScene, INFO_IS_SPELL))
	{
	numbg++;
		bSpell = true;
	}
	else
	{
	numbg=0;
		bSpell = false;
	}
	
 f+= ff;
  f8+=ff;
	yield;
}


function SetViewFrom(radius, azimuth, elevation){
SetCameraRadius(radius);
SetCameraAzimuthAngle(azimuth);
SetCameraElevationAngle(elevation);
}

task StartNamelessPath{


let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	  //SetAlpha(255);
      ObjRender_SetBlendType(obj, BLEND_ALPHA);
	  Obj_SetRenderPriorityI(obj,21);
	 ObjRender_SetPermitCamera(obj, true);
      ObjPrim_SetTexture(obj,GetCurrentScriptDirectory~"./img/nbg1.png");
      ObjSprite3D_SetSourceDestRect(obj, 0, 0+f8, 500, 1500+f8);
      ObjRender_SetAngleXYZ(obj, 90, 0, 0);
      ObjRender_SetScaleXYZ(obj, 1, 1, 1);
      ObjRender_SetPosition(obj, 0, -300, 600);

	   //SetAlpha(255);
	   let obj2 = ObjPrim_Create(OBJ_SPRITE_3D);
	  ObjRender_SetBlendType(obj2, BLEND_ALPHA);
	  Obj_SetRenderPriorityI(obj2,22);
	    ObjRender_SetPermitCamera(obj2, true);
      ObjPrim_SetTexture(obj2,GetCurrentScriptDirectory~"./img/nbg3.png");
      ObjSprite3D_SetSourceDestRect(obj2, 0, 0+f8, 512, 1500+f8);
      ObjRender_SetAngleXYZ(obj2, 90, 0, 0);
      ObjRender_SetScaleXYZ(obj2, 0.5, 1, 1);
      ObjRender_SetPosition(obj2, 0, -300, 600);
	
	
	
	  let obj5 = ObjPrim_Create(OBJ_SPRITE_3D);
      ObjRender_SetBlendType(obj5, BLEND_ALPHA);
	   Obj_SetRenderPriorityI(obj5,20);
      ObjPrim_SetTexture(obj5,GetCurrentScriptDirectory~"./img/nbg2.png");
	  ObjSprite3D_SetSourceDestRect(obj5, 1, 1, 256, 256);
	  ObjRender_SetAngleXYZ(obj5, 0, 180, 180);
      ObjRender_SetScaleXYZ(obj5, 10, 10, 1);
	  ObjRender_SetPosition(obj5, 0, 0, 1900); 
	   ObjRender_SetPermitCamera(obj5, true);
	  

	loop(10){
	  let obj3 = ObjPrim_Create(OBJ_SPRITE_3D);
      ObjRender_SetBlendType(obj3, BLEND_ALPHA);
	  Obj_SetRenderPriorityI(obj3,23);
	  objtrees = objtrees ~ [obj3];
      ObjPrim_SetTexture(obj3,GetCurrentScriptDirectory~"./img/nbg5.png");
	  ObjSprite3D_SetSourceDestRect(obj3, 0, 0, 512, 700);
	  ObjRender_SetAngleXYZ(obj3, 0, 0, 180);
      ObjRender_SetScaleXYZ(obj3, 0.5,0.5, 1);
	  ObjRender_SetPosition(obj3, 180, -150, 1200-f+acc6);
		 ObjRender_SetPermitCamera(obj3, true);

	  let obj4 = ObjPrim_Create(OBJ_SPRITE_3D);
      ObjRender_SetBlendType(obj4, BLEND_ALPHA);
	   Obj_SetRenderPriorityI(obj4,23);
      ObjPrim_SetTexture(obj4,GetCurrentScriptDirectory~"./img/nbg5.png");
	  ObjSprite3D_SetSourceDestRect(obj4, 0, 0, 512, 700);
	  ObjRender_SetAngleXYZ(obj4, 0, 0, 180);
	  objpath = objpath ~ [obj4];
      ObjRender_SetScaleXYZ(obj4, 0.5,0.5, 1);
	  ObjRender_SetPosition(obj4, -180, -150, 1200-f+acc6);  
 ObjRender_SetPermitCamera(obj4, true);	  
acc6-=110;
}


acc6 = 0;
	  loop{
	
	  
	  if(time>=90){
	   if(ff<2){ ff+=0.02; }
	  if(f>110){ f=0; }
	  if(acc7 < 180){acc7 += 1;}
	  SetViewFrom(10, -90, 25+10*cos(acc7));
	  }
	  acc2+=0.1;
	 time++;
	 
	 
	  
      ObjRender_SetBlendType(obj, BLEND_ALPHA);
	  Obj_SetRenderPriorityI(obj,21);
	 
      ObjPrim_SetTexture(obj,GetCurrentScriptDirectory~"./img/nbg1.png");
      ObjSprite3D_SetSourceDestRect(obj, 0, 0+f8, 500, 1500+f8);
      ObjRender_SetAngleXYZ(obj, 90, 0, 0);
      ObjRender_SetScaleXYZ(obj, 1, 1, 1);
      ObjRender_SetPosition(obj, 0, -300, 600);
	  
	  ObjRender_SetBlendType(obj2, BLEND_ALPHA);
	  Obj_SetRenderPriorityI(obj2,22);
	   
      ObjPrim_SetTexture(obj2,GetCurrentScriptDirectory~"./img/nbg3.png");
      ObjSprite3D_SetSourceDestRect(obj2, 0, 0+f8, 512, 1500+f8);
      ObjRender_SetAngleXYZ(obj2, 90, 0, 0);
      ObjRender_SetScaleXYZ(obj2, 0.5, 1, 1);
      ObjRender_SetPosition(obj2, 0, -300, 600);
	
      ObjRender_SetBlendType(obj5, BLEND_ALPHA);
	   Obj_SetRenderPriorityI(obj5,20);
      ObjPrim_SetTexture(obj5,GetCurrentScriptDirectory~"./img/nbg2.png");
	  ObjSprite3D_SetSourceDestRect(obj5, 1, 1, 256, 256);
	  ObjRender_SetAngleXYZ(obj5, 0, 180, 180);
      ObjRender_SetScaleXYZ(obj5, 10, 10, 1);
	  ObjRender_SetPosition(obj5, 0, 0, 1900); 
	  
	  if(numbg>=60){
		Obj_SetVisible(obj, false);
		Obj_SetVisible(obj2, false);
		Obj_SetVisible(obj5, false);
		}
		else{
		Obj_SetVisible(obj, true);
		Obj_SetVisible(obj2, true);
		Obj_SetVisible(obj5, true);
		}
	  
	  

	ascent(i in 0..length(objtrees)){
		if(numbg>=60){
		Obj_SetVisible(objtrees[i], false);
		Obj_SetVisible(objpath[i], false);
		}
		else{
		Obj_SetVisible(objtrees[i], true);
		Obj_SetVisible(objpath[i], true);
		}
		
     
	  ObjRender_SetPosition(objtrees[i], 180, -150, 1200-f+acc6);
	  ObjRender_SetPosition(objpath[i], -180, -150, 1200-f+acc6);
	  acc6-=110;
   
}
	acc6 = 0;	

wait(1);
}
}


function wait(w){ loop(w){ yield; } }

task TSpellBackground
{
	let path1 = current ~ "img/youmu.png";
	let path2 = current ~ "img/sakura02.png";
	
	let obj1 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj1, BLEND_ALPHA);
	Obj_SetRenderPriority(obj1, 0.24);
	ObjPrim_SetTexture(obj1, path1);
	ObjSprite2D_SetSourceRect(obj1, 0, 0, 128, 128);
	ObjSprite2D_SetDestRect(obj1, -5, -20, 622-20, 622-5);
	ObjRender_SetScaleX(obj1, 0.85);
	ObjRender_SetScaleY(obj1, 0.85);
	
	let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj2, BLEND_ALPHA);
	Obj_SetRenderPriority(obj2, 0.24);
	ObjPrim_SetTexture(obj2, path2);
	ObjSprite2D_SetSourceRect(obj2, 0, 0, 1024, 1024);
	ObjSprite2D_SetDestRect(obj2, -512/2, -512/2, 512/2, 512/2);
	ObjRender_SetScaleX(obj2, 1.5);
	ObjRender_SetAlpha(obj2, 255/3.35);
	ObjRender_SetScaleY(obj2, 1.5);
	ObjRender_SetColor(obj2, 186, 85, 211);
	ObjRender_SetX(obj2, GetStgFrameWidth/2);
	ObjRender_SetY(obj2, GetStgFrameHeight/2);
	Obj_SetVisible(obj2, false);

	
	let u1 = 0;
	let v1 = 0;
	let u2 = 0;
	let v2 = 0;
	let su2 = rand(0.25, 1);
	let sv2 = rand(0.25, 1);
	let alpha = 0;
	let frame = 0;
	let turn = 3;
	let turnacc = 3.10;
	loop
	{
	if(GetCommonData("MimaSama", false)==true &&  bloop==true){ path1 = current ~ "img/mima.png";  ObjPrim_SetTexture(obj1, path1); bloop=false;}
		if(bSpell)
		{
			if(alpha<255){
			alpha += 4;
			}
		}
		else if(alpha>0){
			alpha -=8;
		}
			
		v1 -= 2.5;
		if(v1 < 256){v1+=256;}
		u2 += su2;
		if((su2 > 0 && u2 > 256) || (su2 < 0 && u2 < -256))
		{
			u2=0;
			//su2 = rand(0.25, 1);
			if(rand(0, 1) < 0.5){su2 *= -1;}
		}
		v2 += sv2;
		if((sv2 > 0 && v2 > 256) || (sv2 < 0 && v2 < -256))
		{
			v2 = 0;
			//sv2 = rand(0.25, 1);
			if(rand(0, 1) < 0.5){sv2 *= -1;}
		}

		ObjRender_SetAlpha(obj1, alpha);
		Obj_SetVisible(obj1, alpha > 0);
	
		ObjSprite2D_SetSourceRect(obj1, 0, 0-v1, 512, 512-v1);
		if(GetCommonData("petals", false)==true){
		Obj_SetVisible(obj2, true);
		ObjRender_SetAngleXYZ(obj2, 0, 0, 0+turn);
		turn+=turnacc;
		turnacc-=1.5/30;
		}
		else{
		turnacc = 3.10;
		Obj_SetVisible(obj2, false);
		}
		
		frame++;
		yield;
	}
	

}
