#include".\shot\AllStarShot_Const.txt"
#include".\Resources.txt"
#include".\selibrary.txt"
#include".\OzzieEffects.txt"
#include"./Cutin.h"

let Nazrin = ID_INVALID;
let NazrinX = 0;
let NazrinY = 0;

let Cutin = GetCurrentScriptDirectory() ~ "./img/Cutin.png";

@Event{
    SetTimer(60);
    SetLife(300);
    SetScore(1000000);
}
 
@Initialize{
	SetAutoDeleteObject(true);
	Nazrin = ObjEnemy_Create(OBJ_ENEMY_BOSS);	
    ObjEnemy_Regist(Nazrin);	
	
	renderNazrinUFO(Nazrin);
	BossManage;
	MainTask;     
}


@MainLoop{
	yield;
}

task MainTask{	
	movement;
	ObjEnemy_SetDamageRate(Nazrin,0,0);
	wait(60);
	SE_Play(charge1,50);
	if(GetCommonData("Difficulty",1)==1){ConcentrationA3(Nazrin,"MAGIC",40,7,150,[0,255,0]);}
	if(GetCommonData("Difficulty",1)==2){ConcentrationA3(Nazrin,"MAGIC",40,7,150,[255,0,255]);}
	if(GetCommonData("Difficulty",1)==3){ConcentrationA3(Nazrin,"CHERRY",40,7,150,[255,255,0]);}
	if(GetCommonData("Difficulty",1)==4){ConcentrationA3(Nazrin,"STAR",40,7,150,[0,0,255]);}
	wait(60);
	spellcall;
	wait(180);
	ObjEnemy_SetDamageRate(Nazrin,7,4);
	Fire01;
}

task movement{
	ObjMove_SetDestAtSpeed(Nazrin,GetCenterX,GetCenterY-120,5);
}

task Fire01{
let numberofbullets;
let speed;
let angle;
let graphic=823;
let waittime;

if(GetCommonData("Difficulty",1)==1){numberofbullets = 5;
speed = 2;
waittime = 180;}
if(GetCommonData("Difficulty",1)==2){numberofbullets = 8;
speed = 5;
waittime = 120;}
if(GetCommonData("Difficulty",1)==3){numberofbullets = 12;
speed = 7;
waittime = 90;}
if(GetCommonData("Difficulty",1)==4){numberofbullets = 15;
speed = 8;
waittime = 60;}

loop(4){
Pendulum;
wait(90);}
loop{
angle = rand(0,360);
SE_Play(chime,50);
		loop(numberofbullets){
			CreateShotA1(NazrinX,NazrinY, speed, angle, graphic,10);
		angle+=360/numberofbullets;}
		wait(waittime);
			ObjMove_SetDestAtWeight(Nazrin,GetCenterX-rand(-110,+110),GetCenterY-120,20,15);
}
}

task Pendulum{
	let obj1 = ObjEnemy_Create(OBJ_ENEMY);
	let scale = 1;	
	let angle = 90;
	let MaxXaxis = GetStgFrameWidth/2;
	let MaxYaxis = GetStgFrameHeight/2;
	let CurrentXaxis = 0;
	let CurrentYaxis = 0;
	let time = 90;
	let dist;
	let angleadd = 4;
	let frame = 0;
	ObjPrim_SetTexture(obj1, GetCurrentScriptDirectory~"./img/Pendulum.png");
	ObjSprite2D_SetSourceRect(obj1, 16, 4, 84, 89);
	ObjRender_SetScaleXYZ(obj1,scale,scale,0);
	Obj_SetRenderPriorityI(obj1,33);
	ObjSprite2D_SetDestCenter(obj1);
	ObjRender_SetPosition(obj1,NazrinX,NazrinY,0);
	ObjRender_SetBlendType(obj1,BLEND_ALPHA);
	ObjEnemy_Regist(obj1);
	FirePendulum(obj1,angle);
	loop{
	dist = 100*cos(frame);
	frame++;
	ObjMove_SetX(obj1,NazrinX+dist*cos(angle));
	ObjMove_SetY(obj1,NazrinY+dist*sin(angle));
	angle+=360/time;
	yield;}}

task FirePendulum(obj,angle){
loop{
SE_Play(shot,50);
CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),1,angle,87,10);
CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),1,angle-180,87,10);
angle+=11;
if(GetCommonData("Difficulty",1)==1){wait(10);}
if(GetCommonData("Difficulty",1)==2){wait(7);}
if(GetCommonData("Difficulty",1)==3){wait(5);}
if(GetCommonData("Difficulty",1)==4){wait(3);}}
}

task spellcall{
let name;
if(GetCommonData("Difficulty",1)==1){name = "Vision Sign 'Low Sensivility Pendulum'";}
if(GetCommonData("Difficulty",1)==2){name = "Vision Sign 'Nazrin Pendulum'";}
if(GetCommonData("Difficulty",1)==3){name = "Defend Sign 'High Vision Nazrin Pendulum'";}
if(GetCommonData("Difficulty",1)==4){name = "Defend Sign 'Pendulum Guard'";}
	ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID);
	CutInS1(Nazrin, "NAZRIN", name, Cutin, 0, 0,340, 464);
}

task BossManage{

	while(ObjEnemy_GetInfo(Nazrin,INFO_LIFE)>0){
        NazrinX = ObjMove_GetX(Nazrin);
        NazrinY = ObjMove_GetY(Nazrin);

        ObjEnemy_SetIntersectionCircleToShot(Nazrin, NazrinX, NazrinY, 20);
        ObjEnemy_SetIntersectionCircleToPlayer(Nazrin, NazrinX,NazrinY, 10);
		yield;
	}
	
	Obj_Delete(Nazrin);
	
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
    CloseScript(GetOwnScriptID()); 
}