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

let objEnemy;
let negrito = GetCurrentScriptDirectory ~ "img/negrito.jpg";
let overlay = GetCurrentScriptDirectory ~ "img/pruebanegra.jpg";
let objPlayer;
let balasY =0;
let balasY2 =0;
let contador =0;
#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();
	let objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);
	TWork;
	TRender;
	TText;
	TEnd;
	renderBG;
	ObjEnemy_SetDamageRate(objEnemy, 10, 10);
	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(500);//
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(90);//
	}
	case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(1000000);//
	}
}

//----------------------------------------------------
//
//----------------------------------------------------
task TWork
{
	let cx = GetStgFrameWidth() / 2;
	ObjMove_SetDestAtWeight(objEnemy, cx, 120, 10,60);
	wait(60);
	let frame = 0;
	while(!Obj_IsDeleted(objEnemy))
	{

		
		if(frame%4==0)
		{
			shot1;
		}
		frame++;
		if(frame%120==0)
		{
			let px = GetPlayerX();
			wait(180);
		}
		yield;
	}

}

	


task shot1
{
	wait(20);
	PlaySound(GetCurrentScriptDirectory ~ "Shot1.wav");
		let exx = ObjMove_GetX(objEnemy);
		let eyy = ObjMove_GetY(objEnemy);
		let pxx = GetPlayerX();
		let pyy = GetPlayerY();
		let se = GetCurrentScriptDirectory ~ "Shot1.wav";
		LoadSound(se);
		PlaySE(se);
		let angleToPlayer = atan2(rand(exx-300, 380) , rand(eyy-200,300) ); // este es el que pone petalos en toda la etapa rand(exx-300, 380) , rand(eyy-200,300) 
		let angle1=0;
		while(angle1<360)
		{
		let obj1 = CreateShotA2(exx+70,eyy+30 , 5, angle1, -0.30, 1, 19, 20);
		ObjMove_AddPatternA4(obj1, 60,1.55, rand(-30,30), 0, -0.5, 0,objEnemy , NO_CHANGE);
		angle1 += rand(30,35);
		}	
}






task renderBG {
	
	// background
	let objbackground = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(objbackground,20); 
	ObjPrim_SetTexture(objbackground,negrito); 
	ObjSprite2D_SetSourceRect(objbackground,0,0,80,80);
	ObjSprite2D_SetDestRect(objbackground,0,0,GetStgFrameWidth,GetStgFrameHeight);
	yield;	



	let scrollTex = 0;
	// overlay 
	let overlayObj = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(overlayObj,21); 
	ObjPrim_SetTexture(overlayObj,overlay); 
	ObjRender_SetBlendType(overlayObj,BLEND_ADD_ARGB);
	ObjRender_SetAlpha(overlayObj,1000);
	ObjSprite2D_SetSourceRect(overlayObj,0,0,129,129);
	ObjSprite2D_SetDestRect(overlayObj,0,0,GetStgFrameWidth,GetStgFrameHeight);
	
	// As long as the boss is alive, scroll the overlay diagonally towards bottom right.
	while(!Obj_IsDeleted(objEnemy)) {
		ObjSprite2D_SetSourceRect(overlayObj,0-scrollTex,0-scrollTex,129-scrollTex,129-scrollTex); //ancho pirmero
		scrollTex += 0.2;
		yield;
	}
	Obj_Delete(overlayObj);
} 


//----------------------------------------------------
//
//----------------------------------------------------
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,"Magic Sign: Flamestrike");
	ObjText_SetHorizontalAlignment(text,ALIGNMENT_LEFT);
	ObjRender_SetY(text,25);
	Obj_SetRenderPriorityI(text,40);
	ObjText_SetFontSize(text,14);
	ObjText_SetMaxWidth(text, 300);
	ObjRender_SetX(text, 384 - 300-16);
	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,i*255/60,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);
	SetAutoDeleteObject(true);
	loop(30)
	{
		DeleteShotAll(TYPE_ALL, TYPE_IMMEDIATE);
		yield;
	}

	CloseScript(GetOwnScriptID());
}