#ScriptVersion[3]
#System["./lib/System.txt"]

let CSD = GetCurrentScriptDirectory();
let spells = CSD ~ "lib/spell/"; 
CreateCommonDataArea("BossAPositionX");
CreateCommonDataArea("BossAPositionY");
CreateCommonDataArea("BossBPositionX");
CreateCommonDataArea("BossBPositionY");

#include "./lib/lib_background.txt"
#include "./lib/lib_common.txt"
#include "./lib/lib_effect.txt"
#include "./lib/lib_shotdata.txt"
#include "./lib/lib_enemy.txt"
#include "./lib/lib_dialog.txt"
#include "./lib/lib_stage.txt"

@Initialize{
	SetAutoDeleteObject(true);
	TPlural;
	Bg;
}

@MainLoop{
	if(GetPlayerLife < 0){
		CallEnd;
	}

	yield;
}

@Event{
	alternative(GetEventType())
	case(EV_USER_STAGE + 1){
		let a = GetEventArgument(0);
		let id = CreateItemU2(a[0],a[1],a[2],a[1],a[2]-32,1);
	}

	case(EV_USER_STAGE + 2){
		let a = GetEventArgument(0);
		TExplosionB1(a[0], a[1], "maple");
	}

	case(EV_USER_STAGE + 3){
		let a = GetEventArgument(0);
		let color = [255,255,255];
		DeleteShotAnimCircle(a[0],a[1], color);
	}
	
	case(EV_USER_STAGE + 4){
		let a = GetEventArgument(0);
		let color = [a[2],a[3],a[4]];
		TExplosionA1(a[0], a[1], color, "cherry");
	}
} 

task TPlural{

	SetPlayerInvincibilityFrame(30);
	SetPlayerLife(8);
	SetPlayerSpell(2);
	SetPlayerPower(400);
	AddPoint(10000);
	
	_PlayBGM(0);

	Stage_Stuff;

	CallEnd;
}

task CallEnd{
	CreateRenderTarget("ImagePauss");
	RenderToTextureA1("ImagePauss",0,100,true);
	SetCommonData("ImagePause","ImagePauss");

	SetAreaCommonData("Data","Results",GetAreaCommonData("Data","Results",[0]) ~ [GetScore]);

	let def_array = [];
	let data_array = GetAreaCommonData("Data","Results",[0,0,0,0,0,0,0,0,0,0]);
	ascent(a in 0..20){def_array = def_array~[0];}
	ascent(a in 0..length(data_array)){
		let b = 0;
		let en = false;
		loop(20){
			if(data_array[a] > def_array[b] && !en){
				descent(c in 0..length(def_array)-b-1){def_array[b+1+c] = def_array[b+c];}
				def_array[b] = data_array[a];
				en = true;
			}
			b++;
		}
	}
	SetAreaCommonData("Data","Results",def_array);

	CloseStgScene;
}