#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);
	Hearts;
	wait(360);
	SE_Play(alarm,100);
	CAUTION_DECLARE(80,GetStgFrameHeight/2);
	wait(125);
	SE_Play(charge1,100);
	ConcentrationA3(Koishi,"HEART",60,7,150,[213,213,213]);
	wait(85);	
	SE_Play(explode,100);
	WallBubbles_2and3;
	WallBubbles;
}


task spellcall{
	ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID);
	CutInS1(Koishi, "BYAKUREN", "Felling \"Release of Love\"", 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 Hearts{
	let angle;
	let numberofbullets = 30;
	loop{
		angle = rand(0,360);
		SE_Play(shot,30);
		
		loop(numberofbullets){
			let shot = CreateShotA1(KoishiX,KoishiY,2,angle,736,10);
			ObjMove_SetAngularVelocity(shot,0.5);
			
			let shot2 = CreateShotA1(KoishiX,KoishiY,2,angle,737,10);
			ObjMove_SetAngularVelocity(shot2,-0.5);
		angle+=360/numberofbullets;}
	wait(120);}
}

task WallBubbles{
loop{
CreateShotA1(GetCenterX+210,GetCenterY,4,180,735,10);
CreateShotA1(GetCenterX-210,GetCenterY,4,0,735,10);
CreateShotA1(GetCenterX,GetCenterY-230,4,90,735,10);
CreateShotA1(GetCenterX,GetCenterY+230,4,270,735,10);
wait(60);}
}

task WallBubbles_2and3{
loop{
CreateShotA1(GetCenterX+210,GetCenterY+110,4,180,735,10);
CreateShotA1(GetCenterX-210,GetCenterY-110,4,0,735,10);
CreateShotA1(GetCenterX-210,GetCenterY+110,4,0,735,10);
CreateShotA1(GetCenterX+210,GetCenterY-110,4,180,735,10);
CreateShotA1(GetCenterX-100,GetCenterY-230,4,90,735,10);
CreateShotA1(GetCenterX+100,GetCenterY+230,4,270,735,10);
CreateShotA1(GetCenterX+100,GetCenterY-230,4,90,735,10);
CreateShotA1(GetCenterX-100,GetCenterY+230,4,270,735,10);
wait(60);}
}

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

task CAUTION_DECLARE(render,yposition){	
	caution(GetStgFrameWidth/2,yposition+90,render);
	caution(GetStgFrameWidth/2-250,yposition+90,render);
	caution(GetStgFrameWidth/2+250,yposition+90,render);
	
	caution(GetStgFrameWidth/2,yposition-90,render);
	caution(GetStgFrameWidth/2-250,yposition-90,render);
	caution(GetStgFrameWidth/2+250,yposition-90,render);
	
	radiation(GetStgFrameWidth/2,yposition,render);
	radiation(GetStgFrameWidth/2-250,yposition,render);
	radiation(GetStgFrameWidth/2+250,yposition,render);
	yield;
}

task caution(xpos,ypos,renderpriority){
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	let counter = 0;
	let size = 0;
	let alpha = 0;
	
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"./img/caution.png");
	ObjSprite2D_SetSourceRect(obj,0, 216, 256, 256);
	ObjRender_SetScaleXYZ(obj,1,1,0);
	Obj_SetRenderPriorityI(obj,renderpriority);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetPosition(obj,xpos,ypos,0);
	ObjRender_SetAngleXYZ(obj,0,0,0);	
	ObjRender_SetAlpha(obj,alpha);
	
	loop(20){		
		counter++;
		alpha+=255/20;
		ObjRender_SetAlpha(obj,alpha);
		ObjRender_SetPosition(obj,xpos+(counter),ypos,0);		
		yield;
	}
	loop(140){		
		counter++;
		ObjRender_SetPosition(obj,xpos+(counter),ypos,0);		
		yield;
	}
	loop(20){		
		counter++;
		alpha-=255/20;
		ObjRender_SetAlpha(obj,alpha);
		ObjRender_SetPosition(obj,xpos+(counter),ypos,0);		
		yield;
	}
	Obj_Delete(obj);
}

task radiation(xpos,ypos,renderpriority){
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	let counter = 0;
	let size = 0;
	let alpha = 0;
	
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"./img/caution.png");
	ObjSprite2D_SetSourceRect(obj,0, 0, 256, 128);
	ObjRender_SetScaleXYZ(obj,1,1,0);
	Obj_SetRenderPriorityI(obj,renderpriority);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetPosition(obj,xpos,ypos,0);
	ObjRender_SetAngleXYZ(obj,0,0,0);	
	ObjRender_SetAlpha(obj,alpha);
	
	loop(20){		
		counter++;
		alpha+=255/20;
		ObjRender_SetAlpha(obj,alpha);
		ObjRender_SetPosition(obj,xpos-(counter),ypos,0);		
		yield;
	}
	loop(140){		
		counter++;
		ObjRender_SetPosition(obj,xpos-(counter),ypos,0);		
		yield;
	}
	loop(20){		
		counter++;
		alpha-=255/20;
		ObjRender_SetAlpha(obj,alpha);
		ObjRender_SetPosition(obj,xpos-(counter),ypos,0);		
		yield;
	}
	Obj_Delete(obj);
}