//#TouhouDanmakufu
#ScriptVersion[3]
#Title["SakuyaN3_H"]
#Text["Bullets"]


#include"./lib/SHOT_REPLACE/shot_replace.dnh"
#include"./Sakuyainclude.txt"

let objEnemy;
let duration = 0;
let stopstuff = false;
let angle = 0;
let angle2 = 0;
let animframe = 0;
let frame = 0;
let GetSpeedX = 0;
let variable = 0;
let n = 0;
@Event{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
	SetScriptResult(3000);
	}
	case(EV_REQUEST_TIMER){
	SetScriptResult(46);
	}
}


@Initialize{
	//Resource Loading
	shotinit;
	InstallFont("./fontSEI.ttf");
	LoadSound(shot);
	LoadSound(shot2);
	LoadSound(shot3);
	LoadSound(poof);
	LoadSound(shadow);
	LoadSound(explode);
	LoadSound(charge2);
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	Invuln(360, 80, 75);
	SetCommonData("alive", true);
	let Sakuya = GetCurrentScriptDirectory~"sprites/sakuya.png";
	ObjPrim_SetTexture(objEnemy, Sakuya); 
	ObjSprite2D_SetSourceRect(objEnemy, 0, 0, 128, 128);
	ascent(x in 0..3){
	ObjAnim_AddFrameA2(objEnemy, ANIM_IDLE, 0, 0+x*128, 128, 128+x*128, 0, 0);
	}
	ascent(x in 0..2){
	ObjAnim_AddFrameA2(objEnemy, ANIM_IDLE, 128, 0+x*128, 256, 128+x*128, 0, 0);
	}
	ascent(x in 0..2){ObjAnim_AddFrameA2(objEnemy, ANIM_MOVE_LEFT, 256, 128*x, 256+128, 128*x+128, 1, 0);}
	
	
	ascent(x in 2..4){ObjAnim_AddFrameA2(objEnemy, ANIM_MOVE_RIGHT, 256, 128*x, 256+128, 128*x+128, 0, 0);}
	
	ObjAnim_AddFrameA2(objEnemy, ANIM_MOVEEND_RIGHT, 256, 128*2, 128*3, 128*2+128, 0, 0);
	ObjAnim_AddFrameA2(objEnemy, ANIM_MOVEEND_RIGHT, 0, 0, 128, 128, 0, 0);
	ObjAnim_AddFrameA2(objEnemy, ANIM_MOVEEND_LEFT, 256, 0, 128*3, 128, 0, 0);
	ObjAnim_AddFrameA2(objEnemy, ANIM_MOVEEND_LEFT, 0, 0, 128, 128, 0, 0);
	
	ascent(x in 0..2){ObjAnim_AddFrameA2(objEnemy, ANIM_ATTACK, 128*5, 128*x, 128*6, 128*x+128, 0, 0);}
	ascent(x in 0..2){ObjAnim_AddFrameA2(objEnemy, ANIM_ATTACKEND, 128*5, 128*1-128*x, 128*6, 128*2-128*x, 0, 0);}
	
	ObjAnim_SetSpeedA1(objEnemy, ANIM_IDLE, 12);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_MOVE_LEFT, 8);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_MOVE_RIGHT, 8);
	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, 1);
	ObjAnim_StartAnimationA2(objEnemy, 0, 0);
		ObjSprite2D_SetDestCenter(objEnemy);  
	//BottomEnemyMarker(objEnemy, objEnemy);
	ObjMove_SetDestAtFrame(objEnemy, GetCenterX, GetClipMinY+120, 90);
	
	attack1;
	SetShotAutoDeleteClip(40, 40, 40, 20);
	
	
}



task ChangeToAttack1{
ascent(x in 0..3){ObjAnim_ReplaceFrameA2(objEnemy, ANIM_ATTACK, x, 128*4, 128*x, 128*5, 128*x+128, 0, 0);}
	ascent(x in 0..3){ObjAnim_ReplaceFrameA2(objEnemy, ANIM_ATTACKEND, x, 128*4, 128*2-128*x, 128*5, 128*3-128*x, 0, 0);}

}

task ChangeToSpell{

ascent(x in 0..3){ObjAnim_ReplaceFrameA2(objEnemy, ANIM_ATTACK, x, 128*3, 128*x, 128*4, 128*x+128, 0, 0);}
	ascent(x in 0..3){ObjAnim_ReplaceFrameA2(objEnemy, ANIM_ATTACKEND, x, 128*3, 128*2-128*x, 128*4, 128*3-128*x, 0, 0);}
	}


@MainLoop{
	
	
	if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) <=0 && stopstuff==false){stopstuff=true;}

	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));
	SetCommonData("sakuyax", ObjMove_GetX(objEnemy));
	SetCommonData("sakuyay", ObjMove_GetY(objEnemy));




if(stopstuff==true && popped==false){
DestroyBoss;
}	
	
}

task DestroyBoss{
popped=true;
SetCommonData("alive", false);
loop(10){ yield; }
Obj_Delete(objEnemy);
loop(20){ yield; }
		CloseScript(GetOwnScriptID());
		return;
}


function ExMoveInstant { ExMoveA(GetClipMinY+120, 10, rand(40, 60)); }
function ExMoveA(height, ud, length) {
if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0){
	if(GetPlayerX < GetEnemyX) {
		if(GetEnemyX - length <= GetClipMinX+20) {
			ObjMove_SetPosition(objEnemy, GetEnemyX + length+20, GetClipMinY+120);
		} else {
			ObjMove_SetPosition(objEnemy, GetEnemyX - length-20, GetClipMinY+120);
		}
	} else {
		if(GetEnemyX + length >= GetClipMaxX-20) {
			ObjMove_SetPosition(objEnemy, GetEnemyX - length-20, GetClipMinY+120);
		} else {
			ObjMove_SetPosition(objEnemy, GetEnemyX + length+20, GetClipMinY+120);
		}
	}
}
}

task thin{
ascent(i in 0..7){
ObjRender_SetScaleXYZ(objEnemy, 1-(i/7), 1+(i/7), 1);
yield;
}
descent(i in 0..7){
ObjRender_SetScaleXYZ(objEnemy, 1-(i/7), 1+(i/7), 1);
yield;
}
}

task attack1{	
let one = -1;

let angle2 = 0;
let color = 0;
let angle3 = 0;
wait(90);
CreateFamiliar(0, 0, 0);
CreateFamiliar(0, 0, 1);
if(popped==true){return;}
wait(360);

loop{
PlaySE(charge2);
TCharge01(objEnemy, 3, 45, 255, 15, 15);
wait(60);
ObjAnim_SetAttackA2(objEnemy, 12*8);
angle = GetAngleToPlayer(objEnemy);

ascent(p in 0..12){

if(popped==true){ return; }
PlaySE(shot);
			ascent(i in -1-p..p+2){
			
					loop(5){
				CreateShotA1(GetEnemyX, GetEnemyY, 2.5+(p/3.25), angle+(i*1.25), RED31, 10);
					angle+=360/5;
					}
				}
			wait(8);
		}
wait(180);		
yield;
if(popped==true){return;}
}
}

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


task CreateFamiliar(dir, speed, type){
let obj = ObjEnemy_Create(OBJ_ENEMY);
let rotateacc = 0;
let alpha = 255;
let familiar = GetCurrentScriptDirectory~"sprites/familiar.png";
let angle = dir;
let angle2 = 0;
if(type==-1){
angle2 = 180;
}
let graphic = BLUE31;
if(type==-1){ graphic = RED31; }
ObjEnemy_SetLife(obj, 1000);
ObjMove_SetPosition(obj, GetEnemyX, GetEnemyY);
ObjPrim_SetTexture(obj, familiar);
ObjSprite2D_SetSourceRect(obj, 0, 0, 64, 64);
ObjSprite2D_SetDestCenter(obj);
ObjRender_SetScaleXYZ(obj, 0.75, 0.75, 1);
ObjEnemy_Regist(obj);
if(type==0){
ObjMove_SetDestAtWeight(obj, GetEnemyX+80, GetEnemyY, 13, 2);
}
else{
ObjMove_SetDestAtWeight(obj, GetEnemyX-80, GetEnemyY, 13, 2);
}
Fire;
PlaySE(shadow);
while(!Obj_IsDeleted(obj)){
ObjRender_SetAngleZ(obj, 0+rotateacc);
rotateacc+=7;
yield;
}

task Fire{

let one = -1;

let angle2 = 0;
let color = 0;
let angle3 = 0;
wait(90);
if(popped==true){return;}
PlaySE(charge2);
TCharge01(obj, 2, 45, 155, 155, 255);
wait(60);
if(type==0){
wait(45);
angle=75.75;
}
if(popped==true){return;}

loop{

	PlaySE(shot3);
	loop(24){
		let obj2 = CreateShotA1(ObjMove_GetX(obj)+15*cos(angle), ObjMove_GetY(obj)+15*sin(angle), 2.25+0.5*sin((angle)*5), angle, BLUE31, 1);
		RunReflectCode(obj2);
		angle+=360/24;
		}
		if(type==1){
		angle+=7.765382902+rand(-0.05, 0.05);
		}
		else{
		angle-=7.765382902+rand(-0.05, 0.05);
		}
loop(90){
		
if(popped==true){
loop(15){
ObjRender_SetAlpha(obj, alpha);
alpha-=255/15;
yield;
}
Obj_Delete(obj);
return;
}
yield; 
}
}

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

}



}


task CreateLaser1(x, y, angle, graphic, length, width, delay, type){
let obj2 = ObjShot_Create(OBJ_STRAIGHT_LASER);
let angle2 = rand(0, 360);
let bulletgraphic = 0;
let one = 1;
let index = 0;
let waitincrement = 0;
ObjMove_SetPosition(obj2, x, y);
ObjStLaser_SetAngle(obj2, angle);
ObjShot_SetGraphic(obj2, graphic);
ObjShot_SetDelay(obj2, delay*20);
ObjLaser_SetLength(obj2, length);
ObjLaser_SetRenderWidth(obj2, width);
ObjShot_SetSpellResist(obj2, true);
ObjStLaser_SetSource(obj2, false);
ObjShot_Regist(obj2);
wait2(delay-5);
if(type==0){ wait(5); }
if(type==1){ObjShot_FadeDelete(obj2); }
if(type==0){
PlaySE(ping);
}
ObjLaser_SetRenderWidth(obj2, width/2);
ObjLaser_SetIntersectionWidth(obj2, width/2-3);
wait2(delay*2);
ObjShot_FadeDelete(obj2);

function wait2(w){ loop(w){ if(popped==true){ Obj_Delete(obj2); return; } yield; } }
}
