let CSD = GetCurrentScriptDirectory;
let MD = GetModuleDirectory;

let bg1 = "";
let bg2 = "";

let cloud1 = CSD ~ "IMG/Background/cloud1.png";
let moon = CSD ~ "IMG/Background/moon.png";

let stgtime = 0;
let stgspeed = 0;

let ResetStage = false;

let brush_frequency = 0;
let brush_groups = 0;
let brush_organization = 0;
let brush_distance = 0;

let SpellActive = false;
let GetCenterX = GetStgFrameWidth/2;
let GetCenterY = GetStgFrameHeight/2;

@Initialize {
	
	LoadTexture(CSD ~ "IMG/Background/spellbg1.png");
	LoadTexture(CSD ~ "IMG/Background/spellbg2.png");
	LoadTexture(CSD ~ "IMG/Background/ground1.png");
	LoadTexture(CSD ~ "IMG/Background/ground2.png");
	LoadTexture(CSD ~ "IMG/Background/cloud1.png");
	
	
	StageBG;
	SpellBG;
}
@MainLoop {
	let objScene = GetEnemyBossSceneObjectID;
	if(objScene != ID_INVALID && ObjEnemyBossScene_GetInfo(objScene, INFO_IS_SPELL)) {
		SpellActive = true;
	}
	else {
		SpellActive = false;
	}
	yield;
}
task SpellBG {
	let spin = 0;
	let alpha = 0;

	let path1 = CSD ~ "IMG/Background/spellbg1.png";
	let obj1 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj1, BLEND_ALPHA);
	Obj_SetRenderPriority(obj1, 0.21);
	ObjPrim_SetTexture(obj1, path1);
	ObjSprite2D_SetSourceRect(obj1, 0, 0, 256, 256);
	ObjSprite2D_SetDestCenter(obj1);

	let path2 = CSD ~ "IMG/Background/spellbg2.png";
	let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj2, BLEND_ALPHA);
	Obj_SetRenderPriority(obj2, 0.21);
	ObjPrim_SetTexture(obj2, path2);
	ObjSprite2D_SetSourceRect(obj2, 0, 0, 256, 256);
	ObjSprite2D_SetDestCenter(obj2);
	
	loop {
		ObjRender_SetPosition(obj1, GetCenterX, GetCenterY, 0);
		ObjRender_SetPosition(obj2, GetCenterX, GetCenterY, 0);
		ObjRender_SetAngleZ(obj1, -spin);
		ObjRender_SetAngleZ(obj2, spin);
		ObjRender_SetScaleXYZ(obj1, 2.4, 2.4, 2.4);
		ObjRender_SetScaleXYZ(obj2, 2.4, 2.4, 2.4);
		ObjRender_SetAlpha(obj1, alpha);
		ObjRender_SetAlpha(obj2, alpha);
		spin+=0.5;
		if(SpellActive) {
			Obj_SetVisible(obj1, true);
			Obj_SetVisible(obj2, true);
			alpha+=4;
		}
		else {
			Obj_SetVisible(obj1, false);
			Obj_SetVisible(obj2, false);
			spin = 0;
			alpha = 0;
		}
		yield;
	}
}
task ResetBackground {
	ResetStage = true;
	yield;
	ResetStage = false;
}
task StageBG {
	let StageBG1;	
	let StageBG2;	
	let StageBGSky;
	let ypos = 0;
	
	bg1 = CSD ~ "IMG/Background/ground1.png";
	bg2 = CSD ~ "IMG/Background/ground2.png";
	brush_frequency = 4;
	brush_groups = 6;
	brush_organization = 2;
	brush_distance = 10;
	
	
	SetCameraFocusX(0);
	SetCameraFocusY(0);
	SetCameraFocusZ(100);
	SetCameraRadius(300);
	SetCameraElevationAngle(285);
	SetCameraAzimuthAngle(90);
	
	StageBGSky = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjRender_SetBlendType(StageBGSky, BLEND_ADD_RGB);
	ObjRender_SetAlpha(StageBGSky, 255);
	ObjPrim_SetTexture(StageBGSky, NULL);
	ObjSprite3D_SetSourceRect(StageBGSky, 0, 0, 256, 256);
	ObjSprite3D_SetDestRect(StageBGSky, -386, -386, 386, 386);
	ObjRender_SetZWrite(StageBGSky, true);
	ObjRender_SetZTest(StageBGSky, true);
	
	StageBG1 = ObjPrim_Create(OBJ_SPRITE_3D);
	let bg1size = 512;
	ObjRender_SetBlendType(StageBG1, BLEND_ALPHA);
	ObjRender_SetAlpha(StageBG1, 255);
	ObjPrim_SetTexture(StageBG1, bg1);
	ObjSprite3D_SetSourceRect(StageBG1, 0, ypos, bg1size, ypos+bg1size*4);
	ObjSprite3D_SetDestRect(StageBG1, -bg1size/6, -bg1size*1.5, bg1size/6, bg1size*1.5);
	ObjRender_SetZWrite(StageBG1, true);
	ObjRender_SetZTest(StageBG1, true);
	
	StageBG2 = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjRender_SetBlendType(StageBG2, BLEND_ALPHA);
	ObjRender_SetAlpha(StageBG2, 255);
	ObjPrim_SetTexture(StageBG2, bg2);
	ObjSprite3D_SetSourceRect(StageBG2, 0, 0, bg1size*4, bg1size*4);
	ObjSprite3D_SetDestRect(StageBG2, -bg1size*1.5, -bg1size, bg1size*1.5, bg1size);
	ObjRender_SetZWrite(StageBG2, true);
	ObjRender_SetZTest(StageBG2, true);	
	
	ObjRender_SetPosition(StageBG1, 0, 386, -89);
	ObjRender_SetAngleXYZ(StageBG1, 0, 0, 0);
	Obj_SetRenderPriority(StageBG1, 0.22);
	
	ObjRender_SetPosition(StageBG2, 15, 390, -92);
	ObjRender_SetAngleXYZ(StageBG2, 0, 0, 90);
	Obj_SetRenderPriority(StageBG2, 0.22);
	
	ObjRender_SetPosition(StageBGSky, 0, 900, 0);
	ObjRender_SetAngleXYZ(StageBGSky, 90, 90, 0);
	Obj_SetRenderPriority(StageBGSky, 0.22);
		
	SetFogEnable(true);
	
	// CreateRandomizedCloudGroupsA1;
	yield;
	while(!ResetStage){
		if(SpellActive) {
			Obj_SetVisible(StageBG1,false);
			Obj_SetVisible(StageBG2,false);
			Obj_SetVisible(StageBGSky,false);
		}
		else {
			Obj_SetVisible(StageBG1,true);
			Obj_SetVisible(StageBG2,true);
			Obj_SetVisible(StageBGSky,false);
		}
		
		stgspeed = min(stgspeed + 0.025, 5);
		
		SetFogParam(1200, 1500, 0, 0, 0);
		ObjRender_SetColor(StageBG1, 128, 64, 64);
		ObjRender_SetColor(StageBG2, 128, 64, 64);
		ObjRender_SetColor(StageBGSky, 128, 128, 128);
		stgtime = min(stgtime+0.0005, 1);
		
		ObjSprite3D_SetSourceRect(StageBG1, 0, ypos, bg1size, ypos+bg1size*4);
		ObjSprite3D_SetSourceRect(StageBG2, ypos, 0, ypos+bg1size*4, bg1size*4);
		ypos+=stgspeed;
		if(ypos > 512){ypos = 0}
		yield;
	}
	Obj_Delete(StageBG1);
	Obj_Delete(StageBG2);
	Obj_Delete(StageBGSky);
}

task CreateRandomizedCloudGroupsA1{
	let frame = 0;
	
	ascent(i in 0..1600){
		if(frame>=brush_frequency*2){
			loop(rand(0,brush_frequency)){
				CreateCloudA1(i);
			}
			frame = 0;
		}
		frame++;
	}

	loop{
		if(frame<=0){
			loop(rand(0,brush_frequency/3)){
				CreateCloudA1(0);
			}
			frame = 4;
		}
		frame--;
		yield;
	}
}
task CreateCloudA1(beginbrush){
	let brush = Create3DProperties(255, 128, 0+(128*round(rand(0,1))), cloud1, 0.23);
	let locpos = [rand(-450, 450), rand(1300,1600), -64];
	
	while(absolute(locpos[0]) < 96){ 
		//Modify 96 to determine how close bushes can be to the path
		locpos[0] = rand(-450, 450)
	}	
	locpos[0] = locpos[0] - 64;
	ObjRender_SetPosition(brush, locpos[0], locpos[1], locpos[2]);
	ObjRender_SetAngleXYZ(brush, 270, 0, 0);
	ObjRender_SetScaleXYZ(brush, 1, 1, 1);
	Obj_SetRenderPriority(brush, 0.22);
	
	
	if(beginbrush!=0){
		locpos[1] = locpos[1]-beginbrush*4 - 200;
	}
	
	while(locpos[1]>-64 && !ResetStage){
		locpos[1]=locpos[1]-stgspeed/1.25;
		ObjRender_SetPosition(brush, locpos[0], locpos[1], locpos[2]);
		ObjRender_SetColor(brush, 127, 127, 127);
		if(SpellActive) {
			Obj_SetVisible(brush,false);
		}
		else {
			Obj_SetVisible(brush,true);
		}
		yield;
	}
	Obj_Delete(brush);
}

function Create3DProperties(alpha, srcrect, offset, image, priority){
	let StageProp = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjRender_SetBlendType(StageProp, BLEND_ALPHA);
	ObjRender_SetAlpha(StageProp, alpha);
	ObjPrim_SetTexture(StageProp, image);
	Obj_SetRenderPriority(StageProp, priority);
	ObjSprite3D_SetSourceRect(StageProp, 0, 0, 128, 128);
	ObjSprite3D_SetDestRect(StageProp, 0, -64, 128, 64); // Centered middle origin
	ObjRender_SetZWrite(StageProp, true);
	ObjRender_SetZTest(StageProp, true);
	
	return StageProp;
}

task CameraFlight{
	// Static Version
	let pos = [0,0,100];
	let ang = [90,295,300];
	let cammod = 1.25;
	loop{
		if(GetKeyState(KEY_J)==KEY_HOLD){pos[0]=pos[0]+5*cammod;}
		if(GetKeyState(KEY_L)==KEY_HOLD){pos[0]=pos[0]-5*cammod;}
		if(GetKeyState(KEY_I)==KEY_HOLD){pos[1]=pos[1]+5*cammod;}
		if(GetKeyState(KEY_K)==KEY_HOLD){pos[1]=pos[1]-5*cammod;}
		if(GetKeyState(KEY_U)==KEY_HOLD){pos[2]=pos[2]-5*cammod;}
		if(GetKeyState(KEY_O)==KEY_HOLD){pos[2]=pos[2]+5*cammod;}
		
		
		if(GetKeyState(KEY_Q)==KEY_HOLD){ang[0]=ang[0]+2*cammod;}
		if(GetKeyState(KEY_E)==KEY_HOLD){ang[0]=ang[0]-2*cammod;}
		if(GetKeyState(KEY_W)==KEY_HOLD){ang[1]=ang[1]+2*cammod;}
		if(GetKeyState(KEY_S)==KEY_HOLD){ang[1]=ang[1]-2*cammod;}
		if(GetKeyState(KEY_A)==KEY_HOLD){ang[2]=ang[2]+2*cammod;}
		if(GetKeyState(KEY_D)==KEY_HOLD){ang[2]=ang[2]-2*cammod;}
		
		SetCameraFocusX(pos[0]);
		SetCameraFocusY(pos[1]);
		SetCameraFocusZ(pos[2]);
		SetCameraAzimuthAngle(ang[0]);	
		SetCameraElevationAngle(ang[1]);
		SetCameraRadius(ang[2]);
		yield;
	}
}

function DrawQuickText(txtStr, txtX, txtY){
	let SnowDrawText = ObjText_Create;
	let presetTxtColor = "black";
	//DrawText("", GetPlayerX-24, GetPlayerY, 11, 300, "black", 0.80);
	ObjText_SetText(SnowDrawText,txtStr);
	SetTextColor;
	ObjText_SetFontSize(SnowDrawText,16);
	ObjText_SetHorizontalAlignment(SnowDrawText, ALIGNMENT_LEFT);
	ObjText_SetFontBorderWidth(SnowDrawText,1);
	ObjText_SetFontBorderType(SnowDrawText,BORDER_FULL);
	ObjText_SetMaxWidth(SnowDrawText, 300);
	Obj_SetRenderPriority(SnowDrawText, 0.9);
	ObjRender_SetPosition(SnowDrawText, txtX, txtY, 0);
	
	task SetTextColor{
		if(presetTxtColor=="black"){
			ObjText_SetFontColorTop(SnowDrawText,0,0,0);
			ObjText_SetFontColorBottom(SnowDrawText,0,0,0);
			ObjText_SetFontBorderColor(SnowDrawText,161,161,255);
		}
		if(presetTxtColor=="red"){
			ObjText_SetFontColorTop(SnowDrawText,175,20,111);
			ObjText_SetFontColorBottom(SnowDrawText,190,10,123);
			ObjText_SetFontBorderColor(SnowDrawText,0,0,64);
		}
		if(presetTxtColor=="blue"){
			ObjText_SetFontColorTop(SnowDrawText,20,80,175);
			ObjText_SetFontColorBottom(SnowDrawText,80,50,175);
			ObjText_SetFontBorderColor(SnowDrawText,0,0,64);
		}
	}
	
	return SnowDrawText;
}
