
#ScriptVersion[3]
#Title["DoremyEnd"]
#Text["DoremyEnd"]
#Player[DEFAULT]
#System["./system/Default_System.txt"]
#Background["./BG.txt"]
#Player["./MoF_Reimu/Reimu.txt"]
#include"./Functions/Index.txt"
#include"./SetBoss.txt"
#include"./Shot/AllStarShot_Const.dnh"
#include"./system/Default_Effect.Txt"
#include"./Functions/DialogueFunctions.dnh"
#include"./Functions/Portrait_Const.dnh"


let objBoss;
let GCSD=GetCurrentScriptDirectory~"";
let enemy;
let bSpell=false;
let stgx=GetStgFrameWidth()/2;
let stgy=GetStgFrameHeight()/2;
let idle = true;
let atk = false;
let fog = 0;
let objScene=GetEnemyBossSceneObjectID();
let color = [0,0];
@Initialize
{
SetBoss("Doremy",false);
}

@MainLoop
{
	ObjEnemy_SetIntersectionCircleToPlayer(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),25);
	ObjEnemy_SetIntersectionCircleToShot(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),26);
	yield;

}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
	{
		SetScriptResult(0);
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(5);
	}
		case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(35000000);
	}
}

//----------------------------------------------------
task mainTask{
	BlindBackground;
	SetCommonData("ForceLifebar",1);
	ObjMove_SetDestAtWeight(objBoss,ObjMove_GetX( objBoss ) + rand( -30, 30 ),ObjMove_GetY( objBoss ) + rand( -30, 30 ),6,1);
	}

//----------------------------------------------------

let blind = GCSD~"img/Blind.png";

task BlindBackground(){ 
	let value = 0;
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,25);
	ObjPrim_SetTexture(obj,blind);
	ObjSprite2D_SetSourceRect(obj,0,0,384,448);
	ObjSprite2D_SetDestRect(obj,-384,-448,384,448);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,1,1,0);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetPosition(obj,384/2,448/2,0);
	End( obj );
	while( !Obj_IsDeleted(objBoss) && bSpell == false){
	/*descent( x in 0..255 ){
	ObjRender_SetColor(obj,x,x,x);
	yield;
	}
	wait( 30 );
	ascent( x in 0..255 ){
	ObjRender_SetColor(obj,x,x,x);*/
	yield;
	//}
	//wait( 30 );
	}
	Obj_Delete(obj);
}

	task End( obj ){
	descent( i in 0..255/2 ){
	ObjRender_SetAlpha( obj, i*2 );
	yield;
	}
	Obj_Delete( obj );
	}	


//----------------------------------------------------



//----------------------------------------------------

task TEnd{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){yield;}
		let color = GetColor("WHITE");
		ClearBullet(120,10);
		let dtime = 80;
		PlaySound("enemoff1",80,0);
		ScreenShake(80);
		StartSlow( TARGET_ALL, 25 );
		loop(6)
		{
		MapleExplodeC(stgx,150,rand(0.5,1.5),"WHITE");
		wait(7);
		}
		wait(20);
		StopSlow( TARGET_ALL);
		PlaySound("enemoff",100,0);
		PlaySound("enemoff1",60,0);
		ScreenShake(40);
		StartSlow( TARGET_ALL, 35 );
		loop(2)
		{
        ExCircle(GetEnemyX,GetEnemyY,0.05,dtime);
        ExCircle(GetEnemyX-30,GetEnemyY,0.05,dtime);
        ExCircle(GetEnemyX+30,GetEnemyY,0.05,dtime);
        ExCircle(GetEnemyX,GetEnemyY-30,0.05,dtime);
        ExCircle(GetEnemyX,GetEnemyY+30,0.05,dtime);
		loop(10){yield;}
		ExCircle(GetEnemyX,GetEnemyY,0.05,dtime-10);
		StopSlow( TARGET_ALL);
		wait(4);
		}
        //loop(10){yield;}
		//ExCircle(GetEnemyX,GetEnemyY,0.05,dtime-10);
		ObjMove_SetPosition(objBoss,stgx,-1000);
		MapleExplodeC(stgx,150,1.5,"WHITE");
		MapleExplodeC(stgx,150,2,"WHITE");
		descent(i in 0..30){
		Maple(stgx,150,1+i/30,rand(0,360),color[0],color[1],color[2]);
		yield;}
		wait(60);
		
		EndBoss(0);
		yield;
		CloseScript(GetOwnScriptID());
}

task MapleExplodeC(px,py,speed,c){
	let color = GetColor(c);
	let st = rand(0,30);
	//TExpan(px,py,18,60,color[0]*0.7,color[1]*0.7,color[2]*0.7);
	//PlaySound("stars",100,0);
	ascent(i in 0..18){
		Maple(px,py,speed+speed/(5/3)*sin(i*177),i*30+st,color[0],color[1],color[2]);
		wait(i%2);
	}
}

task ExCircle(x,y,sc,dtime){
let obj = ObjPrim_Create(OBJ_SPRITE_2D);
let timer = 0;
let s = 0;


let Target = GetCurrentScriptDirectory~"./img/Effect03.png";
let mx = ObjRender_GetX(obj);
let my = ObjRender_GetY(obj);


	Obj_SetRenderPriorityI(obj, 79);
	ObjPrim_SetTexture(obj, Target);

	ObjRender_SetBlendType(obj, BLEND_INV_DESTRGB);

	ObjSprite2D_SetSourceRect(obj, 0, 0, 512, 512);

        ObjSprite2D_SetDestCenter(obj);
        ObjRender_SetScaleXYZ(obj, s, s, s);
ObjRender_SetPosition(obj,x,y,0);
loop{
timer++;
        s+=sc;
        ObjRender_SetScaleXYZ(obj, s, s, s);

        if(timer == dtime){
        Obj_Delete(obj);
        break;
        }
        yield;
        }
        }
		
task ScreenShake(time){
				loop(time/2){
				Set2DCameraFocusX(Get2DCameraX + rand(-8, 8));
				Set2DCameraFocusY(Get2DCameraY + rand(-8, 8));
				yield;
				Reset2DCamera;
				yield;
				}
				Reset2DCamera;
			}	