#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 Flandre = ID_INVALID;
let FlandreX = 0;
let FlandreY = 0;

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

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

@Event{
    SetTimer(999);
    SetLife(900);
    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);
	ConcentrationA3(Flandre,"MAPLE",60,7,150,[255,0,0]);
	wait(85);	
	SE_Play(explode,100);
	spellcall;
	SpellBG;
	ObjEnemyBossScene_SetSpellTimer(GetEnemyBossSceneObjectID,5940);
	FireTask;	
}

task FireTask{
	wait(130);
	SE_Play(charge1,100);
	ConcentrationA3(Flandre,"MAPLE",60,7,150,[0,0,255]);
	wait(85);	
	SE_Play(explode,100);
	ObjEnemy_SetDamageRate(Flandre,30,4);
	PlayableLazer;
	wait(60);
	SE_Play(lazer,70);	
	PlayableKnifes;
}

task PlayableKnifes{
	let angle;
	let objPlayer = GetPlayerObjectID();
	let numberofbullets = 10;
	loop{
		angle = rand(0,360);
		SE_Play(shot,30);
		
		loop(numberofbullets){
			let obj = CreateShotA2(GetCenterX,GetCenterY,0.1,angle,0.17,2.5,555,10);
			let obj2 = ObjMove_AddPatternA4(obj, 60, 3, 0, 0, 0.8, 1, objPlayer, 551);
			//RunReflectCode(obj);
		angle+=360/numberofbullets;}
	wait(60);}
}

task PlayableLazer{
	let angle = 0;
	
	let obj1 = CreateStraightLaserA1(GetCenterX,GetCenterY,0,250,20,1000000,778,60);
	let obj2 = CreateStraightLaserA1(GetCenterX,GetCenterY,90,250,20,1000000,778,60);
	let obj3 = CreateStraightLaserA1(GetCenterX,GetCenterY,180,250,20,1000000,778,60);
	let obj4 = CreateStraightLaserA1(GetCenterX,GetCenterY,270,250,20,1000000,778,60);

	
	ObjShot_SetAutoDelete(obj1,false);
	ObjShot_SetAutoDelete(obj2,false);
	ObjShot_SetAutoDelete(obj3,false);
	ObjShot_SetAutoDelete(obj4,false);
	
	ObjShot_SetSpellResist(obj1,true);
	ObjShot_SetSpellResist(obj2,true);
	ObjShot_SetSpellResist(obj3,true);
	ObjShot_SetSpellResist(obj4,true);
		
	loop{
		ObjStLaser_SetAngle(obj1,0+angle);
		ObjStLaser_SetAngle(obj2,90+angle);
		ObjStLaser_SetAngle(obj3,180+angle);
		ObjStLaser_SetAngle(obj4,270+angle);

		ObjMove_SetX(obj1,GetCenterX);
		ObjMove_SetY(obj1,GetCenterY);
		
		ObjMove_SetX(obj2,GetCenterX);
		ObjMove_SetY(obj2,GetCenterY);
		
		ObjMove_SetX(obj3,GetCenterX);
		ObjMove_SetY(obj3,GetCenterY);
		
		ObjMove_SetX(obj4,GetCenterX);
		ObjMove_SetY(obj4,GetCenterY);
		
		angle+=1.1;
		yield;}
}

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 Movement{
	loop{
		if(GetPlayerX>GetStgFrameWidth/2-100 && GetPlayerX<GetStgFrameWidth/2+100){
			ObjMove_SetDestAtWeight(Flandre,GetPlayerX,GetStgFrameHeight/2-rand(100,150),20,2);}
		else if(GetPlayerX<GetStgFrameWidth/2-100){
			ObjMove_SetDestAtWeight(Flandre,GetStgFrameWidth/2-100,GetStgFrameHeight/2-rand(100,150),20,2);}
		else if(GetPlayerX>GetStgFrameWidth/2+100){
			ObjMove_SetDestAtWeight(Flandre,GetStgFrameWidth/2+100,GetStgFrameHeight/2-rand(100,150),20,2);}
	wait(180);}
}

task spellcall{
	ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID);
	CutInS1(Flandre, "KANAKO", "Scarlata Taboo \"Playdate\"", Cutin, 0, 0,444, 453);
}

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()); 
}

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