//#TouhouDanmakufu
#ScriptVersion[3]
#Title["MamizouS2_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 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, 22.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, 100, 50, 20);
	
	
}



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

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

PlaySE(cardcall);
ObjCutin_SetSpellcardS4("Ritual Sign &quot;Orreries' Elliptical Orbit&quot;", mimacutin, "DRUNK", 0, 255, 0);
ObjCutin_LaunchS3(KANAKO, mimacutin, "Hard");
wait(60);
attack1;
attack2;
}


task attack2{
let orbitrad = 0;
let orbitang = 0;
wait(270);
loop{
SetCommonData("orbiterx", GetCenterX+(orbitrad*2)*cos(orbitang));
SetCommonData("orbitery", GetCenterY+80+(orbitrad/1.25)*sin(orbitang));
wait(1);
if(orbitrad<40){ orbitrad+=0.5; }
orbitang+=0.85;
if(popped==true){ break; }
}
}
task CreateOrbitars(radius, angle, graphic, lasegra){
let obj2 = ObjShot_Create(OBJ_SHOT);
let maxradius = 400;
let angle2 = 0;
ObjShot_SetSpellResist(obj2, true);
ObjShot_SetGraphic(obj2, graphic);
ObjShot_SetDelay(obj2, 0);
ObjShot_Regist(obj2);
ObjShot_SetAutoDelete(obj2, false);
ObjMove_SetPosition(obj2, ObjMove_GetX(GetPlayerObjectID)+radius*cos(angle+angle2), ObjMove_GetY(GetPlayerObjectID)+radius*sin(angle+angle2));
let obj3 = ObjShot_Create(OBJ_STRAIGHT_LASER);
ObjMove_SetPosition(obj3, ObjMove_GetX(GetPlayerObjectID)+radius*cos(angle+angle2), ObjMove_GetY(GetPlayerObjectID)+radius*sin(angle+angle2));
ObjStLaser_SetAngle(obj3, 0);
ObjShot_SetGraphic(obj3, lasegra);
ObjLaser_SetLength(obj3, 70);
ObjShot_SetAutoDelete(obj3, false);
ObjShot_SetSpellResist(obj3, true);
ObjStLaser_SetSource(obj3, false);
ObjLaser_SetInvalidLength(obj3, 0, 0);
ObjLaser_SetRenderWidth(obj3, 20);
ObjShot_SetDelay(obj2, 60);
ObjLaser_SetIntersectionWidth(obj3, 15);
ObjShot_Regist(obj3);
while(!Obj_IsDeleted(obj2) && ObjEnemy_GetInfo(objEnemy, INFO_LIFE)>0){
ObjMove_SetPosition(obj2, GetCommonData("orbiterx", GetCenterX)+maxradius*cos(angle+angle2), GetCommonData("orbitery", GetCenterY+80)+maxradius*sin(angle+angle2));
ObjMove_SetPosition(obj3, GetCommonData("orbiterx", GetCenterX)+maxradius*cos(angle+angle2), GetCommonData("orbitery", GetCenterY+80)+maxradius*sin(angle+angle2));
ObjStLaser_SetAngle(obj3, atan2(ObjMove_GetY(obj3)+maxradius*sin(angle+angle2+360/4)-ObjMove_GetY(obj3), ObjMove_GetX(obj3)+maxradius*cos(angle+angle2+360/4)-ObjMove_GetX(obj3))+45);
ObjLaser_SetLength(obj3, 115*1.5);
angle2+=2.5;
wait(1);
if(maxradius>radius){ maxradius-=2.5; }
}
Obj_Delete(obj2);
Obj_Delete(obj3);
}

@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;
let one = 1;
ObjAnim_SetAttackA1(objEnemy, true);
PlaySE(charge2);
TCharge01(objEnemy, 3, 45, 255, 255, 255);
wait(45);
CreateOrbitars(115, 0, RED51, RED60);
CreateOrbitars(115, 270, PURPLE51, PURPLE60);
CreateOrbitars(115, 90, GREEN51, GREEN60);
CreateOrbitars(115, 180, BLUE51, BLUE60);
wait(120);
ObjAnim_SetAttackA1(objEnemy, false);
wait(120);
PlaySE(charge2);
TCharge01(objEnemy, 3, 45, 0, 0, 255);
wait(45);
loop{
PlaySE(shot3);
	loop(20){
CreateDNHShotA1(GetEnemyX, GetEnemyY, 2.25+0.35*sin(3*angle+angle2), angle+angle2, [BLUE58, GREEN58, YELLOW58, RED58, PURPLE58][color%5], 10);
angle+=360/20;
}
angle2+=40+rand(-10, 10);
color++;
wait(15);
if(popped==true){ break; }
}
}



task DestroyBoss{
SetCommonData("petals", false);
popped=true;
StopSlow(TARGET_ALL);
loop(10){ yield; }
Obj_Delete(objEnemy);
loop(45){ yield; }
		CloseScript(GetOwnScriptID());
		return;
}
