#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);
	ConcentrationA3(Nazrin,"MAGIC",40,7,150,[255,0,0]);
	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;
loop{
	SE_Play(charge1,60);
	ConcentrationA3(Nazrin,"STAR",40,7,150,[255,0,0]);
	wait(60);
	SE_Play(explode,70);
angle = rand(0,360);
loop(3){
CreateStraightLaserA1(NazrinX,NazrinY,angle,1000,20,30,1003,90);
angle+=360/3;
}
wait(90);
SE_Play(lazer,70);
loop(3){
Fire(832,angle,1,1);
angle+=360/3;
}
wait(180);
ObjMove_SetDestAtWeight(Nazrin,GetCenterX-rand(-110,+110),GetCenterY-120,10,4);
wait(120);
}
}

task Fire(graphic,angle,speed,number){
let shot = CreateShotA1(NazrinX,NazrinY, 8, angle, 1,10);
Obj_SetVisible(shot,false);
while(!Obj_IsDeleted(shot)){
loop(number){
CreateShotA1(ObjMove_GetX(shot),ObjMove_GetY(shot), speed, angle, graphic,10);
angle+=360/number;
}
angle+=11;
wait(3);
}
}

task FireN{
let angle;
loop{
	SE_Play(charge1,60);
	ConcentrationA3(Nazrin,"STAR",40,7,150,[255,0,0]);
	wait(60);
	SE_Play(explode,70);
angle = rand(0,360);
loop(6){
CreateStraightLaserA1(NazrinX,NazrinY,angle,1000,20,30,1003,90);
angle+=360/6;
}
wait(90);
SE_Play(lazer,70);
loop(6){
Fire(832,angle,1,2);
angle+=360/6;
}
wait(180);
ObjMove_SetDestAtWeight(Nazrin,GetCenterX-rand(-110,+110),GetCenterY-120,10,4);
wait(120);
}
}

task FireH{
let angle;
loop{
	SE_Play(charge1,60);
	ConcentrationA3(Nazrin,"STAR",40,7,150,[255,0,0]);
	wait(60);
	SE_Play(explode,70);
angle = rand(0,360);
loop(9){
CreateStraightLaserA1(NazrinX,NazrinY,angle,1000,20,30,1003,90);
angle+=360/9;
}
wait(90);
SE_Play(lazer,70);
loop(9){
Fire(832,angle,2,3);
angle+=360/9;
}
wait(180);
ObjMove_SetDestAtWeight(Nazrin,GetCenterX-rand(-110,+110),GetCenterY-120,10,4);
wait(120);
}
}

task FireL{
let angle;
loop{
	SE_Play(charge1,60);
	ConcentrationA3(Nazrin,"STAR",40,7,150,[255,0,0]);
	wait(60);
	SE_Play(explode,70);
angle = rand(0,360);
loop(11){
CreateStraightLaserA1(NazrinX,NazrinY,angle,1000,20,30,1003,90);
angle+=360/11;
}
wait(90);
SE_Play(lazer,70);
loop(11){
Fire(832,angle,1,2);
angle+=360/11;
}
wait(180);
ObjMove_SetDestAtWeight(Nazrin,GetCenterX-rand(-110,+110),GetCenterY-120,10,4);
wait(120);
}
}

task spellcall{
let name;
if(GetCommonData("Difficulty",1)==1){name = "Loot Sign 'Brillian Treasure'";}
if(GetCommonData("Difficulty",1)==2){name = "Pagoda Sign 'Pagoda Treasure'";}
if(GetCommonData("Difficulty",1)==3){name = "Pagoda Sign 'Brillian Pagoda'";}
if(GetCommonData("Difficulty",1)==4){name = "Treasure Sign 'Mystery Treasure'";}
	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()); 
}