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

#include"./lib/SHOT_REPLACE/shot_replace.dnh"
#include"./Mamizouinclude.txt"
let color2 = 1;
let color = 0;
let invulnvar = 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 ping2 = GetCurrentScriptDirectory~"sfx/ping2.wav";
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(ping2);
	LoadSound(boom);
	
	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, 12.5, 25);
	//BottomEnemyMarker(objEnemy, objEnemy);
	let Mima = GetCurrentScriptDirectory~"sprites/mima.png";
	ObjPrim_SetTexture(objEnemy, Mima); 
	ObjSprite2D_SetSourceRect(objEnemy, 0, 0, 128, 128);
	ascent(i in 0..3){
	ObjAnim_AddFrameA2(objEnemy, ANIM_IDLE, 0, 128*i, 128, 128*i+128, 0, 0);
	}
	ascent(i in 0..2){
	ObjAnim_AddFrameA2(objEnemy, ANIM_IDLE, 128, 128*i, 256, 128*i+128, 0, 0);
	}
	ascent(x in 0..2){ObjAnim_AddFrameA2(objEnemy, ANIM_MOVE_LEFT, 256, 128*x, 256+128, 128*x+128, 0, 0);}
	descent(x in 0..2){
	ObjAnim_AddFrameA2(objEnemy, ANIM_MOVEEND_LEFT, 256, 128*x, 256+128, 128*x+128, 0, 0);
	}
	
	ascent(x in 2..4){ObjAnim_AddFrameA2(objEnemy, ANIM_MOVE_RIGHT, 256, 128*x, 256+128, 128*x+128, 0, 0);}
	descent(x in 2..4){
	ObjAnim_AddFrameA2(objEnemy, ANIM_MOVEEND_RIGHT, 256, 128*x, 256+128, 128*x+128, 0, 0);
	}
	
	
	ascent(x in 0..3){ObjAnim_AddFrameA2(objEnemy, ANIM_ATTACK, 128*5, 128*x, 128*6, 128*x+128, 0, 0);}
	descent(x in 0..3){ObjAnim_AddFrameA2(objEnemy, ANIM_ATTACKEND, 128*5, 128*x, 128*6, 128*x+128, 0, 0);}
	
	ObjAnim_SetSpeedA1(objEnemy, ANIM_IDLE, 14);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_MOVE_LEFT, 6);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_MOVE_RIGHT, 6);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_ATTACK, 8);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_MOVE_RIGHT, 1);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_MOVE_LEFT, 1);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_ATTACK, 2);
	ObjAnim_StartAnimationA2(objEnemy, 0, 0);
	ObjSprite2D_SetDestCenter(objEnemy); 
	//BottomEnemyMarker(objEnemy, objEnemy);
	ObjMove_SetDestAtFrame(objEnemy, GetCenterX, 120, 60);  
	
	
	
	
LoadTexture(white);
	
	delayedstart;
	SetShotAutoDeleteClip(50, 120, 50, 20);
	
	
}

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

task delayedstart{
ObjMove_SetDestAtFrame(objEnemy, GetCenterX, 120, 60);
wait(60);

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

PlaySE(cardcall);
ObjCutin_SetSpellcardS4("Danmaku Transformation &quot;Null Velocity&quot;", mimacutin, "DRUNK", 0, 255, 0);
ObjCutin_LaunchS3(KANAKO, mimacutin, "Hard");
wait(60);
attack1;

}



@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 randx = 0;
let angle = 0;
let angle2 = 0;
let type = 1;
let color = 0;
let one = 1;
ObjAnim_SetAttackA1(objEnemy, true);
PlaySE(charge2);
TCharge01(objEnemy, 3, 45, 255, 0, 0);
wait(45);
nodamage;
loop{
PlaySE(shot);
	loop(20+x){
	if(ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID, INFO_TIMER)<20){
let obj2 = CreateShotA1(GetEnemyX, GetEnemyY, 0.85, angle, RED50, 10);
ObjMove_AddPatternB2(obj2, 90, rand(-0.11, 0.11), 0, 0, rand(0.013, 0.0175), 20, rand(3.5, 3.75)); 
}
ascent(i in 0..2){
if(GetCommonData("bullettype", 1)==1){
let obj = CreateDNHShotA1(GetEnemyX, GetEnemyY, 2.25-(i/1.35), angle, RED50, 10);
ObjMove_AddPatternB2(obj, 90, rand(-0.11, 0.11), 0, 0, rand(0.013, 0.0175), 20, rand(3.5, 3.75)); 
}
else{
let obj = CreateShotA1(GetEnemyX, GetEnemyY, 2.25-(i/1.35), angle, RED50, 10);
ObjMove_AddPatternB2(obj, 90, rand(-0.11, 0.11), 0, 0, rand(0.013, 0.0175), 20, rand(3.5, 3.75)); 

}
}
angle+=360/(20+x);
}
angle = rand(0, 360);
wait(90);
PlaySE(ping2);
x=5;
if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE)>=475 && ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID, INFO_TIMER)>20){ wait(30); x=3;}
if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE)>=800 && ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID, INFO_TIMER)>20){ wait(30); x=1; }
if(popped==true){ break; }
}
}



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