//#TouhouDanmakufu
#ScriptVersion[3]
#Title["KogasaS1_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, 30.25, 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 delayedstart{
ObjMove_SetDestAtFrame(objEnemy, GetCenterX, 120, 60);
wait(60);

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

PlaySE(cardcall);
ObjCutin_SetSpellcardS4("Spooky Rain &quot;100%-Accurate Lightning Bolt&quot;", kogasacutin, "KANAKO", 255, 215, 0);
ObjCutin_LaunchS3(KANAKO, kogasacutin, "Hard");
wait(60);
attack1;
attack2;
}

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(240);
let increment = 0;
let x = 0;
let angle = 0;
let angle2 = 0;
let type = 1;
let color = 0;
loop{
if(popped==true){break;}
//ObjAnim_SetAttackA2(objEnemy, 60);
ascent(i in -2..7){
color++;
CreateDNHShotA1(80*sin(increment*2)+(i*80), rand(-2, 2), 2.20+rand(-0.25, 0.25), rand(86, 94)+9*sin(color/1.05), [BLUE50, AQUA50][color%2], round(rand(10, 15)));
}
color++;
increment+=8;
x=rand(-30, 30);
wait(6);
//ExMove;

}
}

task attack2{
wait(120);
loop{
PlaySE(charge2);
TCharge01(objEnemy, 2, 45, 255, 255, 0);
wait(60);
PlaySE(thunder);
screenflash;
wait(60);
ExMove;
wait(120);
}
}
	


function screenflash{
let time = 0;
let objWhite = ObjPrim_Create(OBJ_SPRITE_2D);

let alphavar = 0;

ObjPrim_SetTexture(objWhite, white);
ObjSprite2D_SetSourceRect(objWhite, 0, 0, 691, 607);
ObjSprite2D_SetDestCenter(objWhite);
ObjRender_SetColor(objWhite, 255, 255, 0);
ObjRender_SetAlpha(objWhite, 127.5);
ObjMove_SetX(objWhite, GetStgFrameWidth/2);
ObjMove_SetY(objWhite, GetStgFrameHeight/2);
ObjRender_SetScaleXYZ(objWhite, 2, 2, 1);
loop(75){
	ObjRender_SetAlpha(objWhite, 125+125*sin(alphavar));
	if(time<=10){
	alphavar+=9;
	}
	if(time>=45){
	alphavar+=180/30;
	}
	wait2(1);
	time++;
	}
Obj_Delete(objWhite);
function wait2(w){ loop(w){ if (popped==true){ Obj_Delete(objWhite); } yield; } }
}
	
	
task DestroyBoss{
SetCommonData("petals", false);
popped=true;
StopSlow(TARGET_ALL);
loop(10){ yield; }
Obj_Delete(objEnemy);
loop(45){ yield; }
		CloseScript(GetOwnScriptID());
		return;
}
