#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 imgKoishi = CSD~"./img/dot_Koishi.png";
let Cutin = GetCurrentScriptDirectory() ~ "./img/KoishiCutin.png";//Anyone

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

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

@MainLoop{
	yield;
}

task MainTask{	
	ObjEnemy_SetDamageRate(Koishi,0,0);
	wait(60);
	SE_Play(charge1,100);
	ConcentrationA3(Koishi,"HEART",60,7,150,[255,100,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(Koishi,"HEART",60,7,150,[255,100,255]);
	wait(85);	
	SE_Play(explode,100);
	screenshake;
	ObjEnemy_SetDamageRate(Koishi,14,0);
	Lazers;
	wait(300);
	SE_Play(charge1,100);
	ConcentrationA3(Koishi,"HEART",60,7,150,[255,0,0]);
	wait(85);	
	SE_Play(explode,100);
	KRings;
}

task Lazers{
	CreateStraightLaserA1(GetCenterX-170,GetCenterY-230,90,1000,20,12000,1003,120);
	CreateStraightLaserA1(GetCenterX+170,GetCenterY-230,90,1000,20,12000,1007,120);
	wait(120);
	SE_Play(lazer,70);	
	loop{
	SE_Play(shot,30);
	CreateShotA1(GetCenterX-170,GetCenterY-rand(-230,+230),3,0,736,10);
	CreateShotA1(GetCenterX+170,GetCenterY-rand(-230,+230),3,180,738,10);
	wait(10);}
}

task KRings{
	let angle;
	let numberofbullets = 60;
	loop{
		angle = rand(0,360);
		SE_Play(shot,30);
		
		loop(numberofbullets){
			CreateShotA1(KoishiX,KoishiY,2,angle,535,10);
		angle+=360/numberofbullets;}
	wait(60);}
}

task spellcall{
	ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID);
	CutInS1(Koishi, "BYAKUREN", "Komeiji Curse \"Reincorporated Philosophy\"", 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 BossManage{

	while(ObjEnemy_GetInfo(Koishi,INFO_LIFE)>0){
        KoishiX = ObjMove_GetX(Koishi);
        KoishiY = ObjMove_GetY(Koishi);

        ObjEnemy_SetIntersectionCircleToShot(Koishi, KoishiX, KoishiY, 20);
		yield;
	}
	
	Obj_Delete(Koishi);
	
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
    CloseScript(GetOwnScriptID()); 
}

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