
let MainSTG = GetMainStgScriptDirectory;
let img_effect = MainSTG~"ds_system/img/effect.png"; // Effect Sheet


function PlayMusic(str){
	let objSound = ObjSound_Create;
	ObjSound_SetSoundDivision(objSound, SOUND_BGM);
	ObjSound_SetLoopEnable(objSound, true);
	ObjSound_SetVolumeRate(objSound, 100);
	if(str=="magusnight"){
		LoadSound(MainSTG~"source/marisabgm.ogg");
		ObjSound_Load(objSound, MainSTG~"source/marisabgm.ogg");
		ObjSound_SetLoopTime(objSound, 0, 126);  //206 ?
		ObjSound_Play(objSound);
	}
	return objSound;
}


function DrawText(txtStr, txtX, txtY, txtSize, txtWidth, presetTxtColor, txtPriority){
	let SnowDrawText = ObjText_Create;
	ObjText_SetText(SnowDrawText,txtStr);
	SetTextColor;
	ObjText_SetFontSize(SnowDrawText,txtSize);
	ObjText_SetHorizontalAlignment(SnowDrawText, ALIGNMENT_LEFT);
	ObjText_SetFontBorderWidth(SnowDrawText,1);
	ObjText_SetFontBorderType(SnowDrawText,BORDER_FULL);
	ObjText_SetMaxWidth(SnowDrawText, txtWidth);
	Obj_SetRenderPriority(SnowDrawText, txtPriority);
	ObjRender_SetPosition(SnowDrawText, txtX, txtY, 0);
	
	SetTextColor;
	
	task SetTextColor{
		if(presetTxtColor=="black"){
			ObjText_SetFontColorTop(SnowDrawText,42,42,42);
			ObjText_SetFontColorBottom(SnowDrawText,42,42,42);
			ObjText_SetFontBorderColor(SnowDrawText,142,142,142);
		}
		if(presetTxtColor=="red"){
			ObjText_SetFontColorTop(SnowDrawText,175,20,111);
			ObjText_SetFontColorBottom(SnowDrawText,190,10,123);
			ObjText_SetFontBorderColor(SnowDrawText,142,142,142);
		}
		if(presetTxtColor=="blue"){
			ObjText_SetFontColorTop(SnowDrawText,20,80,175);
			ObjText_SetFontColorBottom(SnowDrawText,80,50,175);
			ObjText_SetFontBorderColor(SnowDrawText,142,142,142);
		}
		if(presetTxtColor=="white"){
			ObjText_SetFontColorTop(SnowDrawText,255,255,255);
			ObjText_SetFontColorBottom(SnowDrawText,185,185,185);
			ObjText_SetFontBorderColor(SnowDrawText,32,32,32);
		}
	}
	
	return SnowDrawText;
}

function CreateEnemyA1(enemytype, x, y, enmspd, enmang, life, fcolor, argument, argument2){
	let enemyscript;
	if(enemytype == "Enemy1"){enemyscript = LoadScript(dir ~ "Enemy01.txt");}
	if(enemytype == "Boss1"){enemyscript = LoadScript(dir ~ "SnowPlural.txt");}
	SetScriptArgument(enemyscript, 1, fcolor);
	SetScriptArgument(enemyscript, 2, x);
	SetScriptArgument(enemyscript, 3, y);
	SetScriptArgument(enemyscript, 4, enmspd);
	SetScriptArgument(enemyscript, 5, enmang);
	SetScriptArgument(enemyscript, 6, argument);
	SetScriptArgument(enemyscript, 7, argument2);
	SetScriptArgument(enemyscript, 8, life);
	StartScript(enemyscript);
	return enemyscript;
}

function Wait(waitframes){
	loop(waitframes){yield}
}

task LoadStageSounds{
	LoadSound(MainSTG~"source/Sounds/selectok.wav");
	LoadSound(MainSTG~"source/Sounds/selectcancel.wav");
	LoadSound(MainSTG~"source/Sounds/selectinvalid.wav");
	LoadSound(MainSTG~"source/Sounds/select.wav");
	LoadSound(MainSTG~"source/Sounds/cancel.wav");
	LoadSound(MainSTG~"source/Sounds/pause.wav");
	LoadSound(MainSTG~"source/Sounds/barrage.wav");
}

task PlaySound(str){
	PlaySE(MainSTG~"source/Sounds/"~str~".wav");
}

task SelectionScreen{
	let menuscreen = ObjPrim_Create(OBJ_SPRITE_2D);
	let finalalpha = 255;
	ObjRender_SetBlendType(menuscreen, BLEND_ALPHA);
	Obj_SetRenderPriorityI(menuscreen, 74);
	
	while(SelectionSet!=2){
		yield;
	}
	
	SetCommonData("SelectionSet", 2);
	ObjPrim_SetTexture(menuscreen, MainSTG~"source/img_select2.png");
	ObjSprite2D_SetSourceRect(menuscreen, 0, 0, 12, 12);
	ObjSprite2D_SetDestRect(menuscreen, 0, 0, 416, 464);
	while(finalalpha>0){
		finalalpha-=255/35;
		ObjRender_SetAlpha(menuscreen, finalalpha);
		yield;
	}
	
	Obj_Delete(menuscreen);
	
}

task FallingLeafA1(deX, deY, colorR, colorG, colorB, deScale){
	
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	let obj_time = 0;
	let angX = rand(-360, 360);
	let angY = rand(-360, 360);
	let angZ = rand(-360, 360);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	Obj_SetRenderPriority(obj, 0.75);
	ObjPrim_SetTexture(obj, img_effect);
	ObjSprite2D_SetSourceRect(obj, 0, 228, 33, 251);
	ObjSprite2D_SetDestRect(obj, -16, -12, 16, 12);
	ObjRender_SetAngleXYZ(obj, angX, angY, angZ);
	ObjRender_SetPosition(obj, deX, deY, 0);
	ObjRender_SetColor(obj, colorR, colorG, colorB);
	ObjRender_SetAlpha(obj, 0);
	
	let alpha = 0;
	let scale = deScale;
	let randX = rand(50, 500);
	let randY = rand(50, 500);
	let RandZ = rand(17,170);
	//atan2(randY - deX, randX - deY);
	while(Obj_IsDeleted(obj)==false){
		obj_time++;
		
		if(obj_time<180 && alpha<105){alpha+=3}
		if(obj_time>180){alpha += -1;}
		alpha += 0.1;
		deX += randX/250;
		deY += randY/250;
		angX += randX/255;
		angY += randY/255;
		angZ += randX/295+randY/295;
		
		ObjRender_SetPosition(obj, deX, deY, 0);
		ObjRender_SetAngleXYZ(obj, angX, angY, angZ);
		ObjRender_SetAlpha(obj, alpha);
		ObjRender_SetScaleXYZ(obj, scale, scale, scale);
		
		if(alpha<0){Obj_Delete(obj)}
		if(SelectionSet==2){Obj_Delete(obj)}
		yield;
	}
	Obj_Delete(obj);
}

task SetStageDifficulty{
	let Easy;
	let Normal;
	let Hard;
	let Lunatic;
	let Ultra;
	let Instructions1;
	let Instructions2;
	let fontafcolor = "red";
	let StageTime = 0;
	let maxsel = 5;
	
	let CenterX = GetStgFrameWidth/2;
	let CenterY = GetStgFrameHeight/2;
	Easy = DrawText("Easy", CenterX-18, CenterY-150, 18, 300, "black", 0.80);
	Normal = DrawText("Normal", CenterX-27, CenterY-125, 18, 300, "red", 0.80);
	Hard = DrawText("Hard", CenterX-18, CenterY-100, 18, 300, "black", 0.80);
	Lunatic = DrawText("Lunatic", CenterX-31.5, CenterY-75, 18, 300, "black", 0.80);
	Ultra = DrawText("Overdrive", CenterX-42, CenterY-50, 18, 300, "black", 0.80);
	Instructions1 = DrawText("Use the Arrow Keys to change selection.", CenterX-135, CenterY-210, 12, 300, "white", 0.80);
	Instructions2 = DrawText("Press the shot button to select.", CenterX-110, CenterY-190, 12, 300, "white", 0.80);
	Obj_SetVisible(Ultra, false);
	
	while(SelectionSet==0){
	
		if(Selection==0){Selection=maxsel-1}
		if(Selection==1){SetTextColor(Easy, fontafcolor)}else{SetTextColor(Easy, "black")}
		if(Selection==2){SetTextColor(Normal, fontafcolor)}else{SetTextColor(Normal, "black")}
		if(Selection==3){SetTextColor(Hard, fontafcolor)}else{SetTextColor(Hard, "black")}
		if(Selection==4){SetTextColor(Lunatic, fontafcolor)}else{SetTextColor(Lunatic, "black")}
		if(Selection==5){SetTextColor(Ultra, fontafcolor)}else{SetTextColor(Ultra, "black")}
		if(Selection==maxsel){Selection=1}
	
		if(GetVirtualKeyState(VK_UP)==KEY_PUSH){Selection--;PlaySound("select");}
		if(GetVirtualKeyState(VK_DOWN)==KEY_PUSH){Selection++;PlaySound("select");}
		if(GetVirtualKeyState(VK_USER2)==KEY_PUSH){maxsel = 6;PlaySound("barrage");Obj_SetVisible(Ultra, true);}
		
		if(GetVirtualKeyState(VK_SHOT)==KEY_PUSH && StageTime<15 && StageTime>5){PlaySound("selectinvalid");}
		if(StageTime==15){fontafcolor="blue"}
		
		if(GetVirtualKeyState(VK_SHOT)==KEY_PUSH && StageTime>50){
			if(Selection==1){sdiff=12}
			if(Selection==2){sdiff=36}
			if(Selection==3){sdiff=48}
			if(Selection==4){sdiff=72}
			if(Selection==5){sdiff=96}
			SetCommonData("StageDiffic", sdiff);
			SelectionSet = 1;
			PlaySound("selectok");
		}
		StageTime++;
		yield;
	}
	Obj_Delete(Easy);
	Obj_Delete(Normal);
	Obj_Delete(Hard);
	Obj_Delete(Lunatic);
	Obj_Delete(Ultra);
	Obj_Delete(Instructions1);
	Obj_Delete(Instructions2);
}

task SetStageQuality{
	while(SelectionSet==0){yield;}
	let Easy;
	let Normal;
	let Hard;
	let Lunatic;
	let Instructions1;
	let Instructions2;
	let fontafcolor = "red";
	let StageTime = 0;
	Selection = 3;
	let CenterX = GetStgFrameWidth/2;
	let CenterY = GetStgFrameHeight/2;
	Easy = DrawText("None", CenterX-18, CenterY-150, 18, 300, "black", 0.80);
	Normal = DrawText("Little", CenterX-27, CenterY-125, 18, 300, "red", 0.80);
	Hard = DrawText("Normal", CenterX-28, CenterY-100, 18, 300, "black", 0.80);
	Lunatic = DrawText("Very High", CenterX-43, CenterY-75, 18, 300, "black", 0.80);
	let Description = DrawText("", 42, CenterY+135, 18, 300, "white", 0.80);
	ObjText_SetHorizontalAlignment(Description, ALIGNMENT_CENTER);
	
	Instructions1 = DrawText("Use the Arrow Keys to change selection.", CenterX-135, CenterY-210, 12, 300, "white", 0.80);
	Instructions2 = DrawText("Press the shot button to select.", CenterX-110, CenterY-190, 12, 300, "white", 0.80);
	
	while(SelectionSet==1){
	
		if(Selection==0){Selection=4}
		if(Selection==1){
			SetTextColor(Easy, fontafcolor);
			ObjText_SetText(Description,"Nearly all effects removed.  Minimal effects used for key notifications.");
			}else{SetTextColor(Easy, "black");}
		if(Selection==2){
			SetTextColor(Normal, fontafcolor);
			ObjText_SetText(Description,"Many effects reduced. Lifebaris reverted to old school.");
			}else{SetTextColor(Normal, "black");}
		if(Selection==3){
			SetTextColor(Hard, fontafcolor);
			ObjText_SetText(Description,"Default quality. Every effect is left as was intended.");
			}else{SetTextColor(Hard, "black");}
		if(Selection==4){
			SetTextColor(Lunatic, fontafcolor);
			ObjText_SetText(Description,"Many system intensive effectsadded. May cause notable lag.");
			}else{SetTextColor(Lunatic, "black");}
		if(Selection==5){Selection=1}
	
		if(GetVirtualKeyState(VK_UP)==KEY_PUSH){Selection--;PlaySound("select");}
		if(GetVirtualKeyState(VK_DOWN)==KEY_PUSH){Selection++;PlaySound("select");}
		
		if(GetVirtualKeyState(VK_SHOT)==KEY_PUSH && StageTime<50 && StageTime>5){PlaySound("selectinvalid");}
		
		if(GetVirtualKeyState(VK_BOMB)==KEY_PUSH){SelectionSet=0;SetStageDifficulty;SetStageQuality;PlaySound("selectcancel");}
		
		if(StageTime==51){fontafcolor="blue"}
		
		if(GetVirtualKeyState(VK_SHOT)==KEY_PUSH && StageTime>50){
			if(Selection==1){quality=0}
			if(Selection==2){quality=1}
			if(Selection==3){quality=2}
			if(Selection==4){quality=3}
			SetCommonData("quality", quality);
			SelectionSet = 2;
			PlaySound("selectok");
		}
		StageTime++;
		yield;
	}
	Obj_Delete(Easy);
	Obj_Delete(Normal);
	Obj_Delete(Hard);
	Obj_Delete(Lunatic);
	Obj_Delete(Instructions1);
	Obj_Delete(Instructions2);
	Obj_Delete(Description);
}

task SetTextColor(objectID, presetTxtColor){
	if(presetTxtColor=="black"){
		ObjText_SetFontColorTop(objectID,42,42,42);
		ObjText_SetFontColorBottom(objectID,42,42,42);
		ObjText_SetFontBorderColor(objectID,142,142,142);
	}
	if(presetTxtColor=="red"){
		ObjText_SetFontColorTop(objectID,175,20,111);
		ObjText_SetFontColorBottom(objectID,190,10,123);
		ObjText_SetFontBorderColor(objectID,142,142,142);
	}
	if(presetTxtColor=="blue"){
		ObjText_SetFontColorTop(objectID,20,80,175);
		ObjText_SetFontColorBottom(objectID,80,50,175);
		ObjText_SetFontBorderColor(objectID,142,142,142);
	}
}










