#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,[255,0,0]);
	wait(85);	
	SE_Play(explode,100);
	ObjEnemy_SetDamageRate(Flandre,15,4);
	SetForbidPlayerSpell(true);
	fire;
	fire2;
	fire3;
	wait(1500);
	DeleteShotAll(TYPE_ALL, TYPE_FADE);
	SE_Play(charge1,100);
	ConcentrationA3(Flandre,"MAPLE",60,7,150,[255,0,0]);
	wait(85);	
	SE_Play(explode,100);
	ObjEnemyBossScene_SetSpellTimer(GetEnemyBossSceneObjectID,5940);
	CurvyLazers;
	fire4;
	wait(2400);
	DeleteShotAll(TYPE_ALL, TYPE_FADE);
	SE_Play(charge1,100);
	ConcentrationA3(Flandre,"MAPLE",60,7,150,[255,0,0]);
	wait(85);	
	SE_Play(explode,100);
	fire5;
}

task fire{
	let angle = GetAngleToPlayer(GetPlayerObjectID);
	let numberofbullets = 1;
	loop(100){
		
		SE_Play(shot,30);
		
		loop(numberofbullets){
			let shot = CreateShotA1(GetPlayerX,GetPlayerY-90,1,angle,832,10);
			RunReflectCode2(shot);
			Obj_GetValue(shot, "Fireball");
		angle+=360/numberofbullets;}
		angle+=11;
	wait(15);}
}

task fire2{
	let angle = GetAngleToPlayer(Flandre);
	let numberofbullets = 1;
	loop(100){
		
		SE_Play(shot,30);
		
		loop(numberofbullets){
			let shot = CreateShotA1(GetPlayerX,GetPlayerY+90,1,angle,832,10);
			RunReflectCode2(shot);
			Obj_GetValue(shot, "Fireball");
		angle+=360/numberofbullets;}
		angle-=11;
	wait(15);}
}

task fire3{
	let angle;
	loop(25){
		SE_Play(lazer,80);
		angle = GetAngleToPlayer(Flandre);
		loop(3){CreateStraightLaserA1(FlandreX,FlandreY,angle,1000,20,60,1003,10);
		angle+=360/3;}
	wait(60);}
}


task CurvyLazers
{
	let angle;
	let numberofbullets = 10;
	
	loop(20){
			angle = rand(0,360);
			SE_Play(shot,30);
			
			loop(numberofbullets){
				let shot = CreateCurveLaserA1(FlandreX,FlandreY, 10, angle, 30, 20, 1019, 10);
				ObjMove_SetAngularVelocity(shot,1.2);
			angle+=360/numberofbullets;}
			wait(60);
		
			angle = rand(0,360);
			SE_Play(shot,30);
			loop(numberofbullets){
				let shot = CreateCurveLaserA1(FlandreX,FlandreY, 10, angle, 30, 20, 1019, 10);
				ObjMove_SetAngularVelocity(shot,-1.2);
			angle+=360/numberofbullets;}
			wait(60);
	}

}

task fire4{
	loop(80){
		SE_Play(shot,30);
		loop(7){CreateShotA1(FlandreX,FlandreY,3,rand(0,360),875,10);}
	wait(30);}
}

task fire5{
	loop{
		SE_Play(shot,30);
		loop(1){CreateShotA1(FlandreX,FlandreY,9,rand(0,360),875,10);
		CreateShotA1(FlandreX,FlandreY,8,rand(0,360),875,10);
		CreateShotA1(FlandreX,FlandreY,7,rand(0,360),875,10);}
	wait(2);}
}

task RunReflectCode2(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 \"345 year's in a Scenario\"", 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);
		SetForbidPlayerShot(false);
	SetForbidPlayerSpell(false);
	
	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);
}