//#TouhouDanmakufu
#ScriptVersion[3]
#Title["KogasaS2_H"]
#Text["Bullets"]
let white = GetCurrentScriptDirectory~"sprites/white.png";

#include"./lib/SHOT_REPLACE/shot_replace.dnh"
#include"./Kogasainclude.txt"
let color2 = 1;
let color = 0;
let objEnemy;
let duration = 0;
let stopstuff = false;
let angle = 0;
let angle2 = 0;
let one = 1;
let type = 0;
let animframe = 0;
let frame = 0;
let objlasers = [];
let GetSpeedX = 0;
let variable = 0;
let thunder = GetCurrentScriptDirectory~"sfx/thunder.wav";
let n = 0;
@Event{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
	SetScriptResult(1000);
	}
	case(EV_REQUEST_TIMER){
	SetScriptResult(999);
	}
	case(EV_REQUEST_SPELL_SCORE){
	SetScriptResult(10000000);
	}
	case(EV_GAIN_SPELL)
	{
	AddScore(ObjEnemyBossScene_GetInfo(objScene, INFO_SPELL_SCORE));	
	}
}

@Initialize{
	//Resource Loading
	shotinit;
	LoadSound(shot);
	LoadSound(shot2);
	LoadSound(shot3);
	LoadSound(timeslow);
	LoadSound(shot4);
	LoadSound(slash);
	LoadSound(thunder);
	LoadSound(charge2);
	LoadSound(ping);
	LoadSound(cardcall);
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	Invuln(360, 35, 25);
	//BottomEnemyMarker(objEnemy, objEnemy);
	let Kogasa = GetCurrentScriptDirectory~"sprites/kogasa.png";
	ObjPrim_SetTexture(objEnemy, Kogasa); 
	ObjSprite2D_SetSourceRect(objEnemy, 0, 0, 128, 128);
	ObjAnim_AddFrameA2(objEnemy, ANIM_IDLE, 0, 0, 128, 128, 10, 1);
	ascent(x in 0..2){ObjAnim_AddFrameA2(objEnemy, ANIM_MOVE_LEFT, 256, 128*x, 256+128, 128*x+128, 10, 1);}
	ascent(x in 2..4){ObjAnim_AddFrameA2(objEnemy, ANIM_MOVE_RIGHT, 256, 128*x, 256+128, 128*x+128, 10, 1);}
	CheckForFlip;
	ObjAnim_AddFrameA2(objEnemy, ANIM_MOVEEND_RIGHT, 256, 128*2, 128*3, 128*2+128, 10, 1);
	ObjAnim_AddFrameA2(objEnemy, ANIM_MOVEEND_RIGHT, 0, 0, 128, 128, 10, 1);
	ObjAnim_AddFrameA2(objEnemy, ANIM_MOVEEND_LEFT, 256, 128*2, 256+128, 128*2+128, 10, 1);
	
	ascent(x in 0..4){ObjAnim_AddFrameA2(objEnemy, ANIM_ATTACK, 128*4, 128*x, 128*5, 128*x+128, 10, 1);}
	ascent(x in 0..4){ObjAnim_AddFrameA2(objEnemy, ANIM_ATTACKEND, 128*5, 128*x, 128*6, 128*x+128, 10, 1);}
	
	ObjAnim_SetSpeedA1(objEnemy, ANIM_IDLE, 9);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_MOVE_LEFT, 6);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_MOVE_RIGHT, 6);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_ATTACK, 10);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_MOVE_RIGHT, 1);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_MOVE_LEFT, 1);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_ATTACK, 3);
	ObjAnim_StartAnimationA2(objEnemy, 0, 0);
	ObjSprite2D_SetDestCenter(objEnemy); 
	
	
	
LoadTexture(white);
	
	delayedstart;
	SetShotAutoDeleteClip(30, 100, 30, 20);
	
	
}

task RunReflectCode(obj)
{
        while(!Obj_IsDeleted(obj))
        {
               
                if(trunc(ObjMove_GetX(obj))>384)
                {
                        //PlaySE(ping);
                        ObjMove_SetX(obj,384-ObjMove_GetX(obj)+384);
                        ObjMove_SetAngle(obj,180 - ObjMove_GetAngle(obj));
                        CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),ObjMove_GetSpeed(obj)-0.55,ObjMove_GetAngle(obj)-0.55,WHITE04,15);
                        Obj_Delete(obj);
                        return;
                }
   
                if(trunc(ObjMove_GetX(obj))<0)
                {      
                       // PlaySE(ping);
                        ObjMove_SetX(obj,-ObjMove_GetX(obj));
                        ObjMove_SetAngle(obj,180 - ObjMove_GetAngle(obj));
                        CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),ObjMove_GetSpeed(obj)-0.55,ObjMove_GetAngle(obj),WHITE04,15);
                        Obj_Delete(obj);
                        return;
                }
               
        if(trunc(ObjMove_GetY(obj))>448)
                {
                        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)
                {
						return;
                        ObjMove_SetY(obj,-ObjMove_GetY(obj));
                        ObjMove_SetAngle(obj,360 - ObjMove_GetAngle(obj));
                        CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),ObjMove_GetSpeed(obj)-0.55,ObjShot_GetImageID(obj),WHITE04,15);
                        Obj_Delete(obj);
                        return;
        }
                yield;
        }
}



task delayedstart{

ObjMove_SetDestAtFrame(objEnemy, GetCenterX, 120, 60);
wait(60);

ObjEnemyBossScene_StartSpell(objScene);
ObjEnemyBossScene_SetSpellTimer(objScene, 60*47);

PlaySE(cardcall);
ObjCutin_SetSpellcardS4("Spooky Mirror &quot;Spiral Reflection&quot;", kogasacutin, "KANAKO", 124, 252, 0);
ObjCutin_LaunchS3(KANAKO, kogasacutin, "Hard");
wait(60);
attack1;
}

task CheckForFlip{
yield;
loop{
if(CheckDirectionAL1(objEnemy, "left")){
SetCommonData("flipsprite", true);
}
else if(CheckDirectionAL1(objEnemy, "right")){
SetCommonData("flipsprite", false);
}
if(GetCommonData("flipsprite", false)==true && CheckDirectionAL1(objEnemy, "none")){ ObjRender_SetScaleX(objEnemy, -1)}
else{ ObjRender_SetScaleX(objEnemy, 1); }


yield;
}
}


@MainLoop{
	
	
	SetCommonData("kogasax", ObjMove_GetX(objEnemy));
	SetCommonData("kogasay", ObjMove_GetY(objEnemy));
	if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) <=0 && stopstuff==false){stopstuff=true;}

		
if(stopstuff==true && popped==false){
DestroyBoss;
}	
	
	GetEnemyX = ObjMove_GetX(objEnemy);
	GetEnemyY = ObjMove_GetY(objEnemy);
	ObjEnemy_SetIntersectionCircleToShot(objEnemy, GetEnemyX, GetEnemyY, 23);//当たり判定(自機弾用)登録
	ObjEnemy_SetIntersectionCircleToPlayer(objEnemy, GetEnemyX, GetEnemyY, 17);//当たり判定(体当たり用)登録

	frame++;
	yield;
	GetSpeedX = ObjMove_GetSpeed(objEnemy)*cos(ObjMove_GetAngle(objEnemy));
}

task attack1{
wait(120);
let increment = 0;
let x = 0;
let angle = 0;
let angle2 = 0;
let type = 1;
let color = 0;
one = 1;
loop{
/*
PlaySE(charge2);
if(color%2==0){
TCharge01(objEnemy, 3, 45, 0, 255, 255);
}
else{
TCharge01(objEnemy, 3, 45, 255, 165, 0);
}
wait(45);
*/
loop(48){
if(popped==true){break;}
PlaySE(shot3);
loop(6){
if(GetCommonData("bullettype", 1)==1){
let objaaa = CreateDNHShotA1(ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), 2.25, angle, [YELLOW04, ORANGE04][color%2], 10);
RunReflectCode(objaaa);
}
else{
let objaaa = CreateShotA1(ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), 2.25, angle, [YELLOW04, ORANGE04][color%2], 10);
RunReflectCode(objaaa);
}

angle+=360/6;
}
angle+=360/4/7*one;
wait(4);
}
color++;
one*=-1;
ExMove;
wait(90);
}
}	
task DestroyBoss{
SetCommonData("petals", false);
popped=true;
StopSlow(TARGET_ALL);
loop(10){ yield; }
Obj_Delete(objEnemy);
loop(40){ yield; }
		CloseScript(GetOwnScriptID());
		return;
}
