//#TouhouDanmakufu
#ScriptVersion[3]
#Title["MamizouS1_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("Astral Republic &quot;Bouncing Stars&quot;", mimacutin, "DRUNK", 0, 255, 0);
ObjCutin_LaunchS3(YABUSAME, 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);
	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;
loop{
PlaySE(charge2);
TCharge01(objEnemy, 3, 45, 20, 255, 20);
wait(45);
if(popped==true){break;}
angle = 90;
if(GetPlayerX>=0){ one = -1;} else { one = 1; }
ObjAnim_SetAttackA1(objEnemy, true);
	ascent(i in 0..50){
	if(i>=15 && i<30){
	angle+=20.5/13*one;
	}
	if(i>=30){
	angle-=21/13*one;
	}
	PlaySE(shot);
		ascent(p in -3..5){
			if(GetCommonData("bullettype", 1)==1){
			let obj2 = CreateDNHShotA1(GetEnemyX+10*cos(angle+(p*15)-7.5), GetEnemyY+10*sin(angle+(p*15)-7.5), 4.35, angle+(p*15)-7.5, PURPLE59, 10);
			RunReflectCode(obj2);
			}
			else{
			let obj2 = CreateShotA1(GetEnemyX+10*cos(angle+(p*15)-7.5), GetEnemyY+10*sin(angle+(p*15)-7.5), 4.35, angle+(p*15)-7.5, PURPLE59, 10);
			RunReflectCode(obj2);
			}
			}
			if(popped==true){break;}
			wait(6);
			}
			if(popped==true){break;}
			wait(30);
	ObjAnim_SetAttackA1(objEnemy, false);
	if(popped==true){break;}
	wait(65);
	ExMoveBarely;
	if(popped==true){break;}
	wait(95);

}
}

function ExMoveBarely { ExMoveBarely_(GetClipMinY+120, 10, rand(15, 25)); }
function ExMoveBarely_(height, ud, length) {
if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0){
	if(GetPlayerX < GetEnemyX) {
		if(GetEnemyX - length <= GetClipMinX+20) {
			ObjMove_SetDestAtWeight(objEnemy, GetEnemyX + length+10, rand(height+4 - ud, height + ud), 15, 3);
		} else {
			ObjMove_SetDestAtWeight(objEnemy, GetEnemyX - length-10, rand(height - ud, height + ud), 15, 3);
		}
	} else {
		if(GetEnemyX + length >= GetClipMaxX-20) {
			ObjMove_SetDestAtWeight(objEnemy, GetEnemyX - length-10, rand(height - ud, height + ud), 15, 3);
		} else {
			ObjMove_SetDestAtWeight(objEnemy, GetEnemyX + length+10, rand(height - ud, height + ud), 15, 3);
		}
	}
}
}

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