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

let AV = GetCurrentScriptDirectory ~ "AsgoreVoice.wav";
	//LoadSound(se);
	//PlaySE(se);

let objEnemy;
let sky = GetCurrentScriptDirectory ~ "img/throne.png";
let xbala= 0;
#include"./Shot/AllStarShot_Const.dnh"
#include"script/default_system/Default_Effect.txt"
#include"./Function/Basic.txt"

@Initialize
{
	LoadSound(AV);	

	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	SetForbidPlayerShot(true); //no disparar
	SetForbidPlayerSpell(true); //no bombas
	PlayerLock;
	TRender;
	TText;
	TEnd;
	renderBG;
	MainTask;
	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;
}
function AsgoreTalk(string){
let tempstring = [];
let obj = ObjPrim_Create(OBJ_SPRITE_2D);
Obj_SetRenderPriorityI(obj, 50);
ObjPrim_SetTexture(obj,GetCurrentScriptDirectory~"./img/bubble.png");
ObjSprite2D_SetSourceRect(obj,1,0,99,66);  //
ObjRender_SetScaleXYZ(obj,1.8,1,0);
ObjSprite2D_SetDestCenter(obj);
ObjRender_SetPosition(obj,300,90,0); //nose, derecha, altura, nose
let objText = ObjText_Create();

Obj_SetRenderPriorityI(objText, 51);
ObjText_SetFontSize(objText, 14);
ObjText_SetFontBold(objText, true);
ObjText_SetFontBorderType(objText, BORDER_NONE);
ObjText_SetFontColorTop(objText, 0,0,0);
ObjText_SetFontColorBottom(objText, 0,0,0);
ObjText_SetMaxWidth(objText, 130);
ObjRender_SetPosition(objText,245,65,0);
ObjText_SetLinePitch(objText,0);
ascent(i in 0..length(string)){
tempstring =  tempstring ~ [string[i]];
ObjText_SetText(objText, tempstring);
if([string[i]]!=[' ']){
PlaySE(AV);
wait(4);
}

}
wait(45);
Obj_Delete(obj);
Obj_Delete(objText);
}

task MainTask{
let cx = GetStgFrameWidth() / 2;
	ObjMove_SetDestAtWeight(objEnemy, cx+30, 120, 10,60);
		PlayerLocked = true;
	wait(60);
	ObjEnemy_SetDamageRate(objEnemy, 110, 50);
	wait(30);
	AsgoreTalk("Only one soul   is what i need.");
	                                                 //while(GetVirtualKeyState(VK_SHOT)!=KEY_PUSH){yield;}

	AsgoreTalk("Maybe that way  Tori can forgiveme.");
	
	AsgoreTalk("But well,       with Z          you shoot.");
	

	AsgoreTalk("With X,         you can bomb.");
	
	AsgoreTalk("With shift,     you can see     your hitbox.");
	

	AsgoreTalk("I'll must stay       ...");
	

	AsgoreTalk("    DETERMINED.");
	wait(120); 

		PlayerLocked = false;	
		PlaySound(GetCurrentScriptDirectory ~ "AsgoreVoice.wav");
		SetForbidPlayerShot(false); //si disparar
		SetForbidPlayerSpell(false); //si bombas
		TWork;





}

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

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



task TWork
{	
	let frame = 0;
	while(!Obj_IsDeleted(objEnemy))
	{	
		if(frame%10==0)
		{
			if(xbala > 400)
			{
			wait(60);
			xbala=0;
			}
		shot1;
		}
		frame++;
		if(frame%180==120)
		{
			let cx = GetStgFrameWidth() / 2;
			ObjMove_SetDestAtWeight(objEnemy, cx +30 + rand(-20, 20), 120+rand(-10,+10), 10,30);
		}
		yield;
	}

}

	

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

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

	
}

task renderBG {
	


	// background
	let objbackground = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(objbackground,20); 
	ObjPrim_SetTexture(objbackground,sky); 
	ObjSprite2D_SetSourceRect(objbackground,0,0,480,601);
	ObjSprite2D_SetDestRect(objbackground,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);

}

let PlayerLocked = false;

task PlayerLock{
	let PlayerX;
	let PlayerY;
	
	loop{
		if(PlayerLocked==false){
			PlayerX=GetPlayerX;
			PlayerY=GetPlayerY;
		}
		if(PlayerLocked==true){
			ObjMove_SetPosition(GetPlayerObjectID,PlayerX,PlayerY);
		}
	yield;}
}



//----------------------------------------------------
//
//----------------------------------------------------
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());
}