let CSD = GetCurrentScriptDirectory;

#include "./System/Function_Enemy.txt"

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

task Init {
	Op;
	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 Op{
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,24.5);
	ObjPrim_SetTexture(obj,CSD ~ "/Texture/Black.png");
	ObjSprite2D_SetSourceRect(obj,0,0,480,400);
	ObjSprite2D_SetDestRect(obj,0,0,GetStgFrameWidth, GetStgFrameHeight);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetAlpha(obj,255);
	
	let A=255;
	loop(255){
		ObjRender_SetAlpha(obj,A);
		A--;
		wait(1);
		yield;
	}
	Obj_Delete(obj);
 }

function makeFloor(locX,locY,locZ) {
	let A=0;
	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,5,15,0);
	ObjRender_SetZWrite(obj, true);
	
	ObjRender_SetAngleXYZ(obj,90,0,0);
//	ObjRender_SetColor(obj,60,100,60);
	ObjRender_SetPosition(obj,locX,locY,locZ);
	
}

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,24);
	ObjSprite3D_SetSourceDestRect(obj,0,0,800,800);
	ObjRender_SetScaleXYZ(obj,0.5,0.5,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{
		A++;
		//ObjRender_SetAlpha(obj, A);
		ObjRender_SetAngleXYZ(obj,180,A,0);
		yield;
	}
}

task makeFire2(locX,locY,locZ) {
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	let f = 0;
	let i = 0;
	let i2 = 0;
	let A = 0;
	let X = 0;
	let X2 = 48;
	ObjPrim_SetTexture(obj, texFire2);
	Obj_SetRenderPriorityI(obj,25);
	ObjSprite3D_SetSourceDestRect(obj,0,0,894,894);
	ObjRender_SetScaleXYZ(obj,0.3,0.3,0);
	ObjRender_SetZWrite(obj, true);
	ObjRender_SetAlpha(obj, 255);
	
	ObjRender_SetAngleXYZ(obj,180,0,0);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetPosition(obj,locX,locY,locZ);
	ObjRender_SetColor(obj,255,190,50);
	
	loop{
		ObjSprite3D_SetSourceRect(obj,X,0,X2,48);
		X+=48;
		X2+=48;
		A+=2;
		i = sin(f)*36;
		i2 = cos(f)*36;
		f+=2;
		ObjRender_SetAngleXYZ(obj,180,A,0);
		ObjRender_SetPosition(obj,locX+i,locY,locZ+i2);

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

task makeWall(locX,locY,locZ,ang){
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	let A =0;
	ObjPrim_SetTexture(obj, texWall);
	Obj_SetRenderPriorityI(obj,21);
	ObjSprite3D_SetSourceDestRect(obj,-30,0,610,580);
	ObjRender_SetScaleXYZ(obj,2,-2,0);
	ObjRender_SetZWrite(obj, true);
	
	ObjRender_SetAngleXYZ(obj,0,ang,0);
	ObjRender_SetColor(obj,60,50,60);
	ObjRender_SetPosition(obj,locX,locY,locZ);
	loop{
		
	ObjSprite3D_SetSourceDestRect(obj,-30+A,0,610+A,580);
		A++;
	yield;}
}

task renderStage{
	makeFire(0,150,0);
	makeFire2(0,150,0);
	makeFloor(0,0,0);
	
	makeWall(0,200,1000,0);
	makeWall(500,200,500,45);
	makeWall(1000,200,0,90);
	makeWall(500,200,-500,135);
	makeWall(0,200,-1000,180);
	makeWall(-500,200,-500,225);
	makeWall(-1000,200,0,270);
	makeWall(-500,200,500,315);
	
}

task scrollStage{
	
	let i = 0;
	let i2 = 0;
	let i3 = 0;
	let f = 0;
	
	SetCameraPerspectiveClip(16,4096);
	SetCameraAzimuthAngle(-90);
	SetCameraElevationAngle(15);
	SetCameraRadius(64);
	
	SetCameraFocusX(0);
	SetCameraFocusY(155);
	SetCameraFocusZ(-100);
	
	SetCameraYaw(0);
	SetCameraPitch(0);
	SetCameraRoll(0);
	
	loop{
		i = sin(f)*360;
		i2 = cos(f)*360;
		i3 = sin(f)*25;
		f+=1;
		scroll+=1;
		
		SetCameraFocusX(0+i);
		SetCameraFocusZ(0+i2);
		//SetCameraRoll(0+i3);
		SetCameraElevationAngle(i3/4);
		
		SetCameraAzimuthAngle(90-scroll);

	yield;
	}
}