#TouhouDanmakufu[Stage]
#Title["Background Test"]
#Text["COMPLETE THIS SCRIPT."]
#System["./../System/Default_System.txt"]
#Player["./../Player/Reimu/Reimu.txt"]
#ScriptVersion[3]

let CSD = GetCurrentScriptDirectory;

let sceneObj = GetEnemyBossSceneObjectID;

// Textures
let FogTexture = CSD ~ "../Img/BG/Fog.png";
let FloorTexture = CSD ~ "../Img/BG/Frosty Floor.png";
let Wall1Texture = CSD ~ "../Img/BG/Frosty Wall 1.png";
let Wall2Texture = CSD ~ "../Img/BG/Frosty Wall 2.png";
let CeilingTexture = CSD ~ "../Img/BG/Frosty Ceiling.png";

@Initialize{
	Call;
	CallStage;
	VariableTest;
}

@MainLoop{
	yield;
}

task Call{
	loop{
		
		while(GetCommonData("Is Spell?",false) == false){ yield;}
	
		if(GetCommonData("Is Spell?",false) == true){
			SpellBackground;
		}
	
		while(GetCommonData("Is Spell?",false) == true){yield;}
		yield;
	}
}

task CallStage{ //Work on this to change stages depending on phase!
 Phase1;
 SetCamera1;
}

task SetCamera1{
	let scroll = 0;
	
	SetCameraPerspectiveClip(16,4096);
	SetCameraElevationAngle(15);
	SetCameraAzimuthAngle(-90);
	SetCameraRadius(16);
	SetCameraFocusX(0);
	SetCameraFocusY(256);
	SetCameraFocusZ(-512+scroll);
	
	loop{ //fix this when fixing callstage!
		SetCameraFocusZ(-512+scroll);
		
		if(scroll > 512){
			scroll = 0;
		}
		
		scroll+=6;
		yield;
	}
}

task Phase1{
	let objs = MakeFogBG(0,0,2048,200-32,200-32,255-64);
	SetFog1(200-32,200-32,255-64);

	MakeFloor(0,0,0);
	MakeFloor(0,0,512);
	MakeFloor(0,0,1024);
	MakeFloor(0,0,1536);
	
	MakeCeiling(0,390,0);
	MakeCeiling(0,390,512);
	MakeCeiling(0,390,1024);
	MakeCeiling(0,390,1536);
	
	MakeWall1(-255,195,0);
	MakeWall1(-255,195,512);
	MakeWall1(-255,195,1024);
	MakeWall1(-255,195,1536);
	
	MakeWall1(255,195,0);
	MakeWall1(255,195,512);
	MakeWall1(255,195,1024);
	MakeWall1(255,195,1536);
	
	while(GetCommonData("Switch to Phase 2?",true) != true){
		yield;
	}
	ascent(i in 0..280){
		ObjRender_SetColor(objs,200-32-i/2,200-32-i/2,255-64-i/2);
		SetFogParam(GetCommonData("Distance",NULL)[0],GetCommonData("Distance",NULL)[1],200-32-i/2,200-32-i/2,255-64-i/2);
		yield;
	}
}

// --------------------------------------------------------------------

function MakeFogBG(X,Y,Z,R,G,B){
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj,FogTexture);
	Obj_SetRenderPriorityI(obj,20);
	ObjSprite3D_SetSourceDestRect(obj,0,0,4096,4096);
	ObjRender_SetScaleXYZ(obj,1,1,0);
	ObjRender_SetZWrite(obj,true);
	
	ObjSprite3D_SetBillboard(obj,true);
	
	ObjRender_SetPosition(obj,X,Y,Z);
	
	ObjRender_SetColor(obj,R,G,B);
	return obj;
}

task SetFog1(R,G,B){
	SetFogEnable(true);
	SetFogParam(0,0,R,G,B);
	ascent(i in 0..180){
		SetCommonData("Distance",[(256/180)*i,(1536/180)*i]);
		SetFogParam((256/180)*i,(1536/180)*i,R,G,B);
		yield;
	}
	SetFogParam(256,1536,R,G,B);
	SetCommonData("Distance",[256,1536]);
}

function MakeFloor(X,Y,Z){
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj,FloorTexture);
	Obj_SetRenderPriorityI(obj,20);
	ObjSprite3D_SetSourceDestRect(obj,0,0,512,512);
	ObjRender_SetScaleXYZ(obj,1,1,0);
	ObjRender_SetZWrite(obj,true);
	
	ObjRender_SetAngleXYZ(obj,90,0,0);
	
	ObjRender_SetPosition(obj,X,Y,Z);
}

function MakeCeiling(X,Y,Z){
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj,CeilingTexture);
	Obj_SetRenderPriorityI(obj,20);
	ObjSprite3D_SetSourceDestRect(obj,0,0,512,512);
	ObjRender_SetScaleXYZ(obj,1,1,0);
	ObjRender_SetZWrite(obj,true);
	
	ObjRender_SetAngleXYZ(obj,90,0,0);
	
	ObjRender_SetPosition(obj,X,Y,Z);
}

function MakeWall1(X,Y,Z){
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj,Wall1Texture);
	Obj_SetRenderPriorityI(obj,20);
	ObjSprite3D_SetSourceDestRect(obj,0,0,512,390);
	ObjRender_SetScaleXYZ(obj,1,1,0);
	ObjRender_SetZWrite(obj,true);
	
	ObjRender_SetAngleXYZ(obj,0,90,180);
	
	ObjRender_SetPosition(obj,X,Y,Z);
}

function MakeWall2(X,Y,Z){
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj,Wall2Texture);
	Obj_SetRenderPriorityI(obj,20);
	ObjSprite3D_SetSourceDestRect(obj,0,0,512,390);
	ObjRender_SetScaleXYZ(obj,1,1,0);
	ObjRender_SetZWrite(obj,true);
	
	ObjRender_SetAngleXYZ(obj,0,90,180);
	
	ObjRender_SetPosition(obj,X,Y,Z);
}

// --------------------------------------------------------------------

task SpellBackground{
	if(GetCommonData("Boss Type",NULL) == "KOGASA"){
		let down = 0;
		let path1 = CSD ~ "./../Img/BG/KogasaA.png";
		let path2 = CSD ~ "./../Img/BG/KogasaB.png";
		
		let spellObj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(spellObj,path1);
		ObjSprite2D_SetSourceRect(spellObj,0,0,384,512);
		ObjSprite2D_SetDestCenter(spellObj);
		ObjRender_SetBlendType(spellObj,BLEND_ALPHA);
		ObjRender_SetAlpha(spellObj,0);
		ObjRender_SetScaleXYZ(spellObj,1,1,0);
		Obj_SetRenderPriorityI(spellObj,20);
		ObjRender_SetPosition(spellObj,GetStgFrameWidth/2,GetStgFrameHeight/2,0);
		
		let spellObj1 = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(spellObj1,path2);
		ObjSprite2D_SetSourceRect(spellObj1,0,0,256,256);
		ObjSprite2D_SetDestCenter(spellObj1);
		ObjRender_SetBlendType(spellObj1,BLEND_ADD_ARGB);
		ObjRender_SetAlpha(spellObj1,0);
		ObjRender_SetScaleXYZ(spellObj1,2.2,2.2,0);
		Obj_SetRenderPriorityI(spellObj1,21);
		ObjRender_SetPosition(spellObj1,GetStgFrameWidth/2,GetStgFrameHeight/2,0);
		
		let spellObj2 = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(spellObj2,path2);
		ObjSprite2D_SetSourceRect(spellObj2,0,0,256,256);
		ObjSprite2D_SetDestCenter(spellObj2);
		ObjRender_SetBlendType(spellObj2,BLEND_SUBTRACT);
		ObjRender_SetAlpha(spellObj2,0);
		ObjRender_SetScaleXYZ(spellObj2,2.2,2.2,0);
		Obj_SetRenderPriorityI(spellObj2,22);
		ObjRender_SetPosition(spellObj2,GetStgFrameWidth/2,GetStgFrameHeight/2,0);
		
		alp;
			task alp{
			loop(128){
				ObjRender_SetAlpha(spellObj,down);
				ObjRender_SetAlpha(spellObj1,down);
				ObjRender_SetAlpha(spellObj2,down);
				down+=2;
				yield;
			}
			down = 0;
		}
		
		let slide = 0;
		loop{
			if(GetCommonData("Is Spell?",false) == false){dissolve;}
			ObjRender_SetAngleZ(spellObj1,ObjRender_GetAngleZ(spellObj1)+0.3);
			ObjRender_SetAngleZ(spellObj2,ObjRender_GetAngleZ(spellObj2)-0.3);
			ObjSprite2D_SetSourceRect(spellObj,0,0+slide,384,512+slide);
			slide+=0.4;
			yield;
		}
		task dissolve{
			loop(255){
				ObjRender_SetAlpha(spellObj,255-down);
				ObjRender_SetAlpha(spellObj1,255-down);
				ObjRender_SetAlpha(spellObj2,255-down);
				down+=1;
				yield;
			}
			down = 0;
			Obj_Delete(spellObj);
			Obj_Delete(spellObj1);
			Obj_Delete(spellObj2);
		}
	}
	
	if(GetCommonData("Boss Type",NULL) == "LETTY"){
		let down = 0;
		let path1 = CSD ~ "./../Img/BG/Snowflake.png";
		let path2 = CSD ~ "./../Img/BG/LettyBG.png";
		
		let spellObj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(spellObj,path1);
		ObjSprite2D_SetSourceRect(spellObj,0,0,512,512);
		ObjSprite2D_SetDestCenter(spellObj);
		ObjRender_SetBlendType(spellObj,BLEND_SUBTRACT);
		ObjRender_SetAlpha(spellObj,0);
		ObjRender_SetScaleXYZ(spellObj,1,1,0);
		Obj_SetRenderPriorityI(spellObj,22);
		ObjRender_SetPosition(spellObj,GetStgFrameWidth/2,GetStgFrameHeight/2,0);
		
		let spellObj0 = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(spellObj0,path1);
		ObjSprite2D_SetSourceRect(spellObj0,0,0,512,512);
		ObjSprite2D_SetDestCenter(spellObj0);
		ObjRender_SetBlendType(spellObj0,BLEND_ALPHA);
		ObjRender_SetAlpha(spellObj0,0);
		ObjRender_SetScaleXYZ(spellObj0,1.2,1.2,0);
		Obj_SetRenderPriorityI(spellObj0,21);
		ObjRender_SetColor(spellObj0,172,172,172);
		ObjRender_SetPosition(spellObj0,GetStgFrameWidth/2,GetStgFrameHeight/2,0);
		
		let spellObj1 = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(spellObj1,path2);
		ObjSprite2D_SetSourceRect(spellObj1,0,0,512,512);
		ObjSprite2D_SetDestCenter(spellObj1);
		ObjRender_SetBlendType(spellObj1,BLEND_ALPHA);
		ObjRender_SetAlpha(spellObj1,0);
		ObjRender_SetScaleXYZ(spellObj1,1,1,0);
		Obj_SetRenderPriorityI(spellObj1,20);
		ObjRender_SetColor(spellObj1,72,72,72);
		ObjRender_SetPosition(spellObj1,GetStgFrameWidth/2,GetStgFrameHeight/2,0);
		
		
		alp;
			task alp{
			loop(128){
				ObjRender_SetAlpha(spellObj,down/4);
				ObjRender_SetAlpha(spellObj0,down/4);
				ObjRender_SetAlpha(spellObj1,down);
				down+=2;
				yield;
			}
			down = 0;
		}
		
		let slide = 0;
		loop{
			if(GetCommonData("Is Spell?",false) == false){dissolve;}
			ObjRender_SetAngleZ(spellObj,ObjRender_GetAngleZ(spellObj)+0.2);
			ObjRender_SetAngleZ(spellObj0,ObjRender_GetAngleZ(spellObj0)-0.2);
			ObjSprite2D_SetSourceRect(spellObj1,0,0+slide,GetStgFrameWidth+2,GetStgFrameHeight+slide);
			slide+=0.4;
			yield;
		}
		task dissolve{
			loop(255){
				ObjRender_SetAlpha(spellObj,(255/8)-down);
				ObjRender_SetAlpha(spellObj0,(255/8)-down);
				ObjRender_SetAlpha(spellObj1,255-down);
				down+=1;
				yield;
			}
			down = 0;
			Obj_Delete(spellObj);
			Obj_Delete(spellObj0);
			Obj_Delete(spellObj1);
		}
	}
}

// --------------------------------------------------------------------

task VariableTest{ // Debug Function by Ultima, thanks!

	let obj = ObjText_Create;
	Obj_SetVisible(obj,false);
	ObjText_SetText(obj,IntToString(GetCameraFocusX)~"");
	ObjText_SetFontSize(obj,32);
	ObjRender_SetPosition(obj,420,25,0);
	Obj_SetRenderPriorityI(obj,100);
	ObjText_SetFontType(obj,"MeiryoKe_PGothic");
	ObjText_SetFontColorTop(obj, 155, 155, 155);
	ObjText_SetFontColorBottom(obj, 155, 155, 155);
	ObjText_SetFontBold(obj, true);
	ObjText_SetHorizontalAlignment(obj, ALIGNMENT_LEFT);
	ObjText_SetFontBorderType(obj, BORDER_SHADOW);
	ObjText_SetFontBorderWidth(obj, 2);
	ObjText_SetFontBorderColor(obj, 255, 255, 255);
	
	loop{
	if(GetKeyState(KEY_D)==KEY_PUSH&&!Obj_IsVisible(obj)){yield;Obj_SetVisible(obj,true);}
	if(GetKeyState(KEY_D)==KEY_PUSH&&Obj_IsVisible(obj)){yield;Obj_SetVisible(obj,false);}

	ObjText_SetText(obj,"XFoc: "~IntToString(GetCameraFocusX)~"[r]
	YFoc: "~IntToString(GetCameraFocusY)~"[r]
	ZFoc: "~IntToString(GetCameraFocusZ)~"[r]
	AziAng: "~IntToString(GetCameraAzimuthAngle)~"[r]
	CamRad: "~IntToString(GetCameraRadius)~"[r]
	EleAng: "~IntToString(GetCameraElevationAngle)~"[r]
	Yaw: "~IntToString(GetCameraYaw)~"[r]
	Pitch: "~IntToString(GetCameraPitch)~"[r]
	Roll: "~IntToString(GetCameraRoll)~"[r]
	XPos: "~IntToString(GetCameraX)~"[r]
	YPos: "~IntToString(GetCameraY)~"[r]
	ZPos: "~IntToString(GetCameraZ)~""
	);

	yield;
	}
	//END OF TASK
}