let GCSD = GetCurrentScriptDirectory;

let bg1 = GCSD~"bgStageA.png";
let bgS1 = GCSD~"bgSpellA.png";
let bgS2 = GCSD~"bgSpellB.png";
let cherry = GCSD~"cherry.png";

let stgframe = GCSD~"stgframe.png";
let numbers = GCSD~"numbers.png";
let hud = GCSD~"hud.png";

let lifebar1 = GCSD~"lifebar_1.png";
let lifebar2 = GCSD~"lifebar_2.png";

let sprite = GCSD~"sprite.png";
let familiar = GCSD~"familiar.png";
let cutin = GCSD~"cutin.png";
let magiccircle = GCSD~"magiccircle.png";
let circle = GCSD~"circle.png";
let leaf = GCSD~"leaf.png";
let spellline = GCSD~"spellline.png";
let spellnames = GCSD~"spellnames.png";
let deathEff = GCSD~"death.png";
let items = GCSD~"items.png";

let music1 = GCSD ~ "a_fairy_battle.mp3";

let sePlayerShot = GCSD ~ "plshot.wav";
let seExtend = GCSD ~ "extend.wav";
let seItem = GCSD ~ "item.wav";
let seCaution = GCSD ~ "caution.wav";
let seExplode00 = GCSD ~ "enep00.wav";
let seExplode01 = GCSD ~ "enep01.wav";
let seTan00 = GCSD ~ "tan00.wav";
let seTan01 = GCSD ~ "tan01.wav";
let seKira00 = GCSD ~ "kira00.wav";
let seKira01 = GCSD ~ "kira01.wav";
let seKira02 = GCSD ~ "kira02.wav";
let seLaser00 = GCSD ~ "laser00.wav";
let seOptionHide = GCSD ~ "ophide.wav";
let seOptionShow = GCSD ~ "opshow.wav";
let seOption = GCSD ~ "option.wav";
let seCharge = GCSD ~ "charge00.wav";
let seTimer00 = GCSD ~ "timer00.wav";
let seTimer01 = GCSD ~ "timer01.wav";
let seDamage00 = GCSD ~ "damage00.wav";
let seDamage01 = GCSD ~ "damage01.wav";
let seCardCall = GCSD ~ "cardcall.wav";
let seCardGet = GCSD ~ "cardget.wav";
let seTimeStop = GCSD ~ "timestop.wav";
task StartSTGFrame {
	SetDefaultStatusVisible(false);
	SetRateScoreSystemEnable(false);
	SetClearFrameEffectEnable(false);
	yield;
	CreateFrame;
	TDifficulty;
	ascent(i in 0..3) { CreateNumber(i); }
	ascent(i in 0..2) { CreateLifeIndic(i); }
	yield;
	LoadGraphic(bg1); LoadGraphic(bgS1); LoadGraphic(bgS2); LoadGraphic(cherry);
	LoadGraphic(sprite); LoadGraphic(familiar); LoadGraphic(cutin); LoadGraphic(magiccircle); LoadGraphic(circle); LoadGraphic(leaf); LoadGraphic(spellline); LoadGraphic(spellnames); LoadGraphic(lifebar1); LoadGraphic(lifebar2); LoadGraphic(deathEff);
	LoadMusic(music1);
	LoadSE(sePlayerShot); LoadSE(seExtend); LoadSE(seItem); LoadSE(seCaution); LoadSE(seExplode00); LoadSE(seExplode01);
	LoadSE(seTan00); LoadSE(seTan01); LoadSE(seKira00); LoadSE(seKira01); LoadSE(seKira02); LoadSE(seLaser00);
	LoadSE(seOption); LoadSE(seOptionHide); LoadSE(seOptionShow); LoadSE(seCharge);
	LoadSE(seTimer00); LoadSE(seTimer01); LoadSE(seDamage00); LoadSE(seDamage01); LoadSE(seCardCall); LoadSE(seCardGet);
}

task CreateFrame {
	let obj = Obj_Create(OBJ_EFFECT);
	Obj_SetPosition(obj, 0, 0);
	ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
	ObjEffect_SetLayer(obj, 8);
	ObjEffect_SetTexture(obj, stgframe);
	ObjEffect_CreateVertex(obj, 4);
	ObjEffect_SetVertexUV(obj, 0, 0, 0);
	ObjEffect_SetVertexUV(obj, 1, 640, 0);
	ObjEffect_SetVertexUV(obj, 2, 640, 480);
	ObjEffect_SetVertexUV(obj, 3, 0, 480);
	ObjEffect_SetVertexXY(obj, 0, 0, 0);
	ObjEffect_SetVertexXY(obj, 1, 640, 0);
	ObjEffect_SetVertexXY(obj, 2, 640, 480);
	ObjEffect_SetVertexXY(obj, 3, 0, 480);
}

function IntString(let number) { let str = ToString(number); let str2 = ""; let i = 0; while (str[i] != '.') { str2 = str2 ~ [str[i]]; i++; } return str2; }

function CharToNum(let number, let place) {
	alternative(IntString(number)[place])
	case('0') { return 0; } case('1') { return 1; } case('2') { return 2; } case('3') { return 3; } case('4') { return 4; }
	case('5') { return 5; } case('6') { return 6; } case('7') { return 7; } case('8') { return 8; } case('9') { return 9; }
	others { RaiseError("CharToNum is getting an invalid digit", "CharToNum error"); }
}

task CreateNumber(let arg) {
	function GetNumberValue {
		alternative(arg)
		case(0) { return GetScore; }
		case(1) { return GetPoint; }
		case(2) { return GetGraze; }
		others { return 0; }
	}
	let obj = Obj_Create(OBJ_EFFECT);
	ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLELIST);
	ObjEffect_SetLayer(obj, 8);
	ObjEffect_SetTexture(obj, numbers);
	ObjEffect_CreateVertex(obj, 4);
	loop {
		let tempnum = length(IntString(GetNumberValue));
		ObjEffect_ResizeVertexCount(obj, 6*tempnum);
		Obj_SetPosition(obj, 616-12*tempnum, [44, 132, 152][arg]);
		ascent(i in 0..tempnum) {
			let tempnum2 = CharToNum(GetNumberValue, i)*16;
			ObjEffect_SetVertexUV(obj, i*6+0, 0+tempnum2, 0);
			ObjEffect_SetVertexUV(obj, i*6+1, 16+tempnum2, 0);
			ObjEffect_SetVertexUV(obj, i*6+2, 16+tempnum2, 16);
			ObjEffect_SetVertexUV(obj, i*6+3, 0+tempnum2, 0);
			ObjEffect_SetVertexUV(obj, i*6+4, 16+tempnum2, 16);
			ObjEffect_SetVertexUV(obj, i*6+5, 0+tempnum2, 16);
			ObjEffect_SetVertexXY(obj, i*6+0, 0+12*i, 0);
			ObjEffect_SetVertexXY(obj, i*6+1, 16+12*i, 0);
			ObjEffect_SetVertexXY(obj, i*6+2, 16+12*i, 16);
			ObjEffect_SetVertexXY(obj, i*6+3, 0+12*i, 0);
			ObjEffect_SetVertexXY(obj, i*6+4, 16+12*i, 16);
			ObjEffect_SetVertexXY(obj, i*6+5, 0+12*i, 16);
		}
		yield;
	}
}

task CreateLifeIndic(let arg) {
	function GetNumberValue {
		alternative(arg)
		case(0) { return GetPlayerLife-[0,1][GetPlayerLife>0]; }
		case(1) { return GetPlayerBomb; }
		others { return 0; }
	}
	let obj = Obj_Create(OBJ_EFFECT);
	Obj_SetPosition(obj, 508, 80+18*arg);
	ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLELIST);
	ObjEffect_SetLayer(obj, 8);
	ObjEffect_SetTexture(obj, numbers);
	ObjEffect_CreateVertex(obj, 4);
	loop {
		ObjEffect_ResizeVertexCount(obj, 6*GetNumberValue);
		ascent(i in 0..GetNumberValue) {
			ObjEffect_SetVertexUV(obj, i*6+0, 176+16*arg, 0);
			ObjEffect_SetVertexUV(obj, i*6+1, 176+16+16*arg, 0);
			ObjEffect_SetVertexUV(obj, i*6+2, 176+16+16*arg, 16);
			ObjEffect_SetVertexUV(obj, i*6+3, 176+16*arg, 0);
			ObjEffect_SetVertexUV(obj, i*6+4, 176+16+16*arg, 16);
			ObjEffect_SetVertexUV(obj, i*6+5, 176+16*arg, 16);
			ObjEffect_SetVertexXY(obj, i*6+0, 0+16*i, 0);
			ObjEffect_SetVertexXY(obj, i*6+1, 16+16*i, 0);
			ObjEffect_SetVertexXY(obj, i*6+2, 16+16*i, 16);
			ObjEffect_SetVertexXY(obj, i*6+3, 0+16*i, 0);
			ObjEffect_SetVertexXY(obj, i*6+4, 16+16*i, 16);
			ObjEffect_SetVertexXY(obj, i*6+5, 0+16*i, 16);
		}
		yield;
	}
}

task TDifficulty {
	yield;
	let obj = Obj_Create(OBJ_EFFECT);
		Obj_SetPosition(obj, GetCenterX, GetClipMinY+10);
		ObjEffect_SetTexture(obj, numbers);
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_CreateVertex(obj, 4);
		ObjEffect_SetLayer(obj, 8);
		ObjEffect_SetVertexUV(obj, 0, 0, 64+diff*16);
			ObjEffect_SetVertexUV(obj, 1, 256, 64+diff*16);
			ObjEffect_SetVertexUV(obj, 2, 256, 64+diff*16+16);
			ObjEffect_SetVertexUV(obj, 3, 0, 64+diff*16+16);
		ObjEffect_SetVertexXY(obj, 0, -128, -8);
			ObjEffect_SetVertexXY(obj, 1, 128, -8);
			ObjEffect_SetVertexXY(obj, 2, 128, 8);
			ObjEffect_SetVertexXY(obj, 3, -128, 8);
	ascent(i in 0..15) { ObjEffect_SetScale(obj, 1, (i+1)/15); yield; }
	ascent(i in 0..45/3) {
		ascent(j in 0..4) { ObjEffect_SetVertexColor(obj, j, 255, 175+80*sin((i%2)*90), 175+80*sin((i%2)*90), 175+80*sin((i%2)*90)); }
		loop(3) { yield; }
	}
	ascent(j in 0..4) { ObjEffect_SetVertexColor(obj, j, 255, 255, 255, 255); }
	loop(20) { yield; }
	descent(i in 0..20) {
		ascent(j in 0..4) { ObjEffect_SetVertexColor(obj, j, 255*(i/20), 255, 255, 255); }
		yield;
	}
	Obj_SetPosition(obj, GetClipMaxX+112+4, GetClipMinY+10);
	ascent(i in 0..20) {
		ascent(j in 0..4) { ObjEffect_SetVertexColor(obj, j, 255*((i+1)/20), 255, 255, 255); }
		yield;
	}
}

task GetSpellCardBonusDraw(let arg) {
	let objG = Obj_Create(OBJ_EFFECT);
	ObjEffect_SetPrimitiveType(objG, PRIMITIVE_TRIANGLEFAN);
	ObjEffect_SetLayer(objG, 6);
	ObjEffect_SetTexture(objG, hud);
	ObjEffect_CreateVertex(objG, 4);
	Obj_SetPosition(objG, GetCenterX, GetCenterY-100);
		ObjEffect_SetVertexUV(objG, 0, 0, 0);
		ObjEffect_SetVertexUV(objG, 1, 256, 0);
		ObjEffect_SetVertexUV(objG, 2, 256, 32);
		ObjEffect_SetVertexUV(objG, 3, 0, 32);
		ObjEffect_SetVertexXY(objG, 0, -128, -16);
		ObjEffect_SetVertexXY(objG, 1, 128, -16);
		ObjEffect_SetVertexXY(objG, 2, 128, 16);
		ObjEffect_SetVertexXY(objG, 3, -128, 16);
	let obj = Obj_Create(OBJ_EFFECT);
	ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLELIST);
	ObjEffect_SetLayer(obj, 6);
	ObjEffect_SetTexture(obj, numbers);
	ObjEffect_CreateVertex(obj, 4);
	let tempnum = length(IntString(arg));
	ObjEffect_ResizeVertexCount(obj, 6*tempnum);
	Obj_SetPosition(obj, GetCenterX-6*tempnum, GetCenterY-75);
	ascent(i in 0..tempnum) {
		let tempnum2 = CharToNum(arg, i)*16;
		ObjEffect_SetVertexUV(obj, i*6+0, 0+tempnum2, 0);
		ObjEffect_SetVertexUV(obj, i*6+1, 16+tempnum2, 0);
		ObjEffect_SetVertexUV(obj, i*6+2, 16+tempnum2, 16);
		ObjEffect_SetVertexUV(obj, i*6+3, 0+tempnum2, 0);
		ObjEffect_SetVertexUV(obj, i*6+4, 16+tempnum2, 16);
		ObjEffect_SetVertexUV(obj, i*6+5, 0+tempnum2, 16);
		ObjEffect_SetVertexXY(obj, i*6+0, 0+12*i, -8);
		ObjEffect_SetVertexXY(obj, i*6+1, 16+12*i, -8);
		ObjEffect_SetVertexXY(obj, i*6+2, 16+12*i, 8);
		ObjEffect_SetVertexXY(obj, i*6+3, 0+12*i, -8);
		ObjEffect_SetVertexXY(obj, i*6+4, 16+12*i, 8);
		ObjEffect_SetVertexXY(obj, i*6+5, 0+12*i, 8);
	}
	ascent(i in 0..15) {
		ObjEffect_SetScale(obj, 1, (i+1)/15);
		ObjEffect_SetScale(objG, 1, (i+1)/15);
		yield;
	}
	loop(90/6) {
		ascent(i in 0..tempnum*6) { ObjEffect_SetVertexColor(obj, i, 255, 155, 155, 155); }
		ascent(i in 0..4) { ObjEffect_SetVertexColor(objG, i, 255, 155, 155, 155); }
		loop(3) { yield; }
		ascent(i in 0..tempnum*6) { ObjEffect_SetVertexColor(obj, i, 255, 255, 255, 255); }
		ascent(i in 0..4) { ObjEffect_SetVertexColor(objG, i, 255, 255, 255, 255); }
		loop(3) { yield; }
	}
	descent(i in 0..15) {
		ObjEffect_SetScale(obj, 1, (i)/15);
		ObjEffect_SetScale(objG, 1, (i)/15);
		yield;
	}
	Obj_Delete(obj);
	Obj_Delete(objG);
}

task BonusFailedDraw {
	let objG = Obj_Create(OBJ_EFFECT);
	ObjEffect_SetPrimitiveType(objG, PRIMITIVE_TRIANGLEFAN);
	ObjEffect_SetLayer(objG, 6);
	ObjEffect_SetTexture(objG, hud);
	ObjEffect_CreateVertex(objG, 4);
	Obj_SetPosition(objG, GetCenterX, GetCenterY-100);
		ObjEffect_SetVertexUV(objG, 0, 0, 32);
		ObjEffect_SetVertexUV(objG, 1, 256, 32);
		ObjEffect_SetVertexUV(objG, 2, 256, 64);
		ObjEffect_SetVertexUV(objG, 3, 0, 64);
		ObjEffect_SetVertexXY(objG, 0, -128, -16);
		ObjEffect_SetVertexXY(objG, 1, 128, -16);
		ObjEffect_SetVertexXY(objG, 2, 128, 16);
		ObjEffect_SetVertexXY(objG, 3, -128, 16);
	ascent(i in 0..15) {
		ObjEffect_SetScale(objG, 1, (i+1)/15);
		yield;
	}
	loop(90/6) {
		ascent(i in 0..4) { ObjEffect_SetVertexColor(objG, i, 255, 155, 155, 155); }
		loop(3) { yield; }
		ascent(i in 0..4) { ObjEffect_SetVertexColor(objG, i, 255, 255, 255, 255); }
		loop(3) { yield; }
	}
	descent(i in 0..15) {
		ObjEffect_SetScale(objG, 1, (i)/15);
		yield;
	}
	Obj_Delete(objG);
}