#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Settings"]
#Text["By Lushie/Lusushime"]
#include ".\Functions.dnh"
#Background[".\Stage0bg.dnh"]
#System[".\default_system/Default_System.txt"]
#Player[".\player/LoLK Marisa (Lushie)/MarisaH.dnh"]

let objBoss; //global variable, can be used anywhere
let objScene = GetEnemyBossSceneObjectID(); //declares global variable enemy boss scene object


@Event{
	alternative(GetEventType())
    case(EV_REQUEST_LIFE){ //Sets boss's life
		SetScriptResult(2000);
    }
    case(EV_REQUEST_TIMER){ //Sets attack time
        SetScriptResult(9999999);
    }
    case(EV_REQUEST_SPELL_SCORE){ //Spellcard bonus
        SetScriptResult(1000000);
    }
}

@Initialize{
	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS); //gives the variable a value, object ID that makes it a boss
	ObjEnemy_Regist(objBoss); //registers it as an enemy
	 //moves boss to destination

	MainTask; //calls MainTask task
}

@MainLoop{
	ObjEnemy_SetIntersectionCircleToShot(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 32); 
	ObjEnemy_SetIntersectionCircleToPlayer(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 24);
	// CircleToShot means any player shoot hitting this will damage the boss, CircleToPlayer kills the player on colission
	yield; //necessary for task functions to work
}

task startattacking{}

task MainTask {
	let datapath = GetCurrentScriptDirectory() ~ "data/Difficulty.dat";
	CreateCommonDataArea("mode");
	SetPlayerLife(9999);
	SetForbidPlayerShot(true);
	SetForbidPlayerSpell(true);
	SetPlayerSpeed(0, 0);
	bg;
	menugraphic;
	let vl = 45;

	while(stop == 0){
		if(GetVirtualKeyState(VK_OK) == KEY_PULL){
			stop += 1;
			SE_Play(select, vl)
		}

		if(GetVirtualKeyState(VK_UP) == KEY_PUSH){
			row -= 1;
			if(row < 1){row = 5;}
			SE_Play(select, vl);
		}
		else if(GetVirtualKeyState(VK_UP) == KEY_HOLD){
			frameKeyHold++;
			if(frameKeyHold == 30 || (frameKeyHold > 30 && (frameKeyHold % 10 == 0))){
				row -= 1;
				if(row < 1){row = 5;}
				SE_Play(select, vl);
			}
		}
		else if(GetVirtualKeyState(VK_DOWN) == KEY_PUSH){
			row += 1;
			if(row > 5){row = 1;}
			SE_Play(select, vl);
		}
		else if(GetVirtualKeyState(VK_DOWN) == KEY_HOLD){
			frameKeyHold++;
			if(frameKeyHold == 30 || (frameKeyHold > 30 && (frameKeyHold % 10 == 0))){
				row += 1;
				if(row > 5){row = 1;}
				SE_Play(select, vl);
			}
		}
		if(GetVirtualKeyState(VK_LEFT) == KEY_PUSH){
			if(row == 1){
				diffsel -= 1;
				if(diffsel < 0){diffsel = 2;}
			}
			if(row == 2){
				pdsel -= 1;
				if(pdsel < 0){pdsel = 1;}
			}
			if(row == 3){
				if(qsel == 0){qsel = 1;}
				else{qsel = 0;}
			}
			if(row == 4){
				if(wsel == 0){wsel = 1;}
				else{wsel = 0;}
			}
			if(row == 5){
				if(spdsel == 0){spdsel = 1;}
				else{spdsel = 0;}
			}
			SE_Play(select, vl);
		}
		else if(GetVirtualKeyState(VK_LEFT) == KEY_HOLD){
			frameKeyHold++;
			if(frameKeyHold == 30 || (frameKeyHold > 30 && (frameKeyHold % 10 == 0))){
				if(row == 1){
					diffsel -= 1;
					if(diffsel < 0){diffsel = 2;}
				}
				if(row == 2){
					pdsel -= 1;
					if(pdsel < 0){pdsel = 1;}
				}
				if(row == 3){
					if(qsel == 0){qsel = 1;}
					else{qsel = 0;}
				}
				if(row == 4){
					if(wsel == 0){wsel = 1;}
					else{wsel = 0;}
				}
				if(row == 5){
					if(spdsel == 0){spdsel = 1;}
					else{spdsel = 0;}
				}
				SE_Play(select, vl);
			}
		}
		else if(GetVirtualKeyState(VK_RIGHT) == KEY_PUSH){
			if(row == 1){
				diffsel += 1;
				if(diffsel > 2){diffsel = 0;}
			}
			if(row == 2){
				if(pdsel == 0){pdsel = 1;}
				else{pdsel = 0;}
			}
			if(row == 3){
				if(qsel == 0){qsel = 1;}
				else{qsel = 0;}
			}
			if(row == 4){
				if(wsel == 0){wsel = 1;}
				else{wsel = 0;}
			}
			if(row == 5){
				if(spdsel == 0){spdsel = 1;}
				else{spdsel = 0;}
			}
			SE_Play(select, vl);
		}
		
		else if(GetVirtualKeyState(VK_RIGHT) == KEY_HOLD){
			frameKeyHold++;
			if(frameKeyHold == 30 || (frameKeyHold > 30 && (frameKeyHold % 10 == 0))){
				if(row == 1){
					diffsel += 1;
					if(diffsel > 2){diffsel = 0;}
				}
				if(row == 2){
					if(pdsel == 0){pdsel = 1;}
					else{pdsel = 0;}
				}
				if(row == 3){
					if(qsel == 0){qsel = 1;}
					else{qsel = 0;}
				}
				if(row == 4){
					if(wsel == 0){wsel = 1;}
					else{wsel = 0;}
				}
				if(row == 5){
					if(spdsel == 0){spdsel = 1;}
					else{spdsel = 0;}
				}
				SE_Play(select, vl);
			}
		}
		else{
			frameKeyHold = 0;
		}
		yield;
	}

	
	SetAreaCommonData("mode", "difficulty", diffsel+1); 
	SetAreaCommonData("mode", "pointdevice", pdsel);
	SetAreaCommonData("mode", "quality", qsel);
	SetAreaCommonData("mode", "warning", wsel);
	SetAreaCommonData("mode", "speed", spdsel);
	SaveCommonDataAreaA2("mode", datapath);

	wait(ft);
	SetForbidPlayerShot(false);
	SetForbidPlayerSpell(false);
	
	ObjEnemy_SetLife(objBoss, 0);
	Obj_Delete(objBoss);
	CloseScript(GetOwnScriptID()); 
	
}

let row = 1;
let diffsel = 0;
let pdsel = 1;
let qsel = 0;
let wsel = 0;
let spdsel = 0;

let stop = 0;
let frameKeyHold = 0;

let ft = 30;

task menugraphic{
    InstallFont(GetCurrentScriptDirectory~"Meiryo.ttc");
    let yp = 40;
    let y1 =  224-100;
    let x1 = 0;
    let cx1 = 192-100;
	details("Normal", cx1-x1, y1, 1, 0, [0,120,255], ALIGNMENT_LEFT);
	details("Hard", cx1, y1, 1, 1, [0,0,255], ALIGNMENT_CENTER);
	details("Lunatic", cx1+x1, y1, 1, 2, [130,0,130], ALIGNMENT_RIGHT);

	let y2 = y1+yp;
	let x2 = 107;
	details("Legacy", cx1-x2, y2, 2, 1, [170, 170, 0], ALIGNMENT_RIGHT);
	details("Pointdevice", cx1+x2, y2, 2, 0, [170, 170, 0], ALIGNMENT_LEFT);

	let y3 = y1 + yp*2;
	let x3 = x2;
	details("Fancy Backgrounds", cx1-x3, y3, 3, 0, [205, 0,0], ALIGNMENT_RIGHT);
	details("Simple Backgrounds", cx1+x3, y3, 3, 1, [205,0,0], ALIGNMENT_LEFT);

	let y5 = y1 + yp*3;
	let x5 = x2;
	details("Warnings On", cx1-x5, y5, 4, 0, [0,200,0], ALIGNMENT_RIGHT);
	details("Warning Off", cx1+x5, y5, 4, 1, [0,200,0], ALIGNMENT_LEFT);

	let y4 = y1 + yp*4;
	let x4 = x2;
	details("Marisa Speed", cx1-x4, y4, 5, 0, [0,200,0], ALIGNMENT_RIGHT);
	details("Reimu Speed", cx1+x4, y4, 5, 1, [0,200,0], ALIGNMENT_LEFT);


	task details(text, x, y, rown, sel, rgb, alignment){
		let obj = ObjText_Create;
	    ObjText_SetText(obj, text);
		ObjText_SetFontType(obj, "Meiryo");
		ObjText_SetFontSize(obj,25);
		Obj_SetRenderPriorityI(obj, 54);
		ObjText_SetFontBorderType(obj,BORDER_FULL);
		let cl = 255;
		ObjText_SetFontBorderColor(obj, 0, 100, 255);
		ObjText_SetFontColorTop(obj, cl, cl, cl);
		ObjText_SetFontColorBottom(obj, cl, cl, cl);
		ObjText_SetFontBorderWidth(obj,1);
		ObjText_SetMaxWidth(obj, 200);
		ObjText_SetHorizontalAlignment(obj, alignment);
		ObjRender_SetPosition(obj, x, y, 1);
		ObjRender_SetAlpha(obj, 255);

	    let alpha1 = 120;
	    let c = 50;
	    let calpha = 255;
	    let cc = 255;
	    while(stop == 0){
	     	if(rown == 1){
	     		if(rown == row){
	     			ObjRender_SetAlpha(obj, 255);
	     			calpha = 255;
		     		if(sel == diffsel){
		     			ObjRender_SetColor(obj, 255, 255, 255);
		     			cc = 255;
		     		}
		     		else{
		     			ObjRender_SetColor(obj, c, c, c);
		     			cc = c;
		     		}
		     	}
		     	else if(sel == diffsel){
		     		ObjRender_SetAlpha(obj, alpha1);
		     		ObjRender_SetColor(obj, 255, 255, 255);
		     		calpha = alpha1;
		     		cc = 255;
		     	}
		     	else{
		     		ObjRender_SetAlpha(obj, alpha1);
		     		ObjRender_SetColor(obj, c, c, c);
		     		calpha = alpha1;
		     		cc = c;
		     		
		     	}
	     	}
	     	if(rown == 2){
	     		if(rown == row){
	     			ObjRender_SetAlpha(obj, 255);
	     			calpha = 255;
		     		if(sel == pdsel){
		     			ObjRender_SetColor(obj, 255, 255, 255);
		     			cc = 255;
		     		}
		     		else{
		     			ObjRender_SetColor(obj, c, c, c);
		     			cc = c;
		     		}
		     	}
		     	else if(sel == pdsel){
		     		ObjRender_SetAlpha(obj, alpha1);
		     		ObjRender_SetColor(obj, 255, 255, 255);
		     		calpha = alpha1;
		     		cc = 255;
		     	}
		     	else{
		     		ObjRender_SetAlpha(obj, alpha1);
		     		ObjRender_SetColor(obj, c, c, c);
		     		calpha = alpha1;
		     		cc = c;
		     		
		     	}
	     	}
	     	if(rown == 3){
	     		if(rown == row){
	     			ObjRender_SetAlpha(obj, 255);
	     			calpha = 255;
		     		if(sel == qsel){
		     			ObjRender_SetColor(obj, 255, 255, 255);
		     			cc = 255;
		     		}
		     		else{
		     			ObjRender_SetColor(obj, c, c, c);
		     			cc = c;
		     		}
		     	}
		     	else if(sel == qsel){
		     		ObjRender_SetAlpha(obj, alpha1);
		     		ObjRender_SetColor(obj, 255, 255, 255);
		     		calpha = alpha1;
		     		cc = 255;
		     	}
		     	else{
		     		ObjRender_SetAlpha(obj, alpha1);
		     		calpha = alpha1;
		     		ObjRender_SetColor(obj, c, c, c);
		     		cc = c;
		     		
		     	}
	     	}
	     	if(rown == 4){
	     		if(rown == row){
	     			ObjRender_SetAlpha(obj, 255);
	     			calpha = 255;
		     		if(sel == wsel){
		     			ObjRender_SetColor(obj, 255, 255, 255);
		     			cc = 255;
		     		}
		     		else{
		     			ObjRender_SetColor(obj, c, c, c);
		     			cc = c;
		     		}
		     	}
		     	else if(sel == wsel){
		     		ObjRender_SetAlpha(obj, alpha1);
		     		ObjRender_SetColor(obj, 255, 255, 255);
		     		calpha = alpha1;
		     		cc = 255;
		     	}
		     	else{
		     		ObjRender_SetAlpha(obj, alpha1);
		     		ObjRender_SetColor(obj, c, c, c);
		     		calpha = alpha1;
		     		cc = c;
		     		
		     	}
	     	}
	     	if(rown == 5){
	     		if(rown == row){
	     			ObjRender_SetAlpha(obj, 255);
	     			calpha = 255;
		     		if(sel == spdsel){
		     			ObjRender_SetColor(obj, 255, 255, 255);
		     			cc = 255;
		     		}
		     		else{
		     			ObjRender_SetColor(obj, c, c, c);
		     			cc = c;
		     		}
		     	}
		     	else if(sel == spdsel){
		     		ObjRender_SetAlpha(obj, alpha1);
		     		ObjRender_SetColor(obj, 255, 255, 255);
		     		calpha = alpha1;
		     		cc = 255;
		     	}
		     	else{
		     		ObjRender_SetAlpha(obj, alpha1);
		     		ObjRender_SetColor(obj, c, c, c);
		     		calpha = alpha1;
		     		cc = c;
		     		
		     	}
	     	}
	     	yield;
	    }
	    let cm = cc/ft;
	    let am = calpha/ft;
	    loop(ft){
	    	calpha -= am;
	    	ObjRender_SetAlpha(obj, calpha);
	    	wait(1);
	    }
	    Obj_Delete(obj);
	}
}

task bg{

	let obj1 = ObjPrim_Create(OBJ_SPRITE_2D);
    ObjRender_SetBlendType(obj1, BLEND_ALPHA);
    Obj_SetRenderPriorityI(obj1, 52);
    ObjSprite2D_SetSourceRect(obj1, 0, 0, 638, 851);
    ObjSprite2D_SetDestCenter(obj1);
    ObjSprite2D_SetDestRect(obj1, -193, -851/2, 193, 851/2);
    ObjRender_SetPosition(obj1, 384/2, 448/2, 1);
    ObjRender_SetColor(obj1, 0, 0, 0);
   // ObjRender_SetScaleXYZ(obj1, 1, 1, 1);
   	let alpha = 220;
    ObjRender_SetAlpha(obj1,alpha);
    while(stop == 0){
        yield;
    }
    let am = alpha/ft;
    loop(ft){
    	alpha -= am;
    	ObjRender_SetAlpha(obj1,alpha);
    	wait(1);
    }
    Obj_Delete(obj1);
}
