#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);
	if(GetCommonData("Difficulty",1)==1){FireE;}
	if(GetCommonData("Difficulty",1)==2){FireN;}
	if(GetCommonData("Difficulty",1)==3){FireH;}
	if(GetCommonData("Difficulty",1)==4){FireL;}
}

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

task FireE{
let angle = 45;

loop{
angle = 45;
ObjMove_SetDestAtWeight(Nazrin,GetCenterX-90,GetCenterY-130,2,1);
loop(10){
SE_Play(lazer,50);
let laser = CreateLooseLaserA1(NazrinX,NazrinY,4,angle,200,10,1053,0);
Fire2(laser,angle,589,5);
angle+=10;
wait(10);}

angle = 135;

ObjMove_SetDestAtWeight(Nazrin,GetCenterX+50,GetCenterY-130,2,1);
loop(25){
SE_Play(lazer,50);
let laser = CreateLooseLaserA1(NazrinX,NazrinY,4,angle,200,10,1054,0);
Fire2(laser,angle,591,5);
angle-=10;
wait(10);}
wait(120);}
}

task FireN{
let angle = 45;

loop{
angle = 45;
ObjMove_SetDestAtWeight(Nazrin,GetCenterX-90,GetCenterY-130,2,1);
loop(15){
SE_Play(lazer,50);
let laser = CreateLooseLaserA1(NazrinX,NazrinY,4,angle,200,10,1051,0);
Fire2(laser,angle,584,8);
angle+=7;
wait(8);}

angle = 135;

ObjMove_SetDestAtWeight(Nazrin,GetCenterX+50,GetCenterY-130,2,1);
loop(30){
SE_Play(lazer,50);
let laser = CreateLooseLaserA1(NazrinX,NazrinY,4,angle,200,10,1050,0);
Fire2(laser,angle,583,8);
angle-=9;
wait(8);}
wait(120);}
}

task FireH{
let angle = 45;

loop{
angle = 45;
ObjMove_SetDestAtWeight(Nazrin,GetCenterX-90,GetCenterY-130,2,1);
loop(25){
SE_Play(lazer,50);
let laser = CreateLooseLaserA1(NazrinX,NazrinY,3,angle,200,10,1055,0);
Fire2(laser,angle,562,12);
angle+=12;
wait(8);}

angle = 135;

ObjMove_SetDestAtWeight(Nazrin,GetCenterX+50,GetCenterY-130,2,1);
loop(50){
SE_Play(lazer,50);
let laser = CreateLooseLaserA1(NazrinX,NazrinY,3,angle,200,10,1056,0);
Fire2(laser,angle,563,12);
angle-=15;
wait(8);}
wait(180);}
}

task FireL{
let angle = 45;

loop{
angle = 135;
ObjMove_SetDestAtWeight(Nazrin,GetCenterX-90,GetCenterY-130,2,1);
loop(25){
SE_Play(lazer,50);
let laser = CreateLooseLaserA1(NazrinX,NazrinY,3,angle,200,10,1049,0);
Fire2(laser,angle,549,15);
angle-=15;
wait(8);}

angle = 45;

ObjMove_SetDestAtWeight(Nazrin,GetCenterX+50,GetCenterY-130,2,1);
loop(30){
SE_Play(lazer,50);
let laser = CreateLooseLaserA1(NazrinX,NazrinY,3,angle,200,10,1052,0);
Fire2(laser,angle,555,15);
angle+=20;
wait(8);}
wait(180);}
}

task Fire2(obj,angle,graphic,numberofbullets){
	while(!Obj_IsDeleted(obj))
	{	
		if(trunc(ObjMove_GetX(obj))>GetStgFrameWidth)
		{
		loop(numberofbullets){
			CreateShotA2(ObjMove_GetX(obj),ObjMove_GetY(obj), 0.1, angle, 0.11, 3, graphic,10);
		angle+=360/numberofbullets;}
		return;
		}
		
		if(trunc(ObjMove_GetX(obj))<0)
		{
		loop(numberofbullets){
			CreateShotA2(ObjMove_GetX(obj),ObjMove_GetY(obj), 0.1, angle, 0.11, 3, graphic,10);
		angle+=360/numberofbullets;}
		return;
		}
		
		if(trunc(ObjMove_GetY(obj))>GetStgFrameHeight)
		{
		loop(numberofbullets){
			CreateShotA2(ObjMove_GetX(obj),ObjMove_GetY(obj), 0.1, angle, 0.11, 3, graphic,10);
		angle+=360/numberofbullets;}
		return;
		}

		if(trunc(ObjMove_GetY(obj))<0)
		{
		loop(numberofbullets){
			CreateShotA2(ObjMove_GetX(obj),ObjMove_GetY(obj), 0.1, angle, 0.11, 3, graphic,10);
		angle+=360/numberofbullets;}
		return;
		}

	yield;}
}

task spellcall{
let name;
if(GetCommonData("Difficulty",1)==1){name = "Search Sign 'Emerald Detector'";}
if(GetCommonData("Difficulty",1)==2){name = "Search Sign 'Ruby Detector'";}
if(GetCommonData("Difficulty",1)==3){name = "Search Sign 'Golden Detector'";}
if(GetCommonData("Difficulty",1)==4){name = "Search Sign 'Platinium Detector'";}
	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()); 
}