#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 Koishi = ID_INVALID;
let KoishiX = 0;
let KoishiY = 0;

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

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

let imgKoishi = CSD~"./img/dot_Koishi.png";
let imgFlandre = CSD~"./img/dot_Flandre.png";
let imgClownpiece = CSD~"./img/dot_Clownpiece.png";

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

@Event{
    SetTimer(999);
    SetLife(900);
    SetScore(1000000);
}
 
@Initialize{
	SetAutoDeleteObject(true);
	Koishi = ObjEnemy_Create(OBJ_ENEMY_BOSS);	
	Flandre = ObjEnemy_Create(OBJ_ENEMY);	
	Clownpiece = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(Clownpiece);
    ObjEnemy_Regist(Koishi);
    ObjEnemy_Regist(Flandre);		
	
	ObjMove_SetY(Koishi, GetCenterY-120);
	ObjMove_SetY(Flandre, GetCenterY-120);
	ObjMove_SetY(Clownpiece, GetCenterY-120);
	ObjMove_SetX(Koishi, GetCenterX-120);
	ObjMove_SetX(Flandre, GetCenterX+120);
	ObjMove_SetX(Clownpiece, GetCenterX);
	
	RenderKoishi;
	RenderClownpiece;
	RenderFlandre;
	ClownpieceManage;
	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);
}

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

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(Koishi,0,0);
	wait(60);
SE_Play(charge1,100);
	ConcentrationA3(Koishi,"HEART",60,7,150,[255,100,255]);
	SE_Play(charge1,100);
	ConcentrationA3(Clownpiece,"STAR",60,7,150,[0,0,255]);
	ConcentrationA3(Flandre,"MAPLE",60,7,150,[255,0,0]);
	ObjEnemyBossScene_SetSpellTimer(GetEnemyBossSceneObjectID,5940);
	wait(85);	
	SE_Play(explode,100);
	spellcall;
	SpellBG;
	wait(125);
	FireTask;	
}

task FireTask{
	wait(40);
	ObjMove_SetDestAtSpeed(Flandre,GetCenterX+170,GetCenterY-120,5);
	ObjMove_SetDestAtSpeed(Koishi,GetCenterX-170,GetCenterY-120,5);
	wait(30);
	SE_Play(charge1,100);
	ConcentrationA3(Clownpiece,"STAR",60,7,150,[255,0,0]);
	wait(85);	
	SE_Play(explode,100);
	screenshake;
	ObjEnemy_SetDamageRate(Koishi,14,0);
	Moon;
	wait(45);
	SE_Play(charge1,100);
	ConcentrationA3(Clownpiece,"STAR",60,7,150,[255,0,0]);
	wait(85);	
	SE_Play(explode,100);
	FlandreBubble;
	KoishiFires;
}

task Moon{
	let obj1 = ObjEnemy_Create(OBJ_ENEMY);
	let scale = 1;	
	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);
	wait(130);
	let angle;
	let numberofbullets = 40;
	loop{
		angle = rand(0,360);
		SE_Play(shot,30);
		
		loop(numberofbullets){
			CreateShotA1(ObjMove_GetX(obj1),ObjMove_GetY(obj1),1,angle,535,10);
		angle+=360/numberofbullets;}
	wait(30);}
}

task KoishiFires{
	let angle;
	let numberofbullets = 5;
	loop{
		angle = rand(0,360);
		SE_Play(shot,30);
		
		loop(numberofbullets){
			let shot = CreateShotA1(KoishiX,KoishiY,4,angle,832,10);
			Obj_GetValue(shot, "Fireball");
		angle+=360/numberofbullets;}
	wait(60);}
}

task FlandreBubble{
	loop{
		SE_Play(chime,60);
		CreateShotA1(FlandreX,FlandreY,6,GetAngleToPlayer(Flandre),823,10);
	wait(120);}
}


task spellcall{
	ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID);
	CutInS1(Koishi, "NAZRIN", "Scarlet Trauma \"Astronomical Memory\"", Cutin, 0, 0,444, 453);
}

task screenshake{
	let shakeamount = 10;
	let delay = 2;

	Set2DCameraFocusX(GetStgFrameWidth/2+shakeamount);
	Set2DCameraFocusY(GetStgFrameHeight/2+shakeamount);
	wait(delay);
	Set2DCameraFocusX(GetStgFrameWidth/2-shakeamount);
	Set2DCameraFocusY(GetStgFrameHeight/2-shakeamount);
	wait(delay);
	Set2DCameraFocusX(GetStgFrameWidth/2+shakeamount);
	Set2DCameraFocusY(GetStgFrameHeight/2-shakeamount);
	wait(delay);
	Set2DCameraFocusX(GetStgFrameWidth/2-shakeamount);
	Set2DCameraFocusY(GetStgFrameHeight/2+shakeamount);
	wait(delay);
	Set2DCameraFocusX(GetStgFrameWidth/2-shakeamount);
	Set2DCameraFocusY(GetStgFrameHeight/2+shakeamount);
	wait(delay);
	Set2DCameraFocusX(GetStgFrameWidth/2-shakeamount);
	Set2DCameraFocusY(GetStgFrameHeight/2+shakeamount);
	wait(delay);
	Set2DCameraFocusX(GetStgFrameWidth/2-shakeamount);
	Set2DCameraFocusY(GetStgFrameHeight/2+shakeamount);
	wait(delay);
	Set2DCameraFocusX(GetStgFrameWidth/2-shakeamount);
	Set2DCameraFocusY(GetStgFrameHeight/2+shakeamount);
	wait(delay);
	Set2DCameraFocusX(GetStgFrameWidth/2);
	Set2DCameraFocusY(GetStgFrameHeight/2);
}

task ClownpieceManage{

	while(ObjEnemy_GetInfo(Koishi,INFO_LIFE)>0){
        KoishiX = ObjMove_GetX(Koishi);
        KoishiY = ObjMove_GetY(Koishi);
		
		FlandreX = ObjMove_GetX(Flandre);
        FlandreY = ObjMove_GetY(Flandre);
		
		ClownpieceX = ObjMove_GetX(Clownpiece);
        ClownpieceY = ObjMove_GetY(Clownpiece);

        ObjEnemy_SetIntersectionCircleToShot(Koishi, KoishiX, KoishiY, 20);
		ObjEnemy_SetIntersectionCircleToShot(Koishi, FlandreX, FlandreY, 20);
		ObjEnemy_SetIntersectionCircleToShot(Koishi, ClownpieceX, ClownpieceY, 20);
		yield;
	}
	
	Obj_Delete(Koishi);
	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/splbg.png");
	ObjSprite2D_SetSourceRect(obj, 0, 0, 400, 467);
	Obj_SetRenderPriorityI(obj,25);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj,GetCenterX,GetCenterY,0);
}