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

let Clownpiece = ID_INVALID;
let ClownpieceX = 0;
let ClownpieceY = 0;

let imgClownpiece = CSD~"./img/dot_Clownpiece.png";
let Cutin = GetCurrentScriptDirectory() ~ "./img/ClownpieceCutin.png";//Anyone

@Event{
    SetTimer(999);
    SetLife(900);
    SetScore(1000000);
}
 
@Initialize{
	SetAutoDeleteObject(true);
	Clownpiece = ObjEnemy_Create(OBJ_ENEMY_BOSS);	
    ObjEnemy_Regist(Clownpiece);	
	
	ObjMove_SetY(Clownpiece, GetCenterY-120);
	ObjMove_SetX(Clownpiece, GetCenterX);
	
	RenderClownpiece;
	BossManage;
	MainTask;     
}

task RenderClownpiece{
	ObjPrim_SetTexture(Clownpiece,imgClownpiece);
	ObjSprite2D_SetDestCenter(Clownpiece);	
	ObjRender_SetScaleXYZ(Clownpiece,1,1,0);
	
	ObjAnim_AddFrameA1(Clownpiece, ANIM_IDLE, 32, 32, 96, 96);
	ObjAnim_AddFrameA1(Clownpiece, ANIM_IDLE, 32, 160, 96, 224);
	ObjAnim_AddFrameA1(Clownpiece, ANIM_IDLE, 32, 288, 96, 352);
	ObjAnim_AddFrameA1(Clownpiece, ANIM_IDLE, 160, 32, 224, 96);
	ObjAnim_AddFrameA1(Clownpiece, ANIM_IDLE, 160, 160, 224, 224);
	
	ObjAnim_AddFrameA1(Clownpiece, ANIM_MOVE, 288, 288, 352, 352);
	ObjAnim_AddFrameA1(Clownpiece, ANIM_MOVE, 288, 416, 352, 480);
	ObjAnim_AddFrameA1(Clownpiece, ANIM_MOVEEND, 288, 288, 352, 352);
		
	ObjAnim_AddFrameA1(Clownpiece, ANIM_ATTACK, 544, 32, 608, 96);
	ObjAnim_AddFrameA1(Clownpiece, ANIM_ATTACK, 544, 160, 608, 224);
	ObjAnim_AddFrameA1(Clownpiece, ANIM_ATTACK, 544, 288, 608, 352);
	ObjAnim_AddFrameA1(Clownpiece, ANIM_ATTACK, 672, 32, 736, 96);
	ObjAnim_AddFrameA1(Clownpiece, ANIM_ATTACK, 672, 160, 736, 224);
	ObjAnim_AddFrameA1(Clownpiece, ANIM_ATTACK, 672, 288, 736, 352);
	ObjAnim_AddFrameA1(Clownpiece, ANIM_ATTACKEND, 544, 160, 608, 224);
	
	ObjAnim_SetLoopPointA1(Clownpiece, ANIM_MOVE, 1);
	ObjAnim_SetLoopPointA1(Clownpiece, ANIM_ATTACK, 5);
	ObjAnim_SetSpeedA1(Clownpiece, ANIM_IDLE, 7);
	ObjAnim_SetSpeedA1(Clownpiece, ANIM_MOVE, 4);
	ObjAnim_SetSpeedA1(Clownpiece, ANIM_ATTACK, 3);
	ObjAnim_SetSpeedA1(Clownpiece, ANIM_ATTACKEND, 3);
	ObjAnim_StartAnimationA1(Clownpiece, 10, 3);
}

@MainLoop{
	yield;
}

task MainTask{	
	ObjEnemy_SetDamageRate(Clownpiece,0,0);
	wait(60);
	SE_Play(charge1,100);
	ConcentrationA3(Clownpiece,"STAR",60,7,150,[000,000,255]);
	ObjEnemyBossScene_SetSpellTimer(GetEnemyBossSceneObjectID,5940);
	wait(85);	
	SE_Play(explode,100);
	spellcall;
	SpellBG;
	wait(125);
	FireTask;	
}

task FireTask{
	wait(70);
	SE_Play(charge1,100);
	ConcentrationA3(Clownpiece,"STAR",60,7,150,[255,0,0]);
	wait(85);	
	SE_Play(explode,100);
	ObjEnemy_SetDamageRate(Clownpiece,14,0);
	Moon;
	wait(600);
	SE_Play(charge1,100);
	ConcentrationA3(Clownpiece,"STAR",60,7,150,[255,0,0]);
	wait(85);	
	SE_Play(explode,100);
	ObjEnemy_SetDamageRate(Clownpiece,14,0);
	Moon;
	wait(1200);
	SE_Play(charge1,100);
	ConcentrationA3(Clownpiece,"STAR",60,7,150,[255,0,0]);
	wait(85);	
	SE_Play(explode,100);
	ObjEnemy_SetDamageRate(Clownpiece,14,0);
	Moon;
}

task Moon{
	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 = 120;
	let angleadd = 4;
	ObjPrim_SetTexture(obj1, GetCurrentScriptDirectory~"./img/moon.png");
	ObjSprite2D_SetSourceRect(obj1, 0, 0, 128, 128);
	ObjRender_SetScaleXYZ(obj1,scale,scale,0);
	Obj_SetRenderPriorityI(obj1,33);
	ObjSprite2D_SetDestCenter(obj1);
	ObjRender_SetPosition(obj1,GetCenterX,GetCenterY,0);
	ObjRender_SetBlendType(obj1,BLEND_ALPHA);
	ObjEnemy_Regist(obj1);
	MoonFire(obj1);
	loop{
	if(CurrentXaxis<MaxXaxis){CurrentXaxis+=MaxXaxis/time;}
	if(CurrentYaxis<MaxXaxis){CurrentYaxis+=MaxYaxis/time;}
	ObjMove_SetX(obj1,GetCenterX+CurrentXaxis*cos(angle));
	ObjMove_SetY(obj1,GetCenterY+CurrentYaxis*sin(angle));
	angle+=360/time;
	yield;}
}

task MoonFire(obj){
	let angle = GetAngleToPlayer(obj);
	let numberofbullets = 1;
	loop{
		
		SE_Play(shot,30);
		
		loop(numberofbullets){
			let MoonS = CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),2,angle,833,1);
			Obj_GetValue(MoonS, "Fireball");
		angle+=360/numberofbullets;}
		angle+=5;
	wait(5);}
}

task spellcall{
	ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID);
	CutInS1(Clownpiece, "NAZRIN", "Three Evil Moons", Cutin, 0, 0,444, 453);
}

task BossManage{

	while(ObjEnemy_GetInfo(Clownpiece,INFO_LIFE)>0){
        ClownpieceX = ObjMove_GetX(Clownpiece);
        ClownpieceY = ObjMove_GetY(Clownpiece);

        ObjEnemy_SetIntersectionCircleToShot(Clownpiece, ClownpieceX, ClownpieceY, 20);
		yield;
	}
	
	Obj_Delete(Clownpiece);
	
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
    CloseScript(GetOwnScriptID()); 
}

task SpellBG{
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	
	ObjPrim_SetTexture(obj, CSD~"./img/BG/splbgC.png");
	ObjSprite2D_SetSourceRect(obj, 0, 0, 384, 448);
	Obj_SetRenderPriorityI(obj,25);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj,GetCenterX,GetCenterY,0);
}