let dirCurrent = GetCurrentScriptDirectory();


@Initialize
{
	SetPauseScriptPath(GetCurrentScriptDirectory() ~ "Pause.txt");
	SetEndSceneScriptPath(dirCurrent ~ "EndScene.txt");
	SetReplaySaveSceneScriptPath(dirCurrent ~ "ReplayScene.txt");
	camera;
	InitFrame();
	//TScore();
	//TGraze();
	
	//TPlayerLife();
	//TPlayerSpell();
	//TBossLife();
	SetSkipModeKey(KEY_C);
	TBossTimer();
	TCurrentFps();
	TReplayFps();


	
}

let rez = GetScreenHeight()/480;

task camera{
	//SetStgFrame(32, 16, 416, 464, 20, 80);
	SetStgFrame(32*rez, 16*rez, 416*rez, 464*rez, 20, 80);

	Set2DCameraFocusY(448/2);
	Set2DCameraFocusX(384/2);
	Set2DCameraRatio(rez);
	yield;
	SetPlayerClip(+13,+15,384-13,448-15);
	SetShotAutoDeleteClip(32, 32, 32+384-(rez*384), 32+448-(rez*448));
	//SetShotAutoDeleteClip(64, 64, 64+384-(rez*384), 64+448-(rez*448));

}

@MainLoop
{
	yield;
}

@Event
{
	alternative(GetEventType())
	case(EV_START_BOSS_SPELL)
	{
		let path = dirCurrent ~ "Default_System_MagicCircle.txt";
		let id = LoadScript(path);
		StartScript(id);
	}
	case(EV_END_BOSS_STEP)
	{
		let endbullet = GetShotIdInCircleA2(0, 0, 99999, TARGET_ENEMY);
		bulletclear(endbullet,1,0);
		let endenemy = GetAllEnemyID();
		enemyclear(endenemy);
		SetPlayerInvincibilityFrame(30);
		if(GetAreaCommonData("mode", "start", 0) == 1){
			ObjSound_Play(seobj);
			ObjSound_SetVolumeRate(seobj,50);
		}
		
		
		//TGainSpell(score);
	}
	/*case(EV_PLAYER_SHOOTDOWN)
	{	
		let endbullet = GetShotIdInCircleA2(0, 0, 99999, TARGET_ENEMY);
		let n = length(endbullet);
		ascent(i in 0..n){
			bulletclear(endbullet[i],0,1);
		}
	}*/
	case(EV_PLAYER_REBIRTH)
	{
		let endbullet = GetShotIdInCircleA2(0, 0, 99999, TARGET_ENEMY);
		
		bulletclear(endbullet,0,1);
	}
	case(EV_PAUSE_ENTER){
		StopSlow(GetPlayerID());
	}
}

task bulletclear(array,auto,death){
	let l = length(array);
	let array2 = [];
	ascent(i in 0..l){
		if(!Obj_IsDeleted(array[i]) && ObjShot_GetImageID(array[i])!= 1101){
			if(death == 0 || ObjShot_IsSpellResist(array[i]) != true){
				let bulletx = ObjMove_GetX(array[i]);
				let bullety = ObjMove_GetY(array[i]);
				let graphic = ObjShot_GetImageID(array[i]);
				let color = GetShotDataInfoA1(graphic, "./ZUNShot_Const.txt", INFO_DELAY_COLOR);
				Obj_Delete(array[i]);
				let cancel = CreateShotA1(bulletx, bullety, 0, 0, 1101, 0);
				ObjRender_SetBlendType(cancel, BLEND_ADD_ARGB);
				ObjMove_AddPatternA2(cancel, 0, 0, rand(0,360), 0, -4, 0);
				ObjRender_SetScaleXYZ(cancel, 0.9, 0.9, 1);
				ObjRender_SetColor(cancel, color[0], color[1], color[2]);
				ObjShot_SetSpellResist(cancel, true);
				ObjShot_SetAutoDelete(cancel, false);
				ObjShot_SetIntersectionEnable(cancel,false);
				array2 = array2 ~ [cancel];
			}
		}
	}
	loop(31){
		yield;
	}
	l = length(array2);
	ascent(i in 0..l){
		Obj_Delete(array2[i]);
	}
}

task enemyclear(array){
	let l = length(array);
	let array2 = [];
	ascent(i in 0..l){
		if(!Obj_IsDeleted(array[i])){
			
				let bulletx = ObjMove_GetX(array[i]);
				let bullety = ObjMove_GetY(array[i]);
				Obj_Delete(array[i]);
				let cancel = CreateShotA1(bulletx, bullety, 0, 0, 1100, 0);
				ObjRender_SetBlendType(cancel, BLEND_ADD_ARGB);
				ObjMove_AddPatternA2(cancel, 0, 0, rand(0,360), 0, -4, 0);
				ObjShot_SetGraphic(cancel, 1101);
				ObjRender_SetScaleXYZ(cancel, 0.9, 0.9, 1);
				ObjRender_SetColor(cancel, 125, 125, 255);
				ObjShot_SetSpellResist(cancel, true);
				ObjShot_SetAutoDelete(cancel, false);
				ObjShot_SetIntersectionEnable(cancel,false);
				array2 = array2 ~ [cancel];
		}
	}
	loop(31){
		yield;
	}
	l = length(array2);
	ascent(i in 0..l){
		Obj_Delete(array2[i]);
	}
}

function GetDistance(x1,y1,x2,y2)
{
	return ((x2 - x1) ^ 2 + (y2 - y1) ^ 2) ^ 0.5
}

let CSD = GetCurrentScriptDirectory();
let path = CSD ~ "se/attackclear.wav";

let objScene = GetEnemyBossSceneObjectID();
let score = ObjEnemyBossScene_GetInfo(objScene, INFO_SPELL_SCORE);
let seobj = ObjSound_Create;
ObjSound_Load(seobj,path);
//----------------------------------------------------
//枠外の背景表示
//----------------------------------------------------
function InitFrame()
{
	let path = GetCurrentScriptDirectory() ~ "img/Default_SystemBackground.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, path);
	Obj_SetRenderPriority(obj, 0);
	ObjSprite2D_SetSourceRect(obj, 0, 0, SCREEN_WIDTH*2, SCREEN_HEIGHT*2);
	ObjSprite2D_SetDestRect(obj, 0, 0, SCREEN_WIDTH*rez, SCREEN_HEIGHT*rez);
}

task TBossName(){
	let img = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(img, 0.75);
	ObjPrim_SetTexture(img,GetCurrentScriptDirectory() ~ "img/ename.png");	
	ObjSprite2D_SetSourceRect(img, 0, 222, 128, 244);
	ObjRender_SetScaleXYZ(img, 0.7*rez, 0.7*rez, 1);
	ObjSprite2D_SetDestCenter(img);
	ObjRender_SetPosition(img, 47*rez, 11*rez, 1); 
	ObjRender_SetAlpha(img, 0);
	yield;
	let objScene = GetEnemyBossSceneObjectID();
	while(ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER) > 999){yield;}
	ObjRender_SetAlpha(img, 255);
}

task TMode(){
	let img = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(img, 0.1);
	ObjPrim_SetTexture(img,GetCurrentScriptDirectory() ~ "img/font1.png");	
	ObjSprite2D_SetSourceRect(img, 528, 642, 658, 672);
	ObjRender_SetScaleXYZ(img, 0.75*rez, 0.75*rez, 1);
	ObjSprite2D_SetDestCenter(img);
	ObjRender_SetPosition(img, 535*rez, 33*rez, 1); 
	//834-960
	//30-75
}

//----------------------------------------------------
//スコア表示
//----------------------------------------------------
task TScore()
{
	let y = 72;
	let y2 = y;
	//Score文字列
	let objScore = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(objScore, 0.1);
	ObjPrim_SetTexture(objScore,GetCurrentScriptDirectory() ~ "img/front00.png");	
	ObjSprite2D_SetSourceRect(objScore, 517, 39, 586, 66);
	ObjRender_SetScaleXYZ(objScore, 0.7*rez, 0.7*rez, 1);
	ObjSprite2D_SetDestCenter(objScore);
	ObjRender_SetPosition(objScore, 452*rez, y*rez, 1);

	//得点
	let pathDigit = GetCurrentScriptDirectory() ~ "img/Default_SystemDigit.png";
	let count = 11;
	hudline(523.5,y+8.5,2.45);

	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	ObjRender_SetScaleXYZ(obj, 0.5*rez, 0.5*rez, 1);
	Obj_SetRenderPriority(obj, 0.1);
	

	while(true)
	{
		let dn = 0;
		let cn = 0;
		let cd = 0;
		//cn is added to num every 4th/8th "number" in order to make a comma
		//cd is deducated from the distance between numbers every 5th/9th number to decrease distance between commas and numbers
		let score = GetScore();
		score = min(score, 999999999);
		let listNum = DigitToArray(score, count);
		//dc counts 
		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(i in 0 .. count)
		{
			let num = 0;
			if(i < 3){
				num = listNum[i+2];
			}
			else if (i < 8){
				num = listNum[i+1];
			}
			else{
				num = listNum[i];
			}
			if(i == 3 || i == 7){cn = 14 - num; y2 += +3;}
			else{cn = 0; y2 = y;}
			if(i == 4 || i == 8){cd += 10;}
			ObjRender_SetX(obj, (500 + i * 13-cd)*rez);
			ObjRender_SetY(obj, y2*rez);
			ObjSpriteList2D_SetSourceRect(obj, (cn+num) * 32, 0, ((cn+num) + 1) * 32, 32);
			ObjSpriteList2D_SetDestCenter(obj);
			//ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 24);
			ObjSpriteList2D_AddVertex(obj);
		}
		yield;
	}
}

//----------------------------------------------------
//残機表示
//----------------------------------------------------
task TPlayerLife
{
	let y = 130;
	let objLife = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(objLife, 0.1);
	ObjPrim_SetTexture(objLife,GetCurrentScriptDirectory() ~ "img/front00.png");	
	ObjSprite2D_SetSourceRect(objLife, 517, 75, 581, 102);
	ObjRender_SetScaleXYZ(objLife, 0.7*rez, 0.7*rez, 1);
	ObjSprite2D_SetDestCenter(objLife);
	ObjRender_SetPosition(objLife, 450*rez, y*rez, 1);

	let  img = GetCurrentScriptDirectory() ~ "img/font1.png";
	let count = 8;
	hudline(528,y+9,2.6);
	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, img);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	ObjRender_SetScaleXYZ(obj, 0.6*rez, 0.6*rez, 1);
	Obj_SetRenderPriority(obj, 0.1);	

	while(true)
	{
		let life = GetPlayerLife();
		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(i in 0 .. count)
		{
			if(life > i){ObjSpriteList2D_SetSourceRect(obj, 992, 3, 1024, 33);}
			else{ObjSpriteList2D_SetSourceRect(obj, 832, 3, 863, 33);}
			ObjRender_SetX(obj, (500 + i * 17)*rez);
			ObjRender_SetY(obj, y*rez);
			ObjSpriteList2D_SetDestCenter(obj);
			ObjSpriteList2D_AddVertex(obj);
		}
		yield;
	}
}

//----------------------------------------------------
//残スペル表示
//----------------------------------------------------
task TPlayerSpell
{
	let y = 170;
	let objScore = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(objScore, 0.1);
	ObjPrim_SetTexture(objScore,GetCurrentScriptDirectory() ~ "img/front00.png");	
	ObjSprite2D_SetSourceRect(objScore, 661, 1, 725, 35);
	ObjRender_SetScaleXYZ(objScore, 0.7*rez, 0.7*rez, 1);
	ObjSprite2D_SetDestCenter(objScore);
	ObjRender_SetPosition(objScore, 450*rez, (y+1)*rez, 1);
	hudline(528,y+9,2.6);
	let  img = GetCurrentScriptDirectory() ~ "img/font1.png";
	let count = 8;

	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, img);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	ObjRender_SetScaleXYZ(obj, 0.6*rez, 0.6*rez, 1);
	Obj_SetRenderPriority(obj, 0.1);

	while(true)
	{
		let spell = GetPlayerSpell();
		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(i in 0 .. count)
		{
			if(spell > i){ObjSpriteList2D_SetSourceRect(obj, 992, 42, 1024, 73);}
			else{ObjSpriteList2D_SetSourceRect(obj, 832, 42, 863, 73);}
			ObjRender_SetX(obj, (500 + i * 17)*rez);
			ObjRender_SetY(obj, y*rez);
			ObjSpriteList2D_SetDestCenter(obj);
			ObjSpriteList2D_AddVertex(obj);
		}
		yield;
	}
}

task hudline(x,y,scale){
	let objline = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(objline, 0.05);
	ObjPrim_SetTexture(objline,GetCurrentScriptDirectory() ~ "img/font1.png");	
	ObjSprite2D_SetSourceRect(objline, 548, 182, 623, 184);
	ObjRender_SetAlpha(objline, 175);
	ObjRender_SetScaleXYZ(objline, scale*rez, 1*rez, 1);
	ObjSprite2D_SetDestCenter(objline);
	ObjRender_SetPosition(objline, x*rez, y*rez, 1);

}

//----------------------------------------------------
//Graze表示
//----------------------------------------------------
task TGraze()
{
	//Graze文字列
	let objGraze = ObjText_Create();
	ObjText_SetText(objGraze, "Graze");
	ObjText_SetFontSize(objGraze, 20);
	ObjText_SetFontBold(objGraze, true);
	ObjText_SetFontColorTop(objGraze, 255, 255, 255);
	ObjText_SetFontColorBottom(objGraze, 255, 255, 255);
	ObjText_SetFontBorderType(objGraze, BORDER_FULL);
	ObjText_SetFontBorderColor(objGraze,128, 128, 128);
	ObjText_SetFontBorderWidth(objGraze, 2);
	Obj_SetRenderPriority(objGraze, 0.01);
	ObjRender_SetX(objGraze, 428);
	ObjRender_SetY(objGraze, 98);

	//得点
	let pathDigit = GetCurrentScriptDirectory() ~ "img/Default_SystemDigit.png";
	let count = 5;

	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
	Obj_SetRenderPriority(obj, 0.1);
	ObjRender_SetY(obj, 122);

	while(true)
	{
		let graze = GetGraze();
		graze = min(graze, 99999);
		let listNum = DigitToArray(graze, count);

		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. count)
		{
			let num = listNum[iObj];
			ObjRender_SetX(obj, 440 + iObj * 14);
			ObjSpriteList2D_SetSourceRect(obj, num * 36, 0, (num + 1) * 36, 32);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 24);
			ObjSpriteList2D_AddVertex(obj);
		}
		yield;
	}
}

//----------------------------------------------------
//タイマー表示
//----------------------------------------------------
task TBossTimer
{
	let pathDigit = GetCurrentScriptDirectory() ~ "img/Default_SystemDigit.png";
	let y = 11;
	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	Obj_SetRenderPriority(obj, 0.75);
	ObjRender_SetY(obj, y*rez);
	ObjRender_SetScaleXYZ(obj, 0.6*rez, 0.6*rez, 1);
	let count = 2;

	let objScene = ID_INVALID;
	loop
	{
		objScene = GetEnemyBossSceneObjectID();
		ObjSpriteList2D_ClearVertexCount(obj);
		if(objScene != ID_INVALID)
		{
			RenderTimer();
		}
		yield;
	}

	function RenderTimer()
	{
		let timer = ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER);
		if(ObjEnemyBossScene_GetInfo(objScene, INFO_IS_SPELL)){y +=1;}
		else{y -= 1;}
		if(y > 31){y = 31;}
		if(y < 11){y = 11;}
		timer = min(timer, 99.99);
		let listNum = DigitToArray(timer, count);

		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. count)
		{
			let num = listNum[iObj];
			ObjRender_SetX(obj, GetStgFrameWidth()/2 - 8*rez + iObj * 16*rez);
			ObjRender_SetY(obj, y*rez);
			ObjSpriteList2D_SetSourceRect(obj, num * 32, 0, (num + 1) * 32, 32);
			ObjSpriteList2D_SetDestCenter(obj);
			ObjSpriteList2D_AddVertex(obj);
		}
		if(ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER) > 999){ObjSpriteList2D_ClearVertexCount(obj);}
	}

}

//----------------------------------------------------
//スペルカード取得表示
//----------------------------------------------------
task TGainSpell(score)
{
	/*let objText = ObjText_Create();
	ObjText_SetText(objText, "Spell Card Bonus!");
	ObjText_SetFontSize(objText, 32);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 128, 128, 255);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText,255, 255, 255);
	ObjText_SetFontBorderWidth(objText, 1);
	Obj_SetRenderPriority(objText, 0.6);
	ObjRender_SetX(objText, 32);
	ObjRender_SetY(objText, 98);

	let strScore = "+" ~ IntToString(score);
	let objScore = ObjText_Create();
	ObjText_SetText(objScore, strScore);
	ObjText_SetFontSize(objScore, 32);
	ObjText_SetFontBold(objScore, true);
	ObjText_SetFontColorTop(objScore, 255, 255, 255);
	ObjText_SetFontColorBottom(objScore, 255, 128, 128);
	ObjText_SetFontBorderType(objScore, BORDER_FULL);
	ObjText_SetFontBorderColor(objScore,255, 255, 255);
	ObjText_SetFontBorderWidth(objScore, 1);
	Obj_SetRenderPriority(objScore, 0.6);
	ObjRender_SetX(objScore, 180);
	ObjRender_SetY(objScore, 140);

	loop(120)
	{
		yield;
	}
	Obj_Delete(objText);
	Obj_Delete(objScore);*/
}

//----------------------------------------------------
//FPS表示
//----------------------------------------------------
task TCurrentFps()
{
	let objText = ObjText_Create();
	ObjText_SetFontSize(objText, 8*rez);
	ObjText_SetFontBold(objText, true);
	
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText,0, 0, 0);
	ObjText_SetFontBorderWidth(objText, 1*rez);
	ObjText_SetHorizontalAlignment(objText, ALIGNMENT_RIGHT);
	ObjText_SetMaxWidth(objText, GetScreenWidth() - 8);
	Obj_SetRenderPriority(objText, 1.0);
	ObjRender_SetX(objText, 0);
	ObjRender_SetY(objText, GetScreenHeight() - 10*rez);

	loop
	{
		let fps = GetCurrentFps();
		let text = vtos("1.2f", fps) ~ "fps";
		ObjText_SetText(objText, text);
		yield;
	}
}

task TReplayFps()
{
	if(!IsReplay()){return;}

	let objText = ObjText_Create();
	ObjText_SetFontSize(objText, 12);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 128, 128, 255);
	ObjText_SetFontColorBottom(objText, 64, 64, 255);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText,255, 255, 255);
	ObjText_SetFontBorderWidth(objText, 1);
	Obj_SetRenderPriority(objText, 1.0);

	let px = GetStgFrameLeft() + GetStgFrameWidth() - 18;
	let py = GetStgFrameTop() + GetScreenHeight() - 14;
	ObjRender_SetX(objText, px);
	ObjRender_SetY(objText, py);

	loop
	{
		let fps = GetReplayFps();
		let text = vtos("02d", fps);
		ObjText_SetText(objText, text);
		yield;
	}
}

//----------------------------------------------------
//ユーティリティ
//----------------------------------------------------
function DigitToArray(let digit,let count)
{
	let res = [];
	digit = truncate(digit);

	loop
	{
		let tnum = truncate(digit % 10);
		digit /= 10;
		res = [tnum] ~ res;
		if(truncate(digit) == 0){break;}
	}

	loop(max(0, count - length(res)))
	{
		res = [0] ~ res;
	}

	return res;
}

