let CSD = GetCurrentScriptDirectory;

let texFloor = CSD ~ "Texture/stage02a.png";
let texFloor2 = CSD ~ "Texture/fog2.png";
let texWall = CSD ~ "Texture/forest.png";
let texTree = CSD ~ "Texture/tree.png";
let texfogBG = CSD ~ "Texture/fog.png";
let texFire = CSD ~ "Texture/enemy_aura.png";
	let scroll = 0;

task Init {
	InitFrame;
	renderStage;
	scrollStage;
 }

 function InitFrame(){
	let path = CSD ~ "Texture/Background.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, path);
	Obj_SetRenderPriority(obj, 0);
	ObjSprite2D_SetSourceRect(obj, 0, 0, 640, 480);
	ObjSprite2D_SetDestRect(obj, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
}

task makeFogBG(locX,locY,locZ,red,green,blue) {
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	let A = 250;
	let B = 100;
	ObjPrim_SetTexture(obj, texfogBG);
	Obj_SetRenderPriorityI(obj,20);
	ObjSprite3D_SetSourceDestRect(obj,0,0,4096,4096);
	ObjRender_SetScaleXYZ(obj,1,1,0);
	ObjRender_SetZWrite(obj, true);

	ObjRender_SetPosition(obj,locX,locY,locZ);

	ObjRender_SetColor(obj,red,green,blue);

	ObjSprite3D_SetBillboard(obj,true);

	loop{
		ObjRender_SetPosition(obj,locX,locY,locZ+scroll);
		yield;
	}

}

function makeFloor(locX,locY,locZ) {
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj, texFloor);
	Obj_SetRenderPriorityI(obj,21);
	ObjSprite3D_SetSourceDestRect(obj,0,0,512,128);
	ObjRender_SetScaleXYZ(obj,2,5,0);
	ObjRender_SetZWrite(obj, true);
	
	ObjRender_SetAngleXYZ(obj,90,0,0);
	ObjRender_SetColor(obj,60,100,60);
	ObjRender_SetPosition(obj,locX,locY,locZ);
}

task makeFloor2(locX,locY,locZ) {
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	let Y = 0;
	ObjPrim_SetTexture(obj, texFloor2);
	Obj_SetRenderPriorityI(obj,21);
	ObjSprite3D_SetSourceDestRect(obj,0,0,894,894);
	ObjRender_SetScaleXYZ(obj,1,0.6823,0);
	ObjRender_SetZWrite(obj, true);
	
	ObjRender_SetAngleXYZ(obj,90,0,0);
	ObjRender_SetBlendType(obj,BLEND_ADD_RGB);
	ObjRender_SetPosition(obj,locX,locY,locZ);

	ObjRender_SetColor(obj,75,50,75);
	loop{
		ObjSprite3D_SetSourceDestRect(obj,0,Y,894,894+Y);
		Y+=3;
		wait(1);
		yield;
	}
}

task makeFire(locX,locY,locZ) {
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	let A = 0;
	let X = 0;
	let X2 = 48;
	ObjPrim_SetTexture(obj, texFire);
	Obj_SetRenderPriorityI(obj,25);
	ObjSprite3D_SetSourceDestRect(obj,0,0,894,894);
	ObjRender_SetScaleXYZ(obj,0.1,0.1,0);
	ObjRender_SetZWrite(obj, true);
	ObjRender_SetAlpha(obj, 0);
	
	ObjRender_SetAngleXYZ(obj,180,0,0);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetPosition(obj,locX,locY,locZ);
	ObjRender_SetColor(obj,200,200,50);
	
	loop{
		ObjRender_SetPosition(obj,locX,locY,locZ+scroll);
		ObjSprite3D_SetSourceRect(obj,X,0,X2,48);
		X+=48;
		X2+=48;
		if(A<255){A++;}
		ObjRender_SetAlpha(obj, A);

		if(X >= 380-48){ X = 0;}
		if(X2 >= 380){ X2 = 48;}
		wait(6);
		yield;
	}
}

function makeWall(locX,locY,locZ){
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj, texWall);
	Obj_SetRenderPriorityI(obj,21);
	ObjSprite3D_SetSourceDestRect(obj,-30,0,610,580);
	ObjRender_SetScaleXYZ(obj,1,-1,0);
	ObjRender_SetZWrite(obj, true);
	
	ObjRender_SetAngleXYZ(obj,0,90,0);
	ObjRender_SetColor(obj,60,50,60);
	ObjRender_SetPosition(obj,locX,locY,locZ);
}

function makeTree(locX,locY,locZ,AngleY){
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj, texTree);
	Obj_SetRenderPriorityI(obj,21);
	ObjSprite3D_SetSourceDestRect(obj,0,0,800,710);
	ObjRender_SetScaleXYZ(obj,0.5,1,0);
	ObjRender_SetZWrite(obj, true);
	
	ObjRender_SetAngleXYZ(obj,180,AngleY,0);
	ObjRender_SetColor(obj,160,150,160);
	ObjRender_SetPosition(obj,locX,locY,locZ);
}

task renderStage{ 
	SetFogEnable(true);
	SetFogParam(1900,2200,50,0,50);
	makeFogBG(0,0,3050,128,255,128);

	makeFloor(0,0,0);
	makeFloor(0,0,610);
	makeFloor(0,0,1220);
	makeFloor(0,0,1830);
	makeFloor(0,0,2440);
	makeFloor(0,0,2440+610);
	makeFloor(0,0,2440+1220);
	
	makeWall(-400,305,0);
	makeWall(-400,305,610);
	makeWall(-400,305,1220);
	makeWall(-400,305,1830);
	makeWall(-400,305,2440);
	makeWall(-400,305,2440+610);
	makeWall(-400,305,2440+1220);
	
	makeWall(400,305,0);
	makeWall(400,305,610);
	makeWall(400,305,1220);
	makeWall(400,305,1830);
	makeWall(400,305,2440);
	makeWall(400,305,2440+610);
	makeWall(400,305,2440+1220);
	
	makeTree(-200,305,0,0);
	makeTree(-200,305,610,0);
	makeTree(-200,305,1220,0);
	makeTree(-200,305,1830,0);
	makeTree(-200,305,2440,0);
	makeTree(-200,305,2440+610,0);
	makeTree(-200,305,2440+1220,0);
	
	makeTree(200,305,0,180);
	makeTree(200,305,610,180);
	makeTree(200,305,1220,180);
	makeTree(200,305,1830,180);
	makeTree(200,305,2440,180);
	makeTree(200,305,2440+610,180);
	makeTree(200,305,2440+1220,180);
	
	makeFloor2(0,50,0);
	makeFloor2(0,50,610);
	makeFloor2(0,50,1220);
	makeFloor2(0,50,1830);
	makeFloor2(0,50,2440);
	makeFloor2(0,50,2440+610);
	makeFloor2(0,50,2440+1220);
	

}

task scrollStage{
	
	let i = 0;
	let f = 0;
	
	SetCameraPerspectiveClip(16,4096);
	SetCameraAzimuthAngle(-90);
	SetCameraElevationAngle(15);
	SetCameraRadius(64);
	
	SetCameraFocusX(0);
	SetCameraFocusY(255);
	SetCameraFocusZ(0);
	
	SetCameraYaw(0);
	SetCameraPitch(0);
	SetCameraRoll(0);
	
	loop{
		i = sin(f)*25;
		f+=0.5;
		scroll+=4;
		
		SetCameraRoll(0+i);
		SetCameraYaw(0+i/2);
		
		SetCameraFocusZ(-305+scroll);
		if(scroll > 610){
			scroll = 0;
		}
	yield;
	}
}