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

let Flandre = ID_INVALID;
let FlandreX = 0;
let FlandreY = 0;

let imgFlandre = CSD~"./img/dot_Flandre.png";

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

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


@MainLoop{
	yield;
}

task MainTask{
	ObjEnemy_SetDamageRate(Flandre,0,0);
	wait(60);
	wait(130);
	SE_Play(charge1,100);
	ConcentrationA2(Flandre,"MAPLE",60,7,150,[255,0,0]);
	ObjEnemyBossScene_SetSpellTimer(GetEnemyBossSceneObjectID,5940);
	FireTask;
	ObjEnemy_SetDamageRate(Flandre,25,0);	
}

task FireTask{
	wait(130);
	FlanRings;
}

task FlanRings{
	let angle;
	let numberofbullets = 20;
	loop{
		angle = rand(0,360);
		SE_Play(chime,70);
		
		loop(numberofbullets){
		let Shot1 = CreateShotA1(FlandreX,FlandreY,5,angle,800,10);
		let Shot2 = CreateShotA1(FlandreX,FlandreY,6,angle,800,10);
		let Shot3 = CreateShotA1(FlandreX,FlandreY,4,angle,800,10);
		RunReflectCode(Shot1);
		RunReflectCode(Shot2);
		RunReflectCode(Shot3);
		angle+=360/numberofbullets;}
	wait(120);}
}

task RunReflectCode(obj)
{
	while(!Obj_IsDeleted(obj))
	{		
		if(trunc(ObjMove_GetX(obj))>GetStgFrameWidth)
		{
			ObjMove_SetX(obj,GetStgFrameWidth-ObjMove_GetX(obj)+GetStgFrameWidth);
			ObjMove_SetAngle(obj,180 - ObjMove_GetAngle(obj));
			return;
		}
    
		if(trunc(ObjMove_GetX(obj))<0)
		{	
			ObjMove_SetX(obj,-ObjMove_GetX(obj));
			ObjMove_SetAngle(obj,180 - ObjMove_GetAngle(obj));
			return;
		}
		
        if(trunc(ObjMove_GetY(obj))>GetStgFrameHeight)
		{
			return;// comment out to have bottom screen bouncing
			ObjMove_SetY(obj,448-ObjMove_GetY(obj)+448);
			ObjMove_SetAngle(obj,360 - ObjMove_GetAngle(obj));
        }
		
        if(trunc(ObjMove_GetY(obj))<0)
		{
			ObjMove_SetY(obj,-ObjMove_GetY(obj));
			ObjMove_SetAngle(obj,360 - ObjMove_GetAngle(obj));
			return;
        }
		
		yield;
	}
}


task BossManage{

	while(ObjEnemy_GetInfo(Flandre,INFO_LIFE)>0){
	
		FlandreX = ObjMove_GetX(Flandre);
        FlandreY = ObjMove_GetY(Flandre);

		ObjEnemy_SetIntersectionCircleToShot(Flandre, FlandreX, FlandreY, 20);
		yield;
	}
	
	Obj_Delete(Flandre);
	
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
    CloseScript(GetOwnScriptID()); 
}