#ScriptVersion[3]
#Title["England Extra Stage"]
#System["./default_system/Default_System.txt"]
#include".\ZUNShot_Const.txt"
#include".\StageFunctions.txt"

let SShot1 = GetCurrentScriptDirectory~"ZUN/se_kira00.wav";
let SShot2 = GetCurrentScriptDirectory~"ZUN/se_tan00.wav";
let SBoon1 = GetCurrentScriptDirectory~"ZUN/se_boon01.wav";
let SExplode1 = GetCurrentScriptDirectory~"ZUN/se_enep02.wav";
let SCharge1 = GetCurrentScriptDirectory~"ZUN/se_ch02.wav";
let SCharge2 = GetCurrentScriptDirectory~"ZUN/se_ch00.wav";
let SSlash1 = GetCurrentScriptDirectory~"ZUN/se_slash.wav";
	@Initialize
	{
		SetCameraFocusX(0);
		SetCameraFocusY(0);
		SetCameraFocusZ(-5);
		SetCameraRadius(400);
		SetCameraElevationAngle(0);
		SetCameraAzimuthAngle(90);
		SetCommonData("PIVITEM",0);
		TStage();
		PoCCheck;
		SetFogEnable(true);
		SetFogParam(200, 500, 100, 100, 100);
		SetPlayerLife(6);
	}

	@MainLoop
	{
		yield;
	}

	@Finalize
	{
	}

	task PoCCheck{
		let frame = 0;
		loop{
			if(GetPlayerY<120){
				if(frame%30==0){
					PIVItem(GetPlayerX,GetPlayerY,rand(0,360),1.5);
				}
			CollectAllItems;
			}
		frame++;
		yield;
		}
	}
	task TBackground{
		let objBack = ObjPrim_Create(OBJ_SPRITE_3D);
		let image = GetCurrentScriptDirectory~"stg3bg.png";
		let c = 0;
		ObjPrim_SetTexture(objBack,image);
		Obj_SetRenderPriorityI(objBack,20);
		ObjSprite3D_SetDestRect(objBack,-100,0,GetStgFrameWidth+100,GetStgFrameHeight);
		ObjRender_SetPosition(objBack,-GetStgFrameWidth/2,-GetStgFrameHeight/2,20);
		ObjRender_SetAlpha(objBack,200);
		ObjSprite3D_SetSourceRect(objBack,0,0,512,512);
		loop{
			SetCameraFocusX(cos(c/2)*35);
			ObjSprite3D_SetSourceRect(objBack,0,0+c,512,512+c);
			c+=1;
			yield;
	}
}
	task TRandom{
		let xpos = rand(-350,350);
		let zpos = rand(-40,40);
		let angles = [rand(0,360),rand(0,360),rand(0,360)];
		let objBack = ObjPrim_Create(OBJ_SPRITE_3D);
		let image = GetCurrentScriptDirectory~"tree.png";
		let c = 0;
		ObjPrim_SetTexture(objBack,image);
		Obj_SetRenderPriorityI(objBack,21);
		ObjSprite3D_SetDestRect(objBack,0,0,27,20);
		ObjRender_SetPosition(objBack,xpos,0,zpos);
		ObjRender_SetAlpha(objBack,255);
		ObjRender_SetZWrite(objBack, true);
		ObjRender_SetZTest(objBack, true);
		loop(480){
			ObjRender_SetPosition(objBack,xpos-(c/6),(250)-(c/3),zpos-(c/6));
	        ObjRender_SetAngleXYZ(objBack,angles[0]+(c/3),angles[1]+(c/3),angles[2]+(c/3));
			ObjSprite3D_SetSourceRect(objBack,0,0,27,20);
			c+=3;
			yield;
	}
			Obj_Delete(objBack);
}

task TRandomCall{
	loop{
		TRandom;
		loop(10){yield;}
		}
	}
task lifeGet(){
	let pivGet = 200000;
	loop{
		if(GetCommonData("PIValue",1)>=pivGet){
			SetPlayerLife(GetPlayerLife+1);
			pivGet+=100000;
		}
		yield;
	}
}
task THoming(x,y,angle){
	let obj = ObjEnemy_Create(OBJ_ENEMY);
	let frame = 0;
	let dir = -40;
	let familiar = GetCurrentScriptDirectory~"stg3enm.png";
	Obj_SetRenderPriorityI(obj,22);
	ObjEnemy_SetLife(obj,75);
	ObjEnemy_SetDamageRate(obj,100,100);
	ObjPrim_SetTexture(obj,familiar);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjEnemy_Regist(obj);
	ObjRender_SetColor(obj,255,100,255);
	ObjMove_SetPosition(obj,x,y);
	ObjMove_SetAngle(obj,angle);
	ObjMove_SetSpeed(obj,2);
	while(!Obj_IsDeleted(obj)){
		ObjEnemy_SetIntersectionCircleToShot(obj,ObjMove_GetX(obj),ObjMove_GetY(obj),32*(4/5));
		ObjRender_SetAngleXYZ(obj,0,0,frame);
		ObjSprite2D_SetDestCenter(obj);
		ObjSprite2D_SetSourceRect(obj,2,99,63,160); 
		ObjRender_SetScaleXYZ(obj,0.8,0.8,0.8);
		if(ObjMove_GetX(obj)<-64||ObjMove_GetX(obj)>GetStgFrameWidth+64||ObjMove_GetY(obj)<-64||ObjMove_GetY(obj)>GetStgFrameHeight+64){
			Obj_Delete(obj);
			return;
		}
		if(ObjEnemy_GetInfo(obj,INFO_LIFE)<=0){
			loop(2){PIVItem(ObjMove_GetX(obj),ObjMove_GetY(obj),rand(0,360),3); dir+=80/5;}
			Obj_Delete(obj);	
			return;	
		}
		if(frame%120==30){DelaySE(SShot2,0,0); CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),1,GetAngleToPlayer(obj),76,10);}
		frame++;
		yield;
	}	
}
task TLaser(x,y,angle){
	let obj = ObjEnemy_Create(OBJ_ENEMY);
	let frame = 0;
	let dir = -40;
	let familiar = GetCurrentScriptDirectory~"stg3enm.png";
	Obj_SetRenderPriorityI(obj,22);
	ObjEnemy_SetLife(obj,75);
	ObjEnemy_SetDamageRate(obj,100,100);
	ObjPrim_SetTexture(obj,familiar);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjEnemy_Regist(obj);
	ObjRender_SetColor(obj,100,100,255);
	ObjMove_SetPosition(obj,x,y);
	ObjMove_SetAngle(obj,angle);
	ObjMove_SetSpeed(obj,1.5);
	while(!Obj_IsDeleted(obj)){
		ObjEnemy_SetIntersectionCircleToShot(obj,ObjMove_GetX(obj),ObjMove_GetY(obj),32*(4/5));
		ObjRender_SetAngleXYZ(obj,0,0,frame);
		ObjSprite2D_SetDestCenter(obj);
		ObjSprite2D_SetSourceRect(obj,2,99,63,160); 
		ObjRender_SetScaleXYZ(obj,0.8,0.8,0.8);
		if(ObjMove_GetX(obj)<-64||ObjMove_GetX(obj)>GetStgFrameWidth+64||ObjMove_GetY(obj)<-64||ObjMove_GetY(obj)>GetStgFrameHeight+64){
			Obj_Delete(obj);
			return;
		}
		if(ObjEnemy_GetInfo(obj,INFO_LIFE)<=0){
			loop(3){CreateItemA1(ITEM_POINT,ObjMove_GetX(obj)+rand(-50,50),ObjMove_GetY(obj)+rand(-50,50),GetCommonData("PIValue",10000));}
			Obj_Delete(obj);	
			return;	
		}
		if(frame%40==0){DelaySE(SShot1,0,0); 
			CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),3,90,175,10);
			CreateLooseLaserA1(ObjMove_GetX(obj),ObjMove_GetY(obj),3,90,50,10,20,10);
			CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),3,270,175,10);
			CreateLooseLaserA1(ObjMove_GetX(obj),ObjMove_GetY(obj),3,270,50,10,20,10);
		}
		frame++;
		yield;
	}	
}
task TCircle(x,y,angle){
	let obj = ObjEnemy_Create(OBJ_ENEMY);
	let frame = 0;
	let dir = rand(0,360);
	let familiar = GetCurrentScriptDirectory~"stg3enm.png";
	Obj_SetRenderPriorityI(obj,22);
	ObjEnemy_SetLife(obj,175);
	ObjEnemy_SetDamageRate(obj,100,100);
	ObjPrim_SetTexture(obj,familiar);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjEnemy_Regist(obj);
	ObjRender_SetColor(obj,255,100,100);
	ObjMove_SetPosition(obj,x,y);
	ObjMove_SetAngle(obj,angle);
	ObjMove_SetSpeed(obj,0.5);
	while(!Obj_IsDeleted(obj)){
		ObjEnemy_SetIntersectionCircleToShot(obj,ObjMove_GetX(obj),ObjMove_GetY(obj),32*(4/5));
		ObjRender_SetAngleXYZ(obj,0,0,frame);
		ObjSprite2D_SetDestCenter(obj);
		ObjSprite2D_SetSourceRect(obj,2,99,63,160); 
		ObjRender_SetScaleXYZ(obj,0.8,0.8,0.8);
		if(ObjMove_GetX(obj)<-64||ObjMove_GetX(obj)>GetStgFrameWidth+64||ObjMove_GetY(obj)<-64||ObjMove_GetY(obj)>GetStgFrameHeight+64){
			Obj_Delete(obj);
			return;
		}
		if(ObjEnemy_GetInfo(obj,INFO_LIFE)<=0){
			loop(2){PIVItem(ObjMove_GetX(obj),ObjMove_GetY(obj),dir,2); dir+=360/2;}
			loop(5){CreateItemA1(ITEM_POINT,ObjMove_GetX(obj)+rand(-50,50),ObjMove_GetY(obj)+rand(-50,50),GetCommonData("PIValue",10000));}
			Obj_Delete(obj);	
			return;	
		}
		if(frame%120==60){DelaySE(SShot2,0,0); loop(30){CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),1,dir,108,10); dir+=360/30;}}
		frame++;
		yield;
	}	
}
task TSpiral(x,y,angle,dir1,dir2){
	let obj = ObjEnemy_Create(OBJ_ENEMY);
	let frame = 0;
	let dir = rand(0,360);
	let familiar = GetCurrentScriptDirectory~"stg3enm.png";
	Obj_SetRenderPriorityI(obj,22);
	ObjEnemy_SetLife(obj,120);
	ObjEnemy_SetDamageRate(obj,100,100);
	ObjPrim_SetTexture(obj,familiar);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjEnemy_Regist(obj);
	ObjRender_SetColor(obj,100,255,100);
	ObjMove_SetPosition(obj,x,y);
	ObjMove_SetAngle(obj,angle);
	ObjMove_SetSpeed(obj,1);
	while(!Obj_IsDeleted(obj)){
		ObjEnemy_SetIntersectionCircleToShot(obj,ObjMove_GetX(obj),ObjMove_GetY(obj),32*(4/5));
		ObjRender_SetAngleXYZ(obj,0,0,frame);
		ObjSprite2D_SetDestCenter(obj);
		ObjSprite2D_SetSourceRect(obj,2,99,63,160); 
		ObjRender_SetScaleXYZ(obj,0.8,0.8,0.8);
		if(ObjMove_GetX(obj)<-64||ObjMove_GetX(obj)>GetStgFrameWidth+64||ObjMove_GetY(obj)<-64||ObjMove_GetY(obj)>GetStgFrameHeight+64){
			Obj_Delete(obj);
			return;
		}
		if(frame>60&&frame<300){ObjMove_SetSpeed(obj,0);} else { ObjMove_SetSpeed(obj,1);}
		if(frame<300){	ObjEnemy_SetDamageRate(obj,0,0);} else {	ObjEnemy_SetDamageRate(obj,100,100);}
		if(ObjEnemy_GetInfo(obj,INFO_LIFE)<=0){
			loop(50){PIVItem(ObjMove_GetX(obj),ObjMove_GetY(obj),dir,rand(1,3)); dir+=360/50;}
			loop(20){CreateItemA1(ITEM_POINT,ObjMove_GetX(obj)+rand(-50,50),ObjMove_GetY(obj)+rand(-50,50),GetCommonData("PIValue",10000));}
			Obj_Delete(obj);	
			return;	
		}
		if(frame==90){ spiral(dir1,67);}
		if(frame==120){ spiral(dir2,68);}
		frame++;
		yield;
	}	
	task spiral(ang,graphic){
		let dire = 0;
		loop(20){
			if(!Obj_IsDeleted(obj)){
			DelaySE(SShot2,0,0);
			loop(10){
				CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),2.5,dire,graphic,40); 
				dire+=360/10;
			}
			dire+=ang;
			loop(10){yield;}
			}
		}
	}
}
	task TStage
	{
		let dir = GetCurrentScriptDirectory();
		lifeGet;
		TBackground;
		//Creates the boss
		let path = dir ~ "Extra/plural.txt";
		let idScript = LoadScriptInThread(path);
		loop(1){yield;}//Assuming that compiling with finish in about a second.
		let objTxt = ObjText_Create();
		ObjText_SetText(objTxt,"EXTRA");
		Obj_SetRenderPriority(objTxt, 0.1);
		ObjText_SetHorizontalAlignment(objTxt,ALIGNMENT_LEFT);
		ObjText_SetFontSize(objTxt,20);
		ObjText_SetFontColorTop(objTxt,200,255,200);
		ObjText_SetFontColorBottom(objTxt,200,255,200);
		ObjText_SetFontBorderType(objTxt,BORDER_FULL);
		ObjText_SetFontBorderColor(objTxt,100,200,100);
		ObjText_SetFontBorderWidth(objTxt,2);
		ObjRender_SetPosition(objTxt,480,20,0);
		let a = 0;
		loop(120){
			SetCameraElevationAngle(a);
			a-=1/4;
			yield;
		}
		loop(60){yield;}
		loop(10){
			THoming(-32,rand(75,150),0);
			loop(10){yield;}
		}
		loop(120){yield;}
		loop(10){
			THoming(GetStgFrameWidth+32,rand(75,150),180);
			loop(10){yield;}
		}
		loop(120){yield;}
		loop(1000){yield;}//Assuming that compiling with finish in about a second.
		//Stands by until the boss scene is finished
		//〜〜〜Creating the enemy, or looping the creation of an enemy

		loop(240){yield;}

	    //End of the stage
		CloseStgScene();
	}