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

#include"./lib/SHOT_REPLACE/shot_replace.dnh"
#include"./Kogasainclude.txt"
let color2 = 1;
let vacuum = GetCurrentScriptDirectory~"sfx/vacuum.wav";
let color = 0;
let objEnemy;
let duration = 0;
let invulnvar = 0;
let stopstuff = false;
let angle = 0;
let angle2 = 0;
let one = -1;
let type = 0;
let animframe = 0;
let renderangle = 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(vacuum);
	LoadSound(timeslow);
	LoadSound(shot4);
	LoadSound(slash);
	LoadSound(thunder);
	LoadSound(charge2);
	LoadSound(ping);
	LoadSound(boom);
	LoadSound(cardcall);
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	Invuln(420, 22, 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_ATTACK, 128*5, 128*x, 128*6, 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, 12);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_MOVE_RIGHT, 1);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_MOVE_LEFT, 1);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_ATTACK, 0);
	ObjAnim_StartAnimationA2(objEnemy, 0, 0);
	
	ObjSprite2D_SetDestCenter(objEnemy); 
	
	
	
LoadTexture(white);
	delayedstart;
	SetShotAutoDeleteClip(150, 100, 150, 100);
	
	
}

task spinny{
let renderangleacc = 0;
loop{
ObjRender_SetAngleZ(objEnemy, ObjRender_GetAngleZ(objEnemy)+renderangleacc);
wait(1);
if(renderangleacc<=24){
renderangleacc+=0.05;
}
}
}

task delayedstart{

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

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

PlaySE(cardcall);
ObjCutin_SetSpellcardS4("Spooky Vortex &quot;Karakasa's Great Whirlpool&quot;", kogasacutin, "HINA", 0, 191, 255);
ObjCutin_LaunchS3(KANAKO, kogasacutin, "Hard");
wait(60);
attack1;
lazydelete;
attack2;
attack3;
}

task nodamage{
loop{
ObjRender_SetAlpha(objEnemy, 255);
if(GetPlayerInvincibilityFrame>0){
invulnvar = 30;
}
if(invulnvar>0){
ObjRender_SetAlpha(objEnemy, 60);
Invuln(120, 15, 25);
}
invulnvar--;
wait(1);
}
}

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);
	if(invulnvar<=0){
	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;
let suction = 1;
one = 1;
PlaySE(charge2);
TCharge01(objEnemy, 3, 45, 0, 0, 255);
wait(45);
nodamage;
PlaySE(vacuum);
spinny;
loop{
if(popped==true){break;}
wait(1);
ObjMove_SetPosition(GetPlayerObjectID, GetPlayerX-suction*cos(angletoplayer(objEnemy)), GetPlayerY-suction*sin(angletoplayer(objEnemy)));
}
}	

task attack2{
wait(210);
loop{
PlaySE(shot3);
loop(12){
CreateSwirlyShot(ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), -2, angle, BLUE57, 10, 0.4, 300);
angle+=360/12;
}
wait(30);
angle+=360/12/3;
if(popped==true){ break; }
}
}

task CreateSwirlyShot(x, y, v, angle, graphic, delay, angvel, startradius){
let obj = ObjShot_Create(OBJ_SHOT);
ObjShot_SetDelay(obj, delay);
ObjMove_SetPosition(obj, x, y);
ObjShot_SetGraphic(obj, graphic);
ObjMove_SetAngle(obj, angle);
ObjMove_SetSpeed(obj, 0);
ObjShot_Regist(obj);
let radius = startradius;
let decrease = 0;
let angle2 = 0;
let initangle = angle;
let shotangle = 0;

while(!Obj_IsDeleted(obj)){
ObjMove_SetPosition(obj, x+radius*cos(angle+angle2), y+radius*sin(angle+angle2));
angle2+=angvel;
radius+=v;
if(Obj_IsDeleted(objEnemy)){ Obj_Delete(obj); }
yield;
}

}



task attack3{
wait(330);
loop{
ascent(i in 0..8){
PlaySE(shot);
	loop(9){
if(GetCommonData("bullettype", 1)==1){
		let bloop = CreateDNHShotA2(ObjMove_GetX(objEnemy)+10*cos(angle2), ObjMove_GetY(objEnemy)+10*sin(angle2), 3, angle2, -0.075, 0, AQUA54, 10);
		ObjMove_AddPatternA2(bloop, 60, NO_CHANGE, NO_CHANGE, 0.05, 0, 4);
		}
		else{
		let bloop = CreateShotA2(ObjMove_GetX(objEnemy)+10*cos(angle2), ObjMove_GetY(objEnemy)+10*sin(angle2), 3, angle2, -0.075, 0, AQUA54, 10);
		ObjMove_AddPatternA2(bloop, 60, NO_CHANGE, NO_CHANGE, 0.05, 0, 4);
		}
			angle2+=360/9;
			}
			angle2+=-2.5;
			wait(4);
			if(popped==true){ break; }
			
			}
			if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE)>=750 && ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID, INFO_TIMER)>20){
			if(popped==true){ break; }
wait(120);
}
else if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE)<750 || ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID, INFO_TIMER)<=20){
if(popped==true){ break; }
wait(30);
PlaySE(shot);
angle2 = rand(0, 360);
loop(8){
CreateDNHShotA1(GetEnemyX+10*cos(angle2), GetEnemyY+10*sin(angle2), 0.75, angle2, PURPLE03, 20);
angle2+=360/8;
}
if(popped==true){ break; }
if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE)>=425 && ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID, INFO_TIMER)>20){
wait(45);
}
if(ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID, INFO_TIMER)>20){
wait(45);
}
}
//one*=-1;

if(popped==true){ break; }
}
}

task lazydelete{
loop{
DeleteShotInCircle(TYPE_ALL, TYPE_IMMEDIATE, GetCenterX, GetCenterY, 3);
if(popped==true){ break; }
wait(1);
}
}

task DestroyBoss{
SetCommonData("petals", false);
popped=true;
StopSlow(TARGET_ALL);
PlaySE(boom);
StartSlow(TARGET_ALL, 30);
//ObjMove_SetDestAtWeight(objEnemy, ObjMove_GetX(objEnemy)+120, ObjMove_GetY(objEnemy)+rand(-20, 20), 20, 1);
loop(15){ TExpan(ObjMove_GetX(objEnemy)+rand(-35, 35), ObjMove_GetY(objEnemy)+rand(-25, 25), 8, 25, 224, 255, 255, 100); wait(5); }
PlaySE(boom);
TExpan(ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), 55, 120, 224, 255, 255, 255);
Obj_Delete(objEnemy);
loop(60){ yield; }
		CloseScript(GetOwnScriptID());
		StopSlow(TARGET_ALL);
		return;
}
