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


#include"./lib/SHOT_REPLACE/shot_replace.dnh"
#include"./Mamizouinclude.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(47);
	}
}


@Initialize{
	//Resource Loading
	shotinit;
	InstallFont("./fontSEI.ttf");
	LoadSound(shot);
	LoadSound(shot3);
	LoadSound(charge2);
	LoadSound(ping);
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	Invuln(180, 80, 75);
	SetCommonData("alive", true);
	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_SetDestAtWeight(objEnemy, GetCenterX, 120, 12, 4);  
	attack2;
	attack1;
	SetShotAutoDeleteClip(100, 70, 100, 40);
	
	
}



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{
	
	
	if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) <=0 && stopstuff==false){stopstuff=true;}

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




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

task attack2{
let angle2 = 0;
let point = [];
let anglelaser = 0;
wait(120);
loop{
ObjAnim_SetAttackA1(objEnemy, true);
PlaySE(charge2);
TCharge01(objEnemy, 3, 45, 255, 0, 1);
wait(45);
if(popped==true){break;}
angle2 = angletoplayer(objEnemy);
		ascent(i in -1..2){
		CreateLaser1(GetEnemyX, GetEnemyY, angle2+(i*20), WHITE60, 400, 40, 60);
		}
		wait(45);
		ascent(i in -1..2){
		point = GetLineBorderPoint(GetEnemyX, GetEnemyY, GetEnemyX+1*cos(angle2+(i*20)), GetEnemyY+1*sin(angle2+(i*20)));
		if(point[0] == 0){ anglelaser = 0+180; } if(point[0] == GetClipMaxX){ anglelaser = 180+180; } if(point[1]==0){ anglelaser = 90; } if(point[1]==GetClipMaxY){ anglelaser = 270; }
		CreateLaser2(point[0], point[1], anglelaser, RED60, 0, 85, 0);
		}
		ObjAnim_SetAttackA1(objEnemy, false);
		wait(45);
		if(popped==true){break;}

}
}
task attack1{	
let one = 1;
let a = 0;

let firsttime = true;
let color = 0;
let angle3 = 0;
wait(240);

loop{

if(a%75==0){
		one*=-1;
		}
	loop(4){
	ascent(i in 0..4){
	if(GetCommonData("bullettype", 1)==1){
		let obj = CreateDNHShotA1(GetEnemyX+45*cos(angle), GetEnemyY+45*sin(angle), 2.75-(i/2), angle+rand(-i*2, i*2), WHITE05, 10);
		ObjRender_SetBlendType(obj, 3);
	}
	else{
	let obj = CreateShotA1(GetEnemyX+45*cos(angle), GetEnemyY+45*sin(angle), 2.75-(i/2), angle+rand(-i*2, i*2), WHITE05, 10);
		ObjRender_SetBlendType(obj, 3);
		}
		}
		angle+=360/4;
		}
		angle+=14.2765842*one;
if(popped==true){break;}
		wait(6);
		
		a++;
	if(popped==true){break;}
	
	
}
}

task CreateLaser1(x, y, angle, graphic, length, width, delay){
let obj2 = ObjShot_Create(OBJ_STRAIGHT_LASER);
let bulletgraphic = 0;
let waitincrement = 0;
ObjMove_SetPosition(obj2, x, y);
ObjStLaser_SetAngle(obj2, angle);
ObjShot_SetGraphic(obj2, graphic);
ObjShot_SetDelay(obj2, delay+1);
ObjLaser_SetLength(obj2, length);
ObjLaser_SetRenderWidth(obj2, width);
ObjLaser_SetIntersectionWidth(obj2, 20);
ObjShot_SetSpellResist(obj2, true);
ObjStLaser_SetSource(obj2, false);
ObjLaser_SetInvalidLength(obj2, 10, 10);
ObjShot_Regist(obj2);
ObjMove_SetPosition(obj2, x, y);


Wait2(delay);
Obj_Delete(obj2);
ObjLaser_SetRenderWidth(obj2, width/2);
ObjLaser_SetIntersectionWidth(obj2, width/2);
Wait2(delay);


function Wait2(f){ loop(f){ if(popped==true){ Obj_Delete(obj2); }   yield;  } }

}
task CreateLaser2(x, y, angle, graphic, length, width, delay){
let obj2 = ObjShot_Create(OBJ_STRAIGHT_LASER);
let bulletgraphic = 0;
let lengthh = length;
let waitincrement = 0;
ObjMove_SetPosition(obj2, x, y);
ObjStLaser_SetAngle(obj2, angle);
ObjShot_SetGraphic(obj2, graphic);
ObjShot_SetDelay(obj2, delay);
ObjLaser_SetLength(obj2, length);
ObjLaser_SetRenderWidth(obj2, width);
ObjLaser_SetIntersectionWidth(obj2, 20);
ObjShot_SetSpellResist(obj2, true);
ObjStLaser_SetSource(obj2, false);
ObjLaser_SetInvalidLength(obj2, 10, 10);
ObjShot_Regist(obj2);
ObjMove_SetPosition(obj2, x, y);

PlaySE(ping);
Wait2(delay);
ObjLaser_SetRenderWidth(obj2, width/2);
ObjLaser_SetIntersectionWidth(obj2, width/2-22);
Wait2(30);
ObjShot_FadeDelete(obj2);


function Wait2(f){ loop(f){ if(popped==true){ Obj_Delete(obj2); } if(length<500){ lengthh+=15; ObjLaser_SetLength(obj2, lengthh); } yield;  } }

}