
#ScriptVersion[3]
#Title["Asgore Non 03"]
#Text["Code by Sparen[r]My First Bullet!"]
#BGM["./AsgoreTheme.ogg"]

let AsgoreFight = GetCurrentScriptDirectory ~ "AsgoreFight.ogg";
let objEnemy;
let sky = GetCurrentScriptDirectory ~ "img/sky.png";
let objPlayer;
let xbala=200;


#include"./Shot/AllStarShot_Const.dnh"
#include"script/default_system/Default_Effect.txt"
#include"./Function/Basic.txt"

@Initialize
{

	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	objPlayer = GetPlayerObjectID();
	TWork;
	TRender;
	TText;
	TEnd;
	renderBG;
	ObjEnemy_SetDamageRate(objEnemy, 0, 0);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);//	
}

@MainLoop
{
		ObjEnemy_SetIntersectionCircleToPlayer(objEnemy,ObjMove_GetX(objEnemy)-30,ObjMove_GetY(objEnemy),34);
	ObjEnemy_SetIntersectionCircleToShot(objEnemy,ObjMove_GetX(objEnemy)-30,ObjMove_GetY(objEnemy),34);
	yield;
}


@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
	{
		SetScriptResult(3000);//
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(60);//
	}
    
}

//----------------------------------------------------
//
//----------------------------------------------------
task TWork
{
	
	let cx = GetStgFrameWidth() / 2;
	ObjMove_SetDestAtWeight(objEnemy, cx+30, 120, 10,60);
	wait(60);
	ObjEnemy_SetDamageRate(objEnemy, 90, 50);
	let frame = 0;
	while(!Obj_IsDeleted(objEnemy))
	{	
		if(frame%12==0)
		{
			if(xbala<-25)
			{
			xbala+= 100;
			}
			if(xbala> 450)
			{
			xbala+= -100;
			}
			shot1;
			

		}
		frame++;	
		if(frame%180==120)
		{
			let px = GetPlayerX()+30;
			ObjMove_SetDestAtWeight(objEnemy, px + rand(-30, 30), 90+rand(0,45), 10,30);
		}
		yield;
	}

}

	

task shot1
{
    let angle1 = GetAngleToPlayer(objEnemy);
    loop(40){
	PlaySound(GetCurrentScriptDirectory ~ "Shot1.wav");

            CreateShotA1(xbala, 50, 5, rand(angle1+rand(0,0), angle1-rand(0,0)), 17, 20);
 
        angle1 += 360/40;
      
    }
	xbala+=rand(-50,50);
	
	
}






task renderBG {
	
	let scrollTex = 0;

	// background
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,20); 
	ObjPrim_SetTexture(obj,sky); 
	ObjSprite2D_SetSourceRect(obj,0,0,630,716);
	ObjSprite2D_SetDestRect(obj,0,0,GetStgFrameWidth,GetStgFrameHeight);
	yield;
	

} 


//----------------------------------------------------
//
//----------------------------------------------------
task TRender
{
	let imgKitsune = GetCurrentScriptDirectory~"asgoresprite.png";
	ObjPrim_SetTexture(objEnemy, imgKitsune);

	while(!Obj_IsDeleted(objEnemy))
	{
		let angle = ObjMove_GetAngle(objEnemy);
		let speed = ObjMove_GetSpeed(objEnemy);
		if(speed == 0){ObjSprite2D_SetSourceRect(objEnemy, 0, 1, 218, 125);}
		else if(cos(angle) > 0){ObjSprite2D_SetSourceRect(objEnemy, 0, 1, 218, 125);}
		else if(cos(angle) < 0){ObjSprite2D_SetSourceRect(objEnemy, 0, 1, 218, 125);}
		ObjSprite2D_SetDestCenter(objEnemy);

		yield;
	}

}

task TText
{
	wait(60);
	let text = ObjText_Create;
	ObjText_SetText(text,"ASGORE DREEMURR");
	ObjText_SetHorizontalAlignment(text,ALIGNMENT_LEFT);
	ObjRender_SetY(text,12);
	Obj_SetRenderPriorityI(text,40);
	ObjText_SetFontSize(text,10);
	ObjText_SetMaxWidth(text, 300);
	ObjRender_SetX(text, 150);
	ObjText_SetFontBold(text,true);
	ObjText_SetFontBorderType(text,BORDER_FULL);
	ObjText_SetFontBorderWidth(text,1);
	ObjText_SetSidePitch(text,0.3);
	ascent(i in 0..60){
	ObjText_SetFontBorderColor(text,0,0,0);
	yield;
	}
	
	while(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0)
	{
		yield;
	}
	Obj_Delete(text);

}

//----------------------------------------------------
//
//----------------------------------------------------
task TEnd
{
	while(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0)
	{
		yield;
	}

	let ex = ObjMove_GetX(objEnemy);
	let ey = ObjMove_GetY(objEnemy);
	TExplosionA(ex, ey, 10, 0.6);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);//	
	Obj_Delete(objEnemy);

	loop(30)
	{
		DeleteShotAll(TYPE_ALL, TYPE_IMMEDIATE);
		yield;
	}

	CloseScript(GetOwnScriptID());
}