#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Samsara \"Twilight Matrix\""]
#System["./../../System/Miyako_System.txt"]
#Background["./../../Functions/background.txt"]

#include"./Cutin.txt"
#include"./../../Functions/Enemy_Functions.txt"
#include"./../../Functions/Effects/Effects.txt"
#include"./LoadGraphics.txt"
#include"./LoadSounds.txt"

let bossObj;
let bossX;
let bossY;
let bossdamage = 0;

#include"./MiyakoDrawBoss.txt"

@Initialize{	
	let objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);
	bossObj = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(bossObj);
	SetAutoDeleteObject(true);

	animateBossM;
	LoadShotData;
	TEnemyMarker;
	mainTask;
	TSamsaraEnd;}

@Event{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){SetScriptResult(600);}
	case(EV_REQUEST_TIMER){SetScriptResult(60);}
	case(EV_REQUEST_SPELL_SCORE){SetScriptResult(10000000);}
	case(EV_START_BOSS_SPELL){
		let path = GetCurrentScriptDirectory ~ "./../../System/Miyako_System_MagicCircle.txt";
		let id = LoadScript(path);
		StartScript(id);yield;}}

@MainLoop{
	bossX = ObjMove_GetX(bossObj);
	bossY = ObjMove_GetY(bossObj);

	if(bossdamage==0){ObjEnemy_SetDamageRate(bossObj, 0, 0);}
	if(bossdamage==1){ObjEnemy_SetDamageRate(bossObj, 10, 0);}

	ObjEnemy_SetIntersectionCircleToShot(bossObj,bossX,bossY,24);
	ObjEnemy_SetIntersectionCircleToPlayer(bossObj,bossX,bossY,32);
	yield;}


task coloring{
	let dir = GetCurrentScriptDirectory();
	let pathShader = dir ~ "SamplePS01_HLSL.txt";

	let objShader = ObjShader_Create();
	ObjShader_SetShaderF(objShader, pathShader);
	ObjShader_SetTechnique(objShader, "TecMonotone");
		SetShaderI(objShader, 0, 99);}

task flip{
	let a=1/60;let b=1;
	loop(60){
		Set2DCameraRatioX(b);
		b+=-a;yield;}
	loop(60){
		Set2DCameraRatioX(b);
		b+=a;yield;}}

task bgdebug{
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj,GetCurrentScriptDirectory~"./../../Image/bg2.png");
	ObjSprite3D_SetSourceDestRect(obj,0,0,128,128);
	ObjSprite3D_SetBillboard(obj,true);
	Obj_SetRenderPriorityI(obj,22);
	ObjRender_SetScaleXYZ(obj,6,6,0);
	ObjRender_SetPosition(obj,0,200,600);}

task mainTask{
	ObjMove_SetDestAtWeight(bossObj,CenterX,CenterY,8,3);
	TEtama;wait(90);SE(se_spell);
	if(GetCommonData("Replay",0) == 2){
	ObjCutin_SetSpellcardS4("Samsara \"Twilight Matrix\"", cutin, NAZRIN, 240, 104, 104);
	ObjCutin_LaunchS3(MOKOU, cutin, "Practice");}
	else{
	ObjCutin_SetSpellcardS4("Samsara \"Twilight Matrix\"", cutin, NAZRIN, 240, 104, 104);
	ObjCutin_LaunchS3(NAZRIN, cutin, "Extra");}
	TSpellBG;yield;bgdebug;
	SetCommonData("BG",1);
	wait(120);
	SE(se_ch2);MakeConcentrationB1(bossObj,"SAKURA",255,255,255,75);
	wait(75);SE(se_enep3);MakeExplosionA1(bossObj,"SAKURA",255,255,255);flip;
	wait(60);coloring;bossdamage=1;
		TMotionBlur;
		TMatrix;
		wait(120);
		
	while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0){
		ascent(i in -18..19){CreateShotA1(bossX,bossY,1,GetAngleToPlayer(bossObj)+i*9+180,224,30);}
		CreateShotA1(bossX,bossY,1,GetAngleToPlayer(bossObj),824,30);
		wait(60);}}

task TMatrix{
	ascent(a in 0..10){ ascent(i in 0..24){
	CreateStraightLaserA1(-15,-350+i*40,45,600,15,30,1011,60+a*330+i*5);}}
	ascent(a in 0..10){ ascent(i in 0..24){
	CreateStraightLaserA1(384+15,-350+i*40,135,600,15,30,1011,60+a*330+i*5);}}}

task TMotionBlur(){
	let renderTexture = [GetReservedRenderTargetName(0), GetReservedRenderTargetName(1)];
	SetInvalidRenderPriorityA1(20, 79);
	let objCurentFrame = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(objCurentFrame, 80);
	ObjRender_SetPermitCamera(objCurentFrame, false);

	let objOldFrame = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(objOldFrame, 0);
	ObjRender_SetPermitCamera(objOldFrame, false);
	ObjRender_SetAlpha(objOldFrame, 224);
	Obj_SetVisible(objOldFrame, false);

	let frame = 0;
	while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0){
		let currentFrameTexture = renderTexture[frame % 2];
		RenderToTextureA1(currentFrameTexture, 20, 79, true);
		if(GetCommonData("Replay",0) == 2 && GetPlayerLife < 0){break;}
		if(frame >= 1) {
			let oldFrameTexture = renderTexture[(frame - 1) % 2];
			ObjPrim_SetTexture(objOldFrame, oldFrameTexture);
			ObjSprite2D_SetSourceRect(objOldFrame, 32, 16, 416, 464);
			ObjSprite2D_SetDestRect(objOldFrame, 32, 16, 416, 464);
			RenderToTextureB1(currentFrameTexture, objOldFrame, false);}

		ObjPrim_SetTexture(objCurentFrame, currentFrameTexture);
		ObjSprite2D_SetSourceRect(objCurentFrame, 32, 16, 416, 464);
		ObjSprite2D_SetDestRect(objCurentFrame, 32, 16, 416, 464);

		frame++;
		wait(2);}
	Obj_Delete(objCurentFrame);
	Obj_Delete(objOldFrame);
	ClearInvalidRenderPriority();}